01: package net.xoetrope.xml.jaxp;
02:
03: import javax.xml.parsers.DocumentBuilder;
04: import javax.xml.parsers.DocumentBuilderFactory;
05: import javax.xml.parsers.ParserConfigurationException;
06:
07: import org.w3c.dom.Document;
08: import org.w3c.dom.Element;
09:
10: /**
11: * <p>Title: Xui</p>
12: * <p>Description: Used to instantiate an XML document. The root node is constructed
13: * automatically from the passed rootname</p>
14: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
15: * <p>Company: Xoetrope Ltd.</p>
16: * @author not attributable
17: * @version 1.0
18: */
19:
20: public class JaxpDocumentFactory {
21: public JaxpDocumentFactory() {
22: }
23:
24: /**
25: * Create a basic document object and create a node with the passed name.
26: * @param rootName The name of the root node
27: * @return the new Document
28: */
29: public static JaxpXmlElement createDocument(String rootName) {
30: DocumentBuilder docBuilder = createDocumentBuilder();
31: Document doc = docBuilder.newDocument();
32: Element root = doc.createElement(rootName);
33: doc.appendChild(root);
34: return new JaxpXmlElement(doc.getDocumentElement());
35: }
36:
37: /**
38: * Create a DocumentBuilder with defaults
39: * @return the DocumentBuilder
40: */
41: public static DocumentBuilder createDocumentBuilder() {
42: boolean validation = false;
43: boolean ignoreWhitespace = false;
44: boolean ignoreComments = false;
45: boolean putCDATAIntoText = false;
46: boolean createEntityRefs = false;
47: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
48: .newInstance();
49: Document xmlDoc;
50:
51: docBuilderFactory.setValidating(validation);
52: docBuilderFactory.setIgnoringComments(ignoreComments);
53: docBuilderFactory
54: .setIgnoringElementContentWhitespace(ignoreWhitespace);
55: docBuilderFactory.setCoalescing(putCDATAIntoText);
56:
57: // The opposite of creating entity ref nodes is expanding them inline
58: docBuilderFactory.setExpandEntityReferences(!createEntityRefs);
59:
60: // At this point the DocumentBuilderFactory instance can be saved
61: // and reused to create any number of DocumentBuilder instances
62: // with the same configuration options.
63:
64: // Step 2: create a DocumentBuilder that satisfies the constraints
65: // specified by the DocumentBuilderFactory
66: DocumentBuilder docBuilder = null;
67: try {
68: docBuilder = docBuilderFactory.newDocumentBuilder();
69: } catch (ParserConfigurationException pce) {
70: System.err.println(pce);
71: return null;
72: }
73: return docBuilder;
74: }
75:
76: }
|