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

Print this page




  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.xjc.api.J2SJAXBModel;
  31 import com.sun.tools.internal.xjc.api.Reference;
  32 import com.sun.tools.internal.xjc.api.XJC;
  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.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;
  61 
  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 {
  71 
  72     /**
  73      * User-specified schema locations, if any.
  74      */
  75     private final Map<String,File> schemaLocations = new HashMap<String, File>();
  76 
  77     private File episodeFile;
  78 
  79     public SchemaGenerator() {
  80     }
  81 
  82     public SchemaGenerator( Map<String,File> m ) {
  83         schemaLocations.putAll(m);
  84     }
  85 
  86     public void setEpisodeFile(File episodeFile) {
  87         this.episodeFile = episodeFile;
  88     }
  89 
  90     @Override
  91     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  92         final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(processingEnv);
  93 
  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());
  98 
  99         J2SJAXBModel model = XJC.createJavaCompiler().bind(classes, Collections.<QName, Reference>emptyMap(), null, processingEnv);
 100         if (model == null)
 101             return false; // error
 102 
 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                             }


 125                         }
 126                     }, errorListener);
 127 
 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     }
 137 
 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 }


  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                             }


 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 }