KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > gen > GenClass


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.java.gen;
31
32 import com.caucho.java.JavaWriter;
33 import com.caucho.util.L10N;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * Basic class generation.
40  */

41 public class GenClass extends BaseClass {
42   private static L10N L = new L10N(GenClass.class);
43
44   private String JavaDoc _packageName;
45   private String JavaDoc _fullClassName;
46
47   private ArrayList JavaDoc<String JavaDoc> _importList = new ArrayList JavaDoc<String JavaDoc>();
48
49   /**
50    * Creates the base class
51    */

52   public GenClass(String JavaDoc fullClassName)
53   {
54     _fullClassName = fullClassName;
55
56     int p = fullClassName.lastIndexOf('.');
57
58     if (p > 0) {
59       _packageName = fullClassName.substring(0, p);
60       setClassName(fullClassName.substring(p + 1));
61     }
62     else {
63       throw new IllegalArgumentException JavaDoc(L.l("Class '{0}' must belong to a package.",
64                          fullClassName));
65     }
66   }
67
68   /**
69    * Returns the full class name.
70    */

71   public String JavaDoc getFullClassName()
72   {
73     return _fullClassName;
74   }
75
76   /**
77    * Returns the package name
78    */

79   public String JavaDoc getPackageName()
80   {
81     return _packageName;
82   }
83
84   /**
85    * Adds an import package.
86    */

87   public void addImport(String JavaDoc importName)
88   {
89     if (! _importList.contains(importName))
90       _importList.add(importName);
91   }
92
93   /**
94    * Generates the class.
95    */

96   public void generate(JavaWriter out)
97     throws IOException JavaDoc
98   {
99     generateTopComment(out);
100
101     if (_packageName != null) {
102       out.println();
103       out.println("package " + _packageName + ";");
104     }
105
106     if (_importList.size() > 0) {
107       out.println();
108
109       for (int i = 0; i < _importList.size(); i++) {
110     out.println("import " + _importList.get(i) + ";");
111       }
112     }
113
114     out.println();
115
116     super.generate(out);
117   }
118
119   /**
120    * Generates the top comment.
121    */

122   protected void generateTopComment(JavaWriter out)
123     throws IOException JavaDoc
124   {
125     out.println("/*");
126     out.println(" * Generated by " + com.caucho.Version.FULL_VERSION);
127     out.println(" */");
128   }
129 }
130
Popular Tags