001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.nio.charset.Charset;
024: import org.netbeans.api.project.Project;
025: import org.netbeans.api.project.ProjectManager;
026: import org.netbeans.api.queries.FileEncodingQuery;
027: import static org.netbeans.modules.xslt.project.XsltproConstants.*;
028: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
029:
030: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
031: import org.netbeans.spi.project.support.ant.AntProjectHelper;
032: import org.netbeans.spi.project.support.ant.EditableProperties;
033: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
034: import org.netbeans.spi.project.support.ant.ProjectGenerator;
035: import org.openide.ErrorManager;
036: import org.openide.filesystems.FileObject;
037: import org.openide.filesystems.FileStateInvalidException;
038: import org.openide.filesystems.FileSystem;
039: import org.openide.filesystems.FileUtil;
040: import org.openide.util.Mutex;
041: import org.openide.util.MutexException;
042: import org.openide.util.NbBundle;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045:
046: /**
047: *
048: * @author Vitaly Bychkov
049: * @version 1.0
050: */
051: public class XsltproProjectGenerator {
052:
053: private static final String DEFAULT_DOC_BASE_FOLDER = "conf"; //NOI18N
054: private static final String DEFAULT_SRC_FOLDER = "src"; //NOI18N
055: private static final String DEFAULT_RESOURCE_FOLDER = "setup"; //NOI18N
056: private static final String DEFAULT_BUILD_DIR = "build"; //NOI18N
057: private static final String DEFAULT_NBPROJECT_DIR = "nbproject"; // NOI18N
058:
059: private XsltproProjectGenerator() {
060: }
061:
062: /**
063: * Create a new empty J2SE project.
064: * @param dir the top-level directory (need not yet exist but if it does it must be empty)
065: * @param name the code name for the project
066: * @return the helper object permitting it to be further customized
067: * @throws IOException in case something went wrong
068: */
069: public static AntProjectHelper createProject(File dir,
070: final String name) throws IOException {
071: final FileObject fo = createProjectDir(dir);
072: // dir.mkdirs();
073: // // XXX clumsy way to refresh, but otherwise it doesn't work for new folders
074: // File rootF = dir;
075: // while (rootF.getParentFile() != null) {
076: // rootF = rootF.getParentFile();
077: // }
078: // final FileObject fo = FileUtil.toFileObject (rootF);
079: // assert fo != null : "At least disk roots must be mounted! " + rootF;
080: // fo.getFileSystem().refresh(false);
081: // final FileObject fo = FileUtil.toFileObject (dir);
082:
083: final AntProjectHelper[] h = new AntProjectHelper[1];
084:
085: fo.getFileSystem().runAtomicAction(
086: new FileSystem.AtomicAction() {
087:
088: public void run() throws IOException {
089: h[0] = setupProject(fo, name);
090: final Project p = ProjectManager.getDefault()
091: .findProject(fo);
092:
093: fo.createFolder(DEFAULT_SRC_FOLDER); // NOI18N
094: // FileObject bpelasaRoot = srcRoot;
095: // FileObject tMapFo = FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem()
096: // .findResource("org-netbeans-xsltpro/transformmap.xml"), //NOI18N
097: // bpelasaRoot, "transformmap"); //NOI18N
098:
099: try {
100: ProjectManager.mutex().writeAccess(
101: new Mutex.ExceptionAction<Void>() {
102:
103: public Void run()
104: throws Exception {
105: EditableProperties ep = h[0]
106: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
107: ep
108: .put(
109: IcanproProjectProperties.SOURCE_ROOT,
110: DEFAULT_SRC_FOLDER); //NOI18N
111: ep
112: .setProperty(
113: IcanproProjectProperties.META_INF,
114: "${"
115: + IcanproProjectProperties.SOURCE_ROOT
116: + "}/"
117: + DEFAULT_DOC_BASE_FOLDER); //NOI18N
118: // Bing bpelasa ep.setProperty(SRC_DIR, "${"+SOURCE_ROOT+"}/"+DEFAULT_BPELASA_FOLDER); //NOI18N
119: ep
120: .setProperty(
121: IcanproProjectProperties.SRC_DIR,
122: "${"
123: + IcanproProjectProperties.SOURCE_ROOT
124: + "}"); //NOI18N
125: ep
126: .setProperty(
127: IcanproProjectProperties.RESOURCE_DIR,
128: DEFAULT_RESOURCE_FOLDER);
129: h[0]
130: .putProperties(
131: AntProjectHelper.PROJECT_PROPERTIES_PATH,
132: ep);
133:
134: ProjectManager.getDefault()
135: .saveProject(p);
136: return null;
137: }
138: });
139: } catch (MutexException me) {
140: ErrorManager.getDefault().notify(me);
141: }
142: }
143: });
144:
145: return h[0];
146: }
147:
148: private static FileObject createProjectDir(File dir)
149: throws IOException {
150: FileObject dirFO;
151: if (!dir.exists()) {
152: //Refresh before mkdir not to depend on window focus
153: refreshFileSystem(dir);
154: dirFO = FileUtil.createFolder(dir);
155: // dir.mkdirs();
156: // refreshFileSystem (dir);
157: } else {
158: dirFO = FileUtil.toFileObject(dir);
159: }
160:
161: // vlv # 113228
162: if (dirFO == null) {
163: throw new IOException("Can't create " + dir.getName());
164: }
165: assert dirFO.isFolder() : "Not really a dir: " + dir; // NOI18N
166: assert dirFO.getChildren().length == 0 : "Dir must have been empty: "
167: + dir;
168: return dirFO;
169: }
170:
171: private static void refreshFileSystem(final File dir)
172: throws FileStateInvalidException {
173: File rootF = dir;
174: while (rootF.getParentFile() != null) {
175: rootF = rootF.getParentFile();
176: }
177: FileObject dirFO = FileUtil.toFileObject(rootF);
178: assert dirFO != null : "At least disk roots must be mounted! "
179: + rootF; // NOI18N
180: dirFO.getFileSystem().refresh(false);
181: }
182:
183: public static AntProjectHelper importProject(File dir, String name,
184: FileObject wmFO, FileObject javaRoot,
185: FileObject configFilesBase, String j2eeLevel,
186: String buildfile) throws IOException {
187: dir.mkdirs();
188: // XXX clumsy way to refresh, but otherwise it doesn't work for new folders
189: File rootF = dir;
190: while (rootF.getParentFile() != null) {
191: rootF = rootF.getParentFile();
192: }
193: // XXX add code to set meta inf directory (meta-inf and java src)
194: FileObject fo = FileUtil.toFileObject(rootF);
195: assert fo != null : "At least disk roots must be mounted! "
196: + rootF;
197: fo.getFileSystem().refresh(false);
198: fo = FileUtil.toFileObject(dir);
199:
200: // vlv # 113228
201: if (fo == null) {
202: throw new IOException("Can't create " + dir.getName());
203: }
204: assert fo.isFolder() : "Not really a dir: " + dir;
205: AntProjectHelper h = setupProject(fo, name);
206: EditableProperties ep = h
207: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
208: if (FileUtil.isParentOf(fo, wmFO) || fo.equals(wmFO)) {
209: ep.put(IcanproProjectProperties.SOURCE_ROOT, "."); //NOI18N
210: ep.setProperty(IcanproProjectProperties.SRC_DIR,
211: relativePath(fo, javaRoot)); //NOI18N
212: ep.setProperty(IcanproProjectProperties.META_INF,
213: relativePath(fo, configFilesBase)); //NOI18N
214: } else {
215: File wmRoot = FileUtil.toFile(wmFO);
216: ep.put(IcanproProjectProperties.SOURCE_ROOT, wmRoot
217: .getAbsolutePath());
218: String configFilesPath = relativePath(wmFO, configFilesBase);
219: configFilesPath = configFilesPath.length() > 0 ? "${"
220: + IcanproProjectProperties.SOURCE_ROOT + "}/"
221: + configFilesPath // NOI18N
222: : "${" + IcanproProjectProperties.SOURCE_ROOT + "}"; //NOI18N
223: String javaPath = relativePath(wmFO, javaRoot);
224: javaPath = javaPath.length() > 0 ? "${"
225: + IcanproProjectProperties.SOURCE_ROOT + "}/"
226: + javaPath // NOI18N
227: : "${" + IcanproProjectProperties.SOURCE_ROOT + "}"; //NOI18N
228: ep.setProperty(IcanproProjectProperties.SRC_DIR, javaPath);
229: ep.setProperty(IcanproProjectProperties.META_INF,
230: configFilesPath);
231: }
232: if (!GeneratedFilesHelper.BUILD_XML_PATH.equals(buildfile)) {
233: ep.setProperty(IcanproProjectProperties.BUILD_FILE,
234: buildfile);
235: }
236: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
237:
238: Project p = ProjectManager.getDefault().findProject(
239: h.getProjectDirectory());
240: ProjectManager.getDefault().saveProject(p);
241:
242: return h;
243: }
244:
245: private static String relativePath(FileObject parent,
246: FileObject child) {
247: if (child.equals(parent))
248: return EMPTY_STRING;
249: if (!FileUtil.isParentOf(parent, child))
250: throw new IllegalArgumentException(
251: "Cannot find relative path, " + parent
252: + " is not parent of " + child);
253: return child.getPath().substring(parent.getPath().length() + 1);
254: }
255:
256: private static AntProjectHelper setupProject(FileObject dirFO,
257: String name) throws IOException {
258: AntProjectHelper h = ProjectGenerator.createProject(dirFO,
259: XsltproProjectType.TYPE);
260: Element data = h.getPrimaryConfigurationData(true);
261: Document doc = data.getOwnerDocument();
262: Element nameEl = doc.createElementNS(
263: XsltproProjectType.PROJECT_CONFIGURATION_NAMESPACE,
264: "name"); // NOI18N
265: nameEl.appendChild(doc.createTextNode(name));
266: data.appendChild(nameEl);
267: Element minant = doc.createElementNS(
268: XsltproProjectType.PROJECT_CONFIGURATION_NAMESPACE,
269: "minimum-ant-version"); // NOI18N
270: minant.appendChild(doc.createTextNode("1.6")); // NOI18N
271: data.appendChild(minant);
272: h.putPrimaryConfigurationData(data, true);
273:
274: EditableProperties ep = h
275: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
276: // ep.setProperty(JAVAC_CLASSPATH, "${libs.j2ee14.classpath}");
277: ep.setProperty(IcanproProjectProperties.DIST_DIR, "dist"); // NOI18N
278: ep.setProperty(IcanproProjectProperties.DIST_JAR, "${"
279: + IcanproProjectProperties.DIST_DIR + "}/" + name
280: + ".zip"); // NOI18N
281: ep
282: .setProperty(IcanproProjectProperties.JAR_NAME, name
283: + ".jar");
284: ep.setProperty(IcanproProjectProperties.JAR_COMPRESS, "false");
285: // ep.setProperty(JAR_CONTENT_ADDITIONAL, "");
286: Deployment deployment = Deployment.getDefault();
287: String serverInstanceID = deployment
288: .getDefaultServerInstanceID();
289: ep.setProperty(IcanproProjectProperties.JAVAC_SOURCE, "1.4");
290: ep.setProperty(IcanproProjectProperties.JAVAC_DEBUG, "true");
291: ep.setProperty(IcanproProjectProperties.JAVAC_DEPRECATION,
292: "false");
293: // todo r
294: ep.setProperty(VALIDATION_FLAG, "false");
295:
296: ep.setProperty(IcanproProjectProperties.JAVAC_TARGET, "1.4");
297:
298: ep.setProperty(IcanproProjectProperties.BUILD_DIR,
299: DEFAULT_BUILD_DIR);
300: ep.setProperty(IcanproProjectProperties.BUILD_GENERATED_DIR,
301: "${" + IcanproProjectProperties.BUILD_DIR
302: + "}/generated"); // NOI18N
303: ep.setProperty(IcanproProjectProperties.BUILD_CLASSES_DIR, "${"
304: + IcanproProjectProperties.BUILD_DIR + "}/jar"); // NOI18N
305: ep.setProperty(IcanproProjectProperties.BUILD_CLASSES_EXCLUDES,
306: "**/*.java,**/*.form,**/.nbattrs"); // NOI18N
307: ep.setProperty(IcanproProjectProperties.DIST_JAVADOC_DIR, "${"
308: + IcanproProjectProperties.DIST_DIR + "}/javadoc"); // NOI18N
309: ep.setProperty(IcanproProjectProperties.JAVA_PLATFORM,
310: "default_platform"); // NOI18N
311: ep.setProperty(IcanproProjectProperties.DEBUG_CLASSPATH, "${"
312: + IcanproProjectProperties.JAVAC_CLASSPATH + "}:${"
313: + IcanproProjectProperties.BUILD_CLASSES_DIR + "}"); // NOI18N
314: ep.setProperty(IcanproProjectProperties.WSDL_CLASSPATH, "");
315: Charset enc = FileEncodingQuery.getDefaultEncoding();
316: ep.setProperty(IcanproProjectProperties.SOURCE_ENCODING, enc
317: .name());
318:
319: //============= Start of IcanPro========================================//
320: ep.setProperty(IcanproProjectProperties.JBI_SE_TYPE,
321: "sun-xslt-engine"); // NOI18N
322: ep.setProperty(
323: IcanproProjectProperties.SERVICE_UNIT_DESCRIPTION,
324: NbBundle.getMessage(XsltproProjectGenerator.class,
325: "TXT_Service_Unit_Description")); // NOI18N
326:
327: // todo r
328: ep.setProperty("jbi.se.type", "sun-bpel-engine"); // NOI18N
329: ep.setProperty("jbi.service-unit.description", NbBundle
330: .getMessage(XsltproProjectGenerator.class,
331: "TXT_Service_Unit_Description")); // NOI18N
332:
333: ep.setProperty(
334: IcanproProjectProperties.JBI_COMPONENT_CONF_ROOT,
335: "nbproject/private"); // NOI18N
336: ep.setProperty(
337: IcanproProjectProperties.JBI_DEPLOYMENT_CONF_ROOT,
338: "nbproject/deployment"); // NOI18N
339: ep.setProperty(IcanproProjectProperties.BC_DEPLOYMENT_JAR, "${"
340: + IcanproProjectProperties.BUILD_DIR + "}/"
341: + "BCDeployment.jar"); // NOI18N
342: ep.setProperty(IcanproProjectProperties.SE_DEPLOYMENT_JAR, "${"
343: + IcanproProjectProperties.BUILD_DIR + "}/"
344: + "SEDeployment.jar"); // NOI18N
345: //============= End of IcanPro========================================//
346: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
347:
348: ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
349: //============= Start of IcanPro========================================//
350: ep.setProperty(
351: IcanproProjectProperties.JBI_COMPONENT_CONF_FILE,
352: "ComponentInformation.xml"); // NOI18N
353: ep.setProperty(
354: IcanproProjectProperties.JBI_DEPLOYMENT_CONF_FILE,
355: "default.xml"); // NOI18N
356: //============= End of IcanPro========================================//
357: h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
358: Project p = ProjectManager.getDefault().findProject(dirFO);
359: ProjectManager.getDefault().saveProject(p);
360: return h;
361: }
362:
363: }
|