1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.internal.xjc.api.impl.s2j;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33
34import javax.xml.namespace.QName;
35
36import com.sun.codemodel.internal.JCodeModel;
37import com.sun.codemodel.internal.JClass;
38import com.sun.tools.internal.xjc.Plugin;
39import com.sun.tools.internal.xjc.api.ErrorListener;
40import com.sun.tools.internal.xjc.api.JAXBModel;
41import com.sun.tools.internal.xjc.api.Mapping;
42import com.sun.tools.internal.xjc.api.S2JJAXBModel;
43import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
44import com.sun.tools.internal.xjc.model.CClassInfo;
45import com.sun.tools.internal.xjc.model.CElementInfo;
46import com.sun.tools.internal.xjc.model.Model;
47import com.sun.tools.internal.xjc.model.TypeUse;
48import com.sun.tools.internal.xjc.outline.Outline;
49import com.sun.tools.internal.xjc.outline.PackageOutline;
50
51/**
52 * {@link JAXBModel} implementation.
53 *
54 * @author
55 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
56 */
57final class JAXBModelImpl implements S2JJAXBModel {
58    /*package*/ final Outline outline;
59
60    /**
61     * All the known classes.
62     */
63    private final Model model;
64
65    private final Map<QName,Mapping> byXmlName = new HashMap<QName,Mapping>();
66
67    JAXBModelImpl(Outline outline) {
68        this.model = outline.getModel();
69        this.outline = outline;
70
71        for (CClassInfo ci : model.beans().values()) {
72            if(!ci.isElement())
73                continue;
74            byXmlName.put(ci.getElementName(),new BeanMappingImpl(this,ci));
75        }
76        for (CElementInfo ei : model.getElementMappings(null).values()) {
77            byXmlName.put(ei.getElementName(),new ElementMappingImpl(this,ei));
78        }
79    }
80
81    public JCodeModel generateCode(Plugin[] extensions,ErrorListener errorListener) {
82        // we no longer do any code generation
83        return outline.getCodeModel();
84    }
85
86    public List<JClass> getAllObjectFactories() {
87        List<JClass> r = new ArrayList<JClass>();
88        for (PackageOutline pkg : outline.getAllPackageContexts()) {
89            r.add(pkg.objectFactory());
90        }
91        return r;
92    }
93
94    public final Mapping get(QName elementName) {
95        return byXmlName.get(elementName);
96    }
97
98    public final Collection<? extends Mapping> getMappings() {
99        return byXmlName.values();
100    }
101
102    public TypeAndAnnotation getJavaType(QName xmlTypeName) {
103        // TODO: primitive type handling?
104        TypeUse use = model.typeUses().get(xmlTypeName);
105        if(use==null)   return null;
106
107        return new TypeAndAnnotationImpl(outline,use);
108    }
109
110    public final List<String> getClassList() {
111        List<String> classList = new ArrayList<String>();
112
113        // list up root classes
114        for( PackageOutline p : outline.getAllPackageContexts() )
115            classList.add( p.objectFactory().fullName() );
116        return classList;
117    }
118}
119