|
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 |
|
87 // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar |
|
88 StringTokenizer st = new StringTokenizer(value, File.pathSeparator); |
|
89 if (!st.hasMoreTokens()) { |
|
90 errorListener.error(null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue())); |
|
91 return true; |
|
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 } |