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