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:
037: package com.sun.tools.xjc.api.impl.j2s;
038:
039: import java.io.IOException;
040: import java.util.ArrayList;
041: import java.util.Collection;
042: import java.util.HashMap;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Map;
046:
047: import javax.xml.bind.SchemaOutputResolver;
048: import javax.xml.bind.annotation.XmlList;
049: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
050: import javax.xml.namespace.QName;
051: import javax.xml.transform.Result;
052:
053: import com.sun.mirror.declaration.FieldDeclaration;
054: import com.sun.mirror.declaration.MethodDeclaration;
055: import com.sun.mirror.declaration.TypeDeclaration;
056: import com.sun.mirror.type.TypeMirror;
057: import com.sun.mirror.type.PrimitiveType;
058: import com.sun.tools.xjc.api.ErrorListener;
059: import com.sun.tools.xjc.api.J2SJAXBModel;
060: import com.sun.tools.xjc.api.Reference;
061: import com.sun.xml.bind.v2.model.annotation.AnnotationReader;
062: import com.sun.xml.bind.v2.model.core.ArrayInfo;
063: import com.sun.xml.bind.v2.model.core.ClassInfo;
064: import com.sun.xml.bind.v2.model.core.Element;
065: import com.sun.xml.bind.v2.model.core.ElementInfo;
066: import com.sun.xml.bind.v2.model.core.EnumLeafInfo;
067: import com.sun.xml.bind.v2.model.core.NonElement;
068: import com.sun.xml.bind.v2.model.core.Ref;
069: import com.sun.xml.bind.v2.model.core.TypeInfoSet;
070: import com.sun.xml.bind.v2.model.nav.Navigator;
071: import com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator;
072: import com.sun.xml.txw2.output.ResultFactory;
073:
074: /**
075: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
076: */
077: final class JAXBModelImpl implements J2SJAXBModel {
078:
079: private final Map<QName, Reference> additionalElementDecls;
080:
081: private final List<String> classList = new ArrayList<String>();
082:
083: private final TypeInfoSet<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> types;
084:
085: private final AnnotationReader<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> reader;
086:
087: /**
088: * Lazily created schema generator.
089: */
090: private XmlSchemaGenerator<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> xsdgen;
091:
092: /**
093: * Look up table from an externally visible {@link Reference} object
094: * to our internal format.
095: */
096: private final Map<Reference, NonElement<TypeMirror, TypeDeclaration>> refMap = new HashMap<Reference, NonElement<TypeMirror, TypeDeclaration>>();
097:
098: public JAXBModelImpl(
099: TypeInfoSet<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> types,
100: AnnotationReader<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> reader,
101: Collection<Reference> rootClasses,
102: Map<QName, Reference> additionalElementDecls) {
103: this .types = types;
104: this .reader = reader;
105: this .additionalElementDecls = additionalElementDecls;
106:
107: Navigator<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> navigator = types
108: .getNavigator();
109:
110: for (ClassInfo<TypeMirror, TypeDeclaration> i : types.beans()
111: .values()) {
112: classList.add(i.getName());
113: }
114:
115: for (ArrayInfo<TypeMirror, TypeDeclaration> a : types.arrays()
116: .values()) {
117: String javaName = navigator.getTypeName(a.getType());
118: classList.add(javaName);
119: }
120:
121: for (EnumLeafInfo<TypeMirror, TypeDeclaration> l : types
122: .enums().values()) {
123: QName tn = l.getTypeName();
124: if (tn != null) {
125: String javaName = navigator.getTypeName(l.getType());
126: classList.add(javaName);
127: }
128: }
129:
130: for (Reference ref : rootClasses)
131: refMap.put(ref, getXmlType(ref));
132:
133: // check for collision between "additional" ones and the ones given to JAXB
134: // and eliminate duplication
135: Iterator<Map.Entry<QName, Reference>> itr = additionalElementDecls
136: .entrySet().iterator();
137: while (itr.hasNext()) {
138: Map.Entry<QName, Reference> entry = itr.next();
139: if (entry.getValue() == null)
140: continue;
141:
142: NonElement<TypeMirror, TypeDeclaration> xt = getXmlType(entry
143: .getValue());
144:
145: assert xt != null;
146: refMap.put(entry.getValue(), xt);
147: if (xt instanceof ClassInfo) {
148: ClassInfo<TypeMirror, TypeDeclaration> xct = (ClassInfo<TypeMirror, TypeDeclaration>) xt;
149: Element<TypeMirror, TypeDeclaration> elem = xct
150: .asElement();
151: if (elem != null
152: && elem.getElementName().equals(entry.getKey())) {
153: itr.remove();
154: continue;
155: }
156: }
157: ElementInfo<TypeMirror, TypeDeclaration> ei = types
158: .getElementInfo(null, entry.getKey());
159: if (ei != null && ei.getContentType() == xt)
160: itr.remove();
161: }
162: }
163:
164: public List<String> getClassList() {
165: return classList;
166: }
167:
168: public QName getXmlTypeName(Reference javaType) {
169: NonElement<TypeMirror, TypeDeclaration> ti = refMap
170: .get(javaType);
171:
172: if (ti != null)
173: return ti.getTypeName();
174:
175: return null;
176: }
177:
178: private NonElement<TypeMirror, TypeDeclaration> getXmlType(
179: Reference r) {
180: if (r == null)
181: throw new IllegalArgumentException();
182:
183: XmlJavaTypeAdapter xjta = r.annotations
184: .getAnnotation(XmlJavaTypeAdapter.class);
185: XmlList xl = r.annotations.getAnnotation(XmlList.class);
186:
187: Ref<TypeMirror, TypeDeclaration> ref = new Ref<TypeMirror, TypeDeclaration>(
188: reader, types.getNavigator(), r.type, xjta, xl);
189:
190: return types.getTypeInfo(ref);
191: }
192:
193: public void generateSchema(SchemaOutputResolver outputResolver,
194: ErrorListener errorListener) throws IOException {
195: getSchemaGenerator().write(outputResolver, errorListener);
196: }
197:
198: public void generateEpisodeFile(Result output) {
199: getSchemaGenerator().writeEpisodeFile(
200: ResultFactory.createSerializer(output));
201: }
202:
203: private synchronized XmlSchemaGenerator<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> getSchemaGenerator() {
204: if (xsdgen == null) {
205: xsdgen = new XmlSchemaGenerator<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration>(
206: types.getNavigator(), types);
207:
208: for (Map.Entry<QName, Reference> e : additionalElementDecls
209: .entrySet()) {
210: Reference value = e.getValue();
211: if (value != null) {
212: NonElement<TypeMirror, TypeDeclaration> typeInfo = refMap
213: .get(value);
214: if (typeInfo == null)
215: throw new IllegalArgumentException(
216: e.getValue()
217: + " was not specified to JavaCompiler.bind");
218: xsdgen.add(e.getKey(),
219: !(value.type instanceof PrimitiveType),
220: typeInfo);
221: } else {
222: xsdgen.add(e.getKey(), false, null);
223: }
224: }
225: }
226: return xsdgen;
227: }
228: }
|