001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.rebind;
017:
018: import com.google.gwt.core.ext.GeneratorContext;
019:
020: import java.io.PrintWriter;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.Map;
024: import java.util.Set;
025:
026: /**
027: * Factory clas to create <code>ClassSourceFileComposer</code> instances.
028: *
029: */
030: public class ClassSourceFileComposerFactory {
031: /**
032: * Represents a java source file category. Right now support interface and
033: * class, later should support abstract class, static class, etc.
034: */
035: public static class JavaSourceCategory extends Enum {
036: /**
037: * This type is a class.
038: */
039: public static final JavaSourceCategory CLASS;
040:
041: /**
042: * This type is a interface.
043: */
044: public static final JavaSourceCategory INTERFACE;
045: static Map<String, Enum> pool = new HashMap<String, Enum>();
046:
047: static {
048: CLASS = new JavaSourceCategory("class");
049: INTERFACE = new JavaSourceCategory("interface");
050: }
051:
052: public static JavaSourceCategory require(String key) {
053: return (JavaSourceCategory) Enum.require(key, pool);
054: }
055:
056: protected JavaSourceCategory(String key) {
057: super (key, pool);
058: }
059: }
060:
061: private JavaSourceCategory classCategory = JavaSourceCategory.CLASS;
062:
063: private String classComment;
064:
065: private String className;
066:
067: private Set<String> imports = new HashSet<String>();
068:
069: private Set<String> interfaceNames = new HashSet<String>();
070:
071: private String packageName;
072:
073: private String super ClassName;
074:
075: public ClassSourceFileComposerFactory(String packageName,
076: String className) {
077: this .packageName = packageName;
078: this .className = className;
079: }
080:
081: public void addImplementedInterface(String intfName) {
082: interfaceNames.add(intfName);
083: }
084:
085: public void addImport(String typeName) {
086: imports.add(typeName);
087: }
088:
089: /**
090: * Creates an implementation of {@link SourceWriter} that can be used to write
091: * the innards of a class. Note that the subsequent changes to this factory do
092: * not affect the returned instance.
093: *
094: * @throws RuntimeException If the settings on this factory are inconsistent
095: * or invalid
096: */
097: public SourceWriter createSourceWriter(GeneratorContext ctx,
098: PrintWriter printWriter) {
099: return new ClassSourceFileComposer(ctx, printWriter,
100: getCreatedPackage(), getCreatedClassShortName(),
101: getSuperclassName(), getInterfaceNames(), getImports(),
102: classCategory, classComment);
103: }
104:
105: /**
106: * Creates an implementation of {@link SourceWriter} that can be used to write
107: * the innards of a class. Note that the subsequent changes to this factory do
108: * not affect the returned instance.
109: *
110: * @param printWriter underlying writer
111: * @return the source writer
112: * @throws RuntimeException If the settings on this factory are inconsistent
113: * or invalid
114: */
115: public SourceWriter createSourceWriter(PrintWriter printWriter) {
116: return new ClassSourceFileComposer(null, printWriter,
117: getCreatedPackage(), getCreatedClassShortName(),
118: getSuperclassName(), getInterfaceNames(), getImports(),
119: classCategory, classComment);
120: }
121:
122: public String getCreatedClassName() {
123: return getCreatedPackage() + "." + getCreatedClassShortName();
124: }
125:
126: public String getCreatedClassShortName() {
127: return className;
128: }
129:
130: public String getCreatedPackage() {
131: return packageName;
132: }
133:
134: public String[] getInterfaceNames() {
135: return interfaceNames
136: .toArray(new String[interfaceNames.size()]);
137: }
138:
139: public String getSuperclassName() {
140: return super ClassName;
141: }
142:
143: /**
144: * This class is an interface.
145: */
146: public void makeInterface() {
147: classCategory = JavaSourceCategory.INTERFACE;
148: }
149:
150: /**
151: * Sets the java doc comment for <code>this</code>.
152: *
153: * @param comment java doc comment.
154: */
155: public void setJavaDocCommentForClass(String comment) {
156: classComment = comment;
157: }
158:
159: public void setSuperclass(String super className) {
160: super ClassName = super className;
161: }
162:
163: private String[] getImports() {
164: return imports.toArray(new String[imports.size()]);
165: }
166: }
|