KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > EJBProjectGenerator


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.j2ee.ejbfreeform;
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.spi.project.AuxiliaryConfiguration;
27 import org.netbeans.spi.project.support.ant.AntProjectHelper;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * Reads/writes project.xml.
33  *
34  * @author Jesse Glick, David Konecny, Pavel Buzek, Martin Adamek
35  */

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

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

46     private EJBProjectGenerator() {}
47     
48     /**
49      * @param soruces list of pairs[relative path, display name]
50      */

51     public static void putEJBSourceFolder(AntProjectHelper helper, List JavaDoc/*<String>*/ sources) {
52         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
53         Document JavaDoc doc = data.getOwnerDocument();
54         Element JavaDoc foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); // NOI18N
55
if (foldersEl == null) {
56             foldersEl = doc.createElementNS(Util.NAMESPACE, "folders"); // NOI18N
57
Util.appendChildElement(data, foldersEl, rootElementsOrder);
58         }
59         Element JavaDoc viewEl = Util.findElement(data, "view", Util.NAMESPACE); // NOI18N
60
if (viewEl == null) {
61             viewEl = doc.createElementNS(Util.NAMESPACE, "view"); // NOI18N
62
Util.appendChildElement(data, viewEl, rootElementsOrder);
63         }
64         Element JavaDoc itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); // NOI18N
65
if (itemsEl == null) {
66             itemsEl = doc.createElementNS(Util.NAMESPACE, "items"); // NOI18N
67
Util.appendChildElement(viewEl, itemsEl, viewElementsOrder);
68         }
69
70         Iterator JavaDoc it1 = sources.iterator();
71         while (it1.hasNext()) {
72             String JavaDoc path = (String JavaDoc)it1.next();
73             assert it1.hasNext();
74             String JavaDoc dispname = (String JavaDoc)it1.next();
75             Element JavaDoc sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); // NOI18N
76
Element JavaDoc el = doc.createElementNS(Util.NAMESPACE, "label"); // NOI18N
77
el.appendChild(doc.createTextNode(dispname));
78             sourceFolderEl.appendChild(el);
79             el = doc.createElementNS(Util.NAMESPACE, "type"); // NOI18N
80
el.appendChild(doc.createTextNode("configFilesRoot"));
81             sourceFolderEl.appendChild(el);
82             el = doc.createElementNS(Util.NAMESPACE, "location"); // NOI18N
83
el.appendChild(doc.createTextNode(path));
84             sourceFolderEl.appendChild(el);
85             Util.appendChildElement(foldersEl, sourceFolderEl, folderElementsOrder);
86             
87             addSourceFolderViewItem(doc, itemsEl, EJBProjectNature.STYLE_CONFIG_FILES, "Configuration Files", path);
88         }
89         
90         Util.putPrimaryConfigurationData(helper, data);
91     }
92     
93     // #82897: putting Enterprise Beans node to view section needs to be done separately
94
public static void putEJBNodeView(AntProjectHelper helper, List JavaDoc/*<String>*/ sources) {
95         // TODO: ma154696: add support for multiple source roots?
96
// now I get only first one, have to check impact of more roots
97
// to Enterprise beans node (which one to put into view items
98
Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
99         Document JavaDoc doc = data.getOwnerDocument();
100         Element JavaDoc foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); // NOI18N
101
Element JavaDoc viewEl = Util.findElement(data, "view", Util.NAMESPACE); // NOI18N
102
Element JavaDoc itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); // NOI18N
103
List JavaDoc sourceRootNames = getSourceFolders(doc, foldersEl, "java"); // NOI18N
104
if (sourceRootNames.size() > 0) {
105             addSourceFolderViewItem(doc, itemsEl, EJBProjectNature.STYLE_EJBS, "Enterprise Beans", (String JavaDoc) sourceRootNames.get(0)); // NOI18N
106
}
107         Util.putPrimaryConfigurationData(helper, data);
108     }
109     
110     /**
111      * Adds source-folder element in view items in project.xml as the first child element
112      * @param doc document to write to
113      * @param itemsEl items element in project.xml
114      * @param style a view style; will be one of {@link ProjectNature#getSourceFolderViewStyles}
115      * @param label name for the node in logical view (mostly ignored)
116      * @param path path to source folder defined by displayed node
117      */

118     private static void addSourceFolderViewItem(Document JavaDoc doc, Element JavaDoc itemsEl, String JavaDoc style, String JavaDoc label, String JavaDoc path) {
119         // creates source-folder element for
120
Element JavaDoc sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); // NOI18N
121
sourceFolderEl.setAttribute("style", style); // NOI18N
122
Element JavaDoc el = doc.createElementNS(Util.NAMESPACE, "label"); // NOI18N
123
el.appendChild(doc.createTextNode(label));
124         sourceFolderEl.appendChild(el);
125         el = doc.createElementNS(Util.NAMESPACE, "location"); // NOI18N
126
el.appendChild(doc.createTextNode(path));
127         sourceFolderEl.appendChild(el);
128         itemsEl.insertBefore(sourceFolderEl, itemsEl.getFirstChild());
129     }
130     
131     /**
132      * Creates property entry in project.xml.
133      * @param doc document to write to
134      * @param parent parent element
135      * @param key property name
136      * @param value property value
137      */

138     private static void putProperty(Document JavaDoc doc, Element JavaDoc parent, String JavaDoc key, String JavaDoc value) {
139         // TODO: ma154696: check NodeList length
140
Element JavaDoc props = Util.findElement(parent, "properties", Util.NAMESPACE); // NOI18N
141
if (props == null) {
142             // create the <properties> element if it doesn't exist, which it should (#56344)
143
props = doc.createElementNS(Util.NAMESPACE, "properties");
144             Util.appendChildElement(parent, props, rootElementsOrder);
145         }
146         Element JavaDoc property = findPropertyElement(props, key);
147         if (property == null) {
148             property = doc.createElementNS(Util.NAMESPACE, "property"); // NOI18N
149
property.setAttribute("name", key); // NOI18N
150
props.appendChild(property);
151         } else {
152             while (property.getFirstChild() != null)
153                 property.removeChild(property.getFirstChild());
154         }
155         property.appendChild(doc.createTextNode(value));
156     }
157     
158     private static Element JavaDoc findPropertyElement(Element JavaDoc parent, String JavaDoc key) {
159         for (Iterator JavaDoc i = Util.findSubElements(parent).iterator(); i.hasNext();) {
160             Element JavaDoc element = (Element JavaDoc)i.next();
161             if (element.getLocalName().equals("property") && element.getNamespaceURI().equals(Util.NAMESPACE)) { // NOI18N
162
if (element.getAttribute("name").equals(key)) // NOI18N
163
return element;
164             }
165         }
166         return null;
167     }
168     
169     public static void putResourceFolder(AntProjectHelper helper, List JavaDoc/*<String>*/ resources) {
170         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
171         Document JavaDoc doc = data.getOwnerDocument();
172         String JavaDoc value = (String JavaDoc)resources.get(0) != null ? (String JavaDoc)resources.get(0) : ""; // NOI18N
173
putProperty(doc, data, EjbFreeformProperties.RESOURCE_DIR, value);
174         Util.putPrimaryConfigurationData(helper, data);
175     }
176     
177     public static void putServerInstanceID(AntProjectHelper helper, String JavaDoc instanceID) {
178         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
179         Document JavaDoc doc = data.getOwnerDocument();
180         putProperty(doc, data, EjbFreeformProperties.J2EE_SERVER_INSTANCE, instanceID);
181         Util.putPrimaryConfigurationData(helper, data);
182     }
183     
184     public static void putServerID(AntProjectHelper helper, String JavaDoc serverID) {
185         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
186         Document JavaDoc doc = data.getOwnerDocument();
187         putProperty(doc, data, EjbFreeformProperties.J2EE_SERVER_TYPE, serverID);
188         Util.putPrimaryConfigurationData(helper, data);
189     }
190     
191     public static void putJ2EELevel(AntProjectHelper helper, String JavaDoc j2eeLevel) {
192         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
193         Document JavaDoc doc = data.getOwnerDocument();
194         putProperty(doc, data, EjbFreeformProperties.J2EE_PLATFORM, j2eeLevel);
195         Util.putPrimaryConfigurationData(helper, data);
196     }
197     
198     private static List JavaDoc/*<String>*/ getSourceFolders(Document JavaDoc doc, Element JavaDoc parent, String JavaDoc type) {
199         // TODO: ma154696: add check if parent element's name is folder (or better refactor method interface)
200
List JavaDoc result = new ArrayList JavaDoc();
201         List JavaDoc sourceFolderElements = Util.findSubElements(parent);
202         for (int i = 0; i < sourceFolderElements.size(); i++) {
203             Element JavaDoc subElement = (Element JavaDoc) sourceFolderElements.get(i);
204             Element JavaDoc locationEl = Util.findElement(subElement, "location", Util.NAMESPACE);
205             Element JavaDoc typeEl = Util.findElement(subElement, "type", Util.NAMESPACE);
206             if (typeEl != null) {
207                 if (typeEl.getChildNodes().item(0).getNodeValue().equals(type)) {
208                     result.add(locationEl.getChildNodes().item(0).getNodeValue());
209                 }
210             }
211         }
212         return result;
213     }
214     
215     /**
216      * Read EJB modules from the project.
217      * @param helper AntProjectHelper instance
218      * @param aux AuxiliaryConfiguration instance
219      * @return list of EJBModule instances
220      */

221     public static List JavaDoc/*<EJBModule>*/ getEJBmodules(
222             AntProjectHelper helper, AuxiliaryConfiguration aux) {
223         //assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
224
ArrayList JavaDoc list = new ArrayList JavaDoc();
225         Element JavaDoc data = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB_2, true);
226         if (data == null) {
227             data = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB, true);
228         }
229         if (data == null) {
230             return list;
231         }
232         
233         List JavaDoc/*<Element>*/ wms = Util.findSubElements(data);
234         Iterator JavaDoc it = wms.iterator();
235         while (it.hasNext()) {
236             Element JavaDoc wmEl = (Element JavaDoc)it.next();
237             EJBModule wm = new EJBModule();
238             Iterator JavaDoc it2 = Util.findSubElements(wmEl).iterator();
239             while (it2.hasNext()) {
240                 Element JavaDoc el = (Element JavaDoc)it2.next();
241                 if (el.getLocalName().equals("config-files")) { // NOI18N
242
wm.configFiles = Util.findText(el);
243                     continue;
244                 }
245                 if (el.getLocalName().equals("classpath")) { // NOI18N
246
wm.classpath = Util.findText(el);
247                     continue;
248                 }
249                 if (el.getLocalName().equals("j2ee-spec-level")) { // NOI18N
250
wm.j2eeSpecLevel = Util.findText(el);
251                 }
252             }
253             list.add(wm);
254         }
255         return list;
256     }
257     
258     /**
259      * Update EJB modules of the project. Project is left modified
260      * and you must save it explicitely.
261      * @param helper AntProjectHelper instance
262      * @param aux AuxiliaryConfiguration instance
263      * @param ejbModules list of EJBModule instances
264      */

265     public static void putEJBModules(AntProjectHelper helper,
266             AuxiliaryConfiguration aux, List JavaDoc/*<EJBModule>*/ ejbModules) {
267         //assert ProjectManager.mutex().isWriteAccess();
268

269         // This /1 vs. /2 logic is mostly copied from JavaProjectGenerator.java
270
boolean need2 = false;
271         for (Iterator JavaDoc iter = ejbModules.iterator(); iter.hasNext(); ) {
272             EJBModule em = (EJBModule) iter.next();
273             if (em.j2eeSpecLevel.equals("1.5")) {
274                 need2 = true;
275                 break;
276             }
277         }
278         String JavaDoc namespace;
279         // Look for existing /2 data.
280
Element JavaDoc data = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB_2, true); // NOI18N
281
if (data != null) {
282             // if there is one, use it
283
namespace = EJBProjectNature.NS_EJB_2;
284         } else {
285             // Or, for existing /1 data.
286
namespace = need2 ? EJBProjectNature.NS_EJB_2 : EJBProjectNature.NS_EJB;
287             data = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB, true);
288             if (data != null) {
289                 if (need2) {
290                     // Have to upgrade.
291
aux.removeConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB, true);
292                     data = Util.getPrimaryConfigurationData(helper).getOwnerDocument().
293                             createElementNS(EJBProjectNature.NS_EJB_2, EJBProjectNature.EL_EJB);
294                 } // else can use it as is
295
} else {
296                 // Create /1 or /2 data acc. to need.
297
data = Util.getPrimaryConfigurationData(helper).getOwnerDocument().
298                     createElementNS(namespace, EJBProjectNature.EL_EJB);
299             }
300         }
301         
302         Document JavaDoc doc = data.getOwnerDocument();
303         List JavaDoc wms = Util.findSubElements(data);
304         Iterator JavaDoc it = wms.iterator();
305         while (it.hasNext()) {
306             Element JavaDoc wmEl = (Element JavaDoc)it.next();
307             data.removeChild(wmEl);
308         }
309         Iterator JavaDoc it2 = ejbModules.iterator();
310         while (it2.hasNext()) {
311             Element JavaDoc wmEl = doc.createElementNS(namespace, "ejb-module"); // NOI18N
312
data.appendChild(wmEl);
313             EJBModule wm = (EJBModule)it2.next();
314             Element JavaDoc el;
315             if (wm.configFiles != null) {
316                 el = doc.createElementNS(namespace, "config-files"); // NOI18N
317
el.appendChild(doc.createTextNode(wm.configFiles));
318                 wmEl.appendChild(el);
319             }
320             if (wm.classpath != null) {
321                 el = doc.createElementNS(namespace, "classpath"); // NOI18N
322
el.appendChild(doc.createTextNode(wm.classpath));
323                 wmEl.appendChild(el);
324             }
325             if (wm.j2eeSpecLevel != null) {
326                 el = doc.createElementNS(namespace, "j2ee-spec-level"); // NOI18N
327
el.appendChild(doc.createTextNode(wm.j2eeSpecLevel));
328                 wmEl.appendChild(el);
329             }
330         }
331         aux.putConfigurationFragment(data, true);
332     }
333     
334     /**
335      * Structure describing EJB module.
336      * Data in the struct are in the same format as they are stored in XML.
337      */

338     public static final class EJBModule {
339         public String JavaDoc configFiles;
340         public String JavaDoc classpath;
341         public String JavaDoc j2eeSpecLevel;
342     }
343     
344 }
345
Popular Tags