001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.api.impl.j2s;
027:
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import javax.xml.bind.annotation.XmlList;
034: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
035: import javax.xml.namespace.QName;
036:
037: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
038: import com.sun.mirror.apt.Messager;
039: import com.sun.mirror.declaration.FieldDeclaration;
040: import com.sun.mirror.declaration.MethodDeclaration;
041: import com.sun.mirror.declaration.TypeDeclaration;
042: import com.sun.mirror.type.TypeMirror;
043: import com.sun.tools.internal.jxc.apt.InlineAnnotationReaderImpl;
044: import com.sun.tools.internal.jxc.model.nav.APTNavigator;
045: import com.sun.tools.internal.xjc.api.J2SJAXBModel;
046: import com.sun.tools.internal.xjc.api.JavaCompiler;
047: import com.sun.tools.internal.xjc.api.Reference;
048: import com.sun.xml.internal.bind.v2.model.core.ErrorHandler;
049: import com.sun.xml.internal.bind.v2.model.core.Ref;
050: import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet;
051: import com.sun.xml.internal.bind.v2.model.impl.ModelBuilder;
052: import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
053:
054: /**
055: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
056: */
057: public class JavaCompilerImpl implements JavaCompiler {
058: public J2SJAXBModel bind(Collection<Reference> rootClasses,
059: Map<QName, Reference> additionalElementDecls,
060: String defaultNamespaceRemap,
061: AnnotationProcessorEnvironment env) {
062:
063: ModelBuilder<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration> builder = new ModelBuilder<TypeMirror, TypeDeclaration, FieldDeclaration, MethodDeclaration>(
064: InlineAnnotationReaderImpl.theInstance,
065: new APTNavigator(env), defaultNamespaceRemap);
066:
067: builder
068: .setErrorHandler(new ErrorHandlerImpl(env.getMessager()));
069:
070: for (Reference ref : rootClasses) {
071: TypeMirror t = ref.type;
072:
073: XmlJavaTypeAdapter xjta = ref.annotations
074: .getAnnotation(XmlJavaTypeAdapter.class);
075: XmlList xl = ref.annotations.getAnnotation(XmlList.class);
076:
077: builder.getTypeInfo(new Ref<TypeMirror, TypeDeclaration>(
078: builder, t, xjta, xl));
079: }
080:
081: TypeInfoSet r = builder.link();
082: if (r == null)
083: return null;
084:
085: if (additionalElementDecls == null)
086: additionalElementDecls = Collections.emptyMap();
087: else {
088: // fool proof check
089: for (Map.Entry<QName, ? extends Reference> e : additionalElementDecls
090: .entrySet()) {
091: if (e.getKey() == null)
092: throw new IllegalArgumentException(
093: "nulls in additionalElementDecls");
094: }
095: }
096: return new JAXBModelImpl(r, builder.reader, rootClasses,
097: new HashMap(additionalElementDecls));
098: }
099:
100: private static final class ErrorHandlerImpl implements ErrorHandler {
101: private final Messager messager;
102:
103: public ErrorHandlerImpl(Messager messager) {
104: this .messager = messager;
105: }
106:
107: public void error(IllegalAnnotationException e) {
108: messager.printError(e.toString());
109: }
110: }
111: }
|