001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.jxc.apt;
027:
028: import java.io.File;
029: import java.io.IOException;
030: import java.util.Collection;
031: import java.util.Collections;
032: import java.util.Map;
033: import java.util.Set;
034: import java.util.StringTokenizer;
035:
036: import javax.xml.bind.SchemaOutputResolver;
037: import javax.xml.namespace.QName;
038:
039: import com.sun.mirror.apt.AnnotationProcessor;
040: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
041: import com.sun.mirror.declaration.AnnotationTypeDeclaration;
042: import com.sun.tools.internal.jxc.ConfigReader;
043: import com.sun.tools.internal.xjc.ErrorReceiver;
044: import com.sun.tools.internal.xjc.api.J2SJAXBModel;
045: import com.sun.tools.internal.xjc.api.Reference;
046: import com.sun.tools.internal.xjc.api.XJC;
047:
048: import org.xml.sax.SAXException;
049:
050: /**
051: * This class behaves as a JAXB Annotation Processor,
052: * It reads the user specified typeDeclarations
053: * and the config files
054: * It also reads config files
055: *
056: * @author Bhakti Mehta (bhakti.mehta@sun.com)
057: */
058: final class AnnotationParser implements AnnotationProcessor {
059:
060: /**
061: * This is the environment available to the annotationProcessor
062: */
063: private final AnnotationProcessorEnvironment env;
064:
065: private ErrorReceiver errorListener;
066:
067: public AnnotationProcessorEnvironment getEnv() {
068: return env;
069: }
070:
071: AnnotationParser(Set<AnnotationTypeDeclaration> atds,
072: AnnotationProcessorEnvironment env) {
073: this .env = env;
074: errorListener = new ErrorReceiverImpl(env.getMessager(), env
075: .getOptions().containsKey(Const.DEBUG_OPTION));
076: }
077:
078: public void process() {
079: for (Map.Entry<String, String> me : env.getOptions().entrySet()) {
080: String key = me.getKey();
081: if (key.startsWith(Const.CONFIG_FILE_OPTION + '=')) {
082: // somehow the values are passed as a part of the key in APT.
083: // this is ugly
084: String value = key.substring(Const.CONFIG_FILE_OPTION
085: .length() + 1);
086:
087: // For multiple config files we are following the format
088: // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
089: StringTokenizer st = new StringTokenizer(value,
090: File.pathSeparator);
091: if (!st.hasMoreTokens()) {
092: errorListener.error(null,
093: Messages.NO_FILE_SPECIFIED.format());
094: continue;
095: }
096:
097: while (st.hasMoreTokens()) {
098: File configFile = new File(st.nextToken());
099: if (!configFile.exists()) {
100: errorListener.error(null,
101: Messages.NON_EXISTENT_FILE.format());
102: continue;
103: }
104:
105: try {
106: ConfigReader configReader = new ConfigReader(
107: env, env.getTypeDeclarations(),
108: configFile, errorListener);
109:
110: Collection<Reference> classesToBeIncluded = configReader
111: .getClassesToBeIncluded();
112: J2SJAXBModel model = XJC
113: .createJavaCompiler()
114: .bind(
115: classesToBeIncluded,
116: Collections
117: .<QName, Reference> emptyMap(),
118: null, env);
119:
120: SchemaOutputResolver schemaOutputResolver = configReader
121: .getSchemaOutputResolver();
122:
123: model.generateSchema(schemaOutputResolver,
124: errorListener);
125: } catch (IOException e) {
126: errorListener.error(e.getMessage(), e);
127: } catch (SAXException e) {
128: // the error should have already been reported
129: }
130: }
131: }
132: }
133: }
134: }
|