1 package net.sf.logdistiller.xml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.util.Iterator;
18 import java.util.NoSuchElementException;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22
23
24
25
26 public class DOMUtils
27 {
28 private DOMUtils()
29 {
30 }
31
32 public static String getPCDATAByTagName( Element elmt, String tagName )
33 {
34 return getPCDATAByTagName( elmt, tagName, false );
35 }
36
37 public static String getPCDATAByTagName( Element elmt, String tagName, boolean normalize )
38 {
39 Iterator iter = getChildElementsByTagName( elmt, tagName );
40 if ( !iter.hasNext() )
41 {
42 return null;
43 }
44 return getPCDATA( (Element) iter.next(), normalize );
45 }
46
47
48
49
50
51
52
53 public static String getPCDATA( Element element, boolean normalize )
54 {
55 if ( normalize )
56 {
57 element.normalize();
58 }
59 StringBuffer buff = new StringBuffer();
60 NodeList list = element.getChildNodes();
61 int len = list.getLength();
62 for ( int i = 0; i < len; i++ )
63 {
64 Node n = list.item( i );
65 switch ( n.getNodeType() )
66 {
67 case Node.TEXT_NODE:
68 case Node.CDATA_SECTION_NODE:
69 buff.append( n.getNodeValue() );
70 break;
71 case Node.ENTITY_REFERENCE_NODE:
72 buff.append( n.getFirstChild().getNodeValue() );
73 break;
74 case Node.COMMENT_NODE:
75 break;
76 default:
77 throw new IllegalArgumentException( "element '" + element.getNodeName()
78 + "' is not #PCDATA because it " + "contains a " + getNodeTypeAsString( n ) + " node" );
79 }
80 }
81 return buff.toString();
82 }
83
84 public static String getPCDATA( Element elmt )
85 {
86 return getPCDATA( elmt, false );
87 }
88
89 public static Iterator getChildElementsByTagName( Element element, String tagName )
90 {
91 return new ElementIterator( element.getChildNodes(), tagName );
92 }
93
94 public static String getNodeTypeAsString( Node node )
95 {
96 int type = node.getNodeType();
97 switch ( type )
98 {
99 case Node.ELEMENT_NODE:
100 return "Element";
101 case Node.ATTRIBUTE_NODE:
102 return "Attribute";
103 case Node.TEXT_NODE:
104 return "Text";
105 case Node.CDATA_SECTION_NODE:
106 return "CDATASection";
107 case Node.ENTITY_REFERENCE_NODE:
108 return "EntityReference";
109 case Node.ENTITY_NODE:
110 return "Entity";
111 case Node.PROCESSING_INSTRUCTION_NODE:
112 return "ProcessingInstruction";
113 case Node.COMMENT_NODE:
114 return "Comment";
115 case Node.DOCUMENT_NODE:
116 return "Document";
117 case Node.DOCUMENT_TYPE_NODE:
118 return "DocumentType";
119 case Node.DOCUMENT_FRAGMENT_NODE:
120 return "DocumentFragment";
121 case Node.NOTATION_NODE:
122 return "Notation";
123 default:
124 }
125 return "Unknown node type: " + type;
126 }
127
128 private static class ElementIterator
129 implements Iterator
130 {
131 private final NodeList nodeList;
132
133 private final String tagName;
134
135 private int index;
136
137 private Element next;
138
139 public ElementIterator( NodeList nodeList )
140 {
141 this( nodeList, null );
142 }
143
144 public ElementIterator( NodeList nodeList, String tagName )
145 {
146 this.nodeList = nodeList;
147 this.tagName = tagName;
148 findNext();
149 }
150
151 public boolean hasNext()
152 {
153 return ( next != null );
154 }
155
156 private void findNext()
157 {
158 while ( index < nodeList.getLength() )
159 {
160 Node node = nodeList.item( index );
161 index++;
162 if ( ( node.getNodeType() == Node.ELEMENT_NODE )
163 && ( ( tagName == null ) || node.getNodeName().equals( tagName ) ) )
164 {
165
166 next = (Element) node;
167 return;
168 }
169 }
170
171 next = null;
172 }
173
174 public Object next()
175 {
176 if ( next == null )
177 {
178 throw new NoSuchElementException();
179 }
180 Object returnValue = next;
181 findNext();
182 return returnValue;
183 }
184
185 public void remove()
186 {
187 throw new UnsupportedOperationException();
188 }
189 }
190 }