/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the license at
* https://glassfish.dev.java.net/public/CDDLv1.0.html or
* glassfish/bootstrap/legal/CDDLv1.0.txt.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at glassfish/bootstrap/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* you own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright (c) Ericsson AB, 2004-2007. All rights reserved.
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*/
package org.jvnet.glassfish.comms.netbeans.sip.module.wizards;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
class ProjectGenerator {
private ProjectGenerator() {}
static final String PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/web-project/3"; //NOI18N
static final String JSPC_CLASSPATH = "jspc.classpath";
static FileObject createProjectFromTemplate(final FileObject template, File projectLocation, final String name) throws IOException {
assert template != null && projectLocation != null && name != null;
FileObject prjLoc = FileUtil.createFolder(projectLocation);
if (template.getExt().endsWith("zip")) { //NOI18N
unzip(template.getInputStream(), prjLoc);
// update project.xml
try {
File projXml = FileUtil.toFile(prjLoc.getFileObject(AntProjectHelper.PROJECT_XML_PATH));
Document doc = XMLUtil.parse(new InputSource(projXml.toURI().toString()), false, true, null, null);
NodeList nlist = doc.getElementsByTagNameNS(PROJECT_CONFIGURATION_NAMESPACE, "name"); //NOI18N
if (nlist != null) {
for (int i=0; i < nlist.getLength(); i++) {
Node n = nlist.item(i);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Element e = (Element)n;
replaceText(e, name);
}
saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH);
}
// clean up the project.properties, too.
FileObject projPropsFO = prjLoc.getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH);
if (null != projPropsFO) {
java.util.Properties p = new java.util.Properties();
InputStream in = projPropsFO.getInputStream();
OutputStream out = null;
try {
p.load(in);
// this is a hack
// make sure these two keys match keys in the sip-app
// template's project.properties file....
//
p.setProperty("war.name", name+".war");
p.setProperty("war.ear.name", name+".war");
in.close();
out = projPropsFO.getOutputStream();
p.store(out, "Altered war.name property");
} catch (java.io.IOException ioe) {
// this might be a read or a write
Logger.getLogger(ProjectGenerator.class.getName()).log(Level.FINER,
null, ioe);
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
}
//}
} catch (org.xml.sax.SAXException e) {
IOException ioe = new IOException();
ioe.initCause(e);
throw ioe;
}
prjLoc.refresh(false);
}
return prjLoc;
}
private static void unzip(InputStream source, final FileObject targetFolder) throws IOException {
//installation
final ZipInputStream zip=new ZipInputStream(source);
try {
ZipEntry ent;
while ((ent = zip.getNextEntry()) != null) {
if (ent.isDirectory()) {
FileUtil.createFolder(targetFolder, ent.getName());
} else {
final FileObject destFile = FileUtil.createData(targetFolder,ent.getName());
final FileLock lock = destFile.lock();
try {
final OutputStream out = destFile.getOutputStream(lock);
try {
FileUtil.copy(zip, out);
} finally {
out.close();
}
} finally {
lock.releaseLock();
}
}
}
} finally {
zip.close();
}
}
/**
* Extract nested text from an element.
* Currently does not handle coalescing text nodes, CDATA sections, etc.
* @param parent a parent element
* @return the nested text, or null if none was found
*/
private static void replaceText(Element parent, String name) {
NodeList l = parent.getChildNodes();
for (int i = 0; i < l.getLength(); i++) {
if (l.item(i).getNodeType() == Node.TEXT_NODE) {
Text text = (Text)l.item(i);
text.setNodeValue(name);
return;
}
}
}
/**
* Save an XML config file to a named path.
* If the file does not yet exist, it is created.
*/
private static void saveXml(Document doc, FileObject dir, String path) throws IOException {
FileObject xml = FileUtil.createData(dir, path);
FileLock lock = xml.lock();
try {
OutputStream os = xml.getOutputStream(lock);
try {
XMLUtil.write(doc, os, "UTF-8"); // NOI18N
} finally {
os.close();
}
} finally {
lock.releaseLock();
}
}
}