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
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * GenerateWSDL.java
043: *
044: * Created on May 20, 2005, 7:47 PM
045: *
046: * To change this template, choose Tools | Options and locate the template under
047: * the Source Creation and Management node. Right-click the template and choose
048: * Open. You can then make changes to the template in the Source Editor.
049: */
050:
051: package org.netbeans.modules.etl.project.anttasks;
052:
053: import java.io.File;
054: import java.io.FileFilter;
055: import java.util.ArrayList;
056: import java.util.HashMap;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.Map;
060:
061: import javax.wsdl.Definition;
062:
063: import org.apache.tools.ant.BuildException;
064: import org.apache.tools.ant.Project;
065: import org.apache.tools.ant.Task;
066:
067: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
068:
069: /**
070: *
071: */
072: public class GenerateWSDL extends Task {
073: private String mSrcDirectoryLocation;
074:
075: private String mBuildDirectoryLocation;
076:
077: /** Creates a new instance of GenerateWSDL */
078: public GenerateWSDL() {
079: }
080:
081: /**
082: * @return Returns the srcDirectoryLocation.
083: */
084: public String getSrcDirectoryLocation() {
085: return mSrcDirectoryLocation;
086: }
087:
088: /**
089: * @param srcDirectoryLocation
090: * The srcDirectoryLocation to set.
091: */
092: public void setSrcDirectoryLocation(String srcDirectoryLocation) {
093: mSrcDirectoryLocation = srcDirectoryLocation;
094: }
095:
096: /**
097: * @return Returns the srcDirectoryLocation.
098: */
099: public String getBuildDirectoryLocation() {
100: return mBuildDirectoryLocation;
101: }
102:
103: /**
104: * @param buildDirectoryLocation
105: * The srcDirectoryLocation to set.
106: */
107: public void setBuildDirectoryLocation(String buildDirectoryLocation) {
108: mBuildDirectoryLocation = buildDirectoryLocation;
109: }
110:
111: public static List getFilesRecursively(File dir, FileFilter filter) {
112: List ret = new ArrayList();
113: if (!dir.isDirectory()) {
114: return ret;
115: }
116: File[] fileNdirs = dir.listFiles(filter);
117: for (int i = 0, I = fileNdirs.length; i < I; i++) {
118: if (fileNdirs[i].isDirectory()) {
119: ret.addAll(getFilesRecursively(fileNdirs[i], filter));
120: } else {
121: ret.add(fileNdirs[i]);
122: }
123: }
124: return ret;
125: }
126:
127: public static List getFilesRecursively(File dir, String[] extensions) {
128: FileFilter filter = null;
129: if (extensions[0].equals(".etl"))
130: filter = new ETLExtensionFilter(extensions);
131: else
132: filter = new EngineExtensionFilter(extensions);
133: return getFilesRecursively(dir, filter);
134: }
135:
136: public void execute() throws BuildException {
137: File srcDir = new File(mSrcDirectoryLocation);
138: File bulldDir = getBuildDir();
139: if (!srcDir.exists()) {
140: throw new BuildException("Directory "
141: + mSrcDirectoryLocation + " does not exit.");
142: }
143:
144: String[] etlExt = new String[] { ".etl" };
145: List etlFiles = getFilesRecursively(srcDir, etlExt);
146:
147: generateEngineFiles(etlFiles);
148:
149: String[] engineExt = new String[] { "_engine.xml" };
150: List etlEngineFiles = getFilesRecursively(bulldDir, engineExt);
151: Map<String, Definition> wsdlMap = new HashMap<String, Definition>();
152:
153: try {
154: for (int i = 0, I = etlEngineFiles.size(); i < I; i++) {
155: File f = (File) etlEngineFiles.get(i);
156: // TODO generate endpoint and jbi artifacts based on projectName
157: // and Engine file name
158: // String engineFileName =
159: // srcDir.getAbsoluteFile().getParentFile().getName() +
160: // f.getName().substring(0, f.getName().indexOf(".xml"));
161: String engineFileName = f.getName().substring(0,
162: f.getName().indexOf(".xml"));
163:
164: WsdlGenerator wg = new WsdlGenerator(f, engineFileName,
165: srcDir.getCanonicalPath());
166: Definition def = wg.generateWsdl();
167: wsdlMap.put(def.getQName().getLocalPart(), def);
168: }
169:
170: ETLMapWriter ew = new ETLMapWriter(wsdlMap, bulldDir
171: .getCanonicalPath());
172: ew.writeMap();
173:
174: // todo: This needed to be done ONLY at the build time....
175:
176: if (bulldDir.exists()) {
177: // create META-INF if needed..
178: String metaDirFile = bulldDir.getCanonicalPath()
179: + "/META-INF";
180: File metaDir = new File(metaDirFile);
181: if (!metaDir.exists()) {
182: metaDir.mkdir();
183: }
184:
185: }
186: JBIFileWriter fw = new JBIFileWriter(bulldDir
187: .getCanonicalPath()
188: + "/META-INF/jbi.xml", bulldDir.getCanonicalPath()
189: + "/etlmap.xml");
190: fw.writeJBI();
191:
192: } catch (Exception e) {
193: throw new BuildException(e.getMessage());
194: }
195: }
196:
197: private void generateEngineFiles(List etlFiles) {
198: Iterator iterator = etlFiles.iterator();
199: EngineFileGenerator g = new EngineFileGenerator();
200: while (iterator.hasNext()) {
201: File element = (File) iterator.next();
202: try {
203: g.generateEngine(element, getBuildDir());
204: } catch (Exception e) {
205: e.printStackTrace();
206: }
207:
208: }
209:
210: }
211:
212: private File getBuildDir() {
213: File bulldDir = null;
214:
215: Project p = this .getProject();
216: if (p != null) {
217: String projPath = p.getProperty("basedir") + File.separator;
218: String buildDirLoc = projPath
219: + p.getProperty(IcanproProjectProperties.BUILD_DIR);
220: bulldDir = new File(buildDirLoc);
221: } else {
222: bulldDir = new File(mBuildDirectoryLocation);
223: }
224: return bulldDir;
225: }
226:
227: public static void main(String[] args) {
228: GenerateWSDL task = new GenerateWSDL();
229: task.setSrcDirectoryLocation("test");
230: task.setBuildDirectoryLocation("test/build");
231: task.execute();
232:
233: }
234: }
|