|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
WebXmlIo.java | 42.9% | 74.5% | 62.5% | 66.7% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.integration.ant.deployment;
|
|
58 |
|
|
59 |
import java.io.File;
|
|
60 |
import java.io.FileInputStream;
|
|
61 |
import java.io.FileOutputStream;
|
|
62 |
import java.io.IOException;
|
|
63 |
import java.io.InputStream;
|
|
64 |
import java.io.OutputStream;
|
|
65 |
|
|
66 |
import javax.xml.parsers.DocumentBuilder;
|
|
67 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
68 |
import javax.xml.parsers.ParserConfigurationException;
|
|
69 |
|
|
70 |
import org.apache.xml.serialize.OutputFormat;
|
|
71 |
import org.apache.xml.serialize.XMLSerializer;
|
|
72 |
import org.w3c.dom.Document;
|
|
73 |
import org.w3c.dom.DocumentType;
|
|
74 |
import org.xml.sax.EntityResolver;
|
|
75 |
import org.xml.sax.InputSource;
|
|
76 |
import org.xml.sax.SAXException;
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Provides convenience methods for reading and writing web deployment
|
|
80 |
* descriptors.
|
|
81 |
*
|
|
82 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
83 |
*
|
|
84 |
* @since Cactus 1.5
|
|
85 |
* @version $Id: WebXmlIo.java,v 1.4 2003/06/29 14:57:53 cmlenz Exp $
|
|
86 |
*/
|
|
87 |
public class WebXmlIo |
|
88 |
{ |
|
89 |
|
|
90 |
// Inner Classes -----------------------------------------------------------
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Implementation of the SAX EntityResolver interface that looks up the
|
|
94 |
* web-app DTDs from the JAR.
|
|
95 |
*/
|
|
96 |
private static class WebXmlEntityResolver implements EntityResolver |
|
97 |
{ |
|
98 |
|
|
99 |
/**
|
|
100 |
* @see org.xml.sax.EntityResolver#resolveEntity
|
|
101 |
*/
|
|
102 | 24 |
public InputSource resolveEntity(String thePublicId, String theSystemId)
|
103 |
throws SAXException, IOException
|
|
104 |
{ |
|
105 | 24 |
WebXmlVersion version = WebXmlVersion.valueOf(thePublicId); |
106 | 24 |
if (version != null) |
107 |
{ |
|
108 | 24 |
String fileName = version.getSystemId().substring( |
109 |
version.getSystemId().lastIndexOf('/')); |
|
110 | 24 |
InputStream in = this.getClass().getResourceAsStream(
|
111 |
"/org/apache/cactus/integration/ant/deployment/resources"
|
|
112 |
+ fileName); |
|
113 | 24 |
if (in != null) |
114 |
{ |
|
115 | 24 |
return new InputSource(in); |
116 |
} |
|
117 |
} |
|
118 | 0 |
return null; |
119 |
} |
|
120 |
|
|
121 |
} |
|
122 |
|
|
123 |
// Public Methods ----------------------------------------------------------
|
|
124 |
|
|
125 |
/**
|
|
126 |
* Creates a new empty deployment descriptor.
|
|
127 |
*
|
|
128 |
* @param theVersion The version of the descriptor to create
|
|
129 |
* @return The new descriptor
|
|
130 |
* @throws ParserConfigurationException If the XML parser was not correctly
|
|
131 |
* configured
|
|
132 |
*/
|
|
133 | 2 |
public static WebXml newWebXml(WebXmlVersion theVersion) |
134 |
throws ParserConfigurationException
|
|
135 |
{ |
|
136 | 2 |
DocumentBuilderFactory factory = |
137 |
DocumentBuilderFactory.newInstance(); |
|
138 | 2 |
factory.setValidating(false);
|
139 | 2 |
factory.setNamespaceAware(false);
|
140 | 2 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
141 | 2 |
DocumentType docType = null;
|
142 | 2 |
if (theVersion != null) |
143 |
{ |
|
144 | 2 |
docType = |
145 |
builder.getDOMImplementation().createDocumentType("web-app",
|
|
146 |
theVersion.getPublicId(), theVersion.getSystemId()); |
|
147 |
} |
|
148 | 2 |
Document doc = builder.getDOMImplementation().createDocument( |
149 |
"", "web-app", docType); |
|
150 | 2 |
return new WebXml(doc); |
151 |
} |
|
152 |
|
|
153 |
/**
|
|
154 |
* Parses a deployment descriptor stored in a regular file.
|
|
155 |
*
|
|
156 |
* @param theFile The file to parse
|
|
157 |
* @param theEntityResolver A SAX entity resolver, or <code>null</code> to
|
|
158 |
* use the default
|
|
159 |
* @return The parsed descriptor
|
|
160 |
* @throws SAXException If the file could not be parsed
|
|
161 |
* @throws ParserConfigurationException If the XML parser was not correctly
|
|
162 |
* configured
|
|
163 |
* @throws IOException If an I/O error occurs
|
|
164 |
*/
|
|
165 | 0 |
public static WebXml parseWebXmlFromFile(File theFile, |
166 |
EntityResolver theEntityResolver) |
|
167 |
throws SAXException, ParserConfigurationException, IOException
|
|
168 |
{ |
|
169 | 0 |
InputStream in = null;
|
170 | 0 |
try
|
171 |
{ |
|
172 | 0 |
in = new FileInputStream(theFile);
|
173 | 0 |
return parseWebXml(in, theEntityResolver);
|
174 |
} |
|
175 |
finally
|
|
176 |
{ |
|
177 | 0 |
if (in != null) |
178 |
{ |
|
179 | 0 |
try
|
180 |
{ |
|
181 | 0 |
in.close(); |
182 |
} |
|
183 |
catch (IOException ioe)
|
|
184 |
{ |
|
185 |
// we'll pass on the original IO error, so ignore this one
|
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
} |
|
190 |
|
|
191 |
/**
|
|
192 |
* Parses a deployment descriptor provided as input stream.
|
|
193 |
*
|
|
194 |
* @param theInput The input stream
|
|
195 |
* @param theEntityResolver A SAX entity resolver, or <code>null</code> to
|
|
196 |
* use the default
|
|
197 |
* @return The parsed descriptor
|
|
198 |
* @throws SAXException If the input could not be parsed
|
|
199 |
* @throws ParserConfigurationException If the XML parser was not correctly
|
|
200 |
* configured
|
|
201 |
* @throws IOException If an I/O error occurs
|
|
202 |
*/
|
|
203 | 35 |
public static WebXml parseWebXml(InputStream theInput, |
204 |
EntityResolver theEntityResolver) |
|
205 |
throws SAXException, ParserConfigurationException, IOException
|
|
206 |
{ |
|
207 | 35 |
DocumentBuilderFactory factory = |
208 |
DocumentBuilderFactory.newInstance(); |
|
209 | 35 |
factory.setValidating(false);
|
210 | 35 |
factory.setNamespaceAware(false);
|
211 | 35 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
212 | 35 |
if (theEntityResolver != null) |
213 |
{ |
|
214 | 0 |
builder.setEntityResolver(theEntityResolver); |
215 |
} |
|
216 |
else
|
|
217 |
{ |
|
218 | 35 |
builder.setEntityResolver(new WebXmlEntityResolver());
|
219 |
} |
|
220 | 35 |
return new WebXml(builder.parse(theInput)); |
221 |
} |
|
222 |
|
|
223 |
/**
|
|
224 |
* Writes the specified document to a file.
|
|
225 |
*
|
|
226 |
* @param theWebXml The descriptor to serialize
|
|
227 |
* @param theFile The file to write to
|
|
228 |
* @throws IOException If an I/O error occurs
|
|
229 |
*/
|
|
230 | 0 |
public static void writeWebXml(WebXml theWebXml, File theFile) |
231 |
throws IOException
|
|
232 |
{ |
|
233 | 0 |
writeWebXml(theWebXml, theFile, null, false); |
234 |
} |
|
235 |
|
|
236 |
/**
|
|
237 |
* Writes the specified document to a file.
|
|
238 |
*
|
|
239 |
* @param theWebXml The descriptor to serialize
|
|
240 |
* @param theFile The file to write to
|
|
241 |
* @param theEncoding The character encoding to use
|
|
242 |
* @throws IOException If an I/O error occurs
|
|
243 |
*/
|
|
244 | 0 |
public static void writeWebXml(WebXml theWebXml, File theFile, |
245 |
String theEncoding) |
|
246 |
throws IOException
|
|
247 |
{ |
|
248 | 0 |
writeWebXml(theWebXml, theFile, theEncoding, false);
|
249 |
} |
|
250 |
|
|
251 |
/**
|
|
252 |
* Writes the specified document to a file.
|
|
253 |
*
|
|
254 |
* @param theWebXml The descriptor to serialize
|
|
255 |
* @param theFile The file to write to
|
|
256 |
* @param theEncoding The character encoding to use
|
|
257 |
* @param isIndent Whether the written XML should be indented
|
|
258 |
* @throws IOException If an I/O error occurs
|
|
259 |
*/
|
|
260 | 16 |
public static void writeWebXml(WebXml theWebXml, File theFile, |
261 |
String theEncoding, boolean isIndent)
|
|
262 |
throws IOException
|
|
263 |
{ |
|
264 | 16 |
OutputStream out = null;
|
265 | 16 |
try
|
266 |
{ |
|
267 | 16 |
out = new FileOutputStream(theFile);
|
268 | 16 |
writeWebXml(theWebXml, out, theEncoding, isIndent); |
269 |
} |
|
270 |
finally
|
|
271 |
{ |
|
272 | 16 |
if (out != null) |
273 |
{ |
|
274 | 16 |
try
|
275 |
{ |
|
276 | 16 |
out.close(); |
277 |
} |
|
278 |
catch (IOException ioe)
|
|
279 |
{ |
|
280 |
// we'll pass on the original IO error, so ignore this one
|
|
281 |
} |
|
282 |
} |
|
283 |
} |
|
284 |
} |
|
285 |
|
|
286 |
/**
|
|
287 |
* Writes the specified document to an output stream.
|
|
288 |
*
|
|
289 |
* @param theWebXml The descriptor to serialize
|
|
290 |
* @param theOutput The output stream to write to
|
|
291 |
* @param theEncoding The character encoding to use
|
|
292 |
* @param isIndent Whether the written XML should be indented
|
|
293 |
* @throws IOException If an I/O error occurs
|
|
294 |
*/
|
|
295 | 16 |
public static void writeWebXml(WebXml theWebXml, OutputStream theOutput, |
296 |
String theEncoding, boolean isIndent)
|
|
297 |
throws IOException
|
|
298 |
{ |
|
299 | 16 |
OutputFormat outputFormat = |
300 |
new OutputFormat(theWebXml.getDocument());
|
|
301 | 16 |
if (theEncoding != null) |
302 |
{ |
|
303 | 0 |
outputFormat.setEncoding(theEncoding); |
304 |
} |
|
305 | 16 |
outputFormat.setIndenting(isIndent); |
306 | 16 |
outputFormat.setPreserveSpace(false);
|
307 | 16 |
XMLSerializer serializer = new XMLSerializer(theOutput, outputFormat);
|
308 | 16 |
serializer.serialize(theWebXml.getDocument()); |
309 |
} |
|
310 |
|
|
311 |
} |
|
312 |
|
|