1 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.cactus.integration.ant.util.AntLog; 28 import org.apache.cactus.integration.ant.deployment.webapp.WebXml; 29 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlIo; 30 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlMerger; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.Task; 34 import org.apache.tools.ant.types.XMLCatalog; 35 import org.xml.sax.SAXException ; 36 37 44 public class WebXmlMergeTask extends Task 45 { 46 47 49 52 private File srcFile; 53 54 57 private File mergeFile; 58 59 62 private File destFile; 63 64 68 private boolean force = false; 69 70 73 private boolean indent = false; 74 75 78 private String encoding; 79 80 83 private XMLCatalog xmlCatalog = null; 84 85 87 90 public void execute() throws BuildException 91 { 92 if ((this.srcFile == null) || !this.srcFile.isFile()) 93 { 94 throw new BuildException("The [srcfile] attribute is required"); 95 } 96 if (this.destFile == null) 97 { 98 throw new BuildException("The [destfile] attribute is required"); 99 } 100 101 try 102 { 103 if (this.mergeFile != null) 104 { 105 if (!this.mergeFile.isFile()) 106 { 107 throw new BuildException("The merge file doesn't exist"); 108 } 109 if (force 110 || (srcFile.lastModified() > destFile.lastModified()) 111 || (mergeFile.lastModified() > destFile.lastModified())) 112 { 113 WebXml srcWebXml = WebXmlIo.parseWebXmlFromFile( 114 this.srcFile, this.xmlCatalog); 115 WebXml mergeWebXml = WebXmlIo.parseWebXmlFromFile( 116 this.mergeFile, this.xmlCatalog); 117 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 118 merger.setLog(new AntLog(this)); 119 merger.merge(mergeWebXml); 120 WebXmlIo.writeWebXml(srcWebXml, this.destFile, 121 this.encoding, this.indent); 122 } 123 else 124 { 125 log("The destination file is up to date", 126 Project.MSG_VERBOSE); 127 } 128 } 129 else 130 { 131 throw new BuildException("The [mergefile] attribute is " 132 + "required"); 133 } 134 } 135 catch (ParserConfigurationException pce) 136 { 137 throw new BuildException("XML parser configuration problem: " 138 + pce.getMessage(), pce); 139 } 140 catch (SAXException saxe) 141 { 142 throw new BuildException("Failed to parse descriptor: " 143 + saxe.getMessage(), saxe); 144 } 145 catch (IOException ioe) 146 { 147 throw new BuildException("An I/O error occurred: " 148 + ioe.getMessage(), ioe); 149 } 150 } 151 152 157 public final void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) 158 { 159 if (this.xmlCatalog == null) 160 { 161 this.xmlCatalog = new XMLCatalog(); 162 this.xmlCatalog.setProject(getProject()); 163 } 164 this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog); 165 } 166 167 173 public final void setSrcFile(File theSrcFile) 174 { 175 this.srcFile = theSrcFile; 176 } 177 178 183 public final void setMergeFile(File theMergeFile) 184 { 185 this.mergeFile = theMergeFile; 186 } 187 188 193 public final void setDestFile(File theDestFile) 194 { 195 this.destFile = theDestFile; 196 } 197 198 204 public final void setForce(boolean isForce) 205 { 206 this.force = isForce; 207 } 208 209 214 public final void setEncoding(String theEncoding) 215 { 216 this.encoding = theEncoding; 217 } 218 219 225 public final void setIndent(boolean isIndent) 226 { 227 this.indent = isIndent; 228 } 229 230 } 231 | Popular Tags |