src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/AnnotationParser.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/AnnotationParser.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,137 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.jxc.ap;
    1.30 +
    1.31 +import com.sun.tools.internal.jxc.ConfigReader;
    1.32 +import com.sun.tools.internal.xjc.ErrorReceiver;
    1.33 +import com.sun.tools.internal.xjc.api.J2SJAXBModel;
    1.34 +import com.sun.tools.internal.xjc.api.Reference;
    1.35 +import com.sun.tools.internal.xjc.api.XJC;
    1.36 +import org.xml.sax.SAXException;
    1.37 +
    1.38 +import javax.annotation.processing.AbstractProcessor;
    1.39 +import javax.annotation.processing.ProcessingEnvironment;
    1.40 +import javax.annotation.processing.RoundEnvironment;
    1.41 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.42 +import javax.annotation.processing.SupportedOptions;
    1.43 +import javax.annotation.processing.SupportedSourceVersion;
    1.44 +import javax.lang.model.SourceVersion;
    1.45 +import javax.lang.model.element.Element;
    1.46 +import javax.lang.model.element.ElementKind;
    1.47 +import javax.lang.model.element.TypeElement;
    1.48 +import javax.lang.model.util.ElementFilter;
    1.49 +import javax.xml.bind.SchemaOutputResolver;
    1.50 +import javax.xml.namespace.QName;
    1.51 +import java.io.File;
    1.52 +import java.io.IOException;
    1.53 +import java.util.ArrayList;
    1.54 +import java.util.Collection;
    1.55 +import java.util.Collections;
    1.56 +import java.util.List;
    1.57 +import java.util.Set;
    1.58 +import java.util.StringTokenizer;
    1.59 +
    1.60 +/**
    1.61 + * This class behaves as a JAXB Annotation Processor,
    1.62 + * It reads the user specified typeDeclarations
    1.63 + * and the config files
    1.64 + * It also reads config files
    1.65 + *
    1.66 + * @author Bhakti Mehta (bhakti.mehta@sun.com)
    1.67 + */
    1.68 +@SupportedAnnotationTypes("javax.xml.bind.annotation.*")
    1.69 +@SupportedOptions("jaxb.config") // Const.CONFIG_FILE_OPTION.getValue()
    1.70 +@SupportedSourceVersion(SourceVersion.RELEASE_6)
    1.71 +public final class AnnotationParser extends AbstractProcessor {
    1.72 +
    1.73 +    private ErrorReceiver errorListener;
    1.74 +
    1.75 +    @Override
    1.76 +    public void init(ProcessingEnvironment processingEnv) {
    1.77 +        super.init(processingEnv);
    1.78 +        this.processingEnv = processingEnv;
    1.79 +        errorListener = new ErrorReceiverImpl(
    1.80 +                processingEnv.getMessager(),
    1.81 +                processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue())
    1.82 +        );
    1.83 +    }
    1.84 +
    1.85 +    @Override
    1.86 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.87 +        if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) {
    1.88 +            String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue());
    1.89 +
    1.90 +            // For multiple config files we are following the format
    1.91 +            // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
    1.92 +            StringTokenizer st = new StringTokenizer(value, File.pathSeparator);
    1.93 +            if (!st.hasMoreTokens()) {
    1.94 +                errorListener.error(null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue()));
    1.95 +                return true;
    1.96 +            }
    1.97 +
    1.98 +            while (st.hasMoreTokens()) {
    1.99 +                File configFile = new File(st.nextToken());
   1.100 +                if (!configFile.exists()) {
   1.101 +                    errorListener.error(null, Messages.NON_EXISTENT_FILE.format());
   1.102 +                    continue;
   1.103 +                }
   1.104 +
   1.105 +                try {
   1.106 +                    Collection<TypeElement> rootElements = new ArrayList<TypeElement>();
   1.107 +                    filterClass(rootElements, roundEnv.getRootElements());
   1.108 +                    ConfigReader configReader = new ConfigReader(
   1.109 +                            processingEnv,
   1.110 +                            rootElements,
   1.111 +                            configFile,
   1.112 +                            errorListener
   1.113 +                    );
   1.114 +
   1.115 +                    Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded();
   1.116 +                    J2SJAXBModel model = XJC.createJavaCompiler().bind(
   1.117 +                            classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv);
   1.118 +
   1.119 +                    SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver();
   1.120 +
   1.121 +                    model.generateSchema(schemaOutputResolver, errorListener);
   1.122 +                } catch (IOException e) {
   1.123 +                    errorListener.error(e.getMessage(), e);
   1.124 +                } catch (SAXException e) {
   1.125 +                    // the error should have already been reported
   1.126 +                }
   1.127 +            }
   1.128 +        }
   1.129 +        return true;
   1.130 +    }
   1.131 +
   1.132 +    private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) {
   1.133 +        for (Element element : elements) {
   1.134 +            if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE)) {
   1.135 +                rootElements.add((TypeElement) element);
   1.136 +                filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements()));
   1.137 +            }
   1.138 +        }
   1.139 +    }
   1.140 +}

mercurial