1 6 7 package org.contineo.core; 8 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.net.URL ; 14 import java.util.Iterator ; 15 import java.util.LinkedList ; 16 import java.util.List ; 17 import org.apache.log4j.Level; 18 import org.apache.log4j.Logger; 19 import org.jdom.Attribute; 20 import org.jdom.Document; 21 import org.jdom.Element; 22 import org.jdom.JDOMException; 23 import org.jdom.input.SAXBuilder; 24 import org.jdom.output.XMLOutputter; 25 30 public class XMLBean { 31 32 36 private Document doc; 37 38 42 private Element root; 43 44 45 private String docPath = null; 46 47 48 private InputStream docInputStream = null; 49 50 54 private Logger logger; 55 56 60 public XMLBean(String docname) { 61 docPath = docname; 62 docInputStream = null; 63 configLogger(); 64 initDocument(); 65 } 66 67 71 public XMLBean(URL docname) { 72 try { 73 docPath = docname.toURI().getPath(); 74 docInputStream = null; 75 } 76 catch (Exception ex) { 77 logger.error(ex.getMessage()); 78 } 79 configLogger(); 80 initDocument(); 81 } 82 83 86 public XMLBean(InputStream is) { 87 docInputStream = is; 88 docPath = null; 89 configLogger(); 90 initDocument(); 91 } 92 93 private void configLogger() { 94 logger = LoggingManager.getLogger(this.getClass()); 95 } 96 97 private void initDocument() { 98 try { 99 SAXBuilder builder = new SAXBuilder(); 100 if (docPath != null) 101 doc = builder.build(docPath); 102 else 103 doc = builder.build(docInputStream); 104 root = doc.getRootElement(); 105 } 106 catch (JDOMException jdome) { 107 if (logger.isEnabledFor(Level.ERROR)) 108 logger.error(jdome.getMessage()); 109 } 110 } 111 112 115 public Element getRootElement() { 116 return root; 117 } 118 119 124 public Element getChild(String elemname) { 125 if (doc == null) 126 return null; 127 else 128 return root.getChild(elemname); 129 } 130 131 138 public Element getChild(String elemname, String attribute, String value) { 139 if (doc == null) 140 return null; 141 else { 142 Element temp = null; 143 List list = root.getChildren(elemname); 144 Iterator iter = list.iterator(); 145 String val = ""; 146 while(iter.hasNext()) { 147 Element elem = (Element)iter.next(); 148 val = elem.getAttributeValue(attribute); 149 if (val != null && val.equals(value)) { 150 temp = elem; 151 break; 152 } 153 } 154 return temp; 155 } 156 } 157 158 163 public String getText(Element elem) { 164 if (doc == null) 165 return null; 166 else 167 return elem.getText(); 168 } 169 170 public String getText(String elemname, String attrname, String attrvalue) { 171 Element elem = getChild(elemname, attrname, attrvalue); 172 return elem.getText(); 173 } 174 175 183 public String getChildText(String elemname, String childname, String attribute, String value) { 184 Element elem = getChild(elemname,attribute,value); 185 elem = elem.getChild(childname); 186 return getText(elem); 187 } 188 189 195 public String getAllChildText(String elemname, String attribute, String value, String separator1, String separator2) { 196 String result = ""; 197 try { 198 Element elem = getChild(elemname,attribute,value); 199 List list = elem.getChildren(); 200 Iterator iter = list.iterator(); 201 while (iter.hasNext()) { 202 Element child = (Element)iter.next(); 203 result += child.getName(); 204 result += separator1; 205 result += child.getText(); 206 result += separator2; 207 } 208 } 209 catch (Exception ex) { 210 if (logger.isEnabledFor(Level.ERROR)) 211 logger.error(ex.getMessage()); 212 ex.printStackTrace(); 213 } 214 return result; 215 } 216 217 223 public Attribute getAttribute(Element elem, String attrib) { 224 if (doc == null) 225 return null; 226 else 227 return elem.getAttribute(attrib); 228 } 229 230 236 public String getAttributeValue(Element elem, String attrib) { 237 if (doc == null) 238 return null; 239 else 240 return elem.getAttributeValue(attrib); 241 } 242 243 249 public void setAttributeValue(Element elem, String attrib, String value) { 250 elem.getAttribute(attrib).setValue(value); 251 } 252 253 public void setText(Element elem, String text) { 254 elem.setText(text); 255 } 256 257 261 public boolean writeXMLDoc() { 262 if (docPath == null) 265 return false; 266 267 boolean result = true; 268 try { 269 XMLOutputter outputter = new XMLOutputter(docPath); 270 File file = new File (docPath); 271 OutputStream out = new FileOutputStream (file); 272 outputter.output(doc,out); 275 out.close(); 276 } 277 catch (Exception ex) { 278 result = false; 279 if (logger.isEnabledFor(Level.WARN)) 280 logger.warn(ex.getMessage()); 281 } 282 return result; 283 } 284 285 288 public List getRootChild() { 289 if (doc == null) 290 return null; 291 else 292 return root.getChildren(); 293 } 294 295 300 public List getAllAttribute(Element elem) { 301 if (doc == null) 302 return null; 303 else 304 return elem.getAttributes(); 305 } 306 307 310 public List getAllChild(String elemname) { 311 if (doc == null) 312 return null; 313 else 314 return root.getChildren(elemname); 315 } 316 317 321 public List getAllChild(String elemname, String attribute, String value) { 322 List <Element> list = new LinkedList <Element>(); 323 List elems = root.getChildren(elemname); 324 Iterator iter = elems.iterator(); 325 while (iter.hasNext()) { 326 Element elem = (Element)iter.next(); 327 try { 328 String val = elem.getAttributeValue(attribute); 329 if (val != null && val.equals(value)) 330 list.add(elem); 331 } catch (Exception e) { 332 } 333 } 334 return list; 335 } 336 337 342 public boolean removeChild(String elemname) { 343 return root.removeChild(elemname); 344 } 345 } 346 | Popular Tags |