KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > dd > impl > DDTreeWalker


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 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * File: DDTreeWalker.java
21  * Date: August 2, 2005 10:33 AM
22  *
23  * @author Nitya Doraisamy
24  */

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 JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.HashSet JavaDoc;
36
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41
42 /**
43  *
44  * This is a scanner of DOM tree.
45  *
46  * Example:
47  * DDTreeWalker scanner = new DDTreeWalker (document);
48  * scanner.visitDocument();
49  *
50  *
51  * @see org.w3c.dom.Document
52  * @see org.w3c.dom.Element
53  * @see org.w3c.dom.NamedNodeMap
54  */

55 public class DDTreeWalker {
56     
57     Document JavaDoc document;
58     String JavaDoc downgradeVersion;
59     String JavaDoc currentVersion;
60     Transform transInfo;
61     String JavaDoc DATAFILE = "org/netbeans/modules/j2ee/sun/dd/impl/transform/transform.xml"; //NOI18N
62

63     /**
64      * Create new DDTreeWalker with org.w3c.dom.Document.
65      */

66     public DDTreeWalker(Document JavaDoc document, String JavaDoc version, String JavaDoc 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 JavaDoc modElementsList = new Vector JavaDoc();
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 JavaDoc modElementsList = new Vector JavaDoc();
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 JavaDoc modElementsList = new Vector JavaDoc();
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 JavaDoc updateModElementsList(Vector JavaDoc modElementsList, Xmltype type){
136         if(type != null){
137             ModElement[] elementsList = type.getModElement();
138             modElementsList.addAll(new HashSet JavaDoc(Arrays.asList(elementsList)));
139         }
140         return modElementsList;
141     }
142     
143     private void processDocument(Vector JavaDoc elements){
144         Element JavaDoc element = document.getDocumentElement();
145         visitElement(element, elements);
146     }
147     
148     private void visitElement(Element JavaDoc element, Vector JavaDoc elements) {
149         walkElement(element, elements);
150         NodeList JavaDoc nodes = element.getChildNodes();
151         for (int i = 0; i < nodes.getLength(); i++) {
152             Node JavaDoc node = nodes.item(i);
153             switch (node.getNodeType()) {
154                 case Node.ELEMENT_NODE:
155                     Element JavaDoc nodeElement = (Element JavaDoc)node;
156                     String JavaDoc nodeElementName = nodeElement.getTagName();
157                     walkElement(nodeElement, elements);
158                     visitElement(nodeElement, elements);
159                     continue;
160             }
161         }
162     }
163     
164     private void walkElement(Element JavaDoc element, Vector JavaDoc elements){
165         Object JavaDoc[] 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 JavaDoc removeElement = subelements[l].getName();
176                     removeElement(element, removeElement);
177                 }
178             }
179         }//for
180
}
181     
182     private void removeAttribute(Element JavaDoc element, String JavaDoc attrName){
183         org.w3c.dom.NamedNodeMap JavaDoc attrs = element.getAttributes();
184         for (int in = 0; in < attrs.getLength(); in++) {
185             org.w3c.dom.Attr JavaDoc attr = (org.w3c.dom.Attr JavaDoc)attrs.item(in);
186             if (attr.getName().equals(attrName)) {
187                 element.removeAttributeNode(attr);
188             }
189         }
190     }
191     
192     private void removeElement(Element JavaDoc element, String JavaDoc elementName){
193         NodeList JavaDoc nodes = element.getChildNodes();
194         for (int i = 0; i < nodes.getLength(); i++) {
195             Node JavaDoc node = nodes.item(i);
196             switch (node.getNodeType()) {
197                 case Node.ELEMENT_NODE:
198                     Element JavaDoc nodeElement = (Element JavaDoc)node;
199                     if (nodeElement.getTagName().equals(elementName)) {
200                         element.removeChild(node);
201                     }
202                     break;
203             }
204         }//for
205
}
206     
207     public Xmltype getXmlType(Transform transformInfo, String JavaDoc 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 JavaDoc in = Transform.class.getClassLoader().getResourceAsStream(DATAFILE);
220             this.transInfo = Transform.createGraph(in);
221             in.close();
222         }catch(Exception JavaDoc ex){
223             //System.out.println("Unable to get transInfo");
224
}
225         return this.transInfo;
226     }
227     
228     
229 }
230
Popular Tags