1 4 package com.openedit.config; 5 6 import java.io.IOException ; 7 import java.io.Reader ; 8 import java.io.StringWriter ; 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Set ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.dom4j.Attribute; 18 import org.dom4j.Document; 19 import org.dom4j.DocumentHelper; 20 import org.dom4j.Element; 21 import org.dom4j.io.OutputFormat; 22 import org.dom4j.io.XMLWriter; 23 24 import com.openedit.OpenEditRuntimeException; 25 import com.openedit.util.XmlUtil; 26 27 31 public class XMLConfiguration implements Configuration 32 { 33 private static final Log log = LogFactory.getLog(XMLConfiguration.class); 34 protected HashMap fieldAttributes; 35 protected String fieldText; 36 protected String fieldName; 37 protected List fieldChildren; 38 protected Configuration fieldParent; 39 40 43 public XMLConfiguration() 44 { 45 } 46 47 public XMLConfiguration(String inName) 48 { 49 setName(inName); 50 } 51 52 55 public void populate(Element inRootElement) 56 { 57 setName(inRootElement.getName()); 58 setValue(inRootElement.getText()); 59 60 for (Iterator iter = inRootElement.attributeIterator(); iter.hasNext();) 61 { 62 Attribute attrib = (Attribute) iter.next(); 63 addAttribute(attrib.getName(),attrib.getValue()); 64 } 65 66 for (Iterator iter = inRootElement.elementIterator(); iter.hasNext();) 67 { 68 Element element = (Element) iter.next(); 69 70 addChild(new XMLConfiguration(element)); 71 } 72 73 } 74 75 78 public Configuration addChild(Configuration inConfiguration) 79 { 80 if ( fieldChildren == null) 81 { 82 fieldChildren = new ArrayList (); 83 } 84 fieldChildren.add( inConfiguration); 85 inConfiguration.setParent(this); 86 return inConfiguration; 87 } 88 89 93 private void addAttribute(String inName, String inValue) 94 { 95 if ( fieldAttributes == null) 96 { 97 fieldAttributes = new HashMap (); 98 } 99 fieldAttributes.put( inName, inValue); 100 } 101 102 105 public XMLConfiguration(Element inChild) 106 { 107 populate(inChild); 108 } 109 110 public XMLConfiguration(Reader inRead) 111 { 112 readXML( inRead); 113 } 114 115 118 public List getChildren(String inString) 119 { 120 List hits = new ArrayList (realChildren().size()); 122 for (Iterator iter = realChildren().iterator(); iter.hasNext();) 123 { 124 XMLConfiguration element = (XMLConfiguration) iter.next(); 125 if ( inString.equals( element.getName()) ) 126 { 127 hits.add( element); 128 } 129 } 130 131 132 return hits; 133 } 134 135 136 139 public String getAttribute(String inString) 140 { 141 return (String )getAttributes().get( inString); 142 } 143 public void setAttribute(String inKey, String inValue) 144 { 145 getAttributes().put(inKey,inValue); 146 } 147 150 public String getValue() 151 { 152 return fieldText; 153 } 154 public void setValue( String inValue) 155 { 156 fieldText = inValue; 157 if ( fieldText != null) 158 { 159 fieldText = fieldText.trim(); 160 if ( fieldText.length() == 0) 161 { 162 fieldText = null; 163 } 164 } 165 } 166 167 170 public Configuration getChild(String inString) 171 { 172 for (Iterator iter = realChildren().iterator(); iter.hasNext();) 173 { 174 XMLConfiguration config = (XMLConfiguration) iter.next(); 175 if ( inString.equals( config.getName() ) ) 176 { 177 return config; 178 } 179 } 180 return null; 181 } 182 protected List realChildren() 183 { 184 if (fieldChildren == null) 185 { 186 fieldChildren = new ArrayList (); 187 188 } 189 190 return fieldChildren; 191 } 192 193 196 public List getChildren() 197 { 198 199 return realChildren(); 200 201 } 202 203 206 public String getChildValue(String inString) 207 { 208 Configuration config = getChild(inString); 209 if ( config != null) 210 { 211 return config.getValue(); 212 } 213 return null; 214 } 215 216 219 public List getAttributeNames() 220 { 221 Set attribs = getAttributes().keySet(); 222 List names = new ArrayList (attribs.size()); 223 224 for (Iterator iter = attribs.iterator(); iter.hasNext();) 225 { 226 String a = (String ) iter.next(); 227 names.add( a ); 228 } 229 return names; 230 } 231 232 233 public HashMap getAttributes() 234 { 235 if (fieldAttributes == null) 236 { 237 fieldAttributes = new HashMap (2); 238 } 239 return fieldAttributes; 240 } 241 public void setAttributes(HashMap inAttributes) 242 { 243 fieldAttributes = inAttributes; 244 } 245 public String getName() 246 { 247 return fieldName; 248 } 249 public void setName(String inName) 250 { 251 fieldName = inName; 252 } 253 254 258 public Configuration addChild(String inString) 259 { 260 XMLConfiguration config = new XMLConfiguration(inString); 261 addChild(config); 262 return config; 263 } 264 265 269 public Iterator getChildIterator(String inString) 270 { 271 return getChildren(inString).iterator(); 272 } 273 276 public void removeChild(Configuration inElement) 277 { 278 getChildren().remove(inElement); 279 } 280 281 public Configuration getParent() 282 { 283 return fieldParent; 284 } 285 public void setParent(Configuration inParent) 286 { 287 fieldParent = inParent; 288 } 289 public String toString() 290 { 291 return toXml("UTF-8"); 292 } 293 301 public String toXml( String inEncoding ) 302 { 303 Document doc = DocumentHelper.createDocument(); 304 Element root = doc.addElement(getName()); 305 appendXml(this,root); 306 StringWriter text = new StringWriter (); 307 OutputFormat format = OutputFormat.createPrettyPrint(); 308 format.setEncoding(inEncoding); 309 XMLWriter out = new XMLWriter(text, format); 310 try 311 { 312 out.write(doc); 313 } 314 catch (IOException ex) 315 { 316 throw new OpenEditRuntimeException(ex); 317 } 318 return text.toString(); 319 } 320 public Document asXml() 321 { 322 Document doc = DocumentHelper.createDocument(); 323 Element root = doc.addElement(getName()); 324 appendXml(this,root); 325 return doc; 326 } 327 328 329 333 public void appendXml(XMLConfiguration inConfiguration, Element inElement) 334 { 335 for (Iterator iter = inConfiguration.getAttributeNames().iterator(); iter.hasNext();) 336 { 337 String id = (String ) iter.next(); 338 inElement.addAttribute(id,inConfiguration.getAttribute(id)); 339 } 340 for (Iterator iter = inConfiguration.getChildren().iterator(); iter.hasNext();) 341 { 342 XMLConfiguration child = (XMLConfiguration) iter.next(); 343 appendXml(child,inElement.addElement(child.getName())); 344 } 345 if ( inConfiguration.getValue() != null && inConfiguration.getValue().trim().length() > 0) 346 { 347 inElement.setText(inConfiguration.getValue()); 348 } 349 } 350 public boolean hasChildren() 351 { 352 return fieldChildren != null && getChildren().size() > 0; 353 } 354 355 359 public boolean hasChild(String inString) 360 { 361 return getChild(inString) != null; 362 } 363 367 public void readXML(Reader inReader) 368 { 369 XmlUtil util = new XmlUtil(); 370 Element root = util.getXml(inReader, "UTF-8"); 371 populate(root); 372 } 373 } 374 | Popular Tags |