KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > dev > RecordGenerator


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.dev;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import java.io.File JavaDoc;
26
27 /**
28  * Description of the Class
29  *
30  *@author andy
31  *@since May 10, 2002
32  */

33 public class RecordGenerator {
34     /**
35      * The main program for the RecordGenerator class
36      *
37      *@param args The command line arguments
38      *@exception Exception Description of the Exception
39      */

40     public static void main(String JavaDoc[] args)
41              throws Exception JavaDoc {
42         // Force load so that we don't start generating records and realise this hasn't compiled yet.
43
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 JavaDoc defintionsDir, String JavaDoc recordStyleDir, String JavaDoc destSrcPathDir, String JavaDoc testSrcPathDir)
55              throws Exception JavaDoc {
56         File JavaDoc definitionsFile = new File JavaDoc(defintionsDir);
57
58         for (int i = 0; i < definitionsFile.listFiles().length; i++) {
59             File JavaDoc file = definitionsFile.listFiles()[i];
60             if (file.isFile() &&
61                     (file.getName().endsWith("_record.xml") ||
62                     file.getName().endsWith("_type.xml")
63                     )
64                     ) {
65                 // Get record name and package
66
DocumentBuilderFactory JavaDoc factory =
67                         DocumentBuilderFactory.newInstance();
68                 DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
69                 Document JavaDoc document = builder.parse(file);
70                 Element JavaDoc record = document.getDocumentElement();
71                 String JavaDoc extendstg = record.getElementsByTagName("extends").item(0).getFirstChild().getNodeValue();
72                 String JavaDoc suffix = record.getElementsByTagName("suffix").item(0).getFirstChild().getNodeValue();
73                 String JavaDoc recordName = record.getAttributes().getNamedItem("name").getNodeValue();
74                 String JavaDoc packageName = record.getAttributes().getNamedItem("package").getNodeValue();
75                 packageName = packageName.replace('.', '/');
76
77                 // Generate record
78
String JavaDoc destinationPath = destSrcPathDir + "/" + packageName;
79                 File JavaDoc destinationPathFile = new File JavaDoc(destinationPath);
80                 destinationPathFile.mkdirs();
81                 String JavaDoc destinationFilepath = destinationPath + "/" + recordName + suffix + ".java";
82                 String JavaDoc args[] = new String JavaDoc[]{"-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                 // Generate test (if not already generated)
90
destinationPath = testSrcPathDir + "/" + packageName;
91                 destinationPathFile = new File JavaDoc(destinationPath);
92                 destinationPathFile.mkdirs();
93                 destinationFilepath = destinationPath + "/Test" + recordName + suffix + ".java";
94                 if (new File JavaDoc(destinationFilepath).exists() == false) {
95                     String JavaDoc temp = (recordStyleDir + "/" + extendstg.toLowerCase() + "_test.xsl");
96                     args = new String JavaDoc[]{"-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