Changeset 8
- Timestamp:
- 06/29/05 14:55:36 (3 years ago)
- Files:
-
- trunk/src/net/schst/XJConf/Examples/ExampleInclude.java (added)
- trunk/src/net/schst/XJConf/Extension.java (added)
- trunk/src/net/schst/XJConf/TagDefinition.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/XmlReader.java (modified) (8 diffs)
- trunk/src/net/schst/XJConf/ext (added)
- trunk/src/net/schst/XJConf/ext/XInclude.java (added)
- trunk/xml/test-hashmap.xml (modified) (2 diffs)
- trunk/xml/test-xinclude.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/net/schst/XJConf/TagDefinition.java
r5 r8 253 253 } else { 254 254 Class childParamTypes[] = {childValue.getClass()}; 255 255 256 256 Method childMethod = cl.getMethod(methodName, childParamTypes); 257 childMethod.invoke(instance, childParams);257 childMethod.invoke(instance, childParams); 258 258 } 259 259 } catch (Throwable t) { 260 throw new ValueConversionException("Could not add child " + child.getKey() + " to " + this.getType() + " .", t);260 throw new ValueConversionException("Could not add child " + child.getKey() + " to " + this.getType() + " using "+methodName+"().", t); 261 261 } 262 262 } trunk/src/net/schst/XJConf/XmlReader.java
r1 r8 47 47 private int depth = 0; 48 48 49 /** 50 * Extensions used by the parser 51 */ 52 private HashMap extensions = new HashMap(); 53 54 /** 55 * Internal namespace 56 */ 57 private String myNamespace = "http://www.schst.net/XJConf"; 58 59 /** 60 * all files that currently are being processed 61 */ 62 private Stack openFiles = new Stack(); 63 49 64 /** 50 65 * Set the tag definitions … … 61 76 public void setTagDefinitions(NamespaceDefinitions defs) { 62 77 this.tagDefs = defs; 78 } 79 80 /** 81 * Add a new extension 82 * 83 * @param namespace 84 * @param ext 85 */ 86 public void addExtension(String namespace, Extension ext) { 87 this.extensions.put(namespace, ext); 63 88 } 64 89 … … 72 97 SAXParserFactory factory = SAXParserFactory.newInstance(); 73 98 factory.setNamespaceAware(true); 99 100 this.openFiles.push(file); 74 101 75 102 try { … … 79 106 t.printStackTrace(); 80 107 } 81 } 82 108 this.openFiles.pop(); 109 } 110 111 /** 112 * Get the file that currently is being parsed 113 * 114 * @return 115 */ 116 public File getCurrentFile() { 117 return (File)this.openFiles.peek(); 118 } 119 83 120 /** 84 121 * Parse a configuration file. … … 99 136 public void startElement(String namespaceURI, String sName, String qName, Attributes atts) 100 137 throws SAXException { 138 if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 139 return; 140 } 141 101 142 this.depth++; 102 143 … … 110 151 return; 111 152 } 112 113 if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { 114 throw new UnknownNamespaceException("Unknown namespace " + namespaceURI); 115 } 116 117 if (!this.tagDefs.isTagDefined(namespaceURI, sName)) { 118 throw new UnknownTagException("Unknown tag " + sName); 119 } 153 120 154 Tag tag = new Tag(qName, atts); 121 155 // fetch the defintion for this tag 122 156 TagDefinition tagDef = this.tagDefs.getTagDefinition(namespaceURI, sName); 123 157 tag.setDefinition(tagDef); 158 159 if (this.extensions.containsKey(namespaceURI)) { 160 ((Extension)(this.extensions.get(namespaceURI))).startElement(this, tag); 161 } else { 162 if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { 163 throw new UnknownNamespaceException("Unknown namespace " + namespaceURI); 164 } 165 if (!this.tagDefs.isTagDefined(namespaceURI, sName)) { 166 throw new UnknownTagException("Unknown tag " + sName); 167 } 168 } 124 169 this.tagStack.push(tag); 125 170 } … … 133 178 public void endElement(String namespaceURI, String sName, String qName) 134 179 throws SAXException { 180 if (this.myNamespace.equals(namespaceURI) && this.depth > 0) { 181 return; 182 } 183 135 184 this.depth--; 136 185 … … 144 193 return; 145 194 } 146 195 147 196 // get the last tag from the stack 148 197 Tag tag = (Tag)this.tagStack.pop(); 149 150 if (this. depth == 1) {151 this.config.put(tag.getKey(), tag.getConvertedValue());198 199 if (this.extensions.containsKey(namespaceURI)) { 200 ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 152 201 } else { 153 Tag parent = (Tag)this.tagStack.pop(); 154 parent.addChild(tag); 155 this.tagStack.push(parent); 202 203 if (this.depth == 1) { 204 this.config.put(tag.getKey(), tag.getConvertedValue()); 205 } else { 206 Tag parent = (Tag)this.tagStack.pop(); 207 parent.addChild(tag); 208 this.tagStack.push(parent); 209 } 156 210 } 157 211 } trunk/xml/test-hashmap.xml
r3 r8 1 < configuration>1 <xj:configuration xmlns:xj="http://www.schst.net/XJConf"> 2 2 <map> 3 3 <prop name="foo">bar</prop> … … 9 9 <prop name="argh">tomato</prop> 10 10 </properties> 11 </ configuration>11 </xj:configuration>
