001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.jxc.apt;
038:
039: import java.io.File;
040: import java.io.IOException;
041: import java.util.Collection;
042: import java.util.Collections;
043: import java.util.Map;
044: import java.util.Set;
045: import java.util.StringTokenizer;
046:
047: import javax.xml.bind.SchemaOutputResolver;
048: import javax.xml.namespace.QName;
049:
050: import com.sun.mirror.apt.AnnotationProcessor;
051: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
052: import com.sun.mirror.declaration.AnnotationTypeDeclaration;
053: import com.sun.tools.jxc.ConfigReader;
054: import com.sun.tools.xjc.ErrorReceiver;
055: import com.sun.tools.xjc.api.J2SJAXBModel;
056: import com.sun.tools.xjc.api.Reference;
057: import com.sun.tools.xjc.api.XJC;
058:
059: import org.xml.sax.SAXException;
060:
061: /**
062: * This class behaves as a JAXB Annotation Processor,
063: * It reads the user specified typeDeclarations
064: * and the config files
065: * It also reads config files
066: *
067: * @author Bhakti Mehta (bhakti.mehta@sun.com)
068: */
069: final class AnnotationParser implements AnnotationProcessor {
070:
071: /**
072: * This is the environment available to the annotationProcessor
073: */
074: private final AnnotationProcessorEnvironment env;
075:
076: private ErrorReceiver errorListener;
077:
078: public AnnotationProcessorEnvironment getEnv() {
079: return env;
080: }
081:
082: AnnotationParser(Set<AnnotationTypeDeclaration> atds,
083: AnnotationProcessorEnvironment env) {
084: this .env = env;
085: errorListener = new ErrorReceiverImpl(env.getMessager(), env
086: .getOptions().containsKey(Const.DEBUG_OPTION));
087: }
088:
089: public void process() {
090: for (Map.Entry<String, String> me : env.getOptions().entrySet()) {
091: String key = me.getKey();
092: if (key.startsWith(Const.CONFIG_FILE_OPTION + '=')) {
093: // somehow the values are passed as a part of the key in APT.
094: // this is ugly
095: String value = key.substring(Const.CONFIG_FILE_OPTION
096: .length() + 1);
097:
098: // For multiple config files we are following the format
099: // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
100: StringTokenizer st = new StringTokenizer(value,
101: File.pathSeparator);
102: if (!st.hasMoreTokens()) {
103: errorListener.error(null, Messages.OPERAND_MISSING
104: .format(Const.CONFIG_FILE_OPTION));
105: continue;
106: }
107:
108: while (st.hasMoreTokens()) {
109: File configFile = new File(st.nextToken());
110: if (!configFile.exists()) {
111: errorListener.error(null,
112: Messages.NON_EXISTENT_FILE.format());
113: continue;
114: }
115:
116: try {
117: ConfigReader configReader = new ConfigReader(
118: env, env.getTypeDeclarations(),
119: configFile, errorListener);
120:
121: Collection<Reference> classesToBeIncluded = configReader
122: .getClassesToBeIncluded();
123: J2SJAXBModel model = XJC
124: .createJavaCompiler()
125: .bind(
126: classesToBeIncluded,
127: Collections
128: .<QName, Reference> emptyMap(),
129: null, env);
130:
131: SchemaOutputResolver schemaOutputResolver = configReader
132: .getSchemaOutputResolver();
133:
134: model.generateSchema(schemaOutputResolver,
135: errorListener);
136: } catch (IOException e) {
137: errorListener.error(e.getMessage(), e);
138: } catch (SAXException e) {
139: // the error should have already been reported
140: }
141: }
142: }
143: }
144: }
145: }
|