001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.deployment.webapp;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028:
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.parsers.ParserConfigurationException;
032:
033: import org.apache.xml.serialize.OutputFormat;
034: import org.apache.xml.serialize.XMLSerializer;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.DocumentType;
037: import org.xml.sax.EntityResolver;
038: import org.xml.sax.InputSource;
039: import org.xml.sax.SAXException;
040:
041: /**
042: * Provides convenience methods for reading and writing web deployment
043: * descriptors.
044: *
045: * @since Cactus 1.5
046: * @version $Id: WebXmlIo.java 239003 2004-05-31 20:05:27Z vmassol $
047: */
048: public class WebXmlIo {
049:
050: // Inner Classes -----------------------------------------------------------
051:
052: /**
053: * Implementation of the SAX EntityResolver interface that looks up the
054: * web-app DTDs from the JAR.
055: */
056: private static class WebXmlEntityResolver implements EntityResolver {
057:
058: /**
059: * @see org.xml.sax.EntityResolver#resolveEntity
060: */
061: public InputSource resolveEntity(String thePublicId,
062: String theSystemId) throws SAXException, IOException {
063: WebXmlVersion version = WebXmlVersion.valueOf(thePublicId);
064: if (version != null) {
065: String fileName = version.getSystemId().substring(
066: version.getSystemId().lastIndexOf('/'));
067: InputStream in = this .getClass().getResourceAsStream(
068: "/org/apache/cactus/integration/ant/deployment/resources"
069: + fileName);
070: if (in != null) {
071: return new InputSource(in);
072: }
073: }
074: return null;
075: }
076:
077: }
078:
079: // Public Methods ----------------------------------------------------------
080:
081: /**
082: * Creates a new empty deployment descriptor.
083: *
084: * @param theVersion The version of the descriptor to create
085: * @return The new descriptor
086: * @throws ParserConfigurationException If the XML parser was not correctly
087: * configured
088: */
089: public static WebXml newWebXml(WebXmlVersion theVersion)
090: throws ParserConfigurationException {
091: DocumentBuilderFactory factory = DocumentBuilderFactory
092: .newInstance();
093: factory.setValidating(false);
094: factory.setNamespaceAware(false);
095: DocumentBuilder builder = factory.newDocumentBuilder();
096: DocumentType docType = null;
097: if (theVersion != null) {
098: docType = builder.getDOMImplementation()
099: .createDocumentType("web-app",
100: theVersion.getPublicId(),
101: theVersion.getSystemId());
102: }
103: Document doc = builder.getDOMImplementation().createDocument(
104: "", "web-app", docType);
105: return new WebXml(doc);
106: }
107:
108: /**
109: * Parses a deployment descriptor stored in a regular file.
110: *
111: * @param theFile The file to parse
112: * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
113: * use the default
114: * @return The parsed descriptor
115: * @throws SAXException If the file could not be parsed
116: * @throws ParserConfigurationException If the XML parser was not correctly
117: * configured
118: * @throws IOException If an I/O error occurs
119: */
120: public static WebXml parseWebXmlFromFile(File theFile,
121: EntityResolver theEntityResolver) throws SAXException,
122: ParserConfigurationException, IOException {
123: InputStream in = null;
124: try {
125: in = new FileInputStream(theFile);
126: return parseWebXml(in, theEntityResolver);
127: } finally {
128: if (in != null) {
129: try {
130: in.close();
131: } catch (IOException ioe) {
132: // we'll pass on the original IO error, so ignore this one
133: }
134: }
135: }
136: }
137:
138: /**
139: * Parses a deployment descriptor provided as input stream.
140: *
141: * @param theInput The input stream
142: * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
143: * use the default
144: * @return The parsed descriptor
145: * @throws SAXException If the input could not be parsed
146: * @throws ParserConfigurationException If the XML parser was not correctly
147: * configured
148: * @throws IOException If an I/O error occurs
149: */
150: public static WebXml parseWebXml(InputStream theInput,
151: EntityResolver theEntityResolver) throws SAXException,
152: ParserConfigurationException, IOException {
153: DocumentBuilderFactory factory = DocumentBuilderFactory
154: .newInstance();
155: factory.setValidating(false);
156: factory.setNamespaceAware(false);
157: DocumentBuilder builder = factory.newDocumentBuilder();
158: if (theEntityResolver != null) {
159: builder.setEntityResolver(theEntityResolver);
160: } else {
161: builder.setEntityResolver(new WebXmlEntityResolver());
162: }
163: return new WebXml(builder.parse(theInput));
164: }
165:
166: /**
167: * Writes the specified document to a file.
168: *
169: * @param theWebXml The descriptor to serialize
170: * @param theFile The file to write to
171: * @throws IOException If an I/O error occurs
172: */
173: public static void writeWebXml(WebXml theWebXml, File theFile)
174: throws IOException {
175: writeWebXml(theWebXml, theFile, null, false);
176: }
177:
178: /**
179: * Writes the specified document to a file.
180: *
181: * @param theWebXml The descriptor to serialize
182: * @param theFile The file to write to
183: * @param theEncoding The character encoding to use
184: * @throws IOException If an I/O error occurs
185: */
186: public static void writeWebXml(WebXml theWebXml, File theFile,
187: String theEncoding) throws IOException {
188: writeWebXml(theWebXml, theFile, theEncoding, false);
189: }
190:
191: /**
192: * Writes the specified document to a file.
193: *
194: * @param theWebXml The descriptor to serialize
195: * @param theFile The file to write to
196: * @param theEncoding The character encoding to use
197: * @param isIndent Whether the written XML should be indented
198: * @throws IOException If an I/O error occurs
199: */
200: public static void writeWebXml(WebXml theWebXml, File theFile,
201: String theEncoding, boolean isIndent) throws IOException {
202: OutputStream out = null;
203: try {
204: out = new FileOutputStream(theFile);
205: writeWebXml(theWebXml, out, theEncoding, isIndent);
206: } finally {
207: if (out != null) {
208: try {
209: out.close();
210: } catch (IOException ioe) {
211: // we'll pass on the original IO error, so ignore this one
212: }
213: }
214: }
215: }
216:
217: /**
218: * Writes the specified document to an output stream.
219: *
220: * @param theWebXml The descriptor to serialize
221: * @param theOutput The output stream to write to
222: * @param theEncoding The character encoding to use
223: * @param isIndent Whether the written XML should be indented
224: * @throws IOException If an I/O error occurs
225: */
226: public static void writeWebXml(WebXml theWebXml,
227: OutputStream theOutput, String theEncoding, boolean isIndent)
228: throws IOException {
229: OutputFormat outputFormat = new OutputFormat(theWebXml
230: .getDocument());
231: if (theEncoding != null) {
232: outputFormat.setEncoding(theEncoding);
233: }
234: outputFormat.setIndenting(isIndent);
235: outputFormat.setPreserveSpace(false);
236: XMLSerializer serializer = new XMLSerializer(theOutput,
237: outputFormat);
238: serializer.serialize(theWebXml.getDocument());
239: }
240:
241: }
|