KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > groovy > HiveMindBuilder


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 JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import groovy.xml.SAXBuilder;
21
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.Locator JavaDoc;
25 import org.xml.sax.helpers.AttributesImpl JavaDoc;
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
30  * {@link org.apache.hivemind.parse.ModuleDescriptor module descriptors} using a Groovy script. A
31  * single Groovy script must only define one module descriptor.
32  * <p>
33  * The markup in the Groovy script is equivalent to the XML markup for module descriptors. The only
34  * difference being that any dashes in element names and attribute names (which would confuse the
35  * Groovy parser) are replaced by a camelCase notation. So for example
36  * <code>configuration-point</code> becomes <code>configurationPoint</code> in a Groovy script.
37  *
38  * @since 1.1
39  * @author Knut Wannheden
40  */

41 public class HiveMindBuilder extends SAXBuilder
42 {
43     public static final Locator JavaDoc GROOVY_LOCATOR = new GroovyLocator();
44
45     private static final Map JavaDoc CAMEL_TO_HYPHEN_MAP = new HashMap JavaDoc();
46
47     public HiveMindBuilder(ContentHandler JavaDoc parser)
48     {
49         super(parser);
50
51         parser.setDocumentLocator(GROOVY_LOCATOR);
52     }
53
54     protected void nodeCompleted(Object JavaDoc parent, Object JavaDoc node)
55     {
56         super.nodeCompleted(parent, getHyphenatedName(node.toString()));
57     }
58
59     protected void doStartElement(Object JavaDoc name, Attributes JavaDoc attributes)
60     {
61         super.doStartElement(
62                 getHyphenatedName(name.toString()),
63                 getHyphenatedAttributes(attributes));
64     }
65
66     private String JavaDoc getHyphenatedName(String JavaDoc name)
67     {
68         String JavaDoc hyphenatedName = (String JavaDoc) CAMEL_TO_HYPHEN_MAP.get(name);
69
70         if (hyphenatedName == null)
71         {
72             char[] chars = name.toCharArray();
73
74             StringBuffer JavaDoc hyphenated = new StringBuffer JavaDoc();
75
76             for (int i = 0; i < name.length(); i++)
77             {
78                 if (Character.isUpperCase(chars[i]))
79                     hyphenated.append('-').append(Character.toLowerCase(chars[i]));
80                 else
81                     hyphenated.append(chars[i]);
82             }
83
84             hyphenatedName = hyphenated.toString();
85
86             CAMEL_TO_HYPHEN_MAP.put(name, hyphenatedName);
87         }
88
89         return hyphenatedName;
90     }
91
92     private Attributes JavaDoc getHyphenatedAttributes(Attributes JavaDoc attributes)
93     {
94         AttributesImpl JavaDoc result = (AttributesImpl JavaDoc) attributes;
95
96         for (int i = 0; i < result.getLength(); i++)
97         {
98             result.setLocalName(i, getHyphenatedName(result.getLocalName(i)));
99         }
100
101         return result;
102     }
103
104     private static class GroovyLocator implements Locator JavaDoc
105     {
106         public String JavaDoc getPublicId()
107         {
108             return null;
109         }
110
111         public String JavaDoc getSystemId()
112         {
113             return null;
114         }
115
116         public int getLineNumber()
117         {
118             try
119             {
120                 throw new Throwable JavaDoc();
121             }
122             catch (Throwable JavaDoc t)
123             {
124                 StackTraceElement JavaDoc[] trace = t.getStackTrace();
125
126                 for (int i = 0; i < trace.length; i++)
127                     if (trace[i].getFileName().endsWith(".groovy"))
128                         return trace[i].getLineNumber();
129             }
130
131             return -1;
132         }
133
134         public int getColumnNumber()
135         {
136             return -1;
137         }
138     }
139 }
Popular Tags