KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > webapp > WebXmlIo


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.deployment.webapp;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32
33 import org.apache.xml.serialize.OutputFormat;
34 import org.apache.xml.serialize.XMLSerializer;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.DocumentType JavaDoc;
37 import org.xml.sax.EntityResolver JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /**
42  * Provides convenience methods for reading and writing web deployment
43  * descriptors.
44  *
45  * @since Cactus 1.5
46  * @version $Id: WebXmlIo.java,v 1.1 2004/05/31 20:05:23 vmassol Exp $
47  */

48 public class WebXmlIo
49 {
50     
51     // Inner Classes -----------------------------------------------------------
52

53     /**
54      * Implementation of the SAX EntityResolver interface that looks up the
55      * web-app DTDs from the JAR.
56      */

57     private static class WebXmlEntityResolver implements EntityResolver JavaDoc
58     {
59
60         /**
61          * @see org.xml.sax.EntityResolver#resolveEntity
62          */

63         public InputSource JavaDoc resolveEntity(String JavaDoc thePublicId, String JavaDoc theSystemId)
64             throws SAXException JavaDoc, IOException JavaDoc
65         {
66             WebXmlVersion version = WebXmlVersion.valueOf(thePublicId);
67             if (version != null)
68             {
69                 String JavaDoc fileName = version.getSystemId().substring(
70                     version.getSystemId().lastIndexOf('/'));
71                 InputStream JavaDoc in = this.getClass().getResourceAsStream(
72                     "/org/apache/cactus/integration/ant/deployment/resources"
73                     + fileName);
74                 if (in != null)
75                 {
76                     return new InputSource JavaDoc(in);
77                 }
78             }
79             return null;
80         }
81
82     }
83
84     // Public Methods ----------------------------------------------------------
85

86     /**
87      * Creates a new empty deployment descriptor.
88      *
89      * @param theVersion The version of the descriptor to create
90      * @return The new descriptor
91      * @throws ParserConfigurationException If the XML parser was not correctly
92      * configured
93      */

94     public static WebXml newWebXml(WebXmlVersion theVersion)
95         throws ParserConfigurationException JavaDoc
96     {
97         DocumentBuilderFactory JavaDoc factory =
98             DocumentBuilderFactory.newInstance();
99         factory.setValidating(false);
100         factory.setNamespaceAware(false);
101         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
102         DocumentType JavaDoc docType = null;
103         if (theVersion != null)
104         {
105             docType =
106                 builder.getDOMImplementation().createDocumentType("web-app",
107                     theVersion.getPublicId(), theVersion.getSystemId());
108         }
109         Document JavaDoc doc = builder.getDOMImplementation().createDocument(
110             "", "web-app", docType);
111         return new WebXml(doc);
112     }
113
114     /**
115      * Parses a deployment descriptor stored in a regular file.
116      *
117      * @param theFile The file to parse
118      * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
119      * use the default
120      * @return The parsed descriptor
121      * @throws SAXException If the file could not be parsed
122      * @throws ParserConfigurationException If the XML parser was not correctly
123      * configured
124      * @throws IOException If an I/O error occurs
125      */

126     public static WebXml parseWebXmlFromFile(File JavaDoc theFile,
127         EntityResolver JavaDoc theEntityResolver)
128         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
129     {
130         InputStream JavaDoc in = null;
131         try
132         {
133             in = new FileInputStream JavaDoc(theFile);
134             return parseWebXml(in, theEntityResolver);
135         }
136         finally
137         {
138             if (in != null)
139             {
140                 try
141                 {
142                     in.close();
143                 }
144                 catch (IOException JavaDoc ioe)
145                 {
146                     // we'll pass on the original IO error, so ignore this one
147
}
148             }
149         }
150     }
151     
152     /**
153      * Parses a deployment descriptor provided as input stream.
154      *
155      * @param theInput The input stream
156      * @param theEntityResolver A SAX entity resolver, or <code>null</code> to
157      * use the default
158      * @return The parsed descriptor
159      * @throws SAXException If the input could not be parsed
160      * @throws ParserConfigurationException If the XML parser was not correctly
161      * configured
162      * @throws IOException If an I/O error occurs
163      */

164     public static WebXml parseWebXml(InputStream JavaDoc theInput,
165         EntityResolver JavaDoc theEntityResolver)
166         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
167     {
168         DocumentBuilderFactory JavaDoc factory =
169             DocumentBuilderFactory.newInstance();
170         factory.setValidating(false);
171         factory.setNamespaceAware(false);
172         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
173         if (theEntityResolver != null)
174         {
175             builder.setEntityResolver(theEntityResolver);
176         }
177         else
178         {
179             builder.setEntityResolver(new WebXmlEntityResolver());
180         }
181         return new WebXml(builder.parse(theInput));
182     }
183
184     /**
185      * Writes the specified document to a file.
186      *
187      * @param theWebXml The descriptor to serialize
188      * @param theFile The file to write to
189      * @throws IOException If an I/O error occurs
190      */

191     public static void writeWebXml(WebXml theWebXml, File JavaDoc theFile)
192         throws IOException JavaDoc
193     {
194         writeWebXml(theWebXml, theFile, null, false);
195     }
196
197     /**
198      * Writes the specified document to a file.
199      *
200      * @param theWebXml The descriptor to serialize
201      * @param theFile The file to write to
202      * @param theEncoding The character encoding to use
203      * @throws IOException If an I/O error occurs
204      */

205     public static void writeWebXml(WebXml theWebXml, File JavaDoc theFile,
206         String JavaDoc theEncoding)
207         throws IOException JavaDoc
208     {
209         writeWebXml(theWebXml, theFile, theEncoding, false);
210     }
211
212     /**
213      * Writes the specified document to a file.
214      *
215      * @param theWebXml The descriptor to serialize
216      * @param theFile The file to write to
217      * @param theEncoding The character encoding to use
218      * @param isIndent Whether the written XML should be indented
219      * @throws IOException If an I/O error occurs
220      */

221     public static void writeWebXml(WebXml theWebXml, File JavaDoc theFile,
222         String JavaDoc theEncoding, boolean isIndent)
223         throws IOException JavaDoc
224     {
225         OutputStream JavaDoc out = null;
226         try
227         {
228             out = new FileOutputStream JavaDoc(theFile);
229             writeWebXml(theWebXml, out, theEncoding, isIndent);
230         }
231         finally
232         {
233             if (out != null)
234             {
235                 try
236                 {
237                     out.close();
238                 }
239                 catch (IOException JavaDoc ioe)
240                 {
241                     // we'll pass on the original IO error, so ignore this one
242
}
243             }
244         }
245     }
246
247     /**
248      * Writes the specified document to an output stream.
249      *
250      * @param theWebXml The descriptor to serialize
251      * @param theOutput The output stream to write to
252      * @param theEncoding The character encoding to use
253      * @param isIndent Whether the written XML should be indented
254      * @throws IOException If an I/O error occurs
255      */

256     public static void writeWebXml(WebXml theWebXml, OutputStream JavaDoc theOutput,
257         String JavaDoc theEncoding, boolean isIndent)
258         throws IOException JavaDoc
259     {
260         OutputFormat outputFormat =
261             new OutputFormat(theWebXml.getDocument());
262         if (theEncoding != null)
263         {
264             outputFormat.setEncoding(theEncoding);
265         }
266         outputFormat.setIndenting(isIndent);
267         outputFormat.setPreserveSpace(false);
268         XMLSerializer serializer = new XMLSerializer(theOutput, outputFormat);
269         serializer.serialize(theWebXml.getDocument());
270     }
271
272 }
273
Popular Tags