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.util.Collection;
040: import java.util.Collections;
041: import java.util.HashMap;
042: import java.util.Map;
043:
044: import javax.xml.bind.annotation.XmlList;
045: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
046: import javax.xml.namespace.QName;
047:
048: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
049: import com.sun.mirror.apt.Messager;
050: import com.sun.mirror.declaration.FieldDeclaration;
051: import com.sun.mirror.declaration.MethodDeclaration;
052: import com.sun.mirror.declaration.TypeDeclaration;
053: import com.sun.mirror.type.TypeMirror;
054: import com.sun.tools.jxc.apt.InlineAnnotationReaderImpl;
055: import com.sun.tools.jxc.model.nav.APTNavigator;
056: import com.sun.tools.xjc.api.J2SJAXBModel;
057: import com.sun.tools.xjc.api.JavaCompiler;
058: import com.sun.tools.xjc.api.Reference;
059: import com.sun.xml.bind.v2.model.core.ErrorHandler;
060: import com.sun.xml.bind.v2.model.core.Ref;
061: import com.sun.xml.bind.v2.model.core.TypeInfoSet;
062: import com.sun.xml.bind.v2.model.impl.ModelBuilder;
063: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
064:
065: /**
066: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
067: */
068: public class JavaCompilerImpl implements JavaCompiler {
069: public J2SJAXBModel bind(Collection<Reference> rootClasses,
070: Map<QName, Reference> additionalElementDecls,
071: String defaultNamespaceRemap,
072: AnnotationProcessorEnvironment env) {
073:
074: ModelBuilder<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> builder = new ModelBuilder<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration>(
075: InlineAnnotationReaderImpl.theInstance,
076: new APTNavigator(env), Collections
077: .<TypeDeclaration, TypeDeclaration> emptyMap(),
078: defaultNamespaceRemap);
079:
080: builder
081: .setErrorHandler(new ErrorHandlerImpl(env.getMessager()));
082:
083: for (Reference ref : rootClasses) {
084: TypeMirror t = ref.type;
085:
086: XmlJavaTypeAdapter xjta = ref.annotations
087: .getAnnotation(XmlJavaTypeAdapter.class);
088: XmlList xl = ref.annotations.getAnnotation(XmlList.class);
089:
090: builder.getTypeInfo(new Ref<TypeMirror, TypeDeclaration>(
091: builder, t, xjta, xl));
092: }
093:
094: TypeInfoSet r = builder.link();
095: if (r == null)
096: return null;
097:
098: if (additionalElementDecls == null)
099: additionalElementDecls = Collections.emptyMap();
100: else {
101: // fool proof check
102: for (Map.Entry<QName, ? extends Reference> e : additionalElementDecls
103: .entrySet()) {
104: if (e.getKey() == null)
105: throw new IllegalArgumentException(
106: "nulls in additionalElementDecls");
107: }
108: }
109: return new JAXBModelImpl(r, builder.reader, rootClasses,
110: new HashMap(additionalElementDecls));
111: }
112:
113: private static final class ErrorHandlerImpl implements ErrorHandler {
114: private final Messager messager;
115:
116: public ErrorHandlerImpl(Messager messager) {
117: this .messager = messager;
118: }
119:
120: public void error(IllegalAnnotationException e) {
121: messager.printError(e.toString());
122: }
123: }
124: }
|