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;
038:
039: import java.io.File;
040: import java.util.ArrayList;
041: import java.util.HashMap;
042: import java.util.List;
043: import java.util.Map;
044:
045: import com.sun.mirror.apt.AnnotationProcessorFactory;
046: import com.sun.tools.jxc.apt.SchemaGenerator;
047:
048: import org.apache.tools.ant.BuildException;
049: import org.apache.tools.ant.types.Commandline;
050:
051: /**
052: * Ant task to invoke the schema generator.
053: *
054: * @author Kohsuke Kawaguchi
055: */
056: public class SchemaGenTask extends AptBasedTask {
057: private final List/*<Schema>*/schemas = new ArrayList();
058:
059: private File episode;
060:
061: protected void setupCommandlineSwitches(Commandline cmd) {
062: cmd.createArgument().setValue("-nocompile");
063: }
064:
065: protected String getCompilationMessage() {
066: return "Generating schema from ";
067: }
068:
069: protected String getFailedMessage() {
070: return "schema generation failed";
071: }
072:
073: public Schema createSchema() {
074: Schema s = new Schema();
075: schemas.add(s);
076: return s;
077: }
078:
079: /**
080: * Sets the episode file to be generated.
081: * Null to not to generate one, which is the default behavior.
082: */
083: public void setEpisode(File f) {
084: this .episode = f;
085: }
086:
087: protected AnnotationProcessorFactory createFactory() {
088: Map m = new HashMap();
089: for (int i = 0; i < schemas.size(); i++) {
090: Schema schema = (Schema) schemas.get(i);
091:
092: if (m.containsKey(schema.namespace))
093: throw new BuildException(
094: "the same namespace is specified twice");
095: m.put(schema.namespace, schema.file);
096:
097: }
098:
099: SchemaGenerator r = new SchemaGenerator(m);
100: if (episode != null)
101: r.setEpisodeFile(episode);
102: return r;
103: }
104:
105: /**
106: * Nested schema element to specify the namespace -> file name mapping.
107: */
108: public class Schema {
109: private String namespace;
110: private File file;
111:
112: public void setNamespace(String namespace) {
113: this .namespace = namespace;
114: }
115:
116: public void setFile(String fileName) {
117: // resolve the file name relative to the @dest, or otherwise the project base dir.
118: File dest = getDestdir();
119: if (dest == null)
120: dest = getProject().getBaseDir();
121: this .file = new File(dest, fileName);
122: }
123: }
124: }
|