1 19 25 26 package org.netbeans.modules.j2ee.sun.dd.impl; 27 28 import org.netbeans.modules.j2ee.sun.dd.impl.transform.*; 29 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp; 30 import org.netbeans.modules.j2ee.sun.dd.api.ejb.SunEjbJar; 31 import org.netbeans.modules.j2ee.sun.dd.api.client.SunApplicationClient; 32 33 import java.util.Vector ; 34 import java.util.Arrays ; 35 import java.util.HashSet ; 36 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.NodeList ; 40 import org.w3c.dom.Node ; 41 42 55 public class DDTreeWalker { 56 57 Document document; 58 String downgradeVersion; 59 String currentVersion; 60 Transform transInfo; 61 String DATAFILE = "org/netbeans/modules/j2ee/sun/dd/impl/transform/transform.xml"; 63 66 public DDTreeWalker(Document document, String version, String currVersion) { 67 this.document = document; 68 this.downgradeVersion = version; 69 this.currentVersion = currVersion; 70 } 71 72 public void downgradeSunWebAppDocument(){ 73 this.transInfo = getTransformInfo(); 74 if(transInfo != null) { 75 Vector modElementsList = new Vector (); 76 Xmltype type = null; 77 if(currentVersion.equals(SunWebApp.VERSION_2_5_0)){ 78 type = getXmlType(transInfo, "sunWebApp41"); 79 modElementsList = updateModElementsList(modElementsList, type); 80 } 81 if(this.downgradeVersion.equals(SunWebApp.VERSION_2_4_0) || this.downgradeVersion.equals(SunWebApp.VERSION_2_3_0)){ 82 type = getXmlType(transInfo, "sunWebApp40"); 83 modElementsList = updateModElementsList(modElementsList, type); 84 } 85 if(this.downgradeVersion.equals(SunWebApp.VERSION_2_3_0)){ 86 type = getXmlType(transInfo, "sunWebApp30"); 87 modElementsList = updateModElementsList(modElementsList, type); 88 } 89 processDocument(modElementsList); 90 } 91 } 92 93 public void downgradeSunEjbJarDocument(){ 94 this.transInfo = getTransformInfo(); 95 if(transInfo != null) { 96 Vector modElementsList = new Vector (); 97 Xmltype type = null; 98 if(currentVersion.equals(SunEjbJar.VERSION_3_0_0)){ 99 type = getXmlType(transInfo, "sunEjb211"); 100 modElementsList = updateModElementsList(modElementsList, type); 101 } 102 if(this.downgradeVersion.equals(SunEjbJar.VERSION_2_1_0) || this.downgradeVersion.equals(SunEjbJar.VERSION_2_0_0)){ 103 type = getXmlType(transInfo, "sunEjb210"); 104 modElementsList = updateModElementsList(modElementsList, type); 105 } 106 if(this.downgradeVersion.equals(SunEjbJar.VERSION_2_0_0)){ 107 type = getXmlType(transInfo, "sunEjb200"); 108 modElementsList = updateModElementsList(modElementsList, type); 109 } 110 processDocument(modElementsList); 111 } 112 } 113 114 public void downgradeSunClientDocument(){ 115 this.transInfo = getTransformInfo(); 116 if(transInfo != null) { 117 Vector modElementsList = new Vector (); 118 Xmltype type = null; 119 if(currentVersion.equals(SunApplicationClient.VERSION_5_0_0)){ 120 type = getXmlType(transInfo, "sunClient41"); 121 modElementsList = updateModElementsList(modElementsList, type); 122 } 123 if(this.downgradeVersion.equals(SunApplicationClient.VERSION_1_4_0) || this.downgradeVersion.equals(SunApplicationClient.VERSION_1_3_0)){ 124 type = getXmlType(transInfo, "sunClient40"); 125 modElementsList = updateModElementsList(modElementsList, type); 126 } 127 if(this.downgradeVersion.equals(SunApplicationClient.VERSION_1_3_0)){ 128 type = getXmlType(transInfo, "sunClient30"); 129 modElementsList = updateModElementsList(modElementsList, type); 130 } 131 processDocument(modElementsList); 132 } 133 } 134 135 private Vector updateModElementsList(Vector modElementsList, Xmltype type){ 136 if(type != null){ 137 ModElement[] elementsList = type.getModElement(); 138 modElementsList.addAll(new HashSet (Arrays.asList(elementsList))); 139 } 140 return modElementsList; 141 } 142 143 private void processDocument(Vector elements){ 144 Element element = document.getDocumentElement(); 145 visitElement(element, elements); 146 } 147 148 private void visitElement(Element element, Vector elements) { 149 walkElement(element, elements); 150 NodeList nodes = element.getChildNodes(); 151 for (int i = 0; i < nodes.getLength(); i++) { 152 Node node = nodes.item(i); 153 switch (node.getNodeType()) { 154 case Node.ELEMENT_NODE: 155 Element nodeElement = (Element )node; 156 String nodeElementName = nodeElement.getTagName(); 157 walkElement(nodeElement, elements); 158 visitElement(nodeElement, elements); 159 continue; 160 } 161 } 162 } 163 164 private void walkElement(Element element, Vector elements){ 165 Object [] elementsList = elements.toArray(); 166 for(int i=0; i<elementsList.length; i++){ 167 ModElement eachElement = (ModElement)elementsList[i]; 168 if ((element != null) && element.getTagName().equals(eachElement.getName())) { 169 ModAttribute[] attrList = eachElement.getModAttribute(); 170 for(int j=0; j<attrList.length; j++){ 171 removeAttribute(element, attrList[j].getName()); 172 } 173 SubElement[] subelements = eachElement.getSubElement(); 174 for(int l=0; l<subelements.length; l++){ 175 String removeElement = subelements[l].getName(); 176 removeElement(element, removeElement); 177 } 178 } 179 } } 181 182 private void removeAttribute(Element element, String attrName){ 183 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 184 for (int in = 0; in < attrs.getLength(); in++) { 185 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(in); 186 if (attr.getName().equals(attrName)) { 187 element.removeAttributeNode(attr); 188 } 189 } 190 } 191 192 private void removeElement(Element element, String elementName){ 193 NodeList nodes = element.getChildNodes(); 194 for (int i = 0; i < nodes.getLength(); i++) { 195 Node node = nodes.item(i); 196 switch (node.getNodeType()) { 197 case Node.ELEMENT_NODE: 198 Element nodeElement = (Element )node; 199 if (nodeElement.getTagName().equals(elementName)) { 200 element.removeChild(node); 201 } 202 break; 203 } 204 } } 206 207 public Xmltype getXmlType(Transform transformInfo, String webAppVersion) { 208 Xmltype[] types = transformInfo.getXmltype(); 209 for (int i = 0; i < types.length; i++) { 210 if (types[i].getName().equals(webAppVersion)){ 211 return types[i]; 212 } 213 } 214 return null; 215 } 216 217 public Transform getTransformInfo(){ 218 try{ 219 java.io.InputStream in = Transform.class.getClassLoader().getResourceAsStream(DATAFILE); 220 this.transInfo = Transform.createGraph(in); 221 in.close(); 222 }catch(Exception ex){ 223 } 225 return this.transInfo; 226 } 227 228 229 } 230 | Popular Tags |