1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.lib.groovy; |
16 |
| |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Map; |
19 |
| |
20 |
| import groovy.xml.SAXBuilder; |
21 |
| |
22 |
| import org.xml.sax.Attributes; |
23 |
| import org.xml.sax.ContentHandler; |
24 |
| import org.xml.sax.Locator; |
25 |
| import org.xml.sax.helpers.AttributesImpl; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| public class HiveMindBuilder extends SAXBuilder |
42 |
| { |
43 |
| public static final Locator GROOVY_LOCATOR = new GroovyLocator(); |
44 |
| |
45 |
| private static final Map CAMEL_TO_HYPHEN_MAP = new HashMap(); |
46 |
| |
47 |
4
| public HiveMindBuilder(ContentHandler parser)
|
48 |
| { |
49 |
4
| super(parser);
|
50 |
| |
51 |
4
| parser.setDocumentLocator(GROOVY_LOCATOR);
|
52 |
| } |
53 |
| |
54 |
20
| protected void nodeCompleted(Object parent, Object node)
|
55 |
| { |
56 |
20
| super.nodeCompleted(parent, getHyphenatedName(node.toString()));
|
57 |
| } |
58 |
| |
59 |
20
| protected void doStartElement(Object name, Attributes attributes)
|
60 |
| { |
61 |
20
| super.doStartElement(
|
62 |
| getHyphenatedName(name.toString()), |
63 |
| getHyphenatedAttributes(attributes)); |
64 |
| } |
65 |
| |
66 |
68
| private String getHyphenatedName(String name)
|
67 |
| { |
68 |
68
| String hyphenatedName = (String) CAMEL_TO_HYPHEN_MAP.get(name);
|
69 |
| |
70 |
68
| if (hyphenatedName == null)
|
71 |
| { |
72 |
11
| char[] chars = name.toCharArray();
|
73 |
| |
74 |
11
| StringBuffer hyphenated = new StringBuffer();
|
75 |
| |
76 |
11
| for (int i = 0; i < name.length(); i++)
|
77 |
| { |
78 |
97
| if (Character.isUpperCase(chars[i]))
|
79 |
4
| hyphenated.append('-').append(Character.toLowerCase(chars[i]));
|
80 |
| else |
81 |
93
| hyphenated.append(chars[i]);
|
82 |
| } |
83 |
| |
84 |
11
| hyphenatedName = hyphenated.toString();
|
85 |
| |
86 |
11
| CAMEL_TO_HYPHEN_MAP.put(name, hyphenatedName);
|
87 |
| } |
88 |
| |
89 |
68
| return hyphenatedName;
|
90 |
| } |
91 |
| |
92 |
20
| private Attributes getHyphenatedAttributes(Attributes attributes)
|
93 |
| { |
94 |
20
| AttributesImpl result = (AttributesImpl) attributes;
|
95 |
| |
96 |
20
| for (int i = 0; i < result.getLength(); i++)
|
97 |
| { |
98 |
28
| result.setLocalName(i, getHyphenatedName(result.getLocalName(i)));
|
99 |
| } |
100 |
| |
101 |
20
| return result;
|
102 |
| } |
103 |
| |
104 |
| private static class GroovyLocator implements Locator |
105 |
| { |
106 |
0
| public String getPublicId()
|
107 |
| { |
108 |
0
| return null;
|
109 |
| } |
110 |
| |
111 |
0
| public String getSystemId()
|
112 |
| { |
113 |
0
| return null;
|
114 |
| } |
115 |
| |
116 |
19
| public int getLineNumber()
|
117 |
| { |
118 |
19
| try
|
119 |
| { |
120 |
19
| throw new Throwable();
|
121 |
| } |
122 |
| catch (Throwable t) |
123 |
| { |
124 |
19
| StackTraceElement[] trace = t.getStackTrace();
|
125 |
| |
126 |
19
| for (int i = 0; i < trace.length; i++)
|
127 |
| { |
128 |
1330
| String fileName = trace[i].getFileName();
|
129 |
1330
| if (fileName != null && fileName.endsWith(".groovy"))
|
130 |
0
| return trace[i].getLineNumber();
|
131 |
| } |
132 |
| } |
133 |
| |
134 |
19
| return -1;
|
135 |
| } |
136 |
| |
137 |
19
| public int getColumnNumber()
|
138 |
| { |
139 |
19
| return -1;
|
140 |
| } |
141 |
| } |
142 |
| } |