added XMLDocument, XMLSchema, and XSLTransformation classes.
[utils] / support / general / src / main / java / org / wamblee / xml / XMLDocument.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xml;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.net.MalformedURLException;
24 import java.net.URI;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import javax.xml.transform.URIResolver;
29 import javax.xml.transform.dom.DOMSource;
30
31 import org.w3c.dom.Document;
32 import org.w3c.dom.ls.DOMImplementationLS;
33 import org.w3c.dom.ls.LSException;
34 import org.w3c.dom.ls.LSInput;
35 import org.w3c.dom.ls.LSParser;
36 import org.w3c.dom.ls.LSResourceResolver;
37
38 /**
39  * Convenience class for representing an XML document. Together with
40  * {@link XMLProcessor}, {@link XMLSchema}, {@link XMLTextProcessor}, and
41  * {@link XSLTransformation} it provides a simple API for XML parsing,
42  * validation, and transformation.
43  * 
44  * @author Erik Brakkee
45  */
46 public class XMLDocument {
47
48     private static final Logger LOG = Logger.getLogger(XMLDocument.class
49         .getName());
50
51     private DOMSource doc;
52
53     public XMLDocument(DOMSource aDoc) {
54         doc = aDoc;
55     }
56
57     public XMLDocument(URI aUri) throws IOException, XMLException {
58         this(aUri.toString(), aUri.toURL().openStream());
59     }
60     
61     public XMLDocument(String aSystemId, String aContents) throws XMLException { 
62         this(aSystemId, new ByteArrayInputStream(aContents.getBytes()));
63     }
64
65     public XMLDocument(String aSystemId, InputStream aStream)
66         throws XMLException {
67         LSInput input = DomUtils.getDomImplementationLS().createLSInput();
68         input.setByteStream(aStream);
69         input.setSystemId(aSystemId);
70         doc = read(input, null);
71     }
72
73     public XMLDocument(String aSystemId, LSResourceResolver aResolver)
74         throws XMLException {
75         LSInput input = aResolver.resolveResource(null, null, null, aSystemId,
76             null);
77         doc = read(input, aResolver);
78     }
79
80     public static XMLDocument xmldocument(URI aUri) throws IOException,
81         XMLException {
82         return new XMLDocument(aUri);
83     }
84     
85     public static XMLDocument xmldocument(String aSystemId, String aContents) throws XMLException { 
86         return new XMLDocument(aSystemId, aContents);
87     }
88
89     public static XMLDocument xmldocument(String aSystemId, InputStream aStream)
90         throws XMLException {
91         return new XMLDocument(aSystemId, aStream);
92     }
93
94     public static XMLDocument xmldocument(String aSystemId,
95         LSResourceResolver aResolver) throws XMLException {
96         return new XMLDocument(aSystemId, aResolver);
97     }
98
99     public DOMSource getDOMSource() {
100         return doc;
101     }
102     
103     public Document getDocument() { 
104         return (Document)doc.getNode();
105     }
106
107     public XMLDocument process(XMLProcessor aProcessor) throws XMLException {
108         return new XMLDocument(aProcessor.process(doc));
109     }
110
111     public void write(XMLTextProcessor aProcessor, OutputStream aOs)
112         throws XMLException {
113         aProcessor.write(doc, aOs);
114     }
115     
116     public String write(XMLTextProcessor aProcessor) throws XMLException { 
117         ByteArrayOutputStream bos = new ByteArrayOutputStream();
118         write(aProcessor, bos); 
119         return bos.toString();
120     }
121
122     public XMLDocument transform(URI aUri) throws XMLException {
123         return process(new XSLTransformation(aUri));
124     }
125
126     public XMLDocument transform(String aSystemId, URIResolver aResolver)
127         throws XMLException {
128         return process(new XSLTransformation(aSystemId, aResolver));
129     }
130
131     public XMLDocument validate(URI aUri) throws XMLException {
132         return process(new XMLSchema(aUri));
133     }
134
135     public XMLDocument validate(String aSystemId, InputStream aIs,
136         LSResourceResolver aResolver) throws XMLException {
137         return process(new XMLSchema(aSystemId, aIs, aResolver));
138     }
139
140     /**
141      * Prints an XML document.
142      * 
143      * @param aOs
144      *            Output stream to print on.
145      * @param aPrettyPrint
146      *            Pretty print or not.
147      */
148     public void print(OutputStream aOs, boolean aPrettyPrint) {
149         try {
150             write(new XSLTransformation().setPrettyPrint(aPrettyPrint), aOs);
151         } catch (XMLException e) {
152             throw new RuntimeException(
153                 "Programming error, cannot print a DOM tree?: " +
154                     doc.getSystemId(), e);
155         }
156     }
157
158     /**
159      * Prints an XML document.
160      * 
161      * @param aPrettyPrint
162      *            Pretty print or not.
163      * @return XML string.
164      */
165     public String print(boolean aPrettyPrint) {
166         ByteArrayOutputStream bos = new ByteArrayOutputStream();
167         print(bos, aPrettyPrint);
168         return bos.toString();
169     }
170
171     private static DOMSource read(LSInput aInput, LSResourceResolver aResolver)
172         throws XMLException {
173         try {
174             DOMImplementationLS impl = DomUtils.getDomImplementationLS();
175
176             LSParser builder = impl.createLSParser(
177                 DOMImplementationLS.MODE_SYNCHRONOUS, null);
178             if (aResolver != null) {
179                 builder.getDomConfig().setParameter("resource-resolver",
180                     aResolver);
181             }
182             Document docraw = builder.parse(aInput);
183             DOMSource result = new DOMSource(docraw);
184             result.setSystemId(aInput.getSystemId());
185             return result;
186         } catch (LSException e) {
187             throw new XMLException(e.getMessage(), e);
188         }
189     }
190
191 }