001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.java.gen;
031:
032: import com.caucho.java.JavaWriter;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037: import java.util.logging.*;
038:
039: /**
040: * Basic class generation.
041: */
042: public class GenClass extends BaseClass {
043: private static final Logger log = Logger.getLogger(GenClass.class
044: .getName());
045: private static L10N L = new L10N(GenClass.class);
046:
047: private String _packageName;
048: private String _fullClassName;
049:
050: private ArrayList<String> _importList = new ArrayList<String>();
051:
052: /**
053: * Creates the base class
054: */
055: public GenClass(String fullClassName) {
056: _fullClassName = fullClassName;
057:
058: int p = fullClassName.lastIndexOf('.');
059:
060: if (p > 0) {
061: _packageName = fullClassName.substring(0, p);
062: setClassName(fullClassName.substring(p + 1));
063: } else {
064: log.warning(L.l("Class '{0}' must belong to a package.",
065: fullClassName));
066:
067: _packageName = "_dummy";
068: setClassName(fullClassName);
069: _fullClassName = "_dummy." + fullClassName;
070: }
071: }
072:
073: /**
074: * Returns the full class name.
075: */
076: public String getFullClassName() {
077: return _fullClassName;
078: }
079:
080: /**
081: * Returns the package name
082: */
083: public String getPackageName() {
084: return _packageName;
085: }
086:
087: /**
088: * Adds an import package.
089: */
090: public void addImport(String importName) {
091: if (!_importList.contains(importName))
092: _importList.add(importName);
093: }
094:
095: /**
096: * Generates the class.
097: */
098: public void generate(JavaWriter out) throws IOException {
099: 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 {
124: out.println("/*");
125: out.println(" * Generated by "
126: + com.caucho.Version.FULL_VERSION);
127: out.println(" */");
128: }
129: }
|