Changeset 39
- Timestamp:
- 08/03/05 13:00:09 (3 years ago)
- Files:
-
- trunk/src/net/schst/XJConf/AttributeDefinition.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/CDataDefinition.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/ChildDefinition.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/ConstructorDefinition.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/DefinedTag.java (modified) (6 diffs)
- trunk/src/net/schst/XJConf/Definition.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/Examples/ExampleExtension.java (added)
- trunk/src/net/schst/XJConf/Extension.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/GenericTag.java (modified) (3 diffs)
- trunk/src/net/schst/XJConf/ObjectValueConverter.java (modified) (1 diff)
- trunk/src/net/schst/XJConf/Tag.java (modified) (2 diffs)
- trunk/src/net/schst/XJConf/TagDefinition.java (modified) (8 diffs)
- trunk/src/net/schst/XJConf/XmlReader.java (modified) (3 diffs)
- trunk/src/net/schst/XJConf/ext/Math.java (added)
- trunk/src/net/schst/XJConf/ext/XInclude.java (modified) (4 diffs)
- trunk/xml/defines-extension.xml (added)
- trunk/xml/defines-hashmap.xml (modified) (1 diff)
- trunk/xml/test-extension.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/net/schst/XJConf/AttributeDefinition.java
r37 r39 167 167 * @return Class object 168 168 */ 169 public Class getValueType( DefinedTag tag, ClassLoader loader) {169 public Class getValueType(Tag tag, ClassLoader loader) { 170 170 try { 171 171 return this.vConverter.getType(loader); … … 198 198 * @throws ValueConversionException 199 199 */ 200 public Object convertValue( DefinedTag tag, ClassLoader loader)200 public Object convertValue(Tag tag, ClassLoader loader) 201 201 throws ValueConversionException { 202 202 String value; trunk/src/net/schst/XJConf/CDataDefinition.java
r37 r39 74 74 * Convert the character data to any type 75 75 */ 76 public Object convertValue( DefinedTag tag, ClassLoader loader)76 public Object convertValue(Tag tag, ClassLoader loader) 77 77 throws ValueConversionException { 78 78 79 Object params[] = {tag.getData()}; 80 Class paramTypes[] = {String.class}; 79 Object[] params = {tag.getContent()}; 80 Class[] paramTypes = new Class[1]; 81 if (tag.getContent() == null) { 82 paramTypes[0] = String.class; 83 } else { 84 paramTypes[0] = tag.getContent().getClass(); 85 } 81 86 82 87 Object instance = this.vConverter.convertValue(params, paramTypes, loader); … … 96 101 * @return Class object 97 102 */ 98 public Class getValueType( DefinedTag tag, ClassLoader loader) {103 public Class getValueType(Tag tag, ClassLoader loader) { 99 104 try { 100 105 return this.vConverter.getType(loader); trunk/src/net/schst/XJConf/ChildDefinition.java
r37 r39 41 41 * Convert the value 42 42 */ 43 public Object convertValue( DefinedTag tag, ClassLoader loader)43 public Object convertValue(Tag tag, ClassLoader loader) 44 44 throws ValueConversionException { 45 DefinedTag child = tag.getChild(this.getName());45 Tag child = tag.getChild(this.getName()); 46 46 if (child == null) { 47 47 throw new RuntimeException("Child element " + this.getName() + " does not exist"); … … 55 55 * @return Class object 56 56 */ 57 public Class getValueType( DefinedTag tag, ClassLoader loader) {58 DefinedTag child = tag.getChild(this.getName());57 public Class getValueType(Tag tag, ClassLoader loader) { 58 Tag child = tag.getChild(this.getName()); 59 59 if (child == null) { 60 60 throw new RuntimeException("Child element " + this.getName() + " does not exist"); trunk/src/net/schst/XJConf/ConstructorDefinition.java
r37 r39 37 37 * This does not do anything! 38 38 */ 39 public Object convertValue( DefinedTag tag, ClassLoader loader)39 public Object convertValue(Tag tag, ClassLoader loader) 40 40 throws ValueConversionException { 41 41 return null; … … 54 54 * @return Always returns null 55 55 */ 56 public Class getValueType( DefinedTag tag, ClassLoader loader) {56 public Class getValueType(Tag tag, ClassLoader loader) { 57 57 return null; 58 58 } trunk/src/net/schst/XJConf/DefinedTag.java
r38 r39 17 17 * @author Stephan Schmidt 18 18 */ 19 public class DefinedTag {19 public class DefinedTag implements Tag { 20 20 21 21 /** … … 28 28 */ 29 29 private StringBuffer data = new StringBuffer(); 30 31 32 private Object content = null; 30 33 31 34 /** … … 74 77 * @return number of childs added 75 78 */ 76 public int addChild( DefinedTag child) {79 public int addChild(Tag child) { 77 80 this.children.add(child); 78 81 return this.children.size(); … … 123 126 * @return 124 127 */ 125 public DefinedTag getChild(String name) {128 public Tag getChild(String name) { 126 129 for (Iterator iter = this.children.iterator(); iter.hasNext();) { 127 130 DefinedTag child = (DefinedTag) iter.next(); … … 183 186 * @return Class object 184 187 */ 185 public Class getValueType( DefinedTag tag, ClassLoader loader) {188 public Class getValueType(Tag tag, ClassLoader loader) { 186 189 return this.def.getValueType(tag, loader); 187 190 } … … 196 199 return this.def.getKey(this); 197 200 } 201 202 public String getSetterMethod() { 203 return this.def.getSetterMethod(); 204 } 205 206 public void setContent(Object content) { 207 this.content = content; 208 } 209 210 public Object getContent() { 211 if (this.content != null) { 212 return this.content; 213 } 214 return this.getData(); 215 } 216 217 public boolean supportsIndexedChildren() { 218 return this.def.supportsIndexedChildren(); 219 } 198 220 } trunk/src/net/schst/XJConf/Definition.java
r37 r39 27 27 * @throws ValueConversionException 28 28 */ 29 public Object convertValue( DefinedTag tag, ClassLoader loader) throws ValueConversionException;29 public Object convertValue(Tag tag, ClassLoader loader) throws ValueConversionException; 30 30 31 31 /** … … 35 35 * @return 36 36 */ 37 public Class getValueType( DefinedTag tag, ClassLoader loader);37 public Class getValueType(Tag tag, ClassLoader loader); 38 38 39 39 /** trunk/src/net/schst/XJConf/Extension.java
r37 r39 25 25 * @throws XJConfException 26 26 */ 27 public void startElement(XmlReader reader, DefinedTag tag)27 public void startElement(XmlReader reader, Tag tag) 28 28 throws XJConfException; 29 29 … … 35 35 * @throws XJConfException 36 36 */ 37 public Object endElement(XmlReader reader, DefinedTag tag)37 public Tag endElement(XmlReader reader, Tag tag) 38 38 throws XJConfException; 39 39 } trunk/src/net/schst/XJConf/GenericTag.java
r38 r39 28 28 29 29 /** 30 * Content of the tag (overrides data) 31 */ 32 private Object content = null; 33 34 /** 30 35 * attributes of the tag 31 36 */ … … 37 42 private ArrayList children = new ArrayList(); 38 43 44 /** 45 * value of the tag 46 */ 47 private Object value = null; 48 49 /** 50 * Key of the tag 51 */ 52 private String key = null; 53 39 54 /** 40 55 * Create a new tag without attributes … … 61 76 } 62 77 63 /** 64 * Add text data 65 */ 66 public int addData(char buf[], int offset, int len) { 67 String s = new String(buf, offset, len); 68 this.data.append(s); 69 return this.data.length(); 70 } 71 72 /** 73 * Check, whether the tag has a certain attribute 74 * 75 * @param name 76 * @return 77 */ 78 public boolean hasAttribute(String name) { 79 return this.atts.containsKey(name); 80 } 81 82 /** 83 * get an attribute 84 * 85 * @param name of the attribute 86 * @return value of the attribute 87 */ 88 public String getAttribute(String name) { 89 return (String)this.atts.get(name); 90 } 91 92 /** 93 * Get all children of the tag 94 * 95 * @return children 96 */ 97 public ArrayList getChildren() { 98 return this.children; 99 } 100 101 /** 102 * Get the child with a specific name 103 * 104 * @param name 105 * @return 106 */ 107 public Tag getChild(String name) { 108 for (Iterator iter = this.children.iterator(); iter.hasNext();) { 109 Tag child = (Tag) iter.next(); 110 if (child.getName().equals(name)) { 111 return child; 112 } 113 } 114 return null; 115 } 116 117 /** 118 * Get the name of the tag 119 * 120 * @return name of the tag 121 */ 122 public String getName() { 123 return this.name; 124 } 125 126 /** 127 * Get the character data of the tag 128 * 129 * @return character data 130 */ 131 public String getData() { 132 return this.data.toString().trim(); 133 } 134 135 78 /** 79 * Add text data 80 */ 81 public int addData(char buf[], int offset, int len) { 82 String s = new String(buf, offset, len); 83 this.data.append(s); 84 return this.data.length(); 85 } 86 87 /** 88 * Check, whether the tag has a certain attribute 89 * 90 * @param name 91 * @return 92 */ 93 public boolean hasAttribute(String name) { 94 return this.atts.containsKey(name); 95 } 96 97 /** 98 * get an attribute 99 * 100 * @param name of the attribute 101 * @return value of the attribute 102 */ 103 public String getAttribute(String name) { 104 return (String) this.atts.get(name); 105 } 106 107 /** 108 * Get all children of the tag 109 * 110 * @return children 111 */ 112 public ArrayList getChildren() { 113 return this.children; 114 } 115 116 /** 117 * Get the child with a specific name 118 * 119 * @param name 120 * @return 121 */ 122 public Tag getChild(String name) { 123 for (Iterator iter = this.children.iterator(); iter.hasNext();) { 124 Tag child = (Tag) iter.next(); 125 if (child.getName().equals(name)) { 126 return child; 127 } 128 } 129 return null; 130 } 131 132 /** 133 * Get the name of the tag 134 * 135 * @return name of the tag 136 */ 137 public String getName() { 138 return this.name; 139 } 140 141 /** 142 * Get the character data of the tag 143 * 144 * @return character data 145 */ 146 public String getData() { 147 return this.data.toString().trim(); 148 } 149 150 /** 151 * Add a new child to this tag. 152 * 153 * @param child child to add 154 * @return number of childs added 155 */ 136 156 public int addChild(Tag child) { 137 return 0; 138 } 139 157 this.children.add(child); 158 return this.children.size(); 159 } 160 161 /** 162 * Fetch the value 163 * 164 * @param loader 165 * @retun the value of the tag 166 */ 140 167 public Object getConvertedValue(ClassLoader loader) 141 168 throws ValueConversionException { 142 return null; 143 } 144 169 return this.value; 170 } 171 172 /** 173 * Get the key under which the value will be stored 174 */ 145 175 public String getKey() { 146 // TODO Auto-generated method stub 147 return null; 148 } 149 150 public Class getValueType(DefinedTag tag, ClassLoader loader) { 151 // TODO Auto-generated method stub 152 return null; 153 } 176 return this.key; 177 } 178 179 /** 180 * Set the value of the tag 181 * 182 * @param value 183 */ 184 public void setValue(Object value) { 185 this.value = value; 186 } 187 188 /** 189 * Get the type of the value 190 */ 191 public Class getValueType(Tag tag, ClassLoader loader) { 192 if (this.value == null) { 193 return null; 194 } 195 return this.value.getClass(); 196 } 197 198 /** 199 * Set the key 200 * 201 * @param key 202 */ 203 public void setKey(String key) { 204 this.key = key; 205 } 206 207 /** 208 * Get the setter method 209 */ 210 public String getSetterMethod() { 211 if (this.key == null) { 212 return null; 213 } 214 return "set" + this.key.substring(0,1).toUpperCase() + this.key.substring(1); 215 } 216 217 /** 218 * Set the content (overrides the character data) 219 */ 220 public void setContent(Object content) { 221 this.content = content; 222 } 223 224 /** 225 * Get the content 226 */ 227 public Object getContent() { 228 if (this.content != null) { 229 return this.content; 230 } 231 return this.getData(); 232 } 233 234 /** 235 * Checks, whether the tag supports indexed children 236 */ 237 public boolean supportsIndexedChildren() { 238 return true; 239 } 154 240 } trunk/src/net/schst/XJConf/ObjectValueConverter.java
r32 r39 46 46 // try to create a new instance 47 47 try { 48 Constructor co = instanceClass.getConstructor(types); 48 Constructor co; 49 try { 50 co = instanceClass.getConstructor(types); 51 } catch (NoSuchMethodException e) { 52 // try to convert the values to a string 53 for (int i = 0; i < types.length; i++) { 54 types[i] = String.class; 55 values[i] = values[0].toString(); 56 } 57 co = instanceClass.getConstructor(types); 58 } 49 59 instance = co.newInstance(values); 50 60 } catch (Exception e){ trunk/src/net/schst/XJConf/Tag.java
r38 r39 2 2 3 3 import java.util.ArrayList; 4 5 4 import net.schst.XJConf.exceptions.ValueConversionException; 6 5 6 /** 7 * Interface for tag containers 8 * 9 * @author Stephan Schmidt <me@schst.net> 10 */ 7 11 public interface Tag { 8 12 public int addChild(Tag child); 9 13 public int addData(char buf[], int offset, int len); 14 public void setContent(Object content); 10 15 public boolean hasAttribute(String name); 11 16 public String getAttribute(String name); … … 14 19 public String getName(); 15 20 public String getData(); 21 public Object getContent(); 16 22 public Object getConvertedValue(ClassLoader loader) throws ValueConversionException; 17 public Class getValueType( DefinedTag tag, ClassLoader loader);23 public Class getValueType(Tag tag, ClassLoader loader); 18 24 public String getKey(); 25 public String getSetterMethod(); 26 public boolean supportsIndexedChildren(); 19 27 } trunk/src/net/schst/XJConf/TagDefinition.java
r37 r39 41 41 throw new XJConfException("TagDefinition needs a type."); 42 42 } 43 43 44 44 this.name = name; 45 45 this.tagName = name; … … 148 148 * @return Class object 149 149 */ 150 public Class getValueType( DefinedTag tag, ClassLoader loader) {150 public Class getValueType(Tag tag, ClassLoader loader) { 151 151 try { 152 152 return this.vConverter.getType(loader); … … 189 189 * @throws ValueConversionException 190 190 */ 191 public Object convertValue( DefinedTag tag, ClassLoader loader)191 public Object convertValue(Tag tag, ClassLoader loader) 192 192 throws ValueConversionException { 193 193 194 194 // get the data 195 String data = tag.getData();195 Object data = tag.getContent(); 196 196 if (data == null) { 197 197 data = ""; … … 237 237 * @throws ValueConversionException 238 238 */ 239 private void addAttributesToValue(Object instance, DefinedTag tag, ClassLoader loader)239 private void addAttributesToValue(Object instance, Tag tag, ClassLoader loader) 240 240 throws ValueConversionException { 241 241 … … 277 277 * @throws ValueConversionException 278 278 */ 279 private void addChildrenToValue(Object instance, DefinedTag tag, ClassLoader loader)279 private void addChildrenToValue(Object instance, Tag tag, ClassLoader loader) 280 280 throws ValueConversionException { 281 281 … … 292 292 293 293 for (int i = 0; i < children.size(); i++) { 294 DefinedTag child = (DefinedTag)children.get(i);294 Tag child = (Tag)children.get(i); 295 295 296 296 if (ignore.contains(child.getName())) { … … 298 298 } 299 299 300 methodName = child.get Definition().getSetterMethod();301 Object childValue = child.get Definition().convertValue(child,loader);300 methodName = child.getSetterMethod(); 301 Object childValue = child.getConvertedValue(loader); 302 302 Object childParams[] = {childValue}; 303 303 … … 356 356 } 357 357 } 358 359 /** 360 * Check, whether the value supports indexed children 361 * 362 * @return true or false 363 */ 364 public boolean supportsIndexedChildren() { 365 // TODO: Find a better (and working) way to do this check. 366 if (this.type.equals("java.util.ArrayList")) { 367 return true; 368 } 369 return false; 370 } 358 371 359 372 /** trunk/src/net/schst/XJConf/XmlReader.java
r37 r39 236 236 if (this.depth == 1) { 237 237 return; 238 } 239 240 DefinedTag tag = new DefinedTag(sName, atts); 241 // fetch the defintion for this tag 242 TagDefinition tagDef = this.tagDefs.getTagDefinition(namespaceURI, sName); 243 tag.setDefinition(tagDef); 244 238 } 239 240 // This tag needs to be handled by an extension 245 241 if (this.extensions.containsKey(namespaceURI)) { 242 Tag tag = new GenericTag(sName, atts); 246 243 ((Extension)(this.extensions.get(namespaceURI))).startElement(this, tag); 244 this.tagStack.push(tag); 245 246 // This tag has been defined internally 247 247 } else { 248 248 if (!this.tagDefs.isNamespaceDefined(namespaceURI)) { … … 253 253 throw new UnknownTagException("Unknown tag " + sName + " in namespace " + namespaceURI); 254 254 } 255 } 256 this.tagStack.push(tag); 255 DefinedTag tag = new DefinedTag(sName, atts); 256 // fetch the defintion for this tag 257 TagDefinition tagDef = this.tagDefs.getTagDefinition(namespaceURI, sName); 258 tag.setDefinition(tagDef); 259 this.tagStack.push(tag); 260 } 257 261 } 258 262 … … 281 285 282 286 // get the last tag from the stack 283 DefinedTag tag = (DefinedTag)this.tagStack.pop(); 284 287 Tag tag = (Tag)this.tagStack.pop(); 285 288 if (this.extensions.containsKey(namespaceURI)) { 286 Object result = ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 287 289 Tag result = ((Extension)(this.extensions.get(namespaceURI))).endElement(this, tag); 290 if (result != null) { 291 if (this.depth == 1) { 292 this.config.put(tag.getKey(), result.getConvertedValue(this.loader)); 293 } else { 294 Tag parent = (Tag)this.tagStack.pop(); 295 if (result.getKey() == null && !parent.supportsIndexedChildren()) { 296 parent.setContent(result.getConvertedValue(loader)); 297 } else { 298 parent.addChild(result); 299 } 300 this.tagStack.push(parent); 301 } 302 } 288 303 } else { 289 if (this.depth == 1) { 290 this.config.put(tag.getKey(), tag.getConvertedValue(this.loader)); 291 } else { 292 DefinedTag parent = (DefinedTag)this.tagStack.pop(); 293 parent.addChild(tag); 294 this.tagStack.push(parent); 295 } 296 } 304 if (this.depth == 1) { 305 this.config.put(tag.getKey(), tag.getConvertedValue(this.loader)); 306 } else { 307 Tag parent = (Tag)this.tagStack.pop(); 308 parent.addChild(tag); 309 this.tagStack.push(parent); 310 } 311 } 312 297 313 } 298 314 trunk/src/net/schst/XJConf/ext/XInclude.java
r37 r39 4 4 5 5 import net.schst.XJConf.Extension; 6 import net.schst.XJConf. DefinedTag;6 import net.schst.XJConf.Tag; 7 7 import net.schst.XJConf.XmlReader; 8 import net.schst.XJConf.exceptions.UnknownTagException; 8 9 import net.schst.XJConf.ext.xinc.XIncludeException; 9 10 … … 35 36 * child elements. 36 37 */ 37 public void startElement(XmlReader reader, DefinedTag tag) {38 public void startElement(XmlReader reader, Tag tag) { 38 39 } 39 40 … … 43 44 * Does the actual x-include. 44 45 */ 45 public Object endElement(XmlReader reader, DefinedTag tag)46 throws XIncludeException {46 public Tag endElement(XmlReader reader, Tag tag) 47 throws XIncludeException, UnknownTagException { 47 48 48 49 if (tag.getName().equals("include")) { … … 59 60 try { 60 61 reader.parse(href); 62 return null; 61 63 } catch (Exception e) { 62 64 throw new XIncludeException("Could not xInclude " + href, e); 63 65 } 64 66 } 65 return null;67 throw new UnknownTagException("Unknown tag " + tag.getName() + " in XInclude namespace."); 66 68 } 67 69 } trunk/xml/defines-hashmap.xml
r3 r39 3 3 <tag name="map" type="java.util.HashMap"/> 4 4 <tag name="prop" type="java.lang.String" keyAttribute="name"/> 5 <tag name="number" type="java.lang.Integer" keyAttribute="name"/> 5 6 </defines>
