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
26package com.sun.tools.internal.jxc.ap;
27
28import com.sun.tools.internal.jxc.ConfigReader;
29import com.sun.tools.internal.jxc.api.JXC;
30import com.sun.tools.internal.xjc.ErrorReceiver;
31import com.sun.tools.internal.xjc.api.J2SJAXBModel;
32import com.sun.tools.internal.xjc.api.Reference;
33import org.xml.sax.SAXException;
34
35import javax.annotation.processing.AbstractProcessor;
36import javax.annotation.processing.ProcessingEnvironment;
37import javax.annotation.processing.RoundEnvironment;
38import javax.annotation.processing.SupportedAnnotationTypes;
39import javax.annotation.processing.SupportedOptions;
40import javax.lang.model.SourceVersion;
41import javax.lang.model.element.Element;
42import javax.lang.model.element.ElementKind;
43import javax.lang.model.element.TypeElement;
44import javax.lang.model.util.ElementFilter;
45import javax.xml.bind.SchemaOutputResolver;
46import javax.xml.namespace.QName;
47import java.io.File;
48import java.io.IOException;
49import java.util.ArrayList;
50import java.util.Collection;
51import java.util.Collections;
52import java.util.Set;
53import 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")
67public 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}
146