|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| WebXmlMergeTask.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 58 |
|
|
| 59 |
import java.io.File;
|
|
| 60 |
import java.io.IOException;
|
|
| 61 |
|
|
| 62 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 63 |
|
|
| 64 |
import org.apache.cactus.integration.ant.util.AntLog;
|
|
| 65 |
import org.apache.cactus.integration.ant.deployment.WebXml;
|
|
| 66 |
import org.apache.cactus.integration.ant.deployment.WebXmlIo;
|
|
| 67 |
import org.apache.cactus.integration.ant.deployment.WebXmlMerger;
|
|
| 68 |
import org.apache.tools.ant.BuildException;
|
|
| 69 |
import org.apache.tools.ant.Project;
|
|
| 70 |
import org.apache.tools.ant.Task;
|
|
| 71 |
import org.apache.tools.ant.types.XMLCatalog;
|
|
| 72 |
import org.xml.sax.SAXException;
|
|
| 73 |
|
|
| 74 |
/**
|
|
| 75 |
* Ant task that can merge the definitions from two web deployment descriptors
|
|
| 76 |
* into one descriptor.
|
|
| 77 |
*
|
|
| 78 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 79 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
| 80 |
*
|
|
| 81 |
* @since Cactus 1.5
|
|
| 82 |
* @version $Id: WebXmlMergeTask.java,v 1.17 2003/06/18 19:55:45 cmlenz Exp $
|
|
| 83 |
*/
|
|
| 84 |
public class WebXmlMergeTask extends Task |
|
| 85 |
{
|
|
| 86 |
|
|
| 87 |
// Instance Variables ------------------------------------------------------
|
|
| 88 |
|
|
| 89 |
/**
|
|
| 90 |
* Location of the original <code>web.xml</code>
|
|
| 91 |
*/
|
|
| 92 |
private File srcFile;
|
|
| 93 |
|
|
| 94 |
/**
|
|
| 95 |
* Location of the overriding <code>web.xml</code>
|
|
| 96 |
*/
|
|
| 97 |
private File mergeFile;
|
|
| 98 |
|
|
| 99 |
/**
|
|
| 100 |
* Location of the resulting <code>web.xml</code>
|
|
| 101 |
*/
|
|
| 102 |
private File destFile;
|
|
| 103 |
|
|
| 104 |
/**
|
|
| 105 |
* Whether the merge should be performed even when the destination file is
|
|
| 106 |
* up to date.
|
|
| 107 |
*/
|
|
| 108 |
private boolean force = false; |
|
| 109 |
|
|
| 110 |
/**
|
|
| 111 |
* Whether the resulting XML file should be indented.
|
|
| 112 |
*/
|
|
| 113 |
private boolean indent = false; |
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* The encoding of the resulting XML file.
|
|
| 117 |
*/
|
|
| 118 |
private String encoding;
|
|
| 119 |
|
|
| 120 |
/**
|
|
| 121 |
* For resolving entities such as DTDs.
|
|
| 122 |
*/
|
|
| 123 |
private XMLCatalog xmlCatalog = null; |
|
| 124 |
|
|
| 125 |
// Public Methods ----------------------------------------------------------
|
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* @see Task#execute()
|
|
| 129 |
*/
|
|
| 130 | 0 |
public void execute() throws BuildException |
| 131 |
{
|
|
| 132 | 0 |
if ((this.srcFile == null) || !this.srcFile.isFile()) |
| 133 |
{
|
|
| 134 | 0 |
throw new BuildException("The [srcfile] attribute is required"); |
| 135 |
} |
|
| 136 | 0 |
if (this.destFile == null) |
| 137 |
{
|
|
| 138 | 0 |
throw new BuildException("The [destfile] attribute is required"); |
| 139 |
} |
|
| 140 |
|
|
| 141 | 0 |
try
|
| 142 |
{
|
|
| 143 | 0 |
if (this.mergeFile != null) |
| 144 |
{
|
|
| 145 | 0 |
if (!this.mergeFile.isFile()) |
| 146 |
{
|
|
| 147 | 0 |
throw new BuildException("The merge file doesn't exist"); |
| 148 |
} |
|
| 149 | 0 |
if (force
|
| 150 |
|| (srcFile.lastModified() > destFile.lastModified()) |
|
| 151 |
|| (mergeFile.lastModified() > destFile.lastModified())) |
|
| 152 |
{
|
|
| 153 | 0 |
WebXml srcWebXml = WebXmlIo.parseWebXmlFromFile( |
| 154 |
this.srcFile, this.xmlCatalog); |
|
| 155 | 0 |
WebXml mergeWebXml = WebXmlIo.parseWebXmlFromFile( |
| 156 |
this.mergeFile, this.xmlCatalog); |
|
| 157 | 0 |
WebXmlMerger merger = new WebXmlMerger(srcWebXml);
|
| 158 | 0 |
merger.setLog(new AntLog(this)); |
| 159 | 0 |
merger.merge(mergeWebXml); |
| 160 | 0 |
WebXmlIo.writeWebXml(srcWebXml, this.destFile,
|
| 161 |
this.encoding, this.indent); |
|
| 162 |
} |
|
| 163 |
else
|
|
| 164 |
{
|
|
| 165 | 0 |
log("The destination file is up to date",
|
| 166 |
Project.MSG_VERBOSE); |
|
| 167 |
} |
|
| 168 |
} |
|
| 169 |
else
|
|
| 170 |
{
|
|
| 171 | 0 |
throw new BuildException("The [mergefile] attribute is " |
| 172 |
+ "required");
|
|
| 173 |
} |
|
| 174 |
} |
|
| 175 |
catch (ParserConfigurationException pce)
|
|
| 176 |
{
|
|
| 177 | 0 |
throw new BuildException("XML parser configuration problem: " |
| 178 |
+ pce.getMessage(), pce); |
|
| 179 |
} |
|
| 180 |
catch (SAXException saxe)
|
|
| 181 |
{
|
|
| 182 | 0 |
throw new BuildException("Failed to parse descriptor: " |
| 183 |
+ saxe.getMessage(), saxe); |
|
| 184 |
} |
|
| 185 |
catch (IOException ioe)
|
|
| 186 |
{
|
|
| 187 | 0 |
throw new BuildException("An I/O error occurred: " |
| 188 |
+ ioe.getMessage(), ioe); |
|
| 189 |
} |
|
| 190 |
} |
|
| 191 |
|
|
| 192 |
/**
|
|
| 193 |
* Adds an XML catalog to the internal catalog.
|
|
| 194 |
*
|
|
| 195 |
* @param theXmlCatalog the XMLCatalog instance to use to look up DTDs
|
|
| 196 |
*/
|
|
| 197 | 0 |
public final void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) |
| 198 |
{
|
|
| 199 | 0 |
if (this.xmlCatalog == null) |
| 200 |
{
|
|
| 201 | 0 |
this.xmlCatalog = new XMLCatalog(); |
| 202 | 0 |
this.xmlCatalog.setProject(getProject());
|
| 203 |
} |
|
| 204 | 0 |
this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog);
|
| 205 |
} |
|
| 206 |
|
|
| 207 |
/**
|
|
| 208 |
* The original web deployment descriptor into which the new elements will
|
|
| 209 |
* be merged.
|
|
| 210 |
*
|
|
| 211 |
* @param theSrcFile the original <code>web.xml</code>
|
|
| 212 |
*/
|
|
| 213 | 0 |
public final void setSrcFile(File theSrcFile) |
| 214 |
{
|
|
| 215 | 0 |
this.srcFile = theSrcFile;
|
| 216 |
} |
|
| 217 |
|
|
| 218 |
/**
|
|
| 219 |
* The descriptor to merge into the original file.
|
|
| 220 |
*
|
|
| 221 |
* @param theMergeFile the <code>web.xml</code> to merge
|
|
| 222 |
*/
|
|
| 223 | 0 |
public final void setMergeFile(File theMergeFile) |
| 224 |
{
|
|
| 225 | 0 |
this.mergeFile = theMergeFile;
|
| 226 |
} |
|
| 227 |
|
|
| 228 |
/**
|
|
| 229 |
* The destination file where the result of the merge are stored.
|
|
| 230 |
*
|
|
| 231 |
* @param theDestFile the resulting <code>web.xml</code>
|
|
| 232 |
*/
|
|
| 233 | 0 |
public final void setDestFile(File theDestFile) |
| 234 |
{
|
|
| 235 | 0 |
this.destFile = theDestFile;
|
| 236 |
} |
|
| 237 |
|
|
| 238 |
/**
|
|
| 239 |
* Sets whether the merge should be performed even when the destination
|
|
| 240 |
* file is up to date.
|
|
| 241 |
*
|
|
| 242 |
* @param isForce Whether the merge should be forced
|
|
| 243 |
*/
|
|
| 244 | 0 |
public final void setForce(boolean isForce) |
| 245 |
{
|
|
| 246 | 0 |
this.force = isForce;
|
| 247 |
} |
|
| 248 |
|
|
| 249 |
/**
|
|
| 250 |
* Sets the encoding of the resulting XML file. Default is 'UTF-8'.
|
|
| 251 |
*
|
|
| 252 |
* @param theEncoding The encoding to set
|
|
| 253 |
*/
|
|
| 254 | 0 |
public final void setEncoding(String theEncoding) |
| 255 |
{
|
|
| 256 | 0 |
this.encoding = theEncoding;
|
| 257 |
} |
|
| 258 |
|
|
| 259 |
/**
|
|
| 260 |
* Whether the result XML file should be indented for better readability.
|
|
| 261 |
* Default is 'false'.
|
|
| 262 |
*
|
|
| 263 |
* @param isIndent Whether the result should be indented
|
|
| 264 |
*/
|
|
| 265 | 0 |
public final void setIndent(boolean isIndent) |
| 266 |
{
|
|
| 267 | 0 |
this.indent = isIndent;
|
| 268 |
} |
|
| 269 |
|
|
| 270 |
} |
|
| 271 |
|
|
||||||||||