src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/SchemaGenerator.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  */
    28 package com.sun.tools.internal.jxc.ap;
    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;
    34 import javax.annotation.processing.AbstractProcessor;
    35 import javax.annotation.processing.Processor;
    36 import javax.annotation.processing.RoundEnvironment;
    37 import javax.annotation.processing.SupportedAnnotationTypes;
    38 import javax.annotation.processing.SupportedSourceVersion;
    39 import javax.lang.model.SourceVersion;
    40 import javax.lang.model.element.Element;
    41 import javax.lang.model.element.ElementKind;
    42 import javax.lang.model.element.TypeElement;
    43 import javax.lang.model.util.ElementFilter;
    44 import javax.tools.Diagnostic;
    45 import javax.tools.StandardLocation;
    46 import javax.xml.bind.SchemaOutputResolver;
    47 import javax.xml.namespace.QName;
    48 import javax.xml.transform.Result;
    49 import javax.xml.transform.stream.StreamResult;
    50 import java.io.File;
    51 import java.io.FileOutputStream;
    52 import java.io.IOException;
    53 import java.io.OutputStream;
    54 import java.util.ArrayList;
    55 import java.util.Collection;
    56 import java.util.Collections;
    57 import java.util.HashMap;
    58 import java.util.List;
    59 import java.util.Map;
    60 import java.util.Set;
    62 /**
    63  * {@link Processor} that implements the schema generator
    64  * command line tool.
    65  *
    66  * @author Kohsuke Kawaguchi
    67  */
    68 @SupportedAnnotationTypes("*")
    69 @SupportedSourceVersion(SourceVersion.RELEASE_6)
    70 public class SchemaGenerator extends AbstractProcessor {
    72     /**
    73      * User-specified schema locations, if any.
    74      */
    75     private final Map<String,File> schemaLocations = new HashMap<String, File>();
    77     private File episodeFile;
    79     public SchemaGenerator() {
    80     }
    82     public SchemaGenerator( Map<String,File> m ) {
    83         schemaLocations.putAll(m);
    84     }
    86     public void setEpisodeFile(File episodeFile) {
    87         this.episodeFile = episodeFile;
    88     }
    90     @Override
    91     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    92         final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(processingEnv);
    94         List<Reference> classes = new ArrayList<Reference>();
    95         // simply ignore all the interface definitions,
    96         // so that users won't have to manually exclude interfaces, which is silly.
    97         filterClass(classes, roundEnv.getRootElements());
    99         J2SJAXBModel model = XJC.createJavaCompiler().bind(classes, Collections.<QName, Reference>emptyMap(), null, processingEnv);
   100         if (model == null)
   101             return false; // error
   103         try {
   104             model.generateSchema(
   105                     new SchemaOutputResolver() {
   106                         public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
   107                             File file;
   108                             OutputStream out;
   109                             if (schemaLocations.containsKey(namespaceUri)) {
   110                                 file = schemaLocations.get(namespaceUri);
   111                                 if (file == null) return null;    // don't generate
   112                                 out = new FileOutputStream(file);
   113                             } else {
   114                                 // use the default
   115                                 file = new File(suggestedFileName);
   116                                 out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", suggestedFileName)
   117                                         .openOutputStream();
   118                                 file = file.getAbsoluteFile();
   119                             }
   121                             StreamResult ss = new StreamResult(out);
   122                             processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing "+file);
   123                             ss.setSystemId(file.toURL().toExternalForm());
   124                             return ss;
   125                         }
   126                     }, errorListener);
   128             if (episodeFile != null) {
   129                 processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing "+episodeFile);
   130                 model.generateEpisodeFile(new StreamResult(episodeFile));
   131             }
   132         } catch (IOException e) {
   133             errorListener.error(e.getMessage(), e);
   134         }
   135         return false;
   136     }
   138     private void filterClass(List<Reference> classes, Collection<? extends Element> elements) {
   139         for (Element element : elements) {
   140             if (element.getKind().equals(ElementKind.CLASS)) {
   141                 classes.add(new Reference((TypeElement) element, processingEnv));
   142                 filterClass(classes, ElementFilter.typesIn(element.getEnclosedElements()));
   143             }
   144         }
   145     }
   146 }

mercurial