1 2 17 18 package org.apache.poi.dev; 19 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import java.io.File ; 26 27 33 public class RecordGenerator { 34 40 public static void main(String [] args) 41 throws Exception { 42 Class.forName("org.apache.poi.generator.FieldIterator"); 44 45 if (args.length != 4) { 46 System.out.println("Usage:"); 47 System.out.println(" java org.apache.poi.hssf.util.RecordGenerator RECORD_DEFINTIONS RECORD_STYLES DEST_SRC_PATH TEST_SRC_PATH"); 48 } else { 49 generateRecords(args[0], args[1], args[2], args[3]); 50 } 51 } 52 53 54 private static void generateRecords(String defintionsDir, String recordStyleDir, String destSrcPathDir, String testSrcPathDir) 55 throws Exception { 56 File definitionsFile = new File (defintionsDir); 57 58 for (int i = 0; i < definitionsFile.listFiles().length; i++) { 59 File file = definitionsFile.listFiles()[i]; 60 if (file.isFile() && 61 (file.getName().endsWith("_record.xml") || 62 file.getName().endsWith("_type.xml") 63 ) 64 ) { 65 DocumentBuilderFactory factory = 67 DocumentBuilderFactory.newInstance(); 68 DocumentBuilder builder = factory.newDocumentBuilder(); 69 Document document = builder.parse(file); 70 Element record = document.getDocumentElement(); 71 String extendstg = record.getElementsByTagName("extends").item(0).getFirstChild().getNodeValue(); 72 String suffix = record.getElementsByTagName("suffix").item(0).getFirstChild().getNodeValue(); 73 String recordName = record.getAttributes().getNamedItem("name").getNodeValue(); 74 String packageName = record.getAttributes().getNamedItem("package").getNodeValue(); 75 packageName = packageName.replace('.', '/'); 76 77 String destinationPath = destSrcPathDir + "/" + packageName; 79 File destinationPathFile = new File (destinationPath); 80 destinationPathFile.mkdirs(); 81 String destinationFilepath = destinationPath + "/" + recordName + suffix + ".java"; 82 String args[] = new String []{"-in", file.getAbsolutePath(), "-xsl", recordStyleDir + "/" + extendstg.toLowerCase() + ".xsl", 83 "-out", destinationFilepath, 84 "-TEXT"}; 85 86 org.apache.xalan.xslt.Process.main(args); 87 System.out.println("Generated " + suffix + ": " + destinationFilepath); 88 89 destinationPath = testSrcPathDir + "/" + packageName; 91 destinationPathFile = new File (destinationPath); 92 destinationPathFile.mkdirs(); 93 destinationFilepath = destinationPath + "/Test" + recordName + suffix + ".java"; 94 if (new File (destinationFilepath).exists() == false) { 95 String temp = (recordStyleDir + "/" + extendstg.toLowerCase() + "_test.xsl"); 96 args = new String []{"-in", file.getAbsolutePath(), "-xsl", 97 temp, 98 "-out", destinationFilepath, 99 "-TEXT"}; 100 org.apache.xalan.xslt.Process.main(args); 101 System.out.println("Generated test: " + destinationFilepath); 102 } else { 103 System.out.println("Skipped test generation: " + destinationFilepath); 104 } 105 } 106 } 107 } 108 } 109 | Popular Tags |