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.FileOutputStream;
041: import java.io.IOException;
042: import java.io.OutputStream;
043: import java.util.ArrayList;
044: import java.util.Arrays;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Set;
051:
052: import javax.xml.bind.SchemaOutputResolver;
053: import javax.xml.namespace.QName;
054: import javax.xml.transform.Result;
055: import javax.xml.transform.stream.StreamResult;
056:
057: import com.sun.mirror.apt.AnnotationProcessor;
058: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
059: import com.sun.mirror.apt.AnnotationProcessorFactory;
060: import com.sun.mirror.apt.Filer;
061: import com.sun.mirror.declaration.AnnotationTypeDeclaration;
062: import com.sun.mirror.declaration.ClassDeclaration;
063: import com.sun.mirror.declaration.TypeDeclaration;
064: import com.sun.tools.xjc.api.J2SJAXBModel;
065: import com.sun.tools.xjc.api.Reference;
066: import com.sun.tools.xjc.api.XJC;
067:
068: /**
069: * {@link AnnotationProcessorFactory} that implements the schema generator
070: * command line tool.
071: *
072: * @author Kohsuke Kawaguchi
073: */
074: public class SchemaGenerator implements AnnotationProcessorFactory {
075:
076: /**
077: * User-specified schema locations, if any.
078: */
079: private final Map<String, File> schemaLocations = new HashMap<String, File>();
080:
081: private File episodeFile;
082:
083: public SchemaGenerator() {
084: }
085:
086: public SchemaGenerator(Map<String, File> m) {
087: schemaLocations.putAll(m);
088: }
089:
090: public void setEpisodeFile(File episodeFile) {
091: this .episodeFile = episodeFile;
092: }
093:
094: public Collection<String> supportedOptions() {
095: return Collections.emptyList();
096: }
097:
098: public Collection<String> supportedAnnotationTypes() {
099: return Arrays.asList("*");
100: }
101:
102: public AnnotationProcessor getProcessorFor(
103: Set<AnnotationTypeDeclaration> atds,
104: final AnnotationProcessorEnvironment env) {
105: return new AnnotationProcessor() {
106: final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(
107: env);
108:
109: public void process() {
110: List<Reference> decls = new ArrayList<Reference>();
111: for (TypeDeclaration d : env.getTypeDeclarations()) {
112: // simply ignore all the interface definitions,
113: // so that users won't have to manually exclude interfaces, which is silly.
114: if (d instanceof ClassDeclaration)
115: decls.add(new Reference(d, env));
116: }
117:
118: J2SJAXBModel model = XJC.createJavaCompiler().bind(
119: decls,
120: Collections.<QName, Reference> emptyMap(),
121: null, env);
122: if (model == null)
123: return; // error
124:
125: try {
126: model.generateSchema(new SchemaOutputResolver() {
127: public Result createOutput(String namespaceUri,
128: String suggestedFileName)
129: throws IOException {
130: File file;
131: OutputStream out;
132: if (schemaLocations
133: .containsKey(namespaceUri)) {
134: file = schemaLocations
135: .get(namespaceUri);
136: if (file == null)
137: return null; // don't generate
138: out = new FileOutputStream(file);
139: } else {
140: // use the default
141: file = new File(suggestedFileName);
142: out = env.getFiler().createBinaryFile(
143: Filer.Location.CLASS_TREE, "",
144: file);
145: file = file.getAbsoluteFile();
146: }
147:
148: StreamResult ss = new StreamResult(out);
149: env.getMessager().printNotice(
150: "Writing " + file);
151: ss.setSystemId(file.toURL()
152: .toExternalForm());
153: return ss;
154: }
155: }, errorListener);
156:
157: if (episodeFile != null) {
158: env.getMessager().printNotice(
159: "Writing " + episodeFile);
160: model.generateEpisodeFile(new StreamResult(
161: episodeFile));
162: }
163: } catch (IOException e) {
164: errorListener.error(e.getMessage(), e);
165: }
166: }
167: };
168: }
169: }
|