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.api.impl.s2j;
037:
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.HashMap;
041: import java.util.List;
042: import java.util.Map;
043:
044: import javax.xml.namespace.QName;
045:
046: import com.sun.codemodel.JCodeModel;
047: import com.sun.codemodel.JClass;
048: import com.sun.tools.xjc.Plugin;
049: import com.sun.tools.xjc.api.ErrorListener;
050: import com.sun.tools.xjc.api.JAXBModel;
051: import com.sun.tools.xjc.api.Mapping;
052: import com.sun.tools.xjc.api.S2JJAXBModel;
053: import com.sun.tools.xjc.api.TypeAndAnnotation;
054: import com.sun.tools.xjc.model.CClassInfo;
055: import com.sun.tools.xjc.model.CElementInfo;
056: import com.sun.tools.xjc.model.Model;
057: import com.sun.tools.xjc.model.TypeUse;
058: import com.sun.tools.xjc.outline.Outline;
059: import com.sun.tools.xjc.outline.PackageOutline;
060:
061: /**
062: * {@link JAXBModel} implementation.
063: *
064: * @author
065: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
066: */
067: final class JAXBModelImpl implements S2JJAXBModel {
068: /*package*/final Outline outline;
069:
070: /**
071: * All the known classes.
072: */
073: private final Model model;
074:
075: private final Map<QName, Mapping> byXmlName = new HashMap<QName, Mapping>();
076:
077: JAXBModelImpl(Outline outline) {
078: this .model = outline.getModel();
079: this .outline = outline;
080:
081: for (CClassInfo ci : model.beans().values()) {
082: if (!ci.isElement())
083: continue;
084: byXmlName.put(ci.getElementName(), new BeanMappingImpl(
085: this , ci));
086: }
087: for (CElementInfo ei : model.getElementMappings(null).values()) {
088: byXmlName.put(ei.getElementName(), new ElementMappingImpl(
089: this , ei));
090: }
091: }
092:
093: public JCodeModel generateCode(Plugin[] extensions,
094: ErrorListener errorListener) {
095: // we no longer do any code generation
096: return outline.getCodeModel();
097: }
098:
099: public List<JClass> getAllObjectFactories() {
100: List<JClass> r = new ArrayList<JClass>();
101: for (PackageOutline pkg : outline.getAllPackageContexts()) {
102: r.add(pkg.objectFactory());
103: }
104: return r;
105: }
106:
107: public final Mapping get(QName elementName) {
108: return byXmlName.get(elementName);
109: }
110:
111: public final Collection<? extends Mapping> getMappings() {
112: return byXmlName.values();
113: }
114:
115: public TypeAndAnnotation getJavaType(QName xmlTypeName) {
116: // TODO: primitive type handling?
117: TypeUse use = model.typeUses().get(xmlTypeName);
118: if (use == null)
119: return null;
120:
121: return new TypeAndAnnotationImpl(outline, use);
122: }
123:
124: public final List<String> getClassList() {
125: List<String> classList = new ArrayList<String>();
126:
127: // list up root classes
128: for (PackageOutline p : outline.getAllPackageContexts())
129: classList.add(p.objectFactory().fullName());
130: return classList;
131: }
132: }
|