src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/SchemaGenerator.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/SchemaGenerator.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,146 @@
     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 +
    1.30 +
    1.31 +package com.sun.tools.internal.jxc.ap;
    1.32 +
    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 +
    1.37 +import javax.annotation.processing.AbstractProcessor;
    1.38 +import javax.annotation.processing.Processor;
    1.39 +import javax.annotation.processing.RoundEnvironment;
    1.40 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.41 +import javax.annotation.processing.SupportedSourceVersion;
    1.42 +import javax.lang.model.SourceVersion;
    1.43 +import javax.lang.model.element.Element;
    1.44 +import javax.lang.model.element.ElementKind;
    1.45 +import javax.lang.model.element.TypeElement;
    1.46 +import javax.lang.model.util.ElementFilter;
    1.47 +import javax.tools.Diagnostic;
    1.48 +import javax.tools.StandardLocation;
    1.49 +import javax.xml.bind.SchemaOutputResolver;
    1.50 +import javax.xml.namespace.QName;
    1.51 +import javax.xml.transform.Result;
    1.52 +import javax.xml.transform.stream.StreamResult;
    1.53 +import java.io.File;
    1.54 +import java.io.FileOutputStream;
    1.55 +import java.io.IOException;
    1.56 +import java.io.OutputStream;
    1.57 +import java.util.ArrayList;
    1.58 +import java.util.Collection;
    1.59 +import java.util.Collections;
    1.60 +import java.util.HashMap;
    1.61 +import java.util.List;
    1.62 +import java.util.Map;
    1.63 +import java.util.Set;
    1.64 +
    1.65 +/**
    1.66 + * {@link Processor} that implements the schema generator
    1.67 + * command line tool.
    1.68 + *
    1.69 + * @author Kohsuke Kawaguchi
    1.70 + */
    1.71 +@SupportedAnnotationTypes("*")
    1.72 +@SupportedSourceVersion(SourceVersion.RELEASE_6)
    1.73 +public class SchemaGenerator extends AbstractProcessor {
    1.74 +
    1.75 +    /**
    1.76 +     * User-specified schema locations, if any.
    1.77 +     */
    1.78 +    private final Map<String,File> schemaLocations = new HashMap<String, File>();
    1.79 +
    1.80 +    private File episodeFile;
    1.81 +
    1.82 +    public SchemaGenerator() {
    1.83 +    }
    1.84 +
    1.85 +    public SchemaGenerator( Map<String,File> m ) {
    1.86 +        schemaLocations.putAll(m);
    1.87 +    }
    1.88 +
    1.89 +    public void setEpisodeFile(File episodeFile) {
    1.90 +        this.episodeFile = episodeFile;
    1.91 +    }
    1.92 +
    1.93 +    @Override
    1.94 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.95 +        final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(processingEnv);
    1.96 +
    1.97 +        List<Reference> classes = new ArrayList<Reference>();
    1.98 +        // simply ignore all the interface definitions,
    1.99 +        // so that users won't have to manually exclude interfaces, which is silly.
   1.100 +        filterClass(classes, roundEnv.getRootElements());
   1.101 +
   1.102 +        J2SJAXBModel model = XJC.createJavaCompiler().bind(classes, Collections.<QName, Reference>emptyMap(), null, processingEnv);
   1.103 +        if (model == null)
   1.104 +            return false; // error
   1.105 +
   1.106 +        try {
   1.107 +            model.generateSchema(
   1.108 +                    new SchemaOutputResolver() {
   1.109 +                        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
   1.110 +                            File file;
   1.111 +                            OutputStream out;
   1.112 +                            if (schemaLocations.containsKey(namespaceUri)) {
   1.113 +                                file = schemaLocations.get(namespaceUri);
   1.114 +                                if (file == null) return null;    // don't generate
   1.115 +                                out = new FileOutputStream(file);
   1.116 +                            } else {
   1.117 +                                // use the default
   1.118 +                                file = new File(suggestedFileName);
   1.119 +                                out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", suggestedFileName)
   1.120 +                                        .openOutputStream();
   1.121 +                                file = file.getAbsoluteFile();
   1.122 +                            }
   1.123 +
   1.124 +                            StreamResult ss = new StreamResult(out);
   1.125 +                            processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing "+file);
   1.126 +                            ss.setSystemId(file.toURL().toExternalForm());
   1.127 +                            return ss;
   1.128 +                        }
   1.129 +                    }, errorListener);
   1.130 +
   1.131 +            if (episodeFile != null) {
   1.132 +                processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing "+episodeFile);
   1.133 +                model.generateEpisodeFile(new StreamResult(episodeFile));
   1.134 +            }
   1.135 +        } catch (IOException e) {
   1.136 +            errorListener.error(e.getMessage(), e);
   1.137 +        }
   1.138 +        return false;
   1.139 +    }
   1.140 +
   1.141 +    private void filterClass(List<Reference> classes, Collection<? extends Element> elements) {
   1.142 +        for (Element element : elements) {
   1.143 +            if (element.getKind().equals(ElementKind.CLASS)) {
   1.144 +                classes.add(new Reference((TypeElement) element, processingEnv));
   1.145 +                filterClass(classes, ElementFilter.typesIn(element.getEnclosedElements()));
   1.146 +            }
   1.147 +        }
   1.148 +    }
   1.149 +}

mercurial