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