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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2011, 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  */
    26 package com.sun.tools.internal.jxc.ap;
    28 import com.sun.tools.internal.jxc.ConfigReader;
    29 import com.sun.tools.internal.xjc.ErrorReceiver;
    30 import com.sun.tools.internal.xjc.api.J2SJAXBModel;
    31 import com.sun.tools.internal.xjc.api.Reference;
    32 import com.sun.tools.internal.xjc.api.XJC;
    33 import org.xml.sax.SAXException;
    35 import javax.annotation.processing.AbstractProcessor;
    36 import javax.annotation.processing.ProcessingEnvironment;
    37 import javax.annotation.processing.RoundEnvironment;
    38 import javax.annotation.processing.SupportedAnnotationTypes;
    39 import javax.annotation.processing.SupportedOptions;
    40 import javax.annotation.processing.SupportedSourceVersion;
    41 import javax.lang.model.SourceVersion;
    42 import javax.lang.model.element.Element;
    43 import javax.lang.model.element.ElementKind;
    44 import javax.lang.model.element.TypeElement;
    45 import javax.lang.model.util.ElementFilter;
    46 import javax.xml.bind.SchemaOutputResolver;
    47 import javax.xml.namespace.QName;
    48 import java.io.File;
    49 import java.io.IOException;
    50 import java.util.ArrayList;
    51 import java.util.Collection;
    52 import java.util.Collections;
    53 import java.util.List;
    54 import java.util.Set;
    55 import java.util.StringTokenizer;
    57 /**
    58  * This class behaves as a JAXB Annotation Processor,
    59  * It reads the user specified typeDeclarations
    60  * and the config files
    61  * It also reads config files
    62  *
    63  * @author Bhakti Mehta (bhakti.mehta@sun.com)
    64  */
    65 @SupportedAnnotationTypes("javax.xml.bind.annotation.*")
    66 @SupportedOptions("jaxb.config") // Const.CONFIG_FILE_OPTION.getValue()
    67 @SupportedSourceVersion(SourceVersion.RELEASE_6)
    68 public final class AnnotationParser extends AbstractProcessor {
    70     private ErrorReceiver errorListener;
    72     @Override
    73     public void init(ProcessingEnvironment processingEnv) {
    74         super.init(processingEnv);
    75         this.processingEnv = processingEnv;
    76         errorListener = new ErrorReceiverImpl(
    77                 processingEnv.getMessager(),
    78                 processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue())
    79         );
    80     }
    82     @Override
    83     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    84         if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) {
    85             String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue());
    87             // For multiple config files we are following the format
    88             // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
    89             StringTokenizer st = new StringTokenizer(value, File.pathSeparator);
    90             if (!st.hasMoreTokens()) {
    91                 errorListener.error(null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue()));
    92                 return true;
    93             }
    95             while (st.hasMoreTokens()) {
    96                 File configFile = new File(st.nextToken());
    97                 if (!configFile.exists()) {
    98                     errorListener.error(null, Messages.NON_EXISTENT_FILE.format());
    99                     continue;
   100                 }
   102                 try {
   103                     Collection<TypeElement> rootElements = new ArrayList<TypeElement>();
   104                     filterClass(rootElements, roundEnv.getRootElements());
   105                     ConfigReader configReader = new ConfigReader(
   106                             processingEnv,
   107                             rootElements,
   108                             configFile,
   109                             errorListener
   110                     );
   112                     Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded();
   113                     J2SJAXBModel model = XJC.createJavaCompiler().bind(
   114                             classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv);
   116                     SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver();
   118                     model.generateSchema(schemaOutputResolver, errorListener);
   119                 } catch (IOException e) {
   120                     errorListener.error(e.getMessage(), e);
   121                 } catch (SAXException e) {
   122                     // the error should have already been reported
   123                 }
   124             }
   125         }
   126         return true;
   127     }
   129     private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) {
   130         for (Element element : elements) {
   131             if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE)) {
   132                 rootElements.add((TypeElement) element);
   133                 filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements()));
   134             }
   135         }
   136     }
   137 }

mercurial