1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.util.Properties ; 27 28 import org.apache.lenya.xml.parser.Parser; 29 import org.apache.log4j.Category; 30 import org.w3c.dom.CDATASection ; 31 import org.w3c.dom.Comment ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Text ; 38 import org.xml.sax.SAXException ; 39 40 41 45 public class DOMParserFactory { 46 static Category log = Category.getInstance(DOMParserFactory.class); 47 public Parser parser = null; 48 49 52 public DOMParserFactory() { 53 Properties properties = new Properties (); 54 String propertiesFileName = "conf.properties"; 55 56 try { 57 properties.load(DOMParserFactory.class.getResourceAsStream(propertiesFileName)); 58 } catch (Exception e) { 59 log.fatal(": Failed to load properties from resource: " + propertiesFileName); 60 } 61 62 String parserName = properties.getProperty("Parser"); 63 64 if (parserName == null) { 65 log.fatal(": No Parser specified in " + propertiesFileName); 66 } 67 68 try { 69 Class parserClass = Class.forName(parserName); 70 parser = (Parser) parserClass.newInstance(); 71 } catch (Exception e) { 72 log.fatal(": " + e); 73 } 74 } 75 76 81 public static void main(String [] args) { 82 DOMParserFactory dpf = new DOMParserFactory(); 83 84 if (args.length != 1) { 85 System.out.println("Usage: java " + dpf.getClass().getName() + " example.xml"); 86 87 return; 88 } 89 90 Document doc = null; 91 92 try { 93 doc = dpf.getDocument(args[0]); 94 } catch (FileNotFoundException e) { 95 System.err.println("No such file or directory: " + e.getMessage()); 96 97 return; 98 } catch (SAXException e) { 99 System.err.println(e); 100 101 return; 102 } catch (Exception e) { 103 System.err.println(e.getMessage()); 104 105 return; 106 } 107 } 108 109 119 public Document getDocument(String filename) throws FileNotFoundException , Exception { 120 File file = new File (filename); 121 122 if (!file.exists()) { 123 log.error("No such file or directory: " + filename); 124 throw new FileNotFoundException (filename); 125 } 126 127 return parser.getDocument(filename); 128 } 129 130 139 public Document getDocument(InputStream inputStream) 140 throws Exception { 141 return parser.getDocument(inputStream); 142 } 143 144 151 public Document getDocument(Reader reader) throws Exception { 152 return parser.getDocument(reader); 153 } 154 155 160 public Document getDocument() { 161 return parser.getDocument(); 162 } 163 164 172 public Element newElementNode(Document document, String name) { 173 return parser.newElementNode(document, name); 174 } 175 176 185 public Element newElementNSNode(Document document, String namespaceUri, String qualifiedName) { 186 return parser.newElementNSNode(document, namespaceUri, qualifiedName); 187 } 188 189 197 public Text newTextNode(Document document, String data) { 198 return parser.newTextNode(document, data); 199 } 200 201 209 public CDATASection newCDATASection(Document document, String data) { 210 return parser.newCDATASection(document, data); 211 } 212 213 221 public Comment newCommentNode(Document document, String data) { 222 return parser.newCommentNode(document, data); 223 } 224 225 234 public Node cloneNode(Document document, Node original, boolean deep) { 235 Node node = null; 236 short nodeType = original.getNodeType(); 237 238 switch (nodeType) { 239 case Node.ELEMENT_NODE: { 240 Element element = newElementNSNode(document, original.getNamespaceURI(), original.getNodeName()); 241 log.debug(".cloneNode(): Clone element: " + original.getNodeName()); 242 NamedNodeMap attributes = original.getAttributes(); 243 244 for (int i = 0; i < attributes.getLength(); i++) { 245 Node attribute = attributes.item(i); 246 log.debug(".cloneNode(): LocalName: " + attribute.getLocalName() + ", Prefix: " + attribute.getPrefix() + ", NamespaceURI: " + attribute.getNamespaceURI()); 247 element.setAttributeNS(attribute.getNamespaceURI(), attribute.getNodeName(), attribute.getNodeValue()); 248 } 249 250 node = element; 251 252 break; 253 } 254 255 case Node.TEXT_NODE: { 256 Text text = newTextNode(document, original.getNodeValue()); 257 258 node = text; 259 260 break; 261 } 262 263 case Node.CDATA_SECTION_NODE: { 264 CDATASection cdata = newCDATASection(document, original.getNodeValue()); 265 266 node = cdata; 267 268 break; 269 } 270 271 case Node.COMMENT_NODE: { 272 Comment comment = newCommentNode(document, original.getNodeValue()); 273 274 node = comment; 275 276 break; 277 } 278 279 default: 280 log.warn(".cloneNode(): Node type not implemented: " + nodeType); 281 break; 282 } 283 284 if (deep && original.hasChildNodes()) { 285 NodeList nl = original.getChildNodes(); 286 287 for (int i = 0; i < nl.getLength(); i++) { 288 node.appendChild(cloneNode(document, nl.item(i), deep)); 289 } 290 } 291 292 return node; 293 } 294 295 302 public void setElementValue(Document document, Element element, String value) { 303 NodeList nl = element.getChildNodes(); 305 306 for (int i = 0; i < nl.getLength(); i++) { 307 try { 308 element.removeChild(nl.item(i)); 309 } catch (Exception e) { 310 System.err.println("EXCEPTION: " + this.getClass().getName() + 311 ".setElementValue(): " + e); 312 } 313 } 314 315 element.appendChild(newTextNode(document, value)); 317 } 318 } 319 | Popular Tags |