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-2006 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: package org.netbeans.modules.web.examples;
043:
044: import java.io.File;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.OutputStream;
049: import java.util.Stack;
050: import java.util.zip.ZipEntry;
051: import java.util.zip.ZipInputStream;
052: import org.netbeans.spi.project.support.ant.AntProjectHelper;
053: import org.openide.filesystems.FileLock;
054:
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.xml.XMLUtil;
058: import org.w3c.dom.Document;
059: import org.w3c.dom.Element;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062: import org.w3c.dom.Text;
063: import org.xml.sax.InputSource;
064:
065: /**
066: * Create a sample web project by unzipping a template into some directory
067: *
068: * @author Martin Grebac
069: */
070: public class WebSampleProjectGenerator {
071:
072: private WebSampleProjectGenerator() {
073: }
074:
075: public static final String PROJECT_CONFIGURATION_NAMESPACE = "http://www.netbeans.org/ns/web-project/3"; //NOI18N
076: public static final String JSPC_CLASSPATH = "jspc.classpath";
077:
078: public static FileObject createProjectFromTemplate(
079: final FileObject template, File projectLocation,
080: final String name) throws IOException {
081: assert template != null && projectLocation != null
082: && name != null;
083: FileObject prjLoc = createProjectFolder(projectLocation);
084: if (template.getExt().endsWith("zip")) { //NOI18N
085: unzip(template.getInputStream(), prjLoc);
086: // update project.xml
087: try {
088: File projXml = FileUtil
089: .toFile(prjLoc
090: .getFileObject(AntProjectHelper.PROJECT_XML_PATH));
091: Document doc = XMLUtil.parse(new InputSource(projXml
092: .toURI().toString()), false, true, null, null);
093: NodeList nlist = doc.getElementsByTagNameNS(
094: PROJECT_CONFIGURATION_NAMESPACE, "name"); //NOI18N
095: if (nlist != null) {
096: for (int i = 0; i < nlist.getLength(); i++) {
097: Node n = nlist.item(i);
098: if (n.getNodeType() != Node.ELEMENT_NODE) {
099: continue;
100: }
101: Element e = (Element) n;
102:
103: replaceText(e, name);
104: }
105: saveXml(doc, prjLoc,
106: AntProjectHelper.PROJECT_XML_PATH);
107: }
108: } catch (Exception e) {
109: throw new IOException(e.toString());
110: }
111:
112: prjLoc.refresh(false);
113: }
114: return prjLoc;
115: }
116:
117: private static FileObject createProjectFolder(File projectFolder)
118: throws IOException {
119: FileObject projLoc;
120: Stack nameStack = new Stack();
121: while ((projLoc = FileUtil.toFileObject(projectFolder)) == null) {
122: nameStack.push(projectFolder.getName());
123: projectFolder = projectFolder.getParentFile();
124: }
125: while (!nameStack.empty()) {
126: projLoc = projLoc.createFolder((String) nameStack.pop());
127: assert projLoc != null;
128: }
129: return projLoc;
130: }
131:
132: private static void unzip(InputStream source,
133: FileObject targetFolder) throws IOException {
134: //installation
135: ZipInputStream zip = new ZipInputStream(source);
136: try {
137: ZipEntry ent;
138: while ((ent = zip.getNextEntry()) != null) {
139: if (ent.isDirectory()) {
140: FileUtil.createFolder(targetFolder, ent.getName());
141: } else {
142: FileObject destFile = FileUtil.createData(
143: targetFolder, ent.getName());
144: FileLock lock = destFile.lock();
145: try {
146: OutputStream out = destFile
147: .getOutputStream(lock);
148: try {
149: FileUtil.copy(zip, out);
150: } finally {
151: out.close();
152: }
153: } finally {
154: lock.releaseLock();
155: }
156: }
157: }
158: } finally {
159: zip.close();
160: }
161: }
162:
163: /**
164: * Extract nested text from an element.
165: * Currently does not handle coalescing text nodes, CDATA sections, etc.
166: * @param parent a parent element
167: * @return the nested text, or null if none was found
168: */
169: private static void replaceText(Element parent, String name) {
170: NodeList l = parent.getChildNodes();
171: for (int i = 0; i < l.getLength(); i++) {
172: if (l.item(i).getNodeType() == Node.TEXT_NODE) {
173: Text text = (Text) l.item(i);
174: text.setNodeValue(name);
175: return;
176: }
177: }
178: }
179:
180: /**
181: * Save an XML config file to a named path.
182: * If the file does not yet exist, it is created.
183: */
184: private static void saveXml(Document doc, FileObject dir,
185: String path) throws IOException {
186: FileObject xml = FileUtil.createData(dir, path);
187: FileLock lock = xml.lock();
188: try {
189: OutputStream os = xml.getOutputStream(lock);
190: try {
191: XMLUtil.write(doc, os, "UTF-8"); // NOI18N
192: } finally {
193: os.close();
194: }
195: } finally {
196: lock.releaseLock();
197: }
198: }
199:
200: }
|