1    /*
2     *  ====================================================================
3     *  The Apache Software License, Version 1.1
4     *
5     *  Copyright (c) 2002 The Apache Software Foundation.  All rights
6     *  reserved.
7     *
8     *  Redistribution and use in source and binary forms, with or without
9     *  modification, are permitted provided that the following conditions
10    *  are met:
11    *
12    *  1. Redistributions of source code must retain the above copyright
13    *  notice, this list of conditions and the following disclaimer.
14    *
15    *  2. Redistributions in binary form must reproduce the above copyright
16    *  notice, this list of conditions and the following disclaimer in
17    *  the documentation and/or other materials provided with the
18    *  distribution.
19    *
20    *  3. The end-user documentation included with the redistribution,
21    *  if any, must include the following acknowledgment:
22    *  "This product includes software developed by the
23    *  Apache Software Foundation (http://www.apache.org/)."
24    *  Alternately, this acknowledgment may appear in the software itself,
25    *  if and wherever such third-party acknowledgments normally appear.
26    *
27    *  4. The names "Apache" and "Apache Software Foundation" and
28    *  "Apache POI" must not be used to endorse or promote products
29    *  derived from this software without prior written permission. For
30    *  written permission, please contact apache@apache.org.
31    *
32    *  5. Products derived from this software may not be called "Apache",
33    *  "Apache POI", nor may "Apache" appear in their name, without
34    *  prior written permission of the Apache Software Foundation.
35    *
36    *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    *  SUCH DAMAGE.
48    *  ====================================================================
49    *
50    *  This software consists of voluntary contributions made by many
51    *  individuals on behalf of the Apache Software Foundation.  For more
52    *  information on the Apache Software Foundation, please see
53    *  <http://www.apache.org/>.
54    */
55   package org.apache.poi.dev;
56   
57   import org.w3c.dom.Document;
58   import org.w3c.dom.Element;
59   
60   import javax.xml.parsers.DocumentBuilder;
61   import javax.xml.parsers.DocumentBuilderFactory;
62   import java.io.File;
63   
64   /**
65    *  Description of the Class
66    *
67    *@author     andy
68    *@created    May 10, 2002
69    */
70   public class RecordGenerator {
71       /**
72        *  The main program for the RecordGenerator class
73        *
74        *@param  args           The command line arguments
75        *@exception  Exception  Description of the Exception
76        */
77       public static void main(String[] args)
78                throws Exception {
79           // Force load so that we don't start generating records and realise this hasn't compiled yet.
80           Class.forName("org.apache.poi.generator.FieldIterator");
81   
82           if (args.length != 4) {
83               System.out.println("Usage:");
84               System.out.println("  java org.apache.poi.hssf.util.RecordGenerator RECORD_DEFINTIONS RECORD_STYLES DEST_SRC_PATH TEST_SRC_PATH");
85           } else {
86               generateRecords(args[0], args[1], args[2], args[3]);
87           }
88       }
89   
90   
91       /**
92        *  Description of the Method
93        *
94        *@param  defintionsDir   Description of the Parameter
95        *@param  recordStyleDir  Description of the Parameter
96        *@param  destSrcPathDir  Description of the Parameter
97        *@param  testSrcPathDir  Description of the Parameter
98        *@exception  Exception   Description of the Exception
99        */
100      private static void generateRecords(String defintionsDir, String recordStyleDir, String destSrcPathDir, String testSrcPathDir)
101               throws Exception {
102          File definitionsFile = new File(defintionsDir);
103  
104          for (int i = 0; i < definitionsFile.listFiles().length; i++) {
105              File file = definitionsFile.listFiles()[i];
106              if (file.isFile() &&
107                      (file.getName().endsWith("_record.xml") ||
108                      file.getName().endsWith("_type.xml")
109                      )
110                      ) {
111                  // Get record name and package
112                  DocumentBuilderFactory factory =
113                          DocumentBuilderFactory.newInstance();
114                  DocumentBuilder builder = factory.newDocumentBuilder();
115                  Document document = builder.parse(file);
116                  Element record = document.getDocumentElement();
117                  String extendstg = record.getElementsByTagName("extends").item(0).getFirstChild().getNodeValue();
118                  String suffix = record.getElementsByTagName("suffix").item(0).getFirstChild().getNodeValue();
119                  String recordName = record.getAttributes().getNamedItem("name").getNodeValue();
120                  String packageName = record.getAttributes().getNamedItem("package").getNodeValue();
121                  packageName = packageName.replace('.', '/');
122  
123                  // Generate record
124                  String destinationPath = destSrcPathDir + "/" + packageName;
125                  File destinationPathFile = new File(destinationPath);
126                  destinationPathFile.mkdirs();
127                  String destinationFilepath = destinationPath + "/" + recordName + suffix + ".java";
128                  String args[] = new String[]{"-in", file.getAbsolutePath(), "-xsl", recordStyleDir + "/" + extendstg.toLowerCase() + ".xsl",
129                          "-out", destinationFilepath,
130                          "-TEXT"};
131  
132                  org.apache.xalan.xslt.Process.main(args);
133                  System.out.println("Generated " + suffix + ": " + destinationFilepath);
134  
135                  // Generate test (if not already generated)
136                  destinationPath = testSrcPathDir + "/" + packageName;
137                  destinationPathFile = new File(destinationPath);
138                  destinationPathFile.mkdirs();
139                  destinationFilepath = destinationPath + "/Test" + recordName + suffix + ".java";
140                  if (new File(destinationFilepath).exists() == false) {
141                      String temp = (recordStyleDir + "/" + extendstg.toLowerCase() + "_test.xsl");
142                      args = new String[]{"-in", file.getAbsolutePath(), "-xsl",
143                              temp,
144                              "-out", destinationFilepath,
145                              "-TEXT"};
146                      org.apache.xalan.xslt.Process.main(args);
147                      System.out.println("Generated test: " + destinationFilepath);
148                  } else {
149                      System.out.println("Skipped test generation: " + destinationFilepath);
150                  }
151              }
152          }
153      }
154  }
155