|
|||||||||||||||||||
| 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 | |||||||||||||||
| WebXmlMergeTask.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 21 |
|
|
| 22 |
import java.io.File;
|
|
| 23 |
import java.io.IOException;
|
|
| 24 |
|
|
| 25 |
import javax.xml.parsers.ParserConfigurationException;
|
|
| 26 |
|
|
| 27 |
import org.apache.tools.ant.BuildException;
|
|
| 28 |
import org.apache.tools.ant.Project;
|
|
| 29 |
import org.apache.tools.ant.Task;
|
|
| 30 |
import org.apache.tools.ant.types.XMLCatalog;
|
|
| 31 |
import org.codehaus.cargo.module.webapp.WebXml;
|
|
| 32 |
import org.codehaus.cargo.module.webapp.WebXmlIo;
|
|
| 33 |
import org.codehaus.cargo.module.webapp.WebXmlMerger;
|
|
| 34 |
import org.codehaus.cargo.util.monitor.AntMonitor;
|
|
| 35 |
import org.xml.sax.SAXException;
|
|
| 36 |
|
|
| 37 |
/**
|
|
| 38 |
* Ant task that can merge the definitions from two web deployment descriptors
|
|
| 39 |
* into one descriptor.
|
|
| 40 |
*
|
|
| 41 |
* @since Cactus 1.5
|
|
| 42 |
* @version $Id: WebXmlMergeTask.java 239162 2005-04-26 09:57:59Z grimsell $
|
|
| 43 |
*/
|
|
| 44 |
public class WebXmlMergeTask extends Task |
|
| 45 |
{
|
|
| 46 |
|
|
| 47 |
// Instance Variables ------------------------------------------------------
|
|
| 48 |
|
|
| 49 |
/**
|
|
| 50 |
* Location of the original <code>web.xml</code>
|
|
| 51 |
*/
|
|
| 52 |
private File srcFile;
|
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* Location of the overriding <code>web.xml</code>
|
|
| 56 |
*/
|
|
| 57 |
private File mergeFile;
|
|
| 58 |
|
|
| 59 |
/**
|
|
| 60 |
* Location of the resulting <code>web.xml</code>
|
|
| 61 |
*/
|
|
| 62 |
private File destFile;
|
|
| 63 |
|
|
| 64 |
/**
|
|
| 65 |
* Whether the merge should be performed even when the destination file is
|
|
| 66 |
* up to date.
|
|
| 67 |
*/
|
|
| 68 |
private boolean force = false; |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Whether the resulting XML file should be indented.
|
|
| 72 |
*/
|
|
| 73 |
private boolean indent = false; |
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* The encoding of the resulting XML file.
|
|
| 77 |
*/
|
|
| 78 |
private String encoding;
|
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* For resolving entities such as DTDs.
|
|
| 82 |
*/
|
|
| 83 |
private XMLCatalog xmlCatalog = null; |
|
| 84 |
|
|
| 85 |
// Public Methods ----------------------------------------------------------
|
|
| 86 |
|
|
| 87 |
/**
|
|
| 88 |
* @see Task#execute()
|
|
| 89 |
*/
|
|
| 90 | 0 |
public void execute() throws BuildException |
| 91 |
{
|
|
| 92 | 0 |
if ((this.srcFile == null) || !this.srcFile.isFile()) |
| 93 |
{
|
|
| 94 | 0 |
throw new BuildException("The [srcfile] attribute is required"); |
| 95 |
} |
|
| 96 | 0 |
if (this.destFile == null) |
| 97 |
{
|
|
| 98 | 0 |
throw new BuildException("The [destfile] attribute is required"); |
| 99 |
} |
|
| 100 |
|
|
| 101 | 0 |
try
|
| 102 |
{
|
|
| 103 | 0 |
if (this.mergeFile != null) |
| 104 |
{
|
|
| 105 | 0 |
if (!this.mergeFile.isFile()) |
| 106 |
{
|
|
| 107 | 0 |
throw new BuildException("The merge file doesn't exist"); |
| 108 |
} |
|
| 109 | 0 |
if (force
|
| 110 |
|| (srcFile.lastModified() > destFile.lastModified()) |
|
| 111 |
|| (mergeFile.lastModified() > destFile.lastModified())) |
|
| 112 |
{
|
|
| 113 | 0 |
WebXml srcWebXml = WebXmlIo.parseWebXmlFromFile( |
| 114 |
this.srcFile, this.xmlCatalog); |
|
| 115 | 0 |
WebXml mergeWebXml = WebXmlIo.parseWebXmlFromFile( |
| 116 |
this.mergeFile, this.xmlCatalog); |
|
| 117 | 0 |
WebXmlMerger merger = new WebXmlMerger(srcWebXml);
|
| 118 | 0 |
merger.setMonitor(new AntMonitor(this)); |
| 119 | 0 |
merger.merge(mergeWebXml); |
| 120 | 0 |
WebXmlIo.writeWebXml(srcWebXml, this.destFile,
|
| 121 |
this.encoding, this.indent); |
|
| 122 |
} |
|
| 123 |
else
|
|
| 124 |
{
|
|
| 125 | 0 |
log("The destination file is up to date",
|
| 126 |
Project.MSG_VERBOSE); |
|
| 127 |
} |
|
| 128 |
} |
|
| 129 |
else
|
|
| 130 |
{
|
|
| 131 | 0 |
throw new BuildException("The [mergefile] attribute is " |
| 132 |
+ "required");
|
|
| 133 |
} |
|
| 134 |
} |
|
| 135 |
catch (ParserConfigurationException pce)
|
|
| 136 |
{
|
|
| 137 | 0 |
throw new BuildException("XML parser configuration problem: " |
| 138 |
+ pce.getMessage(), pce); |
|
| 139 |
} |
|
| 140 |
catch (SAXException saxe)
|
|
| 141 |
{
|
|
| 142 | 0 |
throw new BuildException("Failed to parse descriptor: " |
| 143 |
+ saxe.getMessage(), saxe); |
|
| 144 |
} |
|
| 145 |
catch (IOException ioe)
|
|
| 146 |
{
|
|
| 147 | 0 |
throw new BuildException("An I/O error occurred: " |
| 148 |
+ ioe.getMessage(), ioe); |
|
| 149 |
} |
|
| 150 |
} |
|
| 151 |
|
|
| 152 |
/**
|
|
| 153 |
* Adds an XML catalog to the internal catalog.
|
|
| 154 |
*
|
|
| 155 |
* @param theXmlCatalog the XMLCatalog instance to use to look up DTDs
|
|
| 156 |
*/
|
|
| 157 | 0 |
public final void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) |
| 158 |
{
|
|
| 159 | 0 |
if (this.xmlCatalog == null) |
| 160 |
{
|
|
| 161 | 0 |
this.xmlCatalog = new XMLCatalog(); |
| 162 | 0 |
this.xmlCatalog.setProject(getProject());
|
| 163 |
} |
|
| 164 | 0 |
this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog);
|
| 165 |
} |
|
| 166 |
|
|
| 167 |
/**
|
|
| 168 |
* The original web deployment descriptor into which the new elements will
|
|
| 169 |
* be merged.
|
|
| 170 |
*
|
|
| 171 |
* @param theSrcFile the original <code>web.xml</code>
|
|
| 172 |
*/
|
|
| 173 | 0 |
public final void setSrcFile(File theSrcFile) |
| 174 |
{
|
|
| 175 | 0 |
this.srcFile = theSrcFile;
|
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* The descriptor to merge into the original file.
|
|
| 180 |
*
|
|
| 181 |
* @param theMergeFile the <code>web.xml</code> to merge
|
|
| 182 |
*/
|
|
| 183 | 0 |
public final void setMergeFile(File theMergeFile) |
| 184 |
{
|
|
| 185 | 0 |
this.mergeFile = theMergeFile;
|
| 186 |
} |
|
| 187 |
|
|
| 188 |
/**
|
|
| 189 |
* The destination file where the result of the merge are stored.
|
|
| 190 |
*
|
|
| 191 |
* @param theDestFile the resulting <code>web.xml</code>
|
|
| 192 |
*/
|
|
| 193 | 0 |
public final void setDestFile(File theDestFile) |
| 194 |
{
|
|
| 195 | 0 |
this.destFile = theDestFile;
|
| 196 |
} |
|
| 197 |
|
|
| 198 |
/**
|
|
| 199 |
* Sets whether the merge should be performed even when the destination
|
|
| 200 |
* file is up to date.
|
|
| 201 |
*
|
|
| 202 |
* @param isForce Whether the merge should be forced
|
|
| 203 |
*/
|
|
| 204 | 0 |
public final void setForce(boolean isForce) |
| 205 |
{
|
|
| 206 | 0 |
this.force = isForce;
|
| 207 |
} |
|
| 208 |
|
|
| 209 |
/**
|
|
| 210 |
* Sets the encoding of the resulting XML file. Default is 'UTF-8'.
|
|
| 211 |
*
|
|
| 212 |
* @param theEncoding The encoding to set
|
|
| 213 |
*/
|
|
| 214 | 0 |
public final void setEncoding(String theEncoding) |
| 215 |
{
|
|
| 216 | 0 |
this.encoding = theEncoding;
|
| 217 |
} |
|
| 218 |
|
|
| 219 |
/**
|
|
| 220 |
* Whether the result XML file should be indented for better readability.
|
|
| 221 |
* Default is 'false'.
|
|
| 222 |
*
|
|
| 223 |
* @param isIndent Whether the result should be indented
|
|
| 224 |
*/
|
|
| 225 | 0 |
public final void setIndent(boolean isIndent) |
| 226 |
{
|
|
| 227 | 0 |
this.indent = isIndent;
|
| 228 |
} |
|
| 229 |
|
|
| 230 |
} |
|
| 231 |
|
|
||||||||||