1 24 25 package org.objectweb.cjdbc.common.util; 26 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.File ; 30 import java.io.FileReader ; 31 import java.io.FileWriter ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Task; 35 36 43 public class SplitXmlTask extends Task 44 { 45 private String xmlFilePath; 46 private String outputDir; 47 private String attributeName; 48 private String startTagName; 49 private String endTagName; 50 51 54 public void execute() throws BuildException 55 { 56 try 57 { 58 BufferedReader reader = new BufferedReader (new FileReader (xmlFilePath)); 59 String lineBuffer; 60 while ((lineBuffer = reader.readLine()) != null) 61 { 62 if (lineBuffer.indexOf(startTagName) != -1) 63 { 64 int index = lineBuffer.indexOf(attributeName) 66 + attributeName.length() + 2; 67 String fileName = lineBuffer.substring(index, lineBuffer.indexOf( 68 '\"', index)); 69 BufferedWriter writer = new BufferedWriter (new FileWriter (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) writer.write(lineBuffer + System.getProperty("line.separator")); 79 writer.flush(); 80 writer.close(); 81 continue; 82 } 83 } 84 } 85 catch (Exception e) 86 { 87 throw new BuildException(e.getMessage()); 88 } 89 } 90 91 96 public void setScriptXmlFile(String xmlFilePath) 97 { 98 this.xmlFilePath = xmlFilePath; 99 } 100 101 106 public void setOutputDir(String outputDirPath) 107 { 108 this.outputDir = outputDirPath; 109 File newDir = new File (outputDir); 110 newDir.mkdirs(); 111 } 112 113 118 public void setParsingTagName(String tagName) 119 { 120 this.startTagName = "<" + tagName + " "; 121 this.endTagName = "</" + tagName + ">"; 122 } 123 124 130 public void setOuputFileAttribute(String attributeName) 131 { 132 this.attributeName = attributeName; 133 } 134 } | Popular Tags |