KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > util > SplitXmlTask


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.util;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.FileWriter JavaDoc;
32
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.Task;
35
36 /**
37  * Defines the SplitXml Ant target used to prepare the C-JDBC scripts
38  * generation.
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
41  * @version 1.0
42  */

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

54   public void execute() throws BuildException
55   {
56     try
57     {
58       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(xmlFilePath));
59       String JavaDoc lineBuffer;
60       while ((lineBuffer = reader.readLine()) != null)
61       {
62         if (lineBuffer.indexOf(startTagName) != -1)
63         {
64           //System.out.println(lineBuffer);
65
int index = lineBuffer.indexOf(attributeName)
66               + attributeName.length() + 2;
67           String JavaDoc fileName = lineBuffer.substring(index, lineBuffer.indexOf(
68               '\"', index));
69           BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(outputDir
70               + File.separator + fileName + ".xml"));
71           writer.write(lineBuffer + System.getProperty("line.separator"));
72           while ((lineBuffer = reader.readLine()) != null
73               && lineBuffer.indexOf(endTagName) == -1)
74           {
75             writer.write(lineBuffer + System.getProperty("line.separator"));
76           }
77           if (lineBuffer != null) // append last line
78
writer.write(lineBuffer + System.getProperty("line.separator"));
79           writer.flush();
80           writer.close();
81           continue;
82         }
83       }
84     }
85     catch (Exception JavaDoc e)
86     {
87       throw new BuildException(e.getMessage());
88     }
89   }
90
91   /**
92    * Set the path to the xml path containing the scripts definition.
93    *
94    * @param xmlFilePath path to the xml file
95    */

96   public void setScriptXmlFile(String JavaDoc xmlFilePath)
97   {
98     this.xmlFilePath = xmlFilePath;
99   }
100
101   /**
102    * Specify the output directory.
103    *
104    * @param outputDirPath the path to the directory
105    */

106   public void setOutputDir(String JavaDoc outputDirPath)
107   {
108     this.outputDir = outputDirPath;
109     File JavaDoc newDir = new File JavaDoc(outputDir);
110     newDir.mkdirs();
111   }
112
113   /**
114    * Set parsing tag name.
115    *
116    * @param tagName the tag name
117    */

118   public void setParsingTagName(String JavaDoc tagName)
119   {
120     this.startTagName = "<" + tagName + " ";
121     this.endTagName = "</" + tagName + ">";
122   }
123
124   /**
125    * Set the attribute that contains the name of the file.
126    *
127    * @param attributeName the name of the attribute to get the name of the file
128    * to write
129    */

130   public void setOuputFileAttribute(String JavaDoc attributeName)
131   {
132     this.attributeName = attributeName;
133   }
134 }
Popular Tags