001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-2005 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.application;
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.xml.sax.EntityResolver;
036: import org.xml.sax.InputSource;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * Provides convenience methods for reading and writing enterprise application
041: * deployment descriptors (application.xml).
042: *
043: * @since Cactus 1.5
044: * @version $Id: ApplicationXmlIo.java 239141 2005-02-15 10:31:44Z vmassol $
045: */
046: public class ApplicationXmlIo {
047:
048: // Inner Classes -----------------------------------------------------------
049:
050: /**
051: * Implementation of the SAX EntityResolver interface that looks up the
052: * application DTDs from the JAR.
053: */
054: private static class ApplicationXmlEntityResolver implements
055: EntityResolver {
056:
057: /**
058: * @see org.xml.sax.EntityResolver#resolveEntity
059: */
060: public InputSource resolveEntity(String thePublicId,
061: String theSystemId) throws SAXException, IOException {
062: ApplicationXmlVersion version = ApplicationXmlVersion
063: .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: * Parses a deployment descriptor stored in a regular file.
083: *
084: * @param theFile The file to parse
085: * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
086: * use the default
087: * @return The parsed descriptor
088: * @throws SAXException If the file could not be parsed
089: * @throws ParserConfigurationException If the XML parser was not correctly
090: * configured
091: * @throws IOException If an I/O error occurs
092: */
093: public static ApplicationXml parseApplicationXmlFromFile(
094: File theFile, EntityResolver theEntityResolver)
095: throws SAXException, ParserConfigurationException,
096: IOException {
097: InputStream in = null;
098: try {
099: in = new FileInputStream(theFile);
100: return parseApplicationXml(in, theEntityResolver);
101: } finally {
102: if (in != null) {
103: try {
104: in.close();
105: } catch (IOException ioe) {
106: // we'll pass on the original IO error, so ignore this one
107: }
108: }
109: }
110: }
111:
112: /**
113: * Parses a deployment descriptor provided as input stream.
114: *
115: * @param theInput The input stream
116: * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
117: * use the default
118: * @return The parsed descriptor
119: * @throws SAXException If the input could not be parsed
120: * @throws ParserConfigurationException If the XML parser was not correctly
121: * configured
122: * @throws IOException If an I/O error occurs
123: */
124: public static ApplicationXml parseApplicationXml(
125: InputStream theInput, EntityResolver theEntityResolver)
126: throws SAXException, ParserConfigurationException,
127: IOException {
128: DocumentBuilderFactory factory = DocumentBuilderFactory
129: .newInstance();
130: factory.setValidating(false);
131: factory.setNamespaceAware(false);
132: DocumentBuilder builder = factory.newDocumentBuilder();
133: if (theEntityResolver != null) {
134: builder.setEntityResolver(theEntityResolver);
135: } else {
136: builder
137: .setEntityResolver(new ApplicationXmlEntityResolver());
138: }
139: return new DefaultApplicationXml(builder.parse(theInput));
140: }
141:
142: /**
143: * Writes the specified document to a file.
144: *
145: * @param theAppXml The descriptor to serialize
146: * @param theFile The file to write to
147: * @throws IOException If an I/O error occurs
148: */
149: public static void writeApplicationXml(ApplicationXml theAppXml,
150: File theFile) throws IOException {
151: writeApplicationXml(theAppXml, theFile, null, false);
152: }
153:
154: /**
155: * Writes the specified document to a file.
156: *
157: * @param theAppXml The descriptor to serialize
158: * @param theFile The file to write to
159: * @param theEncoding The character encoding to use
160: * @throws IOException If an I/O error occurs
161: */
162: public static void writeApplicationXml(ApplicationXml theAppXml,
163: File theFile, String theEncoding) throws IOException {
164: writeApplicationXml(theAppXml, theFile, theEncoding, false);
165: }
166:
167: /**
168: * Writes the specified document to a file.
169: *
170: * @param theAppXml The descriptor to serialize
171: * @param theFile The file to write to
172: * @param theEncoding The character encoding to use
173: * @param isIndent Whether the written XML should be indented
174: * @throws IOException If an I/O error occurs
175: */
176: public static void writeApplicationXml(ApplicationXml theAppXml,
177: File theFile, String theEncoding, boolean isIndent)
178: throws IOException {
179: OutputStream out = null;
180: try {
181: out = new FileOutputStream(theFile);
182: writeApplicationXml(theAppXml, out, theEncoding, isIndent);
183: } finally {
184: if (out != null) {
185: try {
186: out.close();
187: } catch (IOException ioe) {
188: // we'll pass on the original IO error, so ignore this one
189: }
190: }
191: }
192: }
193:
194: /**
195: * Writes the specified document to an output stream.
196: *
197: * @param theAppXml The descriptor to serialize
198: * @param theOutput The output stream to write to
199: * @param theEncoding The character encoding to use
200: * @param isIndent Whether the written XML should be indented
201: * @throws IOException If an I/O error occurs
202: */
203: public static void writeApplicationXml(ApplicationXml theAppXml,
204: OutputStream theOutput, String theEncoding, boolean isIndent)
205: throws IOException {
206: OutputFormat outputFormat = new OutputFormat(theAppXml
207: .getDocument());
208: if (theEncoding != null) {
209: outputFormat.setEncoding(theEncoding);
210: }
211: outputFormat.setIndenting(isIndent);
212: outputFormat.setPreserveSpace(false);
213: XMLSerializer serializer = new XMLSerializer(theOutput,
214: outputFormat);
215: serializer.serialize(theAppXml.getDocument());
216: }
217:
218: }
|