View Javadoc

1   /*
2    * Copyright 2004-2005 Thomas Cotting
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * $Id: SourceGeneratorApp.java,v 1.2 2005/08/06 14:11:18 tcotting Exp $
18   */
19  package ch.oscg.jreleaseinfo;
20  
21  import java.io.BufferedReader;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  
25  /***
26   * The SourceGenerator is a simple implementation of the
27   * SourceGeneratorIF interface. In this class the code to be
28   * generated is hard coded.<br>
29   * This implementation adds code which shows the
30   * build information in a frame as default main-class of
31   * the jar file.<br>
32   * This is usefull for libraries, which do not have a main-class
33   * definition in the manifest.
34   *
35   * @author Thomas Cotting, Tangarena Engineering AG, Luzern
36   * @version $Revision: 1.2 $ ($Date: 2005/08/06 14:11:18 $ / $Author: tcotting $)
37   */
38  public class SourceGeneratorApp extends SourceGenerator {
39  
40     /*** Include filename for text viewer. */
41     private String filenameTextSwingViewer = "ch/oscg/jreleaseinfo/anttask/JReleaseInfoViewer.txt";
42  
43     /*** Include filename for main method. */
44     private String filenameMainMethod = "ch/oscg/jreleaseinfo/anttask/MainMethod.txt";
45  
46     /***
47      * Default constructor.
48      */
49     public SourceGeneratorApp() {
50     }
51  
52     /***
53      * Utility method to write the class java code to a String.
54      * This methods overwrites the method from SourceGenerator,
55      * adding code through createViewerCall().
56      *
57      * @return the created java class in a string
58      */
59     public String createCode() {
60        StringBuffer buf = new StringBuffer();
61  
62        createClassInfoHeader(buf);
63  
64        createViewerClass(buf, filenameTextSwingViewer);
65  
66        
67        createClassHeader(buf);
68  
69        createMethods(buf);
70  
71        createMainMethod(buf, filenameMainMethod);
72  
73        createClassFooter(buf);
74  
75        writeln(buf);
76  
77        return buf.toString();
78     }
79  
80     /***
81      * Utility method to write a the code for the viewer class.
82      *
83      * @param buf StringBuffer to write into
84      */
85     protected void createViewerClass(StringBuffer buf, String viewerType) {
86        ClassLoader cl = SourceGeneratorApp.class.getClassLoader();
87        InputStream fis = cl.getResourceAsStream(viewerType);
88        InputStreamReader isr = new InputStreamReader(fis);
89        try {
90           BufferedReader br = new BufferedReader(isr);
91  
92           String line = null;
93           while ((line = br.readLine()) != null) {
94              buf.append(line + "\n");
95           }
96  
97           isr.close();
98        } catch (Exception e) {
99           buf.append("// File (" + viewerType + ") could not be read");
100       }
101    }
102 
103    /***
104     * Utility method to write a the code for the main method.
105     *
106     * @param buf StringBuffer to write into
107     */
108    protected void createMainMethod(StringBuffer buf, String filename) {
109       ClassLoader cl = SourceGeneratorApp.class.getClassLoader();
110       InputStream fis = cl.getResourceAsStream(filename);
111       InputStreamReader isr = new InputStreamReader(fis);
112       try {
113          BufferedReader br = new BufferedReader(isr);
114 
115          String line = null;
116          while ((line = br.readLine()) != null) {
117             buf.append(line + "\n");
118          }
119 
120          isr.close();
121       } catch (Exception e) {
122          buf.append("// File (" + filename + ") could not be read");
123       }
124    }
125 
126 }