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: package org.netbeans.modules.visualweb.project.jsf.api;
043:
044: import org.netbeans.modules.visualweb.project.jsf.*;
045:
046: import java.io.IOException;
047: import java.net.URL;
048: import java.util.Enumeration;
049: import java.util.Map;
050: import org.netbeans.api.project.Project;
051: import org.openide.ErrorManager;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileSystem;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.filesystems.Repository;
056: import org.openide.loaders.DataFolder;
057: import org.openide.loaders.DataObject;
058: import org.openide.util.Utilities;
059:
060: import org.w3c.dom.NamedNodeMap;
061: import org.w3c.dom.Node;
062:
063: public abstract class ProjectTemplate {
064: private String beanPackage = null;
065:
066: public ProjectTemplate() {
067: }
068:
069: public abstract void addLibrary(Project project) throws IOException;
070:
071: public abstract void create(Project project, String pageName)
072: throws IOException;
073:
074: public abstract void instantiateFile(Project project, Node node,
075: FileObject folder, String pageName) throws IOException;
076:
077: protected FileObject instantiateFileTemplate(FileObject folder,
078: String name, String templateName,
079: Map<String, String> parameters) throws IOException {
080: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
081: FileObject FO = fs.findResource(templateName);
082: if (FO == null) {
083: IOException iox = new IOException(
084: "Can't find template FileObject for "
085: + templateName); // NOI18N - internal error
086: ErrorManager.getDefault().notify(ErrorManager.ERROR, iox);
087: throw iox;
088: }
089:
090: // Allow file been created under subdir of the root.
091: int pindex = name.lastIndexOf('/');
092: if (pindex != -1) {
093: String path = name.substring(0, pindex);
094: folder = FileUtil.createFolder(folder, path);
095: name = name.substring(pindex + 1);
096: }
097:
098: DataFolder folderDataObj = (DataFolder) DataObject.find(folder);
099: DataObject template = DataObject.find(FO);
100: String ext = FileUtil.getExtension(name);
101: if (ext.length() > 0)
102: name = name.substring(0, name.lastIndexOf(ext) - 1);
103: DataObject newDO = null;
104: try {
105: if (parameters != null) {
106: newDO = template.createFromTemplate(folderDataObj,
107: name, parameters);
108: } else {
109: newDO = template
110: .createFromTemplate(folderDataObj, name);
111: }
112: } catch (Exception e) {
113: ErrorManager.getDefault().notify(e);
114: }
115: if (newDO == null)
116: return null;
117: else
118: return newDO.getPrimaryFile();
119: }
120:
121: protected String getAttr(Node node, String key) {
122: String value = ""; // NOI18N
123: NamedNodeMap attrs = node.getAttributes();
124: if (attrs != null) {
125: Node attr = attrs.getNamedItem(key);
126: if (attr != null) {
127: try {
128: value = attr.getNodeValue();
129: } catch (Exception e) {
130: // default prevails
131: }
132: }
133: }
134: return value;
135: }
136:
137: /**
138: * Derive an identifier suitable for a java package name or context path
139: * @param sourceName Original name from which to derive the name
140: * @return An identifier suitable for a java package name or context path
141: */
142: public static String deriveSafeName(String sourceName) {
143: StringBuffer dest = new StringBuffer(sourceName.length());
144: int sourceLen = sourceName.length();
145: if (sourceLen > 0) {
146: int pos = 0;
147: while (pos < sourceLen) {
148: if (Character.isJavaIdentifierStart(sourceName
149: .charAt(pos))) {
150: dest.append(Character.toLowerCase(sourceName
151: .charAt(pos)));
152: pos++;
153: break;
154: }
155: pos++;
156: }
157:
158: for (int i = pos; i < sourceLen; i++) {
159: if (Character
160: .isJavaIdentifierPart(sourceName.charAt(i)))
161: dest.append(Character.toLowerCase(sourceName
162: .charAt(i)));
163: }
164: }
165: if (dest.length() == 0
166: || !Utilities.isJavaIdentifier(dest.toString()))
167: return "untitled"; // NOI18N
168: else
169: return dest.toString();
170: }
171:
172: public void setBeanPackage(String pkg) {
173: beanPackage = pkg;
174: }
175:
176: public String getBeanPackage() {
177: return beanPackage;
178: }
179:
180: /*
181: * Adding a project to the templates list means adding a node to the System filesystem
182: * under Templates/Projects/MyTemplates. We use a boilerplate to clone most of the entries
183: * for such a node. All we really need to supply is the path to the project that will
184: * serve as a template.
185: *
186: */
187:
188: public static void addToTemplateChooser(Project project, String name)
189: throws IOException {
190: FileSystem dfs = Repository.getDefault().getDefaultFileSystem();
191: FileObject templateRootFolder = dfs
192: .findResource(JsfProjectConstants.USER_TEMPLATE_FS_ROOT);
193: assert templateRootFolder != null;
194:
195: FileObject boilerplate = dfs
196: .findResource(JsfProjectConstants.USER_TEMPLATE_FS_BOILERPLATE);
197: assert boilerplate != null;
198:
199: Enumeration attrs = boilerplate.getAttributes();
200:
201: URL templateUrl = null;
202: try {
203: templateUrl = FileUtil
204: .toFile(project.getProjectDirectory()).toURL();
205: } catch (Exception e) {
206: throw new IOException("Unable to create template URLs"); // NOI18N
207: }
208:
209: FileObject template = templateRootFolder.createData(name);
210:
211: // Copy over the attributes from the boilerplate template
212: while (attrs.hasMoreElements()) {
213: String key = (String) attrs.nextElement();
214: template.setAttribute(key, boilerplate.getAttribute(key));
215: }
216:
217: // Point to the user's project on disk
218: template.setAttribute(
219: JsfProjectConstants.USER_TEMPLATE_DIR_TAG, templateUrl);
220: }
221:
222: }
|