KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > builder > Handler


1 package org.jicengine.builder;
2
3 import org.jicengine.operation.Context;
4 import org.jicengine.operation.SimpleContext;
5 import org.jicengine.operation.BeanUtils;
6 import org.jicengine.operation.OperationException;
7 import org.xml.sax.*;
8 import org.xml.sax.helpers.DefaultHandler JavaDoc;
9 import java.util.*;
10 import org.jicengine.element.*;
11 import org.jicengine.element.impl.*;
12 import org.jicengine.expression.*;
13 import org.jicengine.Instructions;
14 import org.jicengine.BuildContext;
15
16 /**
17  *
18  *
19  * <p>
20  * Copyright (C) 2004 Timo Laitinen
21  * </p>
22  *
23  * @author .timo
24  */

25
26 public class Handler extends DefaultHandler JavaDoc {
27
28 // public static final String NAMESPACE_URI_JICE_1_0 = "http://www.jicengine.org/jic/1.0";
29
public static final String JavaDoc NAMESPACE_URI_JICE_2_0 = "http://www.jicengine.org/jic/2.0";
30   public static final String JavaDoc NAMESPACE_URI_JICE_2_1 = "http://www.jicengine.org/jic/2.1";
31
32     private Stack elementCompilers = new Stack();
33     private Element rootElement;
34
35   private String JavaDoc jiceNameSpace;
36     private String JavaDoc jiceNamespacePrefix = "";
37   boolean syntaxBasedCdataConversionSupport = false;
38     private boolean defaultNamespaceUsed = true;
39
40     StringBuffer JavaDoc cdataContent = new StringBuffer JavaDoc();
41     Locator locator;
42
43     public Handler()
44     {
45     }
46
47     public Element getResult() throws ElementException
48     {
49         if( this.rootElement == null ){
50             throw new IllegalStateException JavaDoc("root element is null.");
51         }
52
53         return this.rootElement;
54     }
55
56     public void setDocumentLocator(Locator locator)
57     {
58         this.locator = locator;
59     }
60
61     public void startDocument() throws org.xml.sax.SAXException JavaDoc
62     {
63     }
64
65     public void endDocument() throws org.xml.sax.SAXException JavaDoc
66     {
67     }
68
69     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws org.xml.sax.SAXException JavaDoc
70     {
71         if( uri.equals(NAMESPACE_URI_JICE_2_0) || uri.equals(NAMESPACE_URI_JICE_2_1) ){
72             // a valid JICE namespace prefix has been specified.
73
this.jiceNameSpace = uri;
74             this.jiceNamespacePrefix = prefix;
75             if( this.jiceNamespacePrefix == null || this.jiceNamespacePrefix.length() == 0){
76                 this.defaultNamespaceUsed = true;
77             }
78             else {
79                 this.defaultNamespaceUsed = false;
80             }
81       
82       if( uri.equals(NAMESPACE_URI_JICE_2_1)){
83         syntaxBasedCdataConversionSupport = true;
84       }
85         }
86     }
87
88     public void endPrefixMapping(String JavaDoc prefix) throws org.xml.sax.SAXException JavaDoc
89     {
90         // should we do something if the current org.jicengine-namespace-prefix-mapping ends?
91
// or are we informed with a startPrefixMapping()-call of the future
92
// prefixes?
93
}
94
95     /**
96      * @param jiceAttributeName the local name of a JICE-attribute e.g. 'action'.
97      */

98     protected String JavaDoc getAttribute(Attributes attributes, String JavaDoc jiceAttributeName)
99     {
100         String JavaDoc value = attributes.getValue(jiceAttributeName);
101         if( value == null ){
102             // ok, lets try to specify the namespace also.
103
value = attributes.getValue(this.jiceNameSpace, jiceAttributeName);
104         }
105         return value;
106     }
107
108     public void startElement(String JavaDoc nameSpaceUri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) throws org.xml.sax.SAXException JavaDoc
109     {
110         try {
111             doStartElement(nameSpaceUri,localName,qName,attributes);
112         } catch (Exception JavaDoc e){
113             throw new org.xml.sax.SAXException JavaDoc(e);
114         }
115     }
116
117     protected void doStartElement(String JavaDoc nameSpaceUri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) throws Exception JavaDoc, org.xml.sax.SAXException JavaDoc
118     {
119         Location location;
120         if( this.locator != null ){
121             location = new Location(this.locator.getLineNumber(), locator.getSystemId(), this.elementCompilers.size());
122         }
123         else {
124             location = new Location(-1, "?", 0);
125         }
126
127         if( !nameSpaceUri.equals(NAMESPACE_URI_JICE_2_0) && !nameSpaceUri.equals(NAMESPACE_URI_JICE_2_1)){
128             throw new ElementException("Element '" + localName +"' is in illegal namespace '" + nameSpaceUri + "'. only '" + NAMESPACE_URI_JICE_2_0 + "' and '" + NAMESPACE_URI_JICE_2_1 + "' supported.", localName, location);
129         }
130         final ElementCompiler current;
131
132     Type type;
133         String JavaDoc typeExpression = getAttribute(attributes, ElementCompiler.ATTR_NAME_TYPE);
134         if( typeExpression != null ){
135       type = Type.parse(typeExpression);
136     }
137     else {
138             type = DefaultJiceTypes.getDefaultType();
139         }
140
141         current = DefaultJiceTypes.getTypeManager().createCompiler(localName, location, type);
142
143         // ATTRIBUTE HANDLING
144

145         for (int i = 0; i < attributes.getLength(); i++) {
146
147       String JavaDoc attrName = attributes.getLocalName(i);
148       String JavaDoc attrValue = attributes.getValue(i);
149       String JavaDoc attrUri = attributes.getURI(i);
150
151             if( attrName.length() == 0 ){
152                 // attribute 'xmlns'. ignore
153
continue;
154             }
155             else if( attrUri.length() == 0 && !attributes.getQName(i).startsWith("xmlns:") ){
156         // attribute has no namespace
157
// -> since we are inside a JIC element,
158
// the attribute is a JIC attribute
159
setAttribute(attrName, attrValue, current);
160             }
161             else if( attrUri.equals(NAMESPACE_URI_JICE_2_0) || attrUri.equals(NAMESPACE_URI_JICE_2_1)){
162         // attribute belongs to JICE namespace
163
// -> definitely a JIC attribute
164
setAttribute(attrName, attrValue, current);
165             }
166             else {
167                 //System.out.println("ignored: " + attrName + "=\"" + attrValue + "\"");
168
// attribute belongs to some unknown namespace
169
// -> just ignore it
170
continue;
171             }
172         }
173
174
175         // tell the element that it's attributes have been set
176
current.elementInitialized();
177
178         this.elementCompilers.push(current);
179     }
180
181     protected void setAttribute(String JavaDoc name, String JavaDoc value, ElementCompiler elementCompiler) throws Exception JavaDoc
182     {
183         if( name.equals(ElementCompiler.ATTR_NAME_CLASS) ){
184             elementCompiler.setInstanceClass(value);
185         }
186         else if( name.equals(ElementCompiler.ATTR_NAME_TYPE) ){
187             // nothing..
188
}
189         else if( name.equals(ElementCompiler.ATTR_NAME_INSTANCE) ){
190             elementCompiler.setConstructor(value);
191         }
192         else if( name.equals(ElementCompiler.ATTR_NAME_ACTION) ){
193             elementCompiler.setAction(value);
194         }
195         else if( name.equals(ElementCompiler.ATTR_NAME_CONSTRUCTOR_ARGUMENTS) ){
196             elementCompiler.setConstructorArguments(value);
197         }
198         else if( name.equals(ElementCompiler.ATTR_NAME_VARIABLES) ){
199             elementCompiler.setVariables(value);
200         }
201         else if( name.equals(ElementCompiler.ATTR_NAME_IF) ){
202             elementCompiler.setIf(value);
203         }
204         else if( name.equals(ElementCompiler.ATTR_NAME_OVERRIDABLE_BY) ){
205             elementCompiler.setOverridableBy(value);
206         }
207         else {
208             throw new ElementException("Unsupported attribute: " + name + "=" + value, elementCompiler.getName(), elementCompiler.getLocation());
209         }
210     }
211
212     public void endElement(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName) throws org.xml.sax.SAXException JavaDoc
213     {
214         try {
215             doEndElement(namespaceUri,localName, qName);
216         } catch (ElementException e){
217             throw new org.xml.sax.SAXException JavaDoc(e);
218         } catch (RuntimeException JavaDoc e2){
219             throw new org.xml.sax.SAXException JavaDoc(new org.jicengine.JICException("Unexpected runtime exception at line " + this.locator.getLineNumber(), e2));
220         } catch (Exception JavaDoc e3) {
221             throw new org.xml.sax.SAXException JavaDoc(e3);
222         }
223     }
224
225     protected void doEndElement(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName) throws Exception JavaDoc, org.xml.sax.SAXException JavaDoc
226     {
227         ElementCompiler current = (ElementCompiler) this.elementCompilers.pop();
228
229         // cdata of the element
230
//
231
// NOTE: because we clear the buffer here, there can't be nested
232
// cdata-blocks. Consider the following situation:
233
// --------
234
// <parent>
235
// some text as CData
236
// <child>text of the child-element</child>
237
// </parent>
238
// --------
239
// in that case, the child will receive all the cdata,also
240
// the cdata of the parent, and the parent won't receive any
241
// cdata at all (the child has consumed it all).
242
String JavaDoc cdata = cdataContent.toString().trim();
243         if( cdata.length() > 0 ){
244             current.setCData(cdata, this.syntaxBasedCdataConversionSupport);
245         }
246         cdataContent = new StringBuffer JavaDoc();
247
248         // element processed.. let the compiler create the element.
249

250         Element element = current.createElement();
251
252         if( !this.elementCompilers.isEmpty() ){
253             ElementCompiler parent = (ElementCompiler) this.elementCompilers.peek();
254
255             // let the parent handle the element
256
parent.handleChildElement(element);
257         }
258         else {
259             // no parents: this is the root element?
260
this.rootElement = element;
261         }
262
263     }
264
265     public void characters(char[] cdata, int start, int length) throws org.xml.sax.SAXException JavaDoc
266     {
267         this.cdataContent.append(cdata, start, length);
268     }
269
270     public void ignorableWhitespace(char[] parm1, int parm2, int parm3) throws org.xml.sax.SAXException JavaDoc
271     {
272     }
273
274     public void processingInstruction(String JavaDoc parm1, String JavaDoc parm2) throws org.xml.sax.SAXException JavaDoc
275     {
276     }
277
278     public void skippedEntity(String JavaDoc parm1) throws org.xml.sax.SAXException JavaDoc
279     {
280     }
281 }
282
Popular Tags