KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > WebXmlMergeTask


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 JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
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,v 1.20 2004/05/31 20:05:24 vmassol Exp $
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 JavaDoc srcFile;
53
54     /**
55      * Location of the overriding <code>web.xml</code>
56      */

57     private File JavaDoc mergeFile;
58
59     /**
60      * Location of the resulting <code>web.xml</code>
61      */

62     private File JavaDoc 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 JavaDoc 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     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 JavaDoc pce)
136         {
137             throw new BuildException("XML parser configuration problem: "
138                 + pce.getMessage(), pce);
139         }
140         catch (SAXException JavaDoc saxe)
141         {
142             throw new BuildException("Failed to parse descriptor: "
143                 + saxe.getMessage(), saxe);
144         }
145         catch (IOException JavaDoc ioe)
146         {
147             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     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     /**
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     public final void setSrcFile(File JavaDoc theSrcFile)
174     {
175         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     public final void setMergeFile(File JavaDoc theMergeFile)
184     {
185         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     public final void setDestFile(File JavaDoc theDestFile)
194     {
195         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     public final void setForce(boolean isForce)
205     {
206         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     public final void setEncoding(String JavaDoc theEncoding)
215     {
216         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     public final void setIndent(boolean isIndent)
226     {
227         this.indent = isIndent;
228     }
229
230 }
231
Popular Tags