|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
HiveMindBuilder.java | 91.7% | 89.7% | 77.8% | 88% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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 |
* The HiveMindBuilder is a <a href="http://groovy.codehaus.org/GroovyMarkup">groovy markup builder
|
|
29 |
* </a> which can be used to define HiveMind {@link ModuleDescriptor module descriptors}using a
|
|
30 |
* Groovy script. A single Groovy script must only define one module descriptor.
|
|
31 |
* <p>
|
|
32 |
* The markup in the Groovy script is equivalent to the XML markup for module descriptors. The only
|
|
33 |
* difference being that any dashes in element names and attribute names (which would confuse the
|
|
34 |
* Groovy parser) are replaced by a camelCase notation. So for example
|
|
35 |
* <code>configuration-point</code> becomes <code>configurationPoint</code> in a Groovy script.
|
|
36 |
*
|
|
37 |
* @since 1.1
|
|
38 |
* @author Knut Wannheden
|
|
39 |
*/
|
|
40 |
public class HiveMindBuilder extends SAXBuilder |
|
41 |
{ |
|
42 |
public static final Locator GROOVY_LOCATOR = new GroovyLocator(); |
|
43 |
|
|
44 |
private static final Map CAMEL_TO_HYPHEN_MAP = new HashMap(); |
|
45 |
|
|
46 | 5 |
public HiveMindBuilder(ContentHandler parser)
|
47 |
{ |
|
48 | 5 |
super(parser);
|
49 |
|
|
50 | 5 |
parser.setDocumentLocator(GROOVY_LOCATOR); |
51 |
} |
|
52 |
|
|
53 | 20 |
protected void nodeCompleted(Object parent, Object node) |
54 |
{ |
|
55 | 20 |
super.nodeCompleted(parent, getHyphenatedName(node.toString()));
|
56 |
} |
|
57 |
|
|
58 | 21 |
protected void doStartElement(Object name, Attributes attributes) |
59 |
{ |
|
60 | 21 |
super.doStartElement(
|
61 |
getHyphenatedName(name.toString()), |
|
62 |
getHyphenatedAttributes(attributes)); |
|
63 |
} |
|
64 |
|
|
65 | 69 |
private String getHyphenatedName(String name)
|
66 |
{ |
|
67 | 69 |
String hyphenatedName = (String) CAMEL_TO_HYPHEN_MAP.get(name); |
68 |
|
|
69 | 69 |
if (hyphenatedName == null) |
70 |
{ |
|
71 | 11 |
char[] chars = name.toCharArray();
|
72 |
|
|
73 | 11 |
StringBuffer hyphenated = new StringBuffer();
|
74 |
|
|
75 | 11 |
for (int i = 0; i < name.length(); i++) |
76 |
{ |
|
77 | 97 |
if (Character.isUpperCase(chars[i]))
|
78 | 4 |
hyphenated.append('-').append(Character.toLowerCase(chars[i])); |
79 |
else
|
|
80 | 93 |
hyphenated.append(chars[i]); |
81 |
} |
|
82 |
|
|
83 | 11 |
hyphenatedName = hyphenated.toString(); |
84 |
|
|
85 | 11 |
CAMEL_TO_HYPHEN_MAP.put(name, hyphenatedName); |
86 |
} |
|
87 |
|
|
88 | 69 |
return hyphenatedName;
|
89 |
} |
|
90 |
|
|
91 | 21 |
private Attributes getHyphenatedAttributes(Attributes attributes)
|
92 |
{ |
|
93 | 21 |
AttributesImpl result = (AttributesImpl) attributes; |
94 |
|
|
95 | 21 |
for (int i = 0; i < result.getLength(); i++) |
96 |
{ |
|
97 | 28 |
result.setLocalName(i, getHyphenatedName(result.getLocalName(i))); |
98 |
} |
|
99 |
|
|
100 | 21 |
return result;
|
101 |
} |
|
102 |
|
|
103 |
private static class GroovyLocator implements Locator |
|
104 |
{ |
|
105 | 0 |
public String getPublicId()
|
106 |
{ |
|
107 | 0 |
return null; |
108 |
} |
|
109 |
|
|
110 | 0 |
public String getSystemId()
|
111 |
{ |
|
112 | 0 |
return null; |
113 |
} |
|
114 |
|
|
115 | 21 |
public int getLineNumber() |
116 |
{ |
|
117 | 21 |
try
|
118 |
{ |
|
119 | 21 |
throw new Throwable(); |
120 |
} |
|
121 |
catch (Throwable t)
|
|
122 |
{ |
|
123 | 21 |
StackTraceElement[] trace = t.getStackTrace(); |
124 |
|
|
125 | 393 |
for (int i = 0; i < trace.length; i++) |
126 | 393 |
if (trace[i].getFileName().endsWith(".groovy")) |
127 | 21 |
return trace[i].getLineNumber();
|
128 |
} |
|
129 |
|
|
130 | 0 |
return -1;
|
131 |
} |
|
132 |
|
|
133 | 21 |
public int getColumnNumber() |
134 |
{ |
|
135 | 21 |
return -1;
|
136 |
} |
|
137 |
} |
|
138 |
} |
|