Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/satyr/tools/ResourceLoaderGenerator.java


1   /*
2    * ResourceLoaderGenerator.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.satyr.tools;
11  
12  import java.io.*;
13  
14  import com.aendvari.common.model.*;
15  import com.aendvari.common.model.osm.*;
16  
17  import com.aendvari.common.util.ResourceLoader;
18  
19  /**
20   * <p>Generates {@link ResourceLoader} classes for XML files.</p>
21   *
22   * @author  Trevor Milne
23   *
24   */
25  
26  public class ResourceLoaderGenerator
27  {
28    public static void main(String args[])
29    {
30      // check parameters
31      if ((args.length != 2) && (args.length != 1))
32      {
33        System.err.println("usage: ComponentGenerator search [source]");
34        System.err.println();
35        System.exit(1);
36      }
37  
38      // read root file directory
39      File rootFile = new File(args[0]);
40  
41      if (!rootFile.exists() || !rootFile.isDirectory())
42      {
43        System.err.println("Could not access '" + args[0] + "'.");
44        System.exit(1);
45      }
46  
47      // use file directory is source is not specified
48      String sourceArgument = null;
49  
50      if (args.length == 1)
51      {
52        sourceArgument = args[0];
53      }
54      else
55      {
56        sourceArgument = args[1];
57      }
58  
59      // read source directory
60      File rootSource = new File(sourceArgument);
61  
62      if (!rootSource.exists() || !rootSource.isDirectory())
63      {
64        System.err.println("Could not access '" + sourceArgument + "'.");
65        System.exit(1);
66      }
67  
68      // get full paths
69      String rootPath = null;
70      String sourcePath = null;
71  
72      try
73      {
74        rootPath = rootFile.getCanonicalPath();
75        sourcePath = rootSource.getCanonicalPath();
76      }
77      catch (IOException exception)
78      {
79        System.err.println("Could not determine base paths.");
80        System.exit(1);
81      }
82  
83      // determine the relative source directory
84      if (!rootPath.startsWith(sourcePath))
85      {
86        System.err.println("'search' must be same as or sub-directory of 'source'");
87        System.exit(1);
88      }
89  
90      String sourceDirectory = rootPath.substring(sourcePath.length());
91  
92      // remove separator
93      if (sourceDirectory.startsWith(File.separator))
94      {
95        sourceDirectory = sourceDirectory.substring(1);
96      }
97  
98      // change to package representation
99      String rootPackage = sourceDirectory.replace(File.separatorChar, '.');
100 
101     // generate resource classes
102     scanDirectory(rootFile, rootPackage);
103   }
104 
105   /**
106    * Creates a valid package path from a base and relative name.
107    *
108    * @param    base              The base package.
109    * @param    relative            The relative package name.
110    *
111    */
112 
113   protected static String createPackageName(String base, String relative)
114   {
115     // don't add separator if base is empty
116     if (base.length() == 0)
117     {
118       return relative;
119     }
120 
121     return (base + "." + relative);
122   }
123 
124   /**
125    * Scans the specified directory for XML files to wrap.
126    *
127    * @param    directory            The directory to scan.
128    * @param    packagePath            The name of the package to place the java class.
129    *
130    */
131 
132   protected static void scanDirectory(File directory, String packagePath)
133   {
134     File directoryFiles[] = directory.listFiles();
135 
136     int fileIndex;
137 
138     for (fileIndex = 0; fileIndex < directoryFiles.length; fileIndex++)
139     {
140       File currentFile = directoryFiles[fileIndex];
141 
142       // create relative locations
143       String currentPackagePath = createPackageName(packagePath, currentFile.getName());
144 
145       // recurse into directories
146       if (currentFile.isDirectory())
147       {
148         scanDirectory(currentFile, currentPackagePath);
149         continue;
150       }
151 
152       // ignore files not ending with ".xml"
153       if (!currentFile.getName().endsWith(".xml"))
154       {
155         continue;
156       }
157 
158       // determine type of file
159       try
160       {
161         // read and parse descriptors
162         FileInputStream inputStream = new FileInputStream(currentFile.getAbsolutePath());
163 
164         // parse stream
165         ModelTree modelTree = new OsmModelTree();
166         modelTree.loadFromStream(inputStream);
167 
168         // check for valid content
169         if (modelTree.getRootNode().getFirstChild() == null)
170         {
171           throw new ModelParserException(null);
172         }
173 
174         // generate output
175         generateResourceLoader(directory.getCanonicalPath(), currentFile.getName(), packagePath);
176       }
177       catch (IOException ioException)
178       {
179         System.err.println("Could not open '" + currentFile + "'.");
180       }
181       catch (ModelParserException parserException)
182       {
183         System.err.println("'" + currentFile + "' does not appear to be a XML/OSM compatible file.");
184       }
185     }
186   }
187 
188   /**
189    * Generates a java class file to load the specified XML file as a {@link ResourceLoader}.
190    *
191    * @param    directoryPath          The directory containing the XML file to wrap.
192    * @param    packagePath            The name of the package to place the java class.
193    *
194    * @exception  IOException            A I/O error occured.
195    *
196    */
197 
198   protected static void generateResourceLoader(String directoryPath, String fileName, String packagePath)
199     throws IOException
200   {
201     // extract name of xml file, -4 for ".xml"
202     String baseFileName = fileName.substring(0, fileName.length() - 4);
203 
204     // create name for resource class
205     String resourceFileName = baseFileName + ".java";
206 
207     // create output object
208     File outputFile = new File(directoryPath, resourceFileName);
209 
210     System.out.println("Generating " + createPackageName(packagePath, baseFileName) + "...");
211 
212     // check for existing output
213     if (outputFile.exists())
214     {
215       System.err.println("Class already exists, skipping.");
216       return;
217     }
218 
219     // create output file
220     outputFile.createNewFile();
221 
222     // create output stream
223     FileOutputStream outputStream = new FileOutputStream(outputFile);
224     PrintWriter outputWriter = new PrintWriter(outputStream);
225 
226     // file header
227     outputWriter.println("/*");
228     outputWriter.print(" * ");
229     outputWriter.println(resourceFileName);
230     outputWriter.println(" *");
231     outputWriter.println(" */");
232     outputWriter.println();
233 
234     // package
235     outputWriter.print("package ");
236     outputWriter.print(packagePath);
237     outputWriter.println(";");
238     outputWriter.println();
239 
240     // imports
241     outputWriter.println("import com.aendvari.common.util.ResourceLoader;");
242     outputWriter.println();
243 
244     // class comment
245     outputWriter.println("/**");
246     outputWriter.print(" * <p>A {@link ResourceLoader} for ");
247     outputWriter.print(fileName);
248     outputWriter.println(".</p>");
249     outputWriter.println(" *");
250     outputWriter.print(" * @author ");
251     outputWriter.println(ResourceLoaderGenerator.class);
252     outputWriter.println(" *");
253     outputWriter.println(" */");
254     outputWriter.println();
255 
256     // class definition
257     outputWriter.print("public class ");
258     outputWriter.print(baseFileName);
259     outputWriter.println(" extends ResourceLoader");
260     outputWriter.println("{");
261 
262     // getResourceName() method
263     outputWriter.println("    protected String getResourceName()");
264     outputWriter.println("    {");
265     outputWriter.print("        return \"");
266     outputWriter.print(fileName);
267     outputWriter.println("\";");
268     outputWriter.println("    }");
269 
270     // end class
271     outputWriter.println("}");
272 
273     // end file
274     outputWriter.println();
275 
276     outputWriter.flush();
277     outputStream.close();
278   }
279 }
280