Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

SplitXmlTask.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.util;
00026 
00027 import java.io.BufferedReader;
00028 import java.io.BufferedWriter;
00029 import java.io.File;
00030 import java.io.FileReader;
00031 import java.io.FileWriter;
00032 
00033 import org.apache.tools.ant.BuildException;
00034 import org.apache.tools.ant.Task;
00035 
00036 /**
00037  * Defines the SplitXml Ant target used to prepare the C-JDBC scripts
00038  * generation.
00039  * 
00040  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00041  * @version 1.0
00042  */
00043 public class SplitXmlTask extends Task
00044 {
00045   private String xmlFilePath;
00046   private String outputDir;
00047   private String attributeName;
00048   private String startTagName;
00049   private String endTagName;
00050 
00051   /**
00052    * @see org.apache.tools.ant.Task#execute()
00053    */
00054   public void execute() throws BuildException
00055   {
00056     try
00057     {
00058       BufferedReader reader = new BufferedReader(new FileReader(xmlFilePath));
00059       String lineBuffer;
00060       while ((lineBuffer = reader.readLine()) != null)
00061       {
00062         if (lineBuffer.indexOf(startTagName) != -1)
00063         {
00064           //System.out.println(lineBuffer);
00065           int index = lineBuffer.indexOf(attributeName)
00066               + attributeName.length() + 2;
00067           String fileName = lineBuffer.substring(index, lineBuffer.indexOf(
00068               '\"', index));
00069           BufferedWriter writer = new BufferedWriter(new FileWriter(outputDir
00070               + File.separator + fileName + ".xml"));
00071           writer.write(lineBuffer + System.getProperty("line.separator"));
00072           while ((lineBuffer = reader.readLine()) != null
00073               && lineBuffer.indexOf(endTagName) == -1)
00074           {
00075             writer.write(lineBuffer + System.getProperty("line.separator"));
00076           }
00077           if (lineBuffer != null) // append last line
00078             writer.write(lineBuffer + System.getProperty("line.separator"));
00079           writer.flush();
00080           writer.close();
00081           continue;
00082         }
00083       }
00084     }
00085     catch (Exception e)
00086     {
00087       throw new BuildException(e.getMessage());
00088     }
00089   }
00090 
00091   /**
00092    * Set the path to the xml path containing the scripts definition.
00093    * 
00094    * @param xmlFilePath path to the xml file
00095    */
00096   public void setScriptXmlFile(String xmlFilePath)
00097   {
00098     this.xmlFilePath = xmlFilePath;
00099   }
00100 
00101   /**
00102    * Specify the output directory.
00103    * 
00104    * @param outputDirPath the path to the directory
00105    */
00106   public void setOutputDir(String outputDirPath)
00107   {
00108     this.outputDir = outputDirPath;
00109     File newDir = new File(outputDir);
00110     newDir.mkdirs();
00111   }
00112 
00113   /**
00114    * Set parsing tag name.
00115    * 
00116    * @param tagName the tag name
00117    */
00118   public void setParsingTagName(String tagName)
00119   {
00120     this.startTagName = "<" + tagName + " ";
00121     this.endTagName = "</" + tagName + ">";
00122   }
00123 
00124   /**
00125    * Set the attribute that contains the name of the file.
00126    * 
00127    * @param attributeName the name of the attribute to get the name of the file
00128    *          to write
00129    */
00130   public void setOuputFileAttribute(String attributeName)
00131   {
00132     this.attributeName = attributeName;
00133   }
00134 }

Generated on Mon Apr 11 22:01:35 2005 for C-JDBC by  doxygen 1.3.9.1