1 19 20 package org.netbeans.modules.web.freeform; 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.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 ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 34 39 public class WebProjectGenerator { 40 41 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"}; 45 private static final String [] folderElementsOrder = new String []{"source-folder", "build-folder"}; private static final String [] viewItemElementsOrder = new String []{"source-folder", "source-file"}; 49 private WebProjectGenerator() {} 50 51 54 public static void putWebSourceFolder(AntProjectHelper helper, List sources) { 55 Element data = Util.getPrimaryConfigurationData(helper); 56 Document doc = data.getOwnerDocument(); 57 Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); if (foldersEl == null) { 59 foldersEl = doc.createElementNS(Util.NAMESPACE, "folders"); Util.appendChildElement(data, foldersEl, rootElementsOrder); 61 } else { 62 List l = Util.findSubElements(foldersEl); 63 for (int i = 0; i < l.size(); i++) { 64 Element e = (Element ) l.get(i); 65 Element 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 viewEl = Util.findElement(data, "view", Util.NAMESPACE); if (viewEl == null) { 74 viewEl = doc.createElementNS(Util.NAMESPACE, "view"); Util.appendChildElement(data, viewEl, rootElementsOrder); 76 } 77 Element itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); if (itemsEl == null) { 79 itemsEl = doc.createElementNS(Util.NAMESPACE, "items"); Util.appendChildElement(viewEl, itemsEl, viewElementsOrder); 81 } else { 82 List l = Util.findSubElements(itemsEl); 83 for (int i = 0; i < l.size(); i++) { 84 Element e = (Element ) 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 it1 = sources.iterator(); 95 while (it1.hasNext()) { 96 String path = (String )it1.next(); 97 assert it1.hasNext(); 98 String dispname = (String )it1.next(); 99 Element sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); Element el = doc.createElementNS(Util.NAMESPACE, "label"); el.appendChild(doc.createTextNode(dispname)); 102 sourceFolderEl.appendChild(el); 103 el = doc.createElementNS(Util.NAMESPACE, "type"); el.appendChild(doc.createTextNode(WebProjectConstants.TYPE_DOC_ROOT)); 105 sourceFolderEl.appendChild(el); 106 el = doc.createElementNS(Util.NAMESPACE, "location"); el.appendChild(doc.createTextNode(path)); 108 sourceFolderEl.appendChild(el); 109 Util.appendChildElement(foldersEl, sourceFolderEl, folderElementsOrder); 110 111 sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); sourceFolderEl.setAttribute("style", "tree"); el = doc.createElementNS(Util.NAMESPACE, "label"); el.appendChild(doc.createTextNode(NbBundle.getMessage(WebProjectGenerator.class, "LBL_WebPages"))); 115 sourceFolderEl.appendChild(el); 116 el = doc.createElementNS(Util.NAMESPACE, "location"); el.appendChild(doc.createTextNode(path)); sourceFolderEl.appendChild(el); 119 Node 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 136 public static List getWebmodules ( 137 AntProjectHelper helper, AuxiliaryConfiguration aux) { 138 ArrayList list = new ArrayList (); 140 Element data = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); List wms = Util.findSubElements(data); 142 Iterator it = wms.iterator(); 143 while (it.hasNext()) { 144 Element wmEl = (Element )it.next(); 145 WebModule wm = new WebModule(); 146 Iterator it2 = Util.findSubElements(wmEl).iterator(); 147 while (it2.hasNext()) { 148 Element el = (Element )it2.next(); 149 if (el.getLocalName().equals("doc-root")) { wm.docRoot = Util.findText(el); 151 continue; 152 } 153 if (el.getLocalName().equals("classpath")) { wm.classpath = Util.findText(el); 155 continue; 156 } 157 if (el.getLocalName().equals("context-path")) { wm.contextPath = Util.findText(el); 159 continue; 160 } 161 if (el.getLocalName().equals("j2ee-spec-level")) { wm.j2eeSpecLevel = Util.findText(el); 163 } 164 } 165 list.add(wm); 166 } 167 return list; 168 } 169 170 177 public static void putWebModules(AntProjectHelper helper, 178 AuxiliaryConfiguration aux, List webModules) { 179 Element data = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); if (data == null) { 182 data = Util.getPrimaryConfigurationData(helper).getOwnerDocument(). 183 createElementNS(WebProjectNature.NS_WEB, "web-data"); } 185 Document doc = data.getOwnerDocument(); 186 List wms = Util.findSubElements(data); 187 Iterator it = wms.iterator(); 188 while (it.hasNext()) { 189 Element wmEl = (Element )it.next(); 190 data.removeChild(wmEl); 191 } 192 Iterator it2 = webModules.iterator(); 193 while (it2.hasNext()) { 194 Element wmEl = doc.createElementNS(WebProjectNature.NS_WEB, "web-module"); data.appendChild(wmEl); 196 WebModule wm = (WebModule)it2.next(); 197 Element el; 198 if (wm.docRoot != null) { 199 el = doc.createElementNS(WebProjectNature.NS_WEB, "doc-root"); el.appendChild(doc.createTextNode(wm.docRoot)); 201 wmEl.appendChild(el); 202 } 203 if (wm.classpath != null) { 204 el = doc.createElementNS(WebProjectNature.NS_WEB, "classpath"); 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"); 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"); el.appendChild(doc.createTextNode(wm.j2eeSpecLevel)); 216 wmEl.appendChild(el); 217 } 218 } 219 aux.putConfigurationFragment(data, true); 220 } 221 222 226 public static final class WebModule { 227 public String docRoot; 228 public String classpath; 229 public String contextPath; 230 public String j2eeSpecLevel; 231 } 232 233 } 234 | Popular Tags |