Source code: com/dghda/kent/XMLConstructor.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.kent;
21
22 import javax.xml.parsers.*;
23
24 import org.w3c.dom.*;
25 import org.apache.xerces.dom.*;
26
27 import org.xml.sax.*;
28
29 /**
30 This class provides methods useful for classes which want to construct or parse XML documents.
31 */
32 public class XMLConstructor {
33
34 /** Parse a new document. */
35 public static Document parseDocument (String doc) throws SAXException, ParserConfigurationException {
36 try {
37 DocumentBuilder db = getDocBuilder();
38 return db.parse (new InputSource (new java.io.StringReader (doc)));
39 } catch (java.io.IOException exc) {
40 throw new SAXException (exc);
41 }
42 }
43
44 /** Parse a new document with the given system ID. */
45 public static Document parseDocument (String doc, String systemID) throws SAXException, ParserConfigurationException {
46 try {
47 DocumentBuilder db = getDocBuilder();
48 InputSource source = new InputSource (new java.io.StringReader (doc));
49 source.setSystemId (systemID);
50 return db.parse (source);
51 } catch (java.io.IOException exc) {
52 throw new SAXException (exc);
53 }
54 }
55
56 /** Create a new document. */
57 public static Document createDocument (String name) throws ParserConfigurationException {
58 DocumentBuilder db = getDocBuilder();
59 return db.getDOMImplementation().createDocument (null, name, null);
60 }
61
62 /** Create a new document using an internal DTD. */
63 public static Document createDocument (String name, String dtd) throws ParserConfigurationException {
64 DocumentBuilder db = getDocBuilder();
65 DocumentTypeImpl type = new DocumentTypeImpl (null, name);
66 type.setInternalSubset (dtd);
67 return db.getDOMImplementation().createDocument (null, name, type);
68 }
69
70 /** Create a new document using a DTD. */
71 public static Document createDocument (String qualifiedName, String publicID, String systemID) throws ParserConfigurationException {
72 DocumentBuilder db = getDocBuilder();
73 DocumentType type = db.getDOMImplementation().createDocumentType (qualifiedName, publicID, systemID);
74 return db.getDOMImplementation().createDocument (null, qualifiedName, type);
75 }
76
77 /**
78 Returns the single child element with the given tag.
79 @throws SAXException If there is not exactly one element with the given tag
80 */
81 public static Element getSingleElementByTag (Element elem, String tag) throws SAXException {
82 NodeList nodes = elem.getElementsByTagName (tag);
83 if (nodes.getLength() != 1)
84 throw new SAXException ("Number of " + tag + " blocks != 1");
85 return (Element) nodes.item (0);
86 }
87
88 /**
89 Returns the single child element with the given tag, or null if there is no such element.
90 @throws SAXException If there is more than one element with the given tag
91 */
92 public static Element getOptionalElementByTag (Element elem, String tag) throws SAXException {
93 NodeList nodes = elem.getElementsByTagName (tag);
94 if (nodes.getLength() == 0)
95 return null;
96 else if (nodes.getLength() > 1)
97 throw new SAXException ("Number of " + tag + " blocks > 1");
98 else
99 return (Element) nodes.item (0);
100 }
101
102 /** Returns the contents of any text node children, concatenated together. */
103 public static final String getNodeText (Node elem) {
104 StringBuffer buffer = new StringBuffer();
105
106 // Iterate over all children
107 NodeList children = elem.getChildNodes();
108 for (int index = 0; index != children.getLength(); ++index) {
109 Node node = children.item (index);
110
111 // Confirm the node is in fact a text node
112 if (node.getNodeType() != Node.TEXT_NODE)
113 continue;
114
115 // Add the text
116 buffer.append (node.getNodeValue().trim());
117 }
118
119 return buffer.toString();
120 }
121
122 public static void addIndent (StringBuffer buffer, int indent) {
123 if (indent < 0)
124 throw new IllegalArgumentException ("Indention must not be negative");
125 for (int index = 0; index != indent; ++index)
126 buffer.append (" ");
127 }
128
129 public static void addAttribute (StringBuffer buffer, String attr, String value) {
130 buffer.append (' ');
131 buffer.append (attr);
132 buffer.append ("=\"");
133 buffer.append (value);
134 buffer.append ('"');
135 }
136
137 protected static DocumentBuilder getDocBuilder() throws ParserConfigurationException {
138
139 // Create a document builder factory
140 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
141
142 // Create the document builder
143 return dbf.newDocumentBuilder();
144 }
145
146 private EntityResolver g_EntityResolver;
147 }