1 19 20 package org.netbeans.modules.j2ee.ejbfreeform; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 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 ; 29 import org.w3c.dom.Element ; 30 31 36 public class EJBProjectGenerator { 37 38 private static final String [] rootElementsOrder = new String []{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"}; private static final String [] viewElementsOrder = new String []{"items", "context-menu"}; 42 private static final String [] folderElementsOrder = new String []{"source-folder", "build-folder"}; private static final String [] viewItemElementsOrder = new String []{"source-folder", "source-file"}; 46 private EJBProjectGenerator() {} 47 48 51 public static void putEJBSourceFolder(AntProjectHelper helper, List sources) { 52 Element data = Util.getPrimaryConfigurationData(helper); 53 Document doc = data.getOwnerDocument(); 54 Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); if (foldersEl == null) { 56 foldersEl = doc.createElementNS(Util.NAMESPACE, "folders"); Util.appendChildElement(data, foldersEl, rootElementsOrder); 58 } 59 Element viewEl = Util.findElement(data, "view", Util.NAMESPACE); if (viewEl == null) { 61 viewEl = doc.createElementNS(Util.NAMESPACE, "view"); Util.appendChildElement(data, viewEl, rootElementsOrder); 63 } 64 Element itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); if (itemsEl == null) { 66 itemsEl = doc.createElementNS(Util.NAMESPACE, "items"); Util.appendChildElement(viewEl, itemsEl, viewElementsOrder); 68 } 69 70 Iterator it1 = sources.iterator(); 71 while (it1.hasNext()) { 72 String path = (String )it1.next(); 73 assert it1.hasNext(); 74 String dispname = (String )it1.next(); 75 Element sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); Element el = doc.createElementNS(Util.NAMESPACE, "label"); el.appendChild(doc.createTextNode(dispname)); 78 sourceFolderEl.appendChild(el); 79 el = doc.createElementNS(Util.NAMESPACE, "type"); el.appendChild(doc.createTextNode("configFilesRoot")); 81 sourceFolderEl.appendChild(el); 82 el = doc.createElementNS(Util.NAMESPACE, "location"); 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 public static void putEJBNodeView(AntProjectHelper helper, List sources) { 95 Element data = Util.getPrimaryConfigurationData(helper); 99 Document doc = data.getOwnerDocument(); 100 Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); Element viewEl = Util.findElement(data, "view", Util.NAMESPACE); Element itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); List sourceRootNames = getSourceFolders(doc, foldersEl, "java"); if (sourceRootNames.size() > 0) { 105 addSourceFolderViewItem(doc, itemsEl, EJBProjectNature.STYLE_EJBS, "Enterprise Beans", (String ) sourceRootNames.get(0)); } 107 Util.putPrimaryConfigurationData(helper, data); 108 } 109 110 118 private static void addSourceFolderViewItem(Document doc, Element itemsEl, String style, String label, String path) { 119 Element sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); sourceFolderEl.setAttribute("style", style); Element el = doc.createElementNS(Util.NAMESPACE, "label"); el.appendChild(doc.createTextNode(label)); 124 sourceFolderEl.appendChild(el); 125 el = doc.createElementNS(Util.NAMESPACE, "location"); el.appendChild(doc.createTextNode(path)); 127 sourceFolderEl.appendChild(el); 128 itemsEl.insertBefore(sourceFolderEl, itemsEl.getFirstChild()); 129 } 130 131 138 private static void putProperty(Document doc, Element parent, String key, String value) { 139 Element props = Util.findElement(parent, "properties", Util.NAMESPACE); if (props == null) { 142 props = doc.createElementNS(Util.NAMESPACE, "properties"); 144 Util.appendChildElement(parent, props, rootElementsOrder); 145 } 146 Element property = findPropertyElement(props, key); 147 if (property == null) { 148 property = doc.createElementNS(Util.NAMESPACE, "property"); property.setAttribute("name", key); 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 findPropertyElement(Element parent, String key) { 159 for (Iterator i = Util.findSubElements(parent).iterator(); i.hasNext();) { 160 Element element = (Element )i.next(); 161 if (element.getLocalName().equals("property") && element.getNamespaceURI().equals(Util.NAMESPACE)) { if (element.getAttribute("name").equals(key)) return element; 164 } 165 } 166 return null; 167 } 168 169 public static void putResourceFolder(AntProjectHelper helper, List resources) { 170 Element data = Util.getPrimaryConfigurationData(helper); 171 Document doc = data.getOwnerDocument(); 172 String value = (String )resources.get(0) != null ? (String )resources.get(0) : ""; putProperty(doc, data, EjbFreeformProperties.RESOURCE_DIR, value); 174 Util.putPrimaryConfigurationData(helper, data); 175 } 176 177 public static void putServerInstanceID(AntProjectHelper helper, String instanceID) { 178 Element data = Util.getPrimaryConfigurationData(helper); 179 Document 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 serverID) { 185 Element data = Util.getPrimaryConfigurationData(helper); 186 Document 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 j2eeLevel) { 192 Element data = Util.getPrimaryConfigurationData(helper); 193 Document doc = data.getOwnerDocument(); 194 putProperty(doc, data, EjbFreeformProperties.J2EE_PLATFORM, j2eeLevel); 195 Util.putPrimaryConfigurationData(helper, data); 196 } 197 198 private static List getSourceFolders(Document doc, Element parent, String type) { 199 List result = new ArrayList (); 201 List sourceFolderElements = Util.findSubElements(parent); 202 for (int i = 0; i < sourceFolderElements.size(); i++) { 203 Element subElement = (Element ) sourceFolderElements.get(i); 204 Element locationEl = Util.findElement(subElement, "location", Util.NAMESPACE); 205 Element 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 221 public static List getEJBmodules( 222 AntProjectHelper helper, AuxiliaryConfiguration aux) { 223 ArrayList list = new ArrayList (); 225 Element 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 wms = Util.findSubElements(data); 234 Iterator it = wms.iterator(); 235 while (it.hasNext()) { 236 Element wmEl = (Element )it.next(); 237 EJBModule wm = new EJBModule(); 238 Iterator it2 = Util.findSubElements(wmEl).iterator(); 239 while (it2.hasNext()) { 240 Element el = (Element )it2.next(); 241 if (el.getLocalName().equals("config-files")) { wm.configFiles = Util.findText(el); 243 continue; 244 } 245 if (el.getLocalName().equals("classpath")) { wm.classpath = Util.findText(el); 247 continue; 248 } 249 if (el.getLocalName().equals("j2ee-spec-level")) { wm.j2eeSpecLevel = Util.findText(el); 251 } 252 } 253 list.add(wm); 254 } 255 return list; 256 } 257 258 265 public static void putEJBModules(AntProjectHelper helper, 266 AuxiliaryConfiguration aux, List ejbModules) { 267 269 boolean need2 = false; 271 for (Iterator 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 namespace; 279 Element data = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB_2, true); if (data != null) { 282 namespace = EJBProjectNature.NS_EJB_2; 284 } else { 285 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 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 { 296 data = Util.getPrimaryConfigurationData(helper).getOwnerDocument(). 298 createElementNS(namespace, EJBProjectNature.EL_EJB); 299 } 300 } 301 302 Document doc = data.getOwnerDocument(); 303 List wms = Util.findSubElements(data); 304 Iterator it = wms.iterator(); 305 while (it.hasNext()) { 306 Element wmEl = (Element )it.next(); 307 data.removeChild(wmEl); 308 } 309 Iterator it2 = ejbModules.iterator(); 310 while (it2.hasNext()) { 311 Element wmEl = doc.createElementNS(namespace, "ejb-module"); data.appendChild(wmEl); 313 EJBModule wm = (EJBModule)it2.next(); 314 Element el; 315 if (wm.configFiles != null) { 316 el = doc.createElementNS(namespace, "config-files"); el.appendChild(doc.createTextNode(wm.configFiles)); 318 wmEl.appendChild(el); 319 } 320 if (wm.classpath != null) { 321 el = doc.createElementNS(namespace, "classpath"); el.appendChild(doc.createTextNode(wm.classpath)); 323 wmEl.appendChild(el); 324 } 325 if (wm.j2eeSpecLevel != null) { 326 el = doc.createElementNS(namespace, "j2ee-spec-level"); el.appendChild(doc.createTextNode(wm.j2eeSpecLevel)); 328 wmEl.appendChild(el); 329 } 330 } 331 aux.putConfigurationFragment(data, true); 332 } 333 334 338 public static final class EJBModule { 339 public String configFiles; 340 public String classpath; 341 public String j2eeSpecLevel; 342 } 343 344 } 345 | Popular Tags |