1 22 23 package org.continuent.sequoia.common.util; 24 25 import java.io.BufferedReader ; 26 import java.io.BufferedWriter ; 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.FileWriter ; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Task; 33 34 42 public class SplitXmlTask extends Task 43 { 44 private String xmlFilePath; 45 private String outputDir; 46 private String attributeName; 47 private String startTagName; 48 private String endTagName; 49 50 53 public void execute() throws BuildException 54 { 55 try 56 { 57 File sourceFile = new File (xmlFilePath); 58 BufferedReader reader = new BufferedReader (new FileReader (sourceFile)); 59 String 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 fileName = lineBuffer.substring(index, lineBuffer.indexOf( 67 '\"', index)); 68 File generatedFile = new File (outputDir + File.separator + fileName 69 + ".xml"); 70 if (generatedFile.lastModified() < sourceFile.lastModified()) 71 { 72 BufferedWriter writer = new BufferedWriter (new FileWriter ( 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) writer.write(lineBuffer + System.getProperty("line.separator")); 83 writer.flush(); 84 writer.close(); 85 } 86 else 87 { 88 do 90 { 91 lineBuffer = reader.readLine(); 92 } 93 while ((lineBuffer != null) && lineBuffer.indexOf(endTagName) == -1); 94 } 95 continue; 96 } 97 } 98 } 99 catch (Exception e) 100 { 101 throw new BuildException(e.getMessage()); 102 } 103 } 104 105 110 public void setScriptXmlFile(String xmlFilePath) 111 { 112 this.xmlFilePath = xmlFilePath; 113 } 114 115 120 public void setOutputDir(String outputDirPath) 121 { 122 this.outputDir = outputDirPath; 123 File newDir = new File (outputDir); 124 newDir.mkdirs(); 125 } 126 127 132 public void setParsingTagName(String tagName) 133 { 134 this.startTagName = "<" + tagName + " "; 135 this.endTagName = "</" + tagName + ">"; 136 } 137 138 144 public void setOuputFileAttribute(String attributeName) 145 { 146 this.attributeName = attributeName; 147 } 148 } | Popular Tags |