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 */ 25 26 package com.sun.tools.internal.jxc.ap; 27 28 import com.sun.tools.internal.jxc.ConfigReader; 29 import com.sun.tools.internal.xjc.ErrorReceiver; 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 import org.xml.sax.SAXException; 34 35 import javax.annotation.processing.AbstractProcessor; 36 import javax.annotation.processing.ProcessingEnvironment; 37 import javax.annotation.processing.RoundEnvironment; 38 import javax.annotation.processing.SupportedAnnotationTypes; 39 import javax.annotation.processing.SupportedOptions; 40 import javax.annotation.processing.SupportedSourceVersion; 41 import javax.lang.model.SourceVersion; 42 import javax.lang.model.element.Element; 43 import javax.lang.model.element.ElementKind; 44 import javax.lang.model.element.TypeElement; 45 import javax.lang.model.util.ElementFilter; 46 import javax.xml.bind.SchemaOutputResolver; 47 import javax.xml.namespace.QName; 48 import java.io.File; 49 import java.io.IOException; 50 import java.util.ArrayList; 51 import java.util.Collection; 52 import java.util.Collections; 53 import java.util.List; 54 import java.util.Set; 55 import java.util.StringTokenizer; 56 57 /** 58 * This class behaves as a JAXB Annotation Processor, 59 * It reads the user specified typeDeclarations 60 * and the config files 61 * It also reads config files 62 * 63 * @author Bhakti Mehta (bhakti.mehta@sun.com) 64 */ 65 @SupportedAnnotationTypes("javax.xml.bind.annotation.*") 66 @SupportedOptions("jaxb.config") // Const.CONFIG_FILE_OPTION.getValue() 67 @SupportedSourceVersion(SourceVersion.RELEASE_6) 68 public final class AnnotationParser extends AbstractProcessor { 69 70 private ErrorReceiver errorListener; 71 72 @Override 73 public void init(ProcessingEnvironment processingEnv) { 74 super.init(processingEnv); 75 this.processingEnv = processingEnv; 76 errorListener = new ErrorReceiverImpl( 77 processingEnv.getMessager(), 78 processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue()) 79 ); 80 } 81 82 @Override 83 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 84 if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) { 85 String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue()); 86 87 // For multiple config files we are following the format 93 } 94 95 while (st.hasMoreTokens()) { 96 File configFile = new File(st.nextToken()); 97 if (!configFile.exists()) { 98 errorListener.error(null, Messages.NON_EXISTENT_FILE.format()); 99 continue; 100 } 101 102 try { 103 Collection<TypeElement> rootElements = new ArrayList<TypeElement>(); 104 filterClass(rootElements, roundEnv.getRootElements()); 105 ConfigReader configReader = new ConfigReader( 106 processingEnv, 107 rootElements, 108 configFile, 109 errorListener 110 ); 111 112 Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded(); 113 J2SJAXBModel model = XJC.createJavaCompiler().bind( 114 classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv); 115 116 SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver(); 117 118 model.generateSchema(schemaOutputResolver, errorListener); 119 } catch (IOException e) { 120 errorListener.error(e.getMessage(), e); 121 } catch (SAXException e) { 122 // the error should have already been reported 123 } 124 } 125 } 126 return true; 127 } 128 129 private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) { 130 for (Element element : elements) { 131 if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE)) { 132 rootElements.add((TypeElement) element); 133 filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements())); 134 } 135 } 136 } 137 } | 1 /* 2 * Copyright (c) 1997, 2012, 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 package com.sun.tools.internal.jxc.ap; 27 28 import com.sun.tools.internal.jxc.ConfigReader; 29 import com.sun.tools.internal.jxc.api.JXC; 30 import com.sun.tools.internal.xjc.ErrorReceiver; 31 import com.sun.tools.internal.xjc.api.J2SJAXBModel; 32 import com.sun.tools.internal.xjc.api.Reference; 33 import org.xml.sax.SAXException; 34 35 import javax.annotation.processing.AbstractProcessor; 36 import javax.annotation.processing.ProcessingEnvironment; 37 import javax.annotation.processing.RoundEnvironment; 38 import javax.annotation.processing.SupportedAnnotationTypes; 39 import javax.annotation.processing.SupportedOptions; 40 import javax.lang.model.SourceVersion; 41 import javax.lang.model.element.Element; 42 import javax.lang.model.element.ElementKind; 43 import javax.lang.model.element.TypeElement; 44 import javax.lang.model.util.ElementFilter; 45 import javax.xml.bind.SchemaOutputResolver; 46 import javax.xml.namespace.QName; 47 import java.io.File; 48 import java.io.IOException; 49 import java.util.ArrayList; 50 import java.util.Collection; 51 import java.util.Collections; 52 import java.util.Set; 53 import java.util.StringTokenizer; 54 55 /** 56 * This class behaves as a JAXB Annotation Processor, 57 * It reads the user specified typeDeclarations 58 * and the config files 59 * It also reads config files 60 * 61 * Used in unit tests 62 * 63 * @author Bhakti Mehta (bhakti.mehta@sun.com) 64 */ 65 @SupportedAnnotationTypes("javax.xml.bind.annotation.*") 66 @SupportedOptions("jaxb.config") 67 public final class AnnotationParser extends AbstractProcessor { 68 69 private ErrorReceiver errorListener; 70 71 @Override 72 public void init(ProcessingEnvironment processingEnv) { 73 super.init(processingEnv); 74 this.processingEnv = processingEnv; 75 errorListener = new ErrorReceiverImpl( 76 processingEnv.getMessager(), 77 processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue()) 78 ); 79 } 80 81 @Override 82 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 83 if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) { 84 String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue()); 85 86 // For multiple config files we are following the format 92 } 93 94 while (st.hasMoreTokens()) { 95 File configFile = new File(st.nextToken()); 96 if (!configFile.exists()) { 97 errorListener.error(null, Messages.NON_EXISTENT_FILE.format()); 98 continue; 99 } 100 101 try { 102 Collection<TypeElement> rootElements = new ArrayList<TypeElement>(); 103 filterClass(rootElements, roundEnv.getRootElements()); 104 ConfigReader configReader = new ConfigReader( 105 processingEnv, 106 rootElements, 107 configFile, 108 errorListener 109 ); 110 111 Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded(); 112 J2SJAXBModel model = JXC.createJavaCompiler().bind( 113 classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv); 114 115 SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver(); 116 117 model.generateSchema(schemaOutputResolver, errorListener); 118 } catch (IOException e) { 119 errorListener.error(e.getMessage(), e); 120 } catch (SAXException e) { 121 // the error should have already been reported 122 } 123 } 124 } 125 return true; 126 } 127 128 private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) { 129 for (Element element : elements) { 130 if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE) || 131 element.getKind().equals(ElementKind.ENUM)) { 132 rootElements.add((TypeElement) element); 133 filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements())); 134 } 135 } 136 } 137 138 @Override 139 public SourceVersion getSupportedSourceVersion() { 140 if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0) 141 return SourceVersion.valueOf("RELEASE_7"); 142 else 143 return SourceVersion.RELEASE_6; 144 } 145 } |