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.framework;
043:
044: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectConstants;
045: import org.netbeans.modules.visualweb.project.jsf.api.ProjectTemplate;
046: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
047: import org.netbeans.modules.visualweb.project.jsf.libraries.JsfProjectLibrary;
048:
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.util.HashMap;
052: import java.util.Map;
053: import javax.xml.parsers.DocumentBuilder;
054: import javax.xml.parsers.DocumentBuilderFactory;
055: import org.netbeans.api.project.Project;
056: import org.openide.ErrorManager;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062:
063: /**
064: *
065: * @author dey
066: */
067: public class JsfProjectTemplate extends ProjectTemplate {
068: /** Creates a new instance of JsfProjectTemplate */
069: public JsfProjectTemplate() {
070: }
071:
072: public void addLibrary(Project project) throws IOException {
073: // Add the Creator libraries to the project
074: JsfProjectLibrary.addLibrary(project);
075: }
076:
077: public void create(Project project, String pageName)
078: throws IOException {
079: // Extract the project template file for this project type
080: InputStream is = JsfProjectTemplate.class
081: .getResourceAsStream("JsfProjectTemplate.xml"); // NOI18N
082: Document document = null;
083: DocumentBuilderFactory factory = DocumentBuilderFactory
084: .newInstance();
085: Node node;
086:
087: try {
088: DocumentBuilder builder = factory.newDocumentBuilder();
089: document = builder.parse(is);
090: FileObject projDir = project.getProjectDirectory();
091: instantiateFile(project, document.getDocumentElement(),
092: project.getProjectDirectory(), pageName);
093: } catch (IOException e) {
094: throw e;
095: } catch (Exception e) {
096: ErrorManager.getDefault().notify(e);
097: }
098: }
099:
100: public void instantiateFile(Project project, Node node,
101: FileObject folder, String pageName) throws IOException {
102: String nodeName = node.getNodeName();
103: String fileName = getAttr(node, "name"); // NOI18N
104:
105: if (nodeName.equals("file")) {
106: if ((pageName != null) && "Page1.jsp".equals(fileName)) {
107: fileName = pageName;
108: }
109:
110: Map<String, String> templateParameters = new HashMap<String, String>();
111: templateParameters.put("folder", ""); //NOI18N
112: templateParameters.put("creatingProject", "true"); //NOI18N
113: templateParameters.put("j2eePlatformVersion",
114: JsfProjectUtils.getJ2eePlatformVersion(project)); //NOI18N
115: templateParameters.put("sourceLevel", JsfProjectUtils
116: .getSourceLevel(project)); //NOI18N
117:
118: if ((fileName.length() > 0)
119: && folder.getFileObject(fileName) == null) {
120: instantiateFileTemplate(folder, fileName, getAttr(node,
121: "template"), templateParameters); // NOI18N
122: }
123: } else {
124: FileObject newFolder = null;
125: if (nodeName.equals("folder")) { // NOI18N
126: if (fileName.equals("${" + JsfProjectConstants.SRC_DIR
127: + "}")) {
128: FileObject src = JsfProjectUtils
129: .getSourceRoot(project);
130: fileName = FileUtil.getRelativePath(project
131: .getProjectDirectory(), src);
132: } else if (fileName.equals("${"
133: + JsfProjectConstants.WEB_DOCBASE_DIR + "}")) {
134: FileObject webDocbase = JsfProjectUtils
135: .getDocumentRoot(project);
136: fileName = FileUtil.getRelativePath(project
137: .getProjectDirectory(), webDocbase);
138: } else if (fileName.equals("${"
139: + JsfProjectConstants.PROP_JSF_PAGEBEAN_PACKAGE
140: + "}")) {
141: fileName = getBeanPackage();
142: if (fileName == null) {
143: fileName = deriveSafeName(project
144: .getProjectDirectory().getName());
145: }
146: }
147: fileName = fileName.replace('.', '/');
148: if ((newFolder = folder.getFileObject(fileName)) == null) {
149: newFolder = FileUtil.createFolder(folder, fileName);
150: }
151: } else {
152: newFolder = folder;
153: }
154:
155: NodeList nodes = node.getChildNodes();
156: for (int i = 0; i < nodes.getLength(); i++) {
157: instantiateFile(project, nodes.item(i), newFolder,
158: pageName);
159: }
160: }
161: }
162: }
|