Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gov/lanl/Utility/xml/DomUtils.java


1   /*
2    * DomUtils.java
3    *
4    * Created on September 23, 2002, 2:21 PM
5    */
6   
7   package gov.lanl.Utility.xml;
8   
9   //import javax.xml.transform.dom.*;
10  
11  import org.w3c.dom.*;
12  
13  import java.io.*;
14  import java.util.ArrayList;
15  
16  import org.apache.xml.serialize.XMLSerializer;
17  import org.apache.xml.serialize.OutputFormat;
18  
19  /**
20   *
21   * @author  Manjunath
22   */
23  public class DomUtils implements java.io.Serializable {
24  
25  
26      private Document domTree;
27  
28  
29      /** Creates new DomUtils */
30      public DomUtils() {
31  
32      }
33  
34      public void setdomTree(Document domtree) {
35          domTree = domtree;
36      }
37  
38  
39      public Element getRootElement() {
40          return domTree.getDocumentElement();
41  
42      }
43  
44      /** returns first element on preorder traversal with matching attribname and value
45       * @param elementname containing the attribname
46       * @param attribname  attribute to be selected
47       * @param attribval  attributevalue to be matched
48       * @return Element
49       **/
50  
51      public Element getElementWithAttribute(String elementname, String attribname, String attribval) {
52  
53  
54          return getChildWithAttribute(getRootElement(), elementname, attribname, attribval);
55  
56  
57      }
58  
59      /**
60       * get the element that is a child of parentNode
61       * @param parentNode containing the child
62       * @param childname  name of element to be looked up
63       * @param attribname  name af attribute to be matched
64       * @param attribval  value of attribue to be matched
65       * @return Element
66       */
67  
68      public static Element getChildWithAttribute(Element parentNode, String childname, String attribname, String attribval) {
69  
70          NodeList childnode = parentNode.getElementsByTagName(childname);
71  
72  
73          for (int i = 0; i < childnode.getLength(); i++) {
74              // Get child node
75  
76              Element tempNode = (Element) childnode.item(i);
77  
78              boolean isnode = tempNode.hasAttribute(attribname);
79              if (isnode) {
80                  String tempAttribval = tempNode.getAttribute(attribname);
81                  if (tempAttribval.equals(attribval)) {
82                      return tempNode;
83                  }
84              }
85  
86  
87          }/* end for */
88  
89          return null;
90  
91  
92      }
93  
94      /**
95       * return the first element of child of parent node
96       * @param parentNode
97       * @param childname
98       * @return Element
99       */
100 
101 
102     public static Element getChildName(Element parentNode, String childname) {
103 
104         NodeList childnode = parentNode.getElementsByTagName(childname);
105 
106         if (childnode.getLength() > 0) {
107             return (Element) childnode.item(0);
108         } else
109             return null;
110     }
111 
112     /**
113      * Get a list of Elements that are children of the parent and with the given name
114      * @param parentNode
115      * @param childName
116      * @return
117      */
118     public static Element[] getChildrenNamed(Element parentNode, String childName) {
119         NodeList childnode = parentNode.getElementsByTagName(childName);
120 
121         if (childnode.getLength() > 0) {
122             Element[] nodes = new Element[childnode.getLength()];
123             for (int i = 0; i < nodes.length; i++) {
124                 nodes[i] = (Element) childnode.item(i);
125             }
126             return nodes;
127         }
128         return new Element[0];
129     }
130 
131     /**
132      * returns all nodes with elementname having matching attribute and value
133      * @param elementname
134      * @param attribname
135      * @param attribval
136      * @return
137      */
138     public Element[] getAllElementsWithAttribute(String elementname, String attribname, String attribval) {
139 
140 
141         Element root = getRootElement();
142         return getAllChildrenWithAttribute(root, elementname, attribname, attribval);
143 
144 
145     }
146 
147     /**
148      * returns all nodes of root with elementname having matching attribute and value
149      * @param root
150      * @param elementname
151      * @param attribname
152      * @param attribval
153      * @return
154      */
155     public Element[] getAllChildrenWithAttribute(Element root, String elementname, String attribname, String attribval) {
156 
157 
158         Element tempNode;
159         Element tempNodes[];
160 
161         NodeList tempElements = root.getElementsByTagName(elementname);
162         ArrayList v = new ArrayList();
163         //tempNodes=new Element[tempElements.getLength()];
164         for (int i = 0; i < tempElements.getLength(); i++) {
165             // Get child node
166 
167             tempNode = (Element) tempElements.item(i);
168 
169             boolean isnode = tempNode.hasAttribute(attribname);
170             if (isnode) {
171 
172                 String tempAttribval = tempNode.getAttribute(attribname);
173                 if (tempAttribval.equals(attribval)) v.add(tempNode);
174 
175             }
176 
177         }/* end for */
178         tempNodes = new Element[v.size()];
179         tempNodes = (Element[]) v.toArray(tempNodes);
180         return tempNodes;
181 
182     }
183 
184 
185     /**
186      * returns all nodes of root with elementname having matching attributename
187      * @param root
188      * @param elementname
189      * @param attribname
190      * @return
191      */
192     public Element[] getAllChildrenWithAttributeName(Element root, String elementname, String attribname) {
193 
194 
195         Element tempNode;
196         Element tempNodes[];
197 
198         NodeList tempElements = root.getElementsByTagName(elementname);
199         ArrayList v = new ArrayList();
200         // tempNodes=new Element[tempElements.getLength()];
201         for (int i = 0; i < tempElements.getLength(); i++) {
202             // Get child node
203 
204             tempNode = (Element) tempElements.item(i);
205 
206             boolean isnode = tempNode.hasAttribute(attribname);
207             if (isnode) {
208                 //String tempAttribval=tempNode.getAttribute(attribname);
209                 v.add(tempNode);
210                 //  tempNodes[++count]=tempNode;
211 
212             }
213 
214         }/* end for */
215         tempNodes = new Element[v.size()];
216         tempNodes = (Element[]) v.toArray(tempNodes);
217         return tempNodes;
218         /// if (count>-1)
219         //     return tempNodes;
220         // else
221         //    return null;
222 
223     }
224 
225     /**
226      * returns all nodes with elementname having matching attributenam
227      * @param elementname
228      * @param attribname
229      * @return
230      */
231     public Element[] getAllElementsWithAttributeName(String elementname, String attribname) {
232 
233         Element root = getRootElement();
234         return getAllChildrenWithAttributeName(root, elementname, attribname);
235 
236     }
237 
238     /**
239      * Serialize Content to String
240      */
241     public String toString() {
242         try {
243             OutputFormat format = new OutputFormat("xml", "UTF-8", true);
244 
245             ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
246             XMLSerializer serial = new XMLSerializer(byteArray, format);
247             serial.serialize(domTree);
248             //          fileout.write();
249             return byteArray.toString();
250         } catch (Exception e) {
251             // cat.error("byteArray serializer failed: ",e);
252             System.out.println("byteArray serializer failed: " + e);
253         }
254         return null;
255     }
256 }
257 
258