2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xml;
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;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import javax.xml.transform.URIResolver;
29 import javax.xml.transform.dom.DOMSource;
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;
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.
44 * @author Erik Brakkee
46 public class XMLDocument {
48 private static final Logger LOG = Logger.getLogger(XMLDocument.class
51 private DOMSource doc;
53 public XMLDocument(DOMSource aDoc) {
57 public XMLDocument(URI aUri) throws IOException, XMLException {
58 this(aUri.toString(), aUri.toURL().openStream());
61 public XMLDocument(String aSystemId, String aContents) throws XMLException {
62 this(aSystemId, new ByteArrayInputStream(aContents.getBytes()));
65 public XMLDocument(String aSystemId, InputStream aStream)
67 LSInput input = DomUtils.getDomImplementationLS().createLSInput();
68 input.setByteStream(aStream);
69 input.setSystemId(aSystemId);
70 doc = read(input, null);
73 public XMLDocument(String aSystemId, LSResourceResolver aResolver)
75 LSInput input = aResolver.resolveResource(null, null, null, aSystemId,
77 doc = read(input, aResolver);
80 public static XMLDocument xmldocument(URI aUri) throws IOException,
82 return new XMLDocument(aUri);
85 public static XMLDocument xmldocument(String aSystemId, String aContents) throws XMLException {
86 return new XMLDocument(aSystemId, aContents);
89 public static XMLDocument xmldocument(String aSystemId, InputStream aStream)
91 return new XMLDocument(aSystemId, aStream);
94 public static XMLDocument xmldocument(String aSystemId,
95 LSResourceResolver aResolver) throws XMLException {
96 return new XMLDocument(aSystemId, aResolver);
99 public DOMSource getDOMSource() {
103 public Document getDocument() {
104 return (Document)doc.getNode();
107 public XMLDocument process(XMLProcessor aProcessor) throws XMLException {
108 return new XMLDocument(aProcessor.process(doc));
111 public void write(XMLTextProcessor aProcessor, OutputStream aOs)
112 throws XMLException {
113 aProcessor.write(doc, aOs);
116 public String write(XMLTextProcessor aProcessor) throws XMLException {
117 ByteArrayOutputStream bos = new ByteArrayOutputStream();
118 write(aProcessor, bos);
119 return bos.toString();
122 public XMLDocument transform(URI aUri) throws XMLException {
123 return process(new XSLTransformation(aUri));
126 public XMLDocument transform(String aSystemId, URIResolver aResolver)
127 throws XMLException {
128 return process(new XSLTransformation(aSystemId, aResolver));
131 public XMLDocument validate(URI aUri) throws XMLException {
132 return process(new XMLSchema(aUri));
135 public XMLDocument validate(String aSystemId,
136 LSResourceResolver aResolver) throws XMLException {
137 return process(new XMLSchema(aSystemId, aResolver));
141 * Prints an XML document.
144 * Output stream to print on.
145 * @param aPrettyPrint
146 * Pretty print or not.
148 public void print(OutputStream aOs, boolean aPrettyPrint) {
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);
159 * Prints an XML document.
161 * @param aPrettyPrint
162 * Pretty print or not.
163 * @return XML string.
165 public String print(boolean aPrettyPrint) {
166 ByteArrayOutputStream bos = new ByteArrayOutputStream();
167 print(bos, aPrettyPrint);
168 return bos.toString();
171 private static DOMSource read(LSInput aInput, LSResourceResolver aResolver)
172 throws XMLException {
174 DOMImplementationLS impl = DomUtils.getDomImplementationLS();
176 LSParser builder = impl.createLSParser(
177 DOMImplementationLS.MODE_SYNCHRONOUS, null);
178 if (aResolver != null) {
179 builder.getDomConfig().setParameter("resource-resolver",
182 Document docraw = builder.parse(aInput);
183 DOMSource result = new DOMSource(docraw);
184 result.setSystemId(aInput.getSystemId());
186 } catch (LSException e) {
187 throw new XMLException(e.getMessage(), e);