KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > util > SplitXmlTask


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk.
20  * Contributor(s): Mathieu Peltier.
21  */

22
23 package org.continuent.sequoia.common.util;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.BufferedWriter JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.FileWriter JavaDoc;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Task;
33
34 /**
35  * Defines the SplitXml Ant target used to prepare the Sequoia scripts
36  * generation.
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk</a>
39  * @author <a HREF="mailto:mathieu.peltier@emicnetworks.com">Mathieu Peltier</a>
40  * @version 1.0
41  */

42 public class SplitXmlTask extends Task
43 {
44   private String JavaDoc xmlFilePath;
45   private String JavaDoc outputDir;
46   private String JavaDoc attributeName;
47   private String JavaDoc startTagName;
48   private String JavaDoc endTagName;
49
50   /**
51    * @see org.apache.tools.ant.Task#execute()
52    */

53   public void execute() throws BuildException
54   {
55     try
56     {
57       File JavaDoc sourceFile = new File JavaDoc(xmlFilePath);
58       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(sourceFile));
59       String JavaDoc lineBuffer;
60       while ((lineBuffer = reader.readLine()) != null)
61       {
62         if (lineBuffer.indexOf(startTagName) != -1)
63         {
64           int index = lineBuffer.indexOf(attributeName)
65               + attributeName.length() + 2;
66           String JavaDoc fileName = lineBuffer.substring(index, lineBuffer.indexOf(
67               '\"', index));
68           File JavaDoc generatedFile = new File JavaDoc(outputDir + File.separator + fileName
69               + ".xml");
70           if (generatedFile.lastModified() < sourceFile.lastModified())
71           {
72             // Source file has been modified, so regenerate xml file
73
BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(
74                 generatedFile));
75             writer.write(lineBuffer + System.getProperty("line.separator"));
76             while ((lineBuffer = reader.readLine()) != null
77                 && lineBuffer.indexOf(endTagName) == -1)
78             {
79               writer.write(lineBuffer + System.getProperty("line.separator"));
80             }
81             if (lineBuffer != null) // append last line
82
writer.write(lineBuffer + System.getProperty("line.separator"));
83             writer.flush();
84             writer.close();
85           }
86           else
87           {
88             // Go to next file because source file has not been modified
89
do
90             {
91               lineBuffer = reader.readLine();
92             }
93             while ((lineBuffer != null) && lineBuffer.indexOf(endTagName) == -1);
94           }
95           continue;
96         }
97       }
98     }
99     catch (Exception JavaDoc e)
100     {
101       throw new BuildException(e.getMessage());
102     }
103   }
104
105   /**
106    * Set the path to the xml path containing the scripts definition.
107    *
108    * @param xmlFilePath path to the xml file
109    */

110   public void setScriptXmlFile(String JavaDoc xmlFilePath)
111   {
112     this.xmlFilePath = xmlFilePath;
113   }
114
115   /**
116    * Specify the output directory.
117    *
118    * @param outputDirPath the path to the directory
119    */

120   public void setOutputDir(String JavaDoc outputDirPath)
121   {
122     this.outputDir = outputDirPath;
123     File JavaDoc newDir = new File JavaDoc(outputDir);
124     newDir.mkdirs();
125   }
126
127   /**
128    * Set parsing tag name.
129    *
130    * @param tagName the tag name
131    */

132   public void setParsingTagName(String JavaDoc tagName)
133   {
134     this.startTagName = "<" + tagName + " ";
135     this.endTagName = "</" + tagName + ">";
136   }
137
138   /**
139    * Set the attribute that contains the name of the file.
140    *
141    * @param attributeName the name of the attribute to get the name of the file
142    * to write
143    */

144   public void setOuputFileAttribute(String JavaDoc attributeName)
145   {
146     this.attributeName = attributeName;
147   }
148 }
Popular Tags