KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > helpers > XMLUtils


1 package org.objectweb.celtix.helpers;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.logging.Level JavaDoc;
6 import java.util.logging.Logger JavaDoc;
7
8 import javax.wsdl.Definition;
9 import javax.xml.namespace.QName JavaDoc;
10 import javax.xml.parsers.DocumentBuilder JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
12 import javax.xml.transform.OutputKeys JavaDoc;
13 import javax.xml.transform.Transformer JavaDoc;
14 import javax.xml.transform.TransformerConfigurationException JavaDoc;
15 import javax.xml.transform.TransformerFactory JavaDoc;
16 import javax.xml.transform.dom.DOMSource JavaDoc;
17 import javax.xml.transform.stream.StreamResult JavaDoc;
18
19 import org.w3c.dom.*;
20 import org.xml.sax.SAXException JavaDoc;
21
22 import org.objectweb.celtix.common.logging.LogUtils;
23
24
25 public class XMLUtils {
26     
27     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(XMLUtils.class);
28     private final DocumentBuilderFactory JavaDoc parserFactory;
29     private DocumentBuilder JavaDoc parser;
30     private final TransformerFactory JavaDoc transformerFactory;
31     private String JavaDoc omitXmlDecl = "no";
32     private String JavaDoc charset = "utf-8";
33     private int indent = -1;
34     
35     public XMLUtils() {
36         parserFactory = DocumentBuilderFactory.newInstance();
37         parserFactory.setNamespaceAware(true);
38         
39         transformerFactory = TransformerFactory.newInstance();
40     }
41
42     private Transformer JavaDoc newTransformer() throws TransformerConfigurationException JavaDoc {
43         return transformerFactory.newTransformer();
44     }
45
46     private DocumentBuilder JavaDoc getParser() {
47         if (parser == null) {
48             try {
49                 parser = parserFactory.newDocumentBuilder();
50             } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
51                 LOG.log(Level.SEVERE, "NEW_DOCUMENT_BUILDER_EXCEPTION_MSG");
52             }
53         }
54         return parser;
55     }
56     
57     public Document parse(InputStream in) throws SAXException JavaDoc, IOException {
58         if (in == null && LOG.isLoggable(Level.FINE)) {
59             LOG.fine("XMLUtils trying to parse a null inputstream");
60         }
61         return getParser().parse(in);
62     }
63
64     public Document parse(String JavaDoc in) throws SAXException JavaDoc, IOException {
65         return parse(in.getBytes());
66     }
67
68     public Document parse(byte[] in) throws SAXException JavaDoc, IOException {
69         if (in == null && LOG.isLoggable(Level.FINE)) {
70             LOG.fine("XMLUtils trying to parse a null bytes");
71         }
72         return getParser().parse(new ByteArrayInputStream(in));
73     }
74
75     public Document newDocument() {
76         return getParser().newDocument();
77     }
78
79     public void setOmitXmlDecl(String JavaDoc value) {
80         this.omitXmlDecl = value;
81     }
82     
83     public void setCharsetEncoding(String JavaDoc value) {
84         this.charset = value;
85     }
86
87     public void setIndention(int i) {
88         this.indent = i;
89     }
90
91     private boolean indent() {
92         return this.indent != -1;
93     }
94     
95     public void writeTo(Node node, OutputStream os) {
96         try {
97             Transformer JavaDoc it = newTransformer();
98             
99             it.setOutputProperty(OutputKeys.METHOD, "xml");
100             if (indent()) {
101                 it.setOutputProperty(OutputKeys.INDENT, "yes");
102                 it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
103                                      Integer.toString(this.indent));
104             }
105             it.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDecl);
106             it.setOutputProperty(OutputKeys.ENCODING, charset);
107             it.transform(new DOMSource JavaDoc(node), new StreamResult JavaDoc(os));
108         } catch (Exception JavaDoc e) {
109             e.printStackTrace();
110         }
111     }
112     
113     public String JavaDoc toString(Node node) {
114         ByteArrayOutputStream out = new ByteArrayOutputStream();
115         writeTo(node, out);
116         return out.toString();
117     }
118
119     public void printDOM(Node node) {
120         printDOM("", node);
121     }
122
123     public void printDOM(String JavaDoc words, Node node) {
124         System.out.println(words);
125         System.out.println(toString(node));
126     }
127
128     public Attr getAttribute(Element el, String JavaDoc attrName) {
129         return el.getAttributeNode(attrName);
130     }
131
132     public void replaceAttribute(Element element, String JavaDoc attr, String JavaDoc value) {
133         if (element.hasAttribute(attr)) {
134             element.removeAttribute(attr);
135         }
136         element.setAttribute(attr, value);
137     }
138
139     public boolean hasAttribute(Element element, String JavaDoc value) {
140         NamedNodeMap attributes = element.getAttributes();
141         for (int i = 0; i < attributes.getLength(); i++) {
142             Node node = attributes.item(i);
143             if (value.equals(node.getNodeValue())) {
144                 return true;
145             }
146         }
147         return false;
148     }
149
150     public static void printAttributes(Element element) {
151         NamedNodeMap attributes = element.getAttributes();
152         for (int i = 0; i < attributes.getLength(); i++) {
153             Node node = attributes.item(i);
154             System.err.println("## prefix=" + node.getPrefix() + " localname:"
155                                + node.getLocalName() + " value=" + node.getNodeValue());
156         }
157     }
158
159     public QName JavaDoc getNamespace(Map namespaces, String JavaDoc str, String JavaDoc defaultNamespace) {
160         String JavaDoc prefix = null;
161         String JavaDoc localName = null;
162         
163         StringTokenizer tokenizer = new StringTokenizer(str, ":");
164         if (tokenizer.countTokens() == 2) {
165             prefix = tokenizer.nextToken();
166             localName = tokenizer.nextToken();
167         } else if (tokenizer.countTokens() == 1) {
168             localName = tokenizer.nextToken();
169         }
170
171         String JavaDoc namespceURI = defaultNamespace;
172         if (prefix != null) {
173             namespceURI = (String JavaDoc)namespaces.get(prefix);
174         }
175         return new QName JavaDoc(namespceURI, localName);
176     }
177
178     public void generateXMLFile(Element element, Writer writer) {
179         try {
180             Transformer JavaDoc it = newTransformer();
181             
182             it.setOutputProperty(OutputKeys.METHOD, "xml");
183             it.setOutputProperty(OutputKeys.INDENT, "yes");
184             it.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
185             it.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
186             it.transform(new DOMSource JavaDoc(element), new StreamResult JavaDoc(writer));
187         } catch (Exception JavaDoc e) {
188             e.printStackTrace();
189         }
190     }
191
192     public Element createElementNS(Node node, QName JavaDoc name) {
193         return createElementNS(node.getOwnerDocument(), name.getNamespaceURI(), name.getLocalPart());
194     }
195
196     public Element createElementNS(Document root, QName JavaDoc name) {
197         return createElementNS(root, name.getNamespaceURI(), name.getLocalPart());
198     }
199     
200     public Element createElementNS(Document root, String JavaDoc namespaceURI, String JavaDoc qualifiedName) {
201         return root.createElementNS(namespaceURI, qualifiedName);
202     }
203
204     public Text createTextNode(Document root, String JavaDoc data) {
205         return root.createTextNode(data);
206     }
207
208     public Text createTextNode(Node node, String JavaDoc data) {
209         return createTextNode(node.getOwnerDocument(), data);
210     }
211
212     public void removeContents(Node node) {
213         NodeList list = node.getChildNodes();
214         for (int i = 0; i < list.getLength(); i++) {
215             Node entry = list.item(i);
216             node.removeChild(entry);
217         }
218     }
219     
220     public String JavaDoc writeQName(Definition def, QName JavaDoc qname) {
221         return def.getPrefix(qname.getNamespaceURI()) + ":" + qname.getLocalPart();
222     }
223 }
224
Popular Tags