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