KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > transform > elements > Description


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Element.java
26  *
27  * Created on August 4, 2003, 2:04 PM
28  */

29
30 package com.sun.enterprise.tools.upgrade.transform.elements;
31
32 /**
33  *
34  * @author prakash
35  */

36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40 import com.sun.enterprise.tools.upgrade.transform.ElementToObjectMapper;
41
42 public class Description extends BaseElement {
43     
44     /** Creates a new instance of Element */
45     public Description() {
46     }
47     /**
48      * element - description
49      * parentSource - parent of description
50      * parentResult - result element to which description to be updated
51      */

52     public void transform(Element JavaDoc element, Element JavaDoc parentSource, Element JavaDoc parentResult){
53         // description is updated if exists, else added if not exists.
54
NodeList JavaDoc resultDescs = parentResult.getElementsByTagName("description");
55         Element JavaDoc resultDesc = null;
56         if(resultDescs.getLength() == 0){
57             resultDesc = parentResult.getOwnerDocument().createElement("description");
58             Node JavaDoc textNode = parentResult.getOwnerDocument().createTextNode(this.getTextNodeData(element));
59             resultDesc.appendChild(textNode);
60             parentResult.appendChild(resultDesc);
61         }else {
62             resultDesc = (Element JavaDoc)resultDescs.item(0);
63             if((this.getTextNodeData(element)).equals(this.getTextNodeData((Element JavaDoc)resultDesc))){
64                 // If both are same there is nothing to be done, just break.
65
}else if(this.getTextNode(resultDesc) == null){
66                 Node JavaDoc textNode = parentResult.getOwnerDocument().createTextNode(this.getTextNodeData(element));
67                 resultDesc.appendChild(textNode);
68             }else{
69                 this.setTextNodeData(resultDesc, this.getTextNodeData(element));
70             }
71         }
72     }
73     private String JavaDoc getTextNodeData(Element JavaDoc element){
74         NodeList JavaDoc children = element.getChildNodes();
75         for(int index=0; index < children.getLength(); index++){
76             if(children.item(index).getNodeType() == Node.TEXT_NODE){
77                 return children.item(index).getNodeValue();
78             }
79         }
80         return "";
81     }
82     private Node JavaDoc getTextNode(Element JavaDoc element){
83         NodeList JavaDoc children = element.getChildNodes();
84         for(int index=0; index < children.getLength(); index++){
85             if(children.item(index).getNodeType() == Node.TEXT_NODE){
86                 return children.item(index);
87             }
88         }
89         return null;
90     }
91     private void setTextNodeData(Element JavaDoc element, String JavaDoc textNodeData){
92         NodeList JavaDoc children = element.getChildNodes();
93         for(int index=0; index < children.getLength(); index++){
94             if(children.item(index).getNodeType() == Node.TEXT_NODE){
95                 children.item(index).setNodeValue(textNodeData);
96                 return;
97             }
98         }
99     }
100     
101 }
102
Popular Tags