001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.xjc.writer;
037:
038: import java.io.IOException;
039: import java.io.Writer;
040: import java.util.Collection;
041: import java.util.Comparator;
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.Map;
045: import java.util.Set;
046: import java.util.TreeSet;
047:
048: import com.sun.codemodel.JClass;
049: import com.sun.codemodel.JClassContainer;
050: import com.sun.codemodel.JDefinedClass;
051: import com.sun.codemodel.JPackage;
052: import com.sun.codemodel.JType;
053: import com.sun.tools.xjc.outline.ClassOutline;
054: import com.sun.tools.xjc.outline.FieldOutline;
055: import com.sun.tools.xjc.outline.Outline;
056:
057: /**
058: * Dumps an annotated grammar in a simple format that
059: * makes signature check easy.
060: *
061: * @author
062: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
063: */
064: public class SignatureWriter {
065:
066: public static void write(Outline model, Writer out)
067: throws IOException {
068:
069: new SignatureWriter(model, out).dump();
070: }
071:
072: private SignatureWriter(Outline model, Writer out) {
073: this .out = out;
074: this .classes = model.getClasses();
075:
076: for (ClassOutline ci : classes)
077: classSet.put(ci.ref, ci);
078: }
079:
080: /** All the ClassItems in this grammar. */
081: private final Collection<? extends ClassOutline> classes;
082: /** Map from content interfaces to ClassItem. */
083: private final Map<JDefinedClass, ClassOutline> classSet = new HashMap<JDefinedClass, ClassOutline>();
084:
085: private final Writer out;
086: private int indent = 0;
087:
088: private void printIndent() throws IOException {
089: for (int i = 0; i < indent; i++)
090: out.write(" ");
091: }
092:
093: private void println(String s) throws IOException {
094: printIndent();
095: out.write(s);
096: out.write('\n');
097: }
098:
099: private void dump() throws IOException {
100:
101: // collect packages used in the class.
102: Set<JPackage> packages = new TreeSet<JPackage>(
103: new Comparator<JPackage>() {
104: public int compare(JPackage lhs, JPackage rhs) {
105: return lhs.name().compareTo(rhs.name());
106: }
107: });
108: for (ClassOutline ci : classes)
109: packages.add(ci._package()._package());
110:
111: for (JPackage pkg : packages)
112: dump(pkg);
113:
114: out.flush();
115: }
116:
117: private void dump(JPackage pkg) throws IOException {
118: println("package " + pkg.name() + " {");
119: indent++;
120: dumpChildren(pkg);
121: indent--;
122: println("}");
123: }
124:
125: private void dumpChildren(JClassContainer cont) throws IOException {
126: Iterator itr = cont.classes();
127: while (itr.hasNext()) {
128: JDefinedClass cls = (JDefinedClass) itr.next();
129: ClassOutline ci = classSet.get(cls);
130: if (ci != null)
131: dump(ci);
132: }
133: }
134:
135: private void dump(ClassOutline ci) throws IOException {
136: JDefinedClass cls = ci.implClass;
137:
138: StringBuilder buf = new StringBuilder();
139: buf.append("interface ");
140: buf.append(cls.name());
141:
142: boolean first = true;
143: Iterator itr = cls._implements ();
144: while (itr.hasNext()) {
145: if (first) {
146: buf.append(" extends ");
147: first = false;
148: } else {
149: buf.append(", ");
150: }
151: buf.append(printName((JClass) itr.next()));
152: }
153: buf.append(" {");
154: println(buf.toString());
155: indent++;
156:
157: // dump the field
158: for (FieldOutline fo : ci.getDeclaredFields()) {
159: String type = printName(fo.getRawType());
160: println(type + ' ' + fo.getPropertyInfo().getName(true)
161: + ';');
162: }
163:
164: dumpChildren(cls);
165:
166: indent--;
167: println("}");
168: }
169:
170: /** Get the display name of a type. */
171: private String printName(JType t) {
172: String name = t.fullName();
173: if (name.startsWith("java.lang."))
174: name = name.substring(10); // chop off the package name
175: return name;
176: }
177: }
|