325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.jxc.apt;
325N/A
325N/Aimport java.io.File;
325N/Aimport java.io.FileOutputStream;
325N/Aimport java.io.IOException;
325N/Aimport java.io.OutputStream;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Arrays;
325N/Aimport java.util.Collection;
325N/Aimport java.util.Collections;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.List;
325N/Aimport java.util.Map;
325N/Aimport java.util.Set;
325N/A
325N/Aimport javax.xml.bind.SchemaOutputResolver;
325N/Aimport javax.xml.namespace.QName;
325N/Aimport javax.xml.transform.Result;
325N/Aimport javax.xml.transform.stream.StreamResult;
325N/A
325N/Aimport com.sun.mirror.apt.AnnotationProcessor;
325N/Aimport com.sun.mirror.apt.AnnotationProcessorEnvironment;
325N/Aimport com.sun.mirror.apt.AnnotationProcessorFactory;
325N/Aimport com.sun.mirror.apt.Filer;
325N/Aimport com.sun.mirror.declaration.AnnotationTypeDeclaration;
325N/Aimport com.sun.mirror.declaration.ClassDeclaration;
325N/Aimport com.sun.mirror.declaration.TypeDeclaration;
325N/Aimport com.sun.tools.internal.xjc.api.J2SJAXBModel;
325N/Aimport com.sun.tools.internal.xjc.api.Reference;
325N/Aimport com.sun.tools.internal.xjc.api.XJC;
325N/A
325N/A/**
325N/A * {@link AnnotationProcessorFactory} that implements the schema generator
325N/A * command line tool.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic class SchemaGenerator implements AnnotationProcessorFactory {
325N/A
325N/A /**
325N/A * User-specified schema locations, if any.
325N/A */
325N/A private final Map<String,File> schemaLocations = new HashMap<String, File>();
325N/A
325N/A private File episodeFile;
325N/A
325N/A public SchemaGenerator() {
325N/A }
325N/A
325N/A public SchemaGenerator( Map<String,File> m ) {
325N/A schemaLocations.putAll(m);
325N/A }
325N/A
325N/A public void setEpisodeFile(File episodeFile) {
325N/A this.episodeFile = episodeFile;
325N/A }
325N/A
325N/A public Collection<String> supportedOptions() {
325N/A return Collections.emptyList();
325N/A }
325N/A
325N/A public Collection<String> supportedAnnotationTypes() {
325N/A return Arrays.asList("*");
325N/A }
325N/A
325N/A public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds, final AnnotationProcessorEnvironment env) {
325N/A return new AnnotationProcessor() {
325N/A final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(env);
325N/A
325N/A public void process() {
325N/A List<Reference> decls = new ArrayList<Reference>();
325N/A for(TypeDeclaration d : env.getTypeDeclarations()) {
325N/A // simply ignore all the interface definitions,
325N/A // so that users won't have to manually exclude interfaces, which is silly.
325N/A if(d instanceof ClassDeclaration)
325N/A decls.add(new Reference(d,env));
325N/A }
325N/A
325N/A J2SJAXBModel model = XJC.createJavaCompiler().bind(decls,Collections.<QName,Reference>emptyMap(),null,env);
325N/A if(model==null)
325N/A return; // error
325N/A
325N/A try {
325N/A model.generateSchema(
325N/A new SchemaOutputResolver() {
325N/A public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
325N/A File file;
325N/A OutputStream out;
325N/A if(schemaLocations.containsKey(namespaceUri)) {
325N/A file = schemaLocations.get(namespaceUri);
325N/A if(file==null) return null; // don't generate
325N/A out = new FileOutputStream(file);
325N/A } else {
325N/A // use the default
325N/A file = new File(suggestedFileName);
325N/A out = env.getFiler().createBinaryFile(Filer.Location.CLASS_TREE,"",file);
325N/A file = file.getAbsoluteFile();
325N/A }
325N/A
325N/A StreamResult ss = new StreamResult(out);
325N/A env.getMessager().printNotice("Writing "+file);
325N/A ss.setSystemId(file.toURL().toExternalForm());
325N/A return ss;
325N/A }
325N/A }, errorListener);
325N/A
325N/A if(episodeFile!=null) {
325N/A env.getMessager().printNotice("Writing "+episodeFile);
325N/A model.generateEpisodeFile(new StreamResult(episodeFile));
325N/A }
325N/A } catch (IOException e) {
325N/A errorListener.error(e.getMessage(),e);
325N/A }
325N/A }
325N/A };
325N/A }
325N/A}