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 ; 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 25 26 public class Handler extends DefaultHandler { 27 28 public static final String NAMESPACE_URI_JICE_2_0 = "http://www.jicengine.org/jic/2.0"; 30 public static final String 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 jiceNameSpace; 36 private String jiceNamespacePrefix = ""; 37 boolean syntaxBasedCdataConversionSupport = false; 38 private boolean defaultNamespaceUsed = true; 39 40 StringBuffer cdataContent = new StringBuffer (); 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 ("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  62 { 63 } 64 65 public void endDocument() throws org.xml.sax.SAXException  66 { 67 } 68 69 public void startPrefixMapping(String prefix, String uri) throws org.xml.sax.SAXException  70 { 71 if( uri.equals(NAMESPACE_URI_JICE_2_0) || uri.equals(NAMESPACE_URI_JICE_2_1) ){ 72 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 prefix) throws org.xml.sax.SAXException  89 { 90 } 94 95 98 protected String getAttribute(Attributes attributes, String jiceAttributeName) 99 { 100 String value = attributes.getValue(jiceAttributeName); 101 if( value == null ){ 102 value = attributes.getValue(this.jiceNameSpace, jiceAttributeName); 104 } 105 return value; 106 } 107 108 public void startElement(String nameSpaceUri, String localName, String qName, Attributes attributes) throws org.xml.sax.SAXException  109 { 110 try { 111 doStartElement(nameSpaceUri,localName,qName,attributes); 112 } catch (Exception e){ 113 throw new org.xml.sax.SAXException (e); 114 } 115 } 116 117 protected void doStartElement(String nameSpaceUri, String localName, String qName, Attributes attributes) throws Exception , org.xml.sax.SAXException  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 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 145 for (int i = 0; i < attributes.getLength(); i++) { 146 147 String attrName = attributes.getLocalName(i); 148 String attrValue = attributes.getValue(i); 149 String attrUri = attributes.getURI(i); 150 151 if( attrName.length() == 0 ){ 152 continue; 154 } 155 else if( attrUri.length() == 0 && !attributes.getQName(i).startsWith("xmlns:") ){ 156 setAttribute(attrName, attrValue, current); 160 } 161 else if( attrUri.equals(NAMESPACE_URI_JICE_2_0) || attrUri.equals(NAMESPACE_URI_JICE_2_1)){ 162 setAttribute(attrName, attrValue, current); 165 } 166 else { 167 continue; 171 } 172 } 173 174 175 current.elementInitialized(); 177 178 this.elementCompilers.push(current); 179 } 180 181 protected void setAttribute(String name, String value, ElementCompiler elementCompiler) throws Exception  182 { 183 if( name.equals(ElementCompiler.ATTR_NAME_CLASS) ){ 184 elementCompiler.setInstanceClass(value); 185 } 186 else if( name.equals(ElementCompiler.ATTR_NAME_TYPE) ){ 187 } 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 namespaceUri, String localName, String qName) throws org.xml.sax.SAXException  213 { 214 try { 215 doEndElement(namespaceUri,localName, qName); 216 } catch (ElementException e){ 217 throw new org.xml.sax.SAXException (e); 218 } catch (RuntimeException e2){ 219 throw new org.xml.sax.SAXException (new org.jicengine.JICException("Unexpected runtime exception at line " + this.locator.getLineNumber(), e2)); 220 } catch (Exception e3) { 221 throw new org.xml.sax.SAXException (e3); 222 } 223 } 224 225 protected void doEndElement(String namespaceUri, String localName, String qName) throws Exception , org.xml.sax.SAXException  226 { 227 ElementCompiler current = (ElementCompiler) this.elementCompilers.pop(); 228 229 String cdata = cdataContent.toString().trim(); 243 if( cdata.length() > 0 ){ 244 current.setCData(cdata, this.syntaxBasedCdataConversionSupport); 245 } 246 cdataContent = new StringBuffer (); 247 248 250 Element element = current.createElement(); 251 252 if( !this.elementCompilers.isEmpty() ){ 253 ElementCompiler parent = (ElementCompiler) this.elementCompilers.peek(); 254 255 parent.handleChildElement(element); 257 } 258 else { 259 this.rootElement = element; 261 } 262 263 } 264 265 public void characters(char[] cdata, int start, int length) throws org.xml.sax.SAXException  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  271 { 272 } 273 274 public void processingInstruction(String parm1, String parm2) throws org.xml.sax.SAXException  275 { 276 } 277 278 public void skippedEntity(String parm1) throws org.xml.sax.SAXException  279 { 280 } 281 } 282
| Popular Tags
|