|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ApplicationXmlIo.java | 8.3% | 18.4% | 14.3% | 15.8% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2003-2005 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.application;
|
|
| 21 |
|
|
| 22 |
import java.io.File;
|
|
| 23 |
import java.io.FileInputStream;
|
|
| 24 |
import java.io.FileOutputStream;
|
|
| 25 |
import java.io.IOException;
|
|
| 26 |
import java.io.InputStream;
|
|
| 27 |
import java.io.OutputStream;
|
|
| 28 |
|
|
| 29 |
import javax.xml.parsers.DocumentBuilder;
|
|
| 30 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
| 31 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 32 |
|
|
| 33 |
import org.apache.xml.serialize.OutputFormat;
|
|
| 34 |
import org.apache.xml.serialize.XMLSerializer;
|
|
| 35 |
import org.xml.sax.EntityResolver;
|
|
| 36 |
import org.xml.sax.InputSource;
|
|
| 37 |
import org.xml.sax.SAXException;
|
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
* Provides convenience methods for reading and writing enterprise application
|
|
| 41 |
* deployment descriptors (application.xml).
|
|
| 42 |
*
|
|
| 43 |
* @since Cactus 1.5
|
|
| 44 |
* @version $Id: ApplicationXmlIo.java 239141 2005-02-15 10:31:44Z vmassol $
|
|
| 45 |
*/
|
|
| 46 |
public class ApplicationXmlIo |
|
| 47 |
{
|
|
| 48 |
|
|
| 49 |
// Inner Classes -----------------------------------------------------------
|
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* Implementation of the SAX EntityResolver interface that looks up the
|
|
| 53 |
* application DTDs from the JAR.
|
|
| 54 |
*/
|
|
| 55 |
private static class ApplicationXmlEntityResolver implements EntityResolver |
|
| 56 |
{
|
|
| 57 |
|
|
| 58 |
/**
|
|
| 59 |
* @see org.xml.sax.EntityResolver#resolveEntity
|
|
| 60 |
*/
|
|
| 61 | 0 |
public InputSource resolveEntity(String thePublicId, String theSystemId)
|
| 62 |
throws SAXException, IOException
|
|
| 63 |
{
|
|
| 64 | 0 |
ApplicationXmlVersion version = |
| 65 |
ApplicationXmlVersion.valueOf(thePublicId); |
|
| 66 | 0 |
if (version != null) |
| 67 |
{
|
|
| 68 | 0 |
String fileName = version.getSystemId().substring( |
| 69 |
version.getSystemId().lastIndexOf('/'));
|
|
| 70 | 0 |
InputStream in = this.getClass().getResourceAsStream(
|
| 71 |
"/org/apache/cactus/integration/ant/deployment/resources"
|
|
| 72 |
+ fileName); |
|
| 73 | 0 |
if (in != null) |
| 74 |
{
|
|
| 75 | 0 |
return new InputSource(in); |
| 76 |
} |
|
| 77 |
} |
|
| 78 | 0 |
return null; |
| 79 |
} |
|
| 80 |
|
|
| 81 |
} |
|
| 82 |
|
|
| 83 |
// Public Methods ----------------------------------------------------------
|
|
| 84 |
|
|
| 85 |
/**
|
|
| 86 |
* Parses a deployment descriptor stored in a regular file.
|
|
| 87 |
*
|
|
| 88 |
* @param theFile The file to parse
|
|
| 89 |
* @param theEntityResolver A SAX entity resolver, or <code>null</code> to
|
|
| 90 |
* use the default
|
|
| 91 |
* @return The parsed descriptor
|
|
| 92 |
* @throws SAXException If the file could not be parsed
|
|
| 93 |
* @throws ParserConfigurationException If the XML parser was not correctly
|
|
| 94 |
* configured
|
|
| 95 |
* @throws IOException If an I/O error occurs
|
|
| 96 |
*/
|
|
| 97 | 0 |
public static ApplicationXml parseApplicationXmlFromFile(File theFile, |
| 98 |
EntityResolver theEntityResolver) |
|
| 99 |
throws SAXException, ParserConfigurationException, IOException
|
|
| 100 |
{
|
|
| 101 | 0 |
InputStream in = null;
|
| 102 | 0 |
try
|
| 103 |
{
|
|
| 104 | 0 |
in = new FileInputStream(theFile);
|
| 105 | 0 |
return parseApplicationXml(in, theEntityResolver);
|
| 106 |
} |
|
| 107 |
finally
|
|
| 108 |
{
|
|
| 109 | 0 |
if (in != null) |
| 110 |
{
|
|
| 111 | 0 |
try
|
| 112 |
{
|
|
| 113 | 0 |
in.close(); |
| 114 |
} |
|
| 115 |
catch (IOException ioe)
|
|
| 116 |
{
|
|
| 117 |
// we'll pass on the original IO error, so ignore this one
|
|
| 118 |
} |
|
| 119 |
} |
|
| 120 |
} |
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
/**
|
|
| 124 |
* Parses a deployment descriptor provided as input stream.
|
|
| 125 |
*
|
|
| 126 |
* @param theInput The input stream
|
|
| 127 |
* @param theEntityResolver A SAX entity resolver, or <code>null</code> to
|
|
| 128 |
* use the default
|
|
| 129 |
* @return The parsed descriptor
|
|
| 130 |
* @throws SAXException If the input could not be parsed
|
|
| 131 |
* @throws ParserConfigurationException If the XML parser was not correctly
|
|
| 132 |
* configured
|
|
| 133 |
* @throws IOException If an I/O error occurs
|
|
| 134 |
*/
|
|
| 135 | 4 |
public static ApplicationXml parseApplicationXml(InputStream theInput, |
| 136 |
EntityResolver theEntityResolver) |
|
| 137 |
throws SAXException, ParserConfigurationException, IOException
|
|
| 138 |
{
|
|
| 139 | 4 |
DocumentBuilderFactory factory = |
| 140 |
DocumentBuilderFactory.newInstance(); |
|
| 141 | 4 |
factory.setValidating(false);
|
| 142 | 4 |
factory.setNamespaceAware(false);
|
| 143 | 4 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 144 | 4 |
if (theEntityResolver != null) |
| 145 |
{
|
|
| 146 | 0 |
builder.setEntityResolver(theEntityResolver); |
| 147 |
} |
|
| 148 |
else
|
|
| 149 |
{
|
|
| 150 | 4 |
builder.setEntityResolver(new ApplicationXmlEntityResolver());
|
| 151 |
} |
|
| 152 | 4 |
return new DefaultApplicationXml(builder.parse(theInput)); |
| 153 |
} |
|
| 154 |
|
|
| 155 |
/**
|
|
| 156 |
* Writes the specified document to a file.
|
|
| 157 |
*
|
|
| 158 |
* @param theAppXml The descriptor to serialize
|
|
| 159 |
* @param theFile The file to write to
|
|
| 160 |
* @throws IOException If an I/O error occurs
|
|
| 161 |
*/
|
|
| 162 | 0 |
public static void writeApplicationXml(ApplicationXml theAppXml, |
| 163 |
File theFile) |
|
| 164 |
throws IOException
|
|
| 165 |
{
|
|
| 166 | 0 |
writeApplicationXml(theAppXml, theFile, null, false); |
| 167 |
} |
|
| 168 |
|
|
| 169 |
/**
|
|
| 170 |
* Writes the specified document to a file.
|
|
| 171 |
*
|
|
| 172 |
* @param theAppXml The descriptor to serialize
|
|
| 173 |
* @param theFile The file to write to
|
|
| 174 |
* @param theEncoding The character encoding to use
|
|
| 175 |
* @throws IOException If an I/O error occurs
|
|
| 176 |
*/
|
|
| 177 | 0 |
public static void writeApplicationXml(ApplicationXml theAppXml, |
| 178 |
File theFile, |
|
| 179 |
String theEncoding) |
|
| 180 |
throws IOException
|
|
| 181 |
{
|
|
| 182 | 0 |
writeApplicationXml(theAppXml, theFile, theEncoding, false);
|
| 183 |
} |
|
| 184 |
|
|
| 185 |
/**
|
|
| 186 |
* Writes the specified document to a file.
|
|
| 187 |
*
|
|
| 188 |
* @param theAppXml The descriptor to serialize
|
|
| 189 |
* @param theFile The file to write to
|
|
| 190 |
* @param theEncoding The character encoding to use
|
|
| 191 |
* @param isIndent Whether the written XML should be indented
|
|
| 192 |
* @throws IOException If an I/O error occurs
|
|
| 193 |
*/
|
|
| 194 | 0 |
public static void writeApplicationXml(ApplicationXml theAppXml, |
| 195 |
File theFile, |
|
| 196 |
String theEncoding, |
|
| 197 |
boolean isIndent)
|
|
| 198 |
throws IOException
|
|
| 199 |
{
|
|
| 200 | 0 |
OutputStream out = null;
|
| 201 | 0 |
try
|
| 202 |
{
|
|
| 203 | 0 |
out = new FileOutputStream(theFile);
|
| 204 | 0 |
writeApplicationXml(theAppXml, out, theEncoding, isIndent); |
| 205 |
} |
|
| 206 |
finally
|
|
| 207 |
{
|
|
| 208 | 0 |
if (out != null) |
| 209 |
{
|
|
| 210 | 0 |
try
|
| 211 |
{
|
|
| 212 | 0 |
out.close(); |
| 213 |
} |
|
| 214 |
catch (IOException ioe)
|
|
| 215 |
{
|
|
| 216 |
// we'll pass on the original IO error, so ignore this one
|
|
| 217 |
} |
|
| 218 |
} |
|
| 219 |
} |
|
| 220 |
} |
|
| 221 |
|
|
| 222 |
/**
|
|
| 223 |
* Writes the specified document to an output stream.
|
|
| 224 |
*
|
|
| 225 |
* @param theAppXml The descriptor to serialize
|
|
| 226 |
* @param theOutput The output stream to write to
|
|
| 227 |
* @param theEncoding The character encoding to use
|
|
| 228 |
* @param isIndent Whether the written XML should be indented
|
|
| 229 |
* @throws IOException If an I/O error occurs
|
|
| 230 |
*/
|
|
| 231 | 0 |
public static void writeApplicationXml(ApplicationXml theAppXml, |
| 232 |
OutputStream theOutput, |
|
| 233 |
String theEncoding, |
|
| 234 |
boolean isIndent)
|
|
| 235 |
throws IOException
|
|
| 236 |
{
|
|
| 237 | 0 |
OutputFormat outputFormat = |
| 238 |
new OutputFormat(theAppXml.getDocument());
|
|
| 239 | 0 |
if (theEncoding != null) |
| 240 |
{
|
|
| 241 | 0 |
outputFormat.setEncoding(theEncoding); |
| 242 |
} |
|
| 243 | 0 |
outputFormat.setIndenting(isIndent); |
| 244 | 0 |
outputFormat.setPreserveSpace(false);
|
| 245 | 0 |
XMLSerializer serializer = new XMLSerializer(theOutput, outputFormat);
|
| 246 | 0 |
serializer.serialize(theAppXml.getDocument()); |
| 247 |
} |
|
| 248 |
|
|
| 249 |
} |
|
| 250 |
|
|
||||||||||