KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > freeform > WebProjectGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.freeform;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.modules.ant.freeform.spi.support.Util;
26 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
27 import org.netbeans.spi.project.AuxiliaryConfiguration;
28 import org.netbeans.spi.project.support.ant.AntProjectHelper;
29 import org.openide.util.NbBundle;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33
34 /**
35  * Reads/writes project.xml.
36  *
37  * @author Jesse Glick, David Konecny, Pavel Buzek
38  */

39 public class WebProjectGenerator {
40
41 // /** Keep root elements in the order specified by project's XML schema. */
42
private static final String JavaDoc[] rootElementsOrder = new String JavaDoc[]{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"}; // NOI18N
43
private static final String JavaDoc[] viewElementsOrder = new String JavaDoc[]{"items", "context-menu"}; // NOI18N
44

45 // // this order is not required by schema, but follow it to minimize randomness a bit
46
private static final String JavaDoc[] folderElementsOrder = new String JavaDoc[]{"source-folder", "build-folder"}; // NOI18N
47
private static final String JavaDoc[] viewItemElementsOrder = new String JavaDoc[]{"source-folder", "source-file"}; // NOI18N
48

49     private WebProjectGenerator() {}
50
51     /**
52      * @param soruces list of pairs[relative path, display name]
53      */

54     public static void putWebSourceFolder(AntProjectHelper helper, List JavaDoc/*<String>*/ sources) {
55         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
56         Document JavaDoc doc = data.getOwnerDocument();
57         Element JavaDoc foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); // NOI18N
58
if (foldersEl == null) {
59             foldersEl = doc.createElementNS(Util.NAMESPACE, "folders"); // NOI18N
60
Util.appendChildElement(data, foldersEl, rootElementsOrder);
61         } else {
62             List JavaDoc l = Util.findSubElements(foldersEl);
63             for (int i = 0; i < l.size(); i++) {
64                 Element JavaDoc e = (Element JavaDoc) l.get(i);
65                 Element JavaDoc te = Util.findElement(e, "type", Util.NAMESPACE);
66                 if (te != null && Util.findText(te).equals("doc_root")) {
67                     foldersEl.removeChild(e);
68                     break;
69                 }
70             }
71         }
72         Element JavaDoc viewEl = Util.findElement(data, "view", Util.NAMESPACE); // NOI18N
73
if (viewEl == null) {
74             viewEl = doc.createElementNS(Util.NAMESPACE, "view"); // NOI18N
75
Util.appendChildElement(data, viewEl, rootElementsOrder);
76         }
77         Element JavaDoc itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); // NOI18N
78
if (itemsEl == null) {
79             itemsEl = doc.createElementNS(Util.NAMESPACE, "items"); // NOI18N
80
Util.appendChildElement(viewEl, itemsEl, viewElementsOrder);
81         } else {
82             List JavaDoc l = Util.findSubElements(itemsEl);
83             for (int i = 0; i < l.size(); i++) {
84                 Element JavaDoc e = (Element JavaDoc) l.get(i);
85                 if (e.hasAttribute("style")) {
86                     if (e.getAttribute("style").equals("tree")) {
87                         itemsEl.removeChild(e);
88                         break;
89                     }
90                 }
91             }
92         }
93         
94         Iterator JavaDoc it1 = sources.iterator();
95         while (it1.hasNext()) {
96             String JavaDoc path = (String JavaDoc)it1.next();
97             assert it1.hasNext();
98             String JavaDoc dispname = (String JavaDoc)it1.next();
99             Element JavaDoc sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); // NOI18N
100
Element JavaDoc el = doc.createElementNS(Util.NAMESPACE, "label"); // NOI18N
101
el.appendChild(doc.createTextNode(dispname));
102             sourceFolderEl.appendChild(el);
103             el = doc.createElementNS(Util.NAMESPACE, "type"); // NOI18N
104
el.appendChild(doc.createTextNode(WebProjectConstants.TYPE_DOC_ROOT));
105             sourceFolderEl.appendChild(el);
106             el = doc.createElementNS(Util.NAMESPACE, "location"); // NOI18N
107
el.appendChild(doc.createTextNode(path));
108             sourceFolderEl.appendChild(el);
109             Util.appendChildElement(foldersEl, sourceFolderEl, folderElementsOrder);
110             
111             sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); // NOI18N
112
sourceFolderEl.setAttribute("style", "tree"); // NOI18N
113
el = doc.createElementNS(Util.NAMESPACE, "label"); // NOI18N
114
el.appendChild(doc.createTextNode(NbBundle.getMessage(WebProjectGenerator.class, "LBL_WebPages")));
115             sourceFolderEl.appendChild(el);
116             el = doc.createElementNS(Util.NAMESPACE, "location"); // NOI18N
117
el.appendChild(doc.createTextNode(path)); // NOI18N
118
sourceFolderEl.appendChild(el);
119             Node JavaDoc firstNode = itemsEl.getFirstChild();
120             if (firstNode != null) {
121                 itemsEl.insertBefore(sourceFolderEl, firstNode);
122             } else {
123                 Util.appendChildElement(itemsEl, sourceFolderEl, viewItemElementsOrder);
124             }
125         }
126         Util.putPrimaryConfigurationData(helper, data);
127     }
128     
129     
130     /**
131      * Read web modules from the project.
132      * @param helper AntProjectHelper instance
133      * @param aux AuxiliaryConfiguration instance
134      * @return list of WebModule instances
135      */

136     public static List JavaDoc/*<WebModule>*/ getWebmodules (
137             AntProjectHelper helper, AuxiliaryConfiguration aux) {
138         //assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
139
ArrayList JavaDoc list = new ArrayList JavaDoc();
140         Element JavaDoc data = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); // NOI18N
141
List JavaDoc/*<Element>*/ wms = Util.findSubElements(data);
142         Iterator JavaDoc it = wms.iterator();
143         while (it.hasNext()) {
144             Element JavaDoc wmEl = (Element JavaDoc)it.next();
145             WebModule wm = new WebModule();
146             Iterator JavaDoc it2 = Util.findSubElements(wmEl).iterator();
147             while (it2.hasNext()) {
148                 Element JavaDoc el = (Element JavaDoc)it2.next();
149                 if (el.getLocalName().equals("doc-root")) { // NOI18N
150
wm.docRoot = Util.findText(el);
151                     continue;
152                 }
153                 if (el.getLocalName().equals("classpath")) { // NOI18N
154
wm.classpath = Util.findText(el);
155                     continue;
156                 }
157                 if (el.getLocalName().equals("context-path")) { // NOI18N
158
wm.contextPath = Util.findText(el);
159                     continue;
160                 }
161                 if (el.getLocalName().equals("j2ee-spec-level")) { // NOI18N
162
wm.j2eeSpecLevel = Util.findText(el);
163                 }
164             }
165             list.add(wm);
166         }
167         return list;
168     }
169
170     /**
171      * Update web modules of the project. Project is left modified
172      * and you must save it explicitely.
173      * @param helper AntProjectHelper instance
174      * @param aux AuxiliaryConfiguration instance
175      * @param webModules list of WebModule instances
176      */

177     public static void putWebModules(AntProjectHelper helper,
178             AuxiliaryConfiguration aux, List JavaDoc/*<WebModule>*/ webModules) {
179         //assert ProjectManager.mutex().isWriteAccess();
180
Element JavaDoc data = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); // NOI18N
181
if (data == null) {
182             data = Util.getPrimaryConfigurationData(helper).getOwnerDocument().
183                 createElementNS(WebProjectNature.NS_WEB, "web-data"); // NOI18N
184
}
185         Document JavaDoc doc = data.getOwnerDocument();
186         List JavaDoc wms = Util.findSubElements(data);
187         Iterator JavaDoc it = wms.iterator();
188         while (it.hasNext()) {
189             Element JavaDoc wmEl = (Element JavaDoc)it.next();
190             data.removeChild(wmEl);
191         }
192         Iterator JavaDoc it2 = webModules.iterator();
193         while (it2.hasNext()) {
194             Element JavaDoc wmEl = doc.createElementNS(WebProjectNature.NS_WEB, "web-module"); // NOI18N
195
data.appendChild(wmEl);
196             WebModule wm = (WebModule)it2.next();
197             Element JavaDoc el;
198             if (wm.docRoot != null) {
199                 el = doc.createElementNS(WebProjectNature.NS_WEB, "doc-root"); // NOI18N
200
el.appendChild(doc.createTextNode(wm.docRoot));
201                 wmEl.appendChild(el);
202             }
203             if (wm.classpath != null) {
204                 el = doc.createElementNS(WebProjectNature.NS_WEB, "classpath"); // NOI18N
205
el.appendChild(doc.createTextNode(wm.classpath));
206                 wmEl.appendChild(el);
207             }
208             if (wm.contextPath != null) {
209                 el = doc.createElementNS(WebProjectNature.NS_WEB, "context-path"); // NOI18N
210
el.appendChild(doc.createTextNode(wm.contextPath));
211                 wmEl.appendChild(el);
212             }
213             if (wm.j2eeSpecLevel != null) {
214                 el = doc.createElementNS(WebProjectNature.NS_WEB, "j2ee-spec-level"); // NOI18N
215
el.appendChild(doc.createTextNode(wm.j2eeSpecLevel));
216                 wmEl.appendChild(el);
217             }
218         }
219         aux.putConfigurationFragment(data, true);
220     }
221     
222     /**
223      * Structure describing web module.
224      * Data in the struct are in the same format as they are stored in XML.
225      */

226     public static final class WebModule {
227         public String JavaDoc docRoot;
228         public String JavaDoc classpath;
229         public String JavaDoc contextPath;
230         public String JavaDoc j2eeSpecLevel;
231     }
232
233 }
234
Popular Tags