001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk.
020: * Contributor(s): Mathieu Peltier.
021: */package org.continuent.sequoia.common.util;
022:
023: import java.io.BufferedReader;
024: import java.io.BufferedWriter;
025: import java.io.File;
026: import java.io.FileReader;
027: import java.io.FileWriter;
028:
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031:
032: /**
033: * Defines the SplitXml Ant target used to prepare the Sequoia scripts
034: * generation.
035: *
036: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk</a>
037: * @author <a href="mailto:mathieu.peltier@emicnetworks.com">Mathieu Peltier</a>
038: * @version 1.0
039: */
040: public class SplitXmlTask extends Task {
041: private String xmlFilePath;
042: private String outputDir;
043: private String attributeName;
044: private String startTagName;
045: private String endTagName;
046:
047: /**
048: * @see org.apache.tools.ant.Task#execute()
049: */
050: public void execute() throws BuildException {
051: try {
052: File sourceFile = new File(xmlFilePath);
053: BufferedReader reader = new BufferedReader(new FileReader(
054: sourceFile));
055: String lineBuffer;
056: while ((lineBuffer = reader.readLine()) != null) {
057: if (lineBuffer.indexOf(startTagName) != -1) {
058: int index = lineBuffer.indexOf(attributeName)
059: + attributeName.length() + 2;
060: String fileName = lineBuffer.substring(index,
061: lineBuffer.indexOf('\"', index));
062: File generatedFile = new File(outputDir
063: + File.separator + fileName + ".xml");
064: if (generatedFile.lastModified() < sourceFile
065: .lastModified()) {
066: // Source file has been modified, so regenerate xml file
067: BufferedWriter writer = new BufferedWriter(
068: new FileWriter(generatedFile));
069: writer.write(lineBuffer
070: + System.getProperty("line.separator"));
071: while ((lineBuffer = reader.readLine()) != null
072: && lineBuffer.indexOf(endTagName) == -1) {
073: writer
074: .write(lineBuffer
075: + System
076: .getProperty("line.separator"));
077: }
078: if (lineBuffer != null) // append last line
079: writer
080: .write(lineBuffer
081: + System
082: .getProperty("line.separator"));
083: writer.flush();
084: writer.close();
085: } else {
086: // Go to next file because source file has not been modified
087: do {
088: lineBuffer = reader.readLine();
089: } while ((lineBuffer != null)
090: && lineBuffer.indexOf(endTagName) == -1);
091: }
092: continue;
093: }
094: }
095: } catch (Exception e) {
096: throw new BuildException(e.getMessage());
097: }
098: }
099:
100: /**
101: * Set the path to the xml path containing the scripts definition.
102: *
103: * @param xmlFilePath path to the xml file
104: */
105: public void setScriptXmlFile(String xmlFilePath) {
106: this .xmlFilePath = xmlFilePath;
107: }
108:
109: /**
110: * Specify the output directory.
111: *
112: * @param outputDirPath the path to the directory
113: */
114: public void setOutputDir(String outputDirPath) {
115: this .outputDir = outputDirPath;
116: File newDir = new File(outputDir);
117: newDir.mkdirs();
118: }
119:
120: /**
121: * Set parsing tag name.
122: *
123: * @param tagName the tag name
124: */
125: public void setParsingTagName(String tagName) {
126: this .startTagName = "<" + tagName + " ";
127: this .endTagName = "</" + tagName + ">";
128: }
129:
130: /**
131: * Set the attribute that contains the name of the file.
132: *
133: * @param attributeName the name of the attribute to get the name of the file
134: * to write
135: */
136: public void setOuputFileAttribute(String attributeName) {
137: this.attributeName = attributeName;
138: }
139: }
|