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.txw2;
027:
028: import com.sun.codemodel.writer.FileCodeWriter;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Project;
031: import org.kohsuke.rngom.parse.compact.CompactParseable;
032: import org.kohsuke.rngom.parse.xml.SAXParseable;
033: import org.xml.sax.InputSource;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.net.MalformedURLException;
038:
039: /**
040: * Ant task interface for txw compiler.
041: *
042: * @author ryan_shoemaker@dev.java.net
043: */
044: public class TxwTask extends org.apache.tools.ant.Task {
045:
046: // txw options - reuse command line options from the main driver
047: private final TxwOptions options = new TxwOptions();
048:
049: // schema file
050: private File schemaFile;
051:
052: // syntax style of RELAX NG source schema - "xml" or "compact"
053: private static enum Style {
054: COMPACT, XML, XMLSCHEMA, AUTO_DETECT
055: }
056:
057: private Style style = Style.AUTO_DETECT;
058:
059: public TxwTask() {
060: // default package
061: options._package = options.codeModel.rootPackage();
062:
063: // default codewriter
064: try {
065: options.codeWriter = new FileCodeWriter(new File("."));
066: } catch (IOException e) {
067: throw new BuildException(e);
068: }
069: }
070:
071: /**
072: * Parse @package
073: *
074: * @param pkg name of the package to generate the java classes into
075: */
076: public void setPackage(String pkg) {
077: options._package = options.codeModel._package(pkg);
078: }
079:
080: /**
081: * Parse @syntax
082: *
083: * @param style either "compact" for RELAX NG compact syntax or "XML"
084: * for RELAX NG xml syntax
085: */
086: public void setSyntax(String style) {
087: this .style = Style.valueOf(style.toUpperCase());
088: }
089:
090: /**
091: * parse @schema
092: *
093: * @param schema the schema file to be processed by txw
094: */
095: public void setSchema(File schema) {
096: schemaFile = schema;
097: }
098:
099: /**
100: * parse @destdir
101: *
102: * @param dir the directory to produce generated source code in
103: */
104: public void setDestdir(File dir) {
105: try {
106: options.codeWriter = new FileCodeWriter(dir);
107: } catch (IOException e) {
108: throw new BuildException(e);
109: }
110: }
111:
112: /**
113: * parse @methodChaining
114: *
115: * @param flg true if the txw should generate api's that allow
116: * method chaining (when possible, false otherwise
117: */
118: public void setMethodChaining(boolean flg) {
119: options.chainMethod = flg;
120: }
121:
122: /**
123: * launch txw
124: */
125: public void execute() throws BuildException {
126: options.errorListener = new AntErrorListener(getProject());
127:
128: try {
129: InputSource in = new InputSource(schemaFile.toURL()
130: .toExternalForm());
131:
132: String msg = "Compiling: " + in.getSystemId();
133: log(msg, Project.MSG_INFO);
134:
135: if (style == Style.AUTO_DETECT) {
136: String fileName = schemaFile.getPath().toLowerCase();
137: if (fileName.endsWith("rnc"))
138: style = Style.COMPACT;
139: else if (fileName.endsWith("xsd"))
140: style = Style.XMLSCHEMA;
141: else
142: style = Style.XML;
143: }
144:
145: switch (style) {
146: case COMPACT:
147: options.source = new RELAXNGLoader(
148: new CompactParseable(in, options.errorListener));
149: break;
150: case XML:
151: options.source = new RELAXNGLoader(new SAXParseable(in,
152: options.errorListener));
153: break;
154: case XMLSCHEMA:
155: options.source = new XmlSchemaLoader(in);
156: break;
157: }
158: } catch (MalformedURLException e) {
159: throw new BuildException(e);
160: }
161:
162: // kick off the compiler
163: Main.run(options);
164: log("Compilation complete.", Project.MSG_INFO);
165: }
166: }
|