KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > strutsel > taglib > utils > DOMHelper


1 /*
2  * $Id: DOMHelper.java 54933 2004-10-16 17:04:52Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.strutsel.taglib.utils;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.xml.transform.TransformerException JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.xpath.CachedXPathAPI;
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.Text JavaDoc;
34
35
36 public class DOMHelper {
37
38     protected static final String JavaDoc spaces = " ";
39
40     private static Log log = LogFactory.getLog(DOMHelper.class);
41
42     public static String JavaDoc getNodeText(org.w3c.dom.Document JavaDoc document,
43                                      String JavaDoc xpath)
44                               throws TransformerException JavaDoc {
45
46         String JavaDoc result = null;
47
48         if (!xpath.endsWith("text()")) {
49
50             if (!xpath.endsWith("/"))
51                 xpath += "/";
52
53             xpath += "text()";
54         }
55
56         CachedXPathAPI xpathAPI = new CachedXPathAPI();
57         Node JavaDoc foundNode = xpathAPI.selectSingleNode(document, xpath);
58
59         if (foundNode == null)
60             result = "";
61         else if (foundNode.getNodeType() == Node.TEXT_NODE)
62             result = ((Text JavaDoc)foundNode).getData();
63
64         return (result);
65     }
66
67     public static void recordFoundAttributes(org.w3c.dom.Document JavaDoc document,
68                                              String JavaDoc xpath, Map JavaDoc map)
69                                       throws TransformerException JavaDoc {
70
71         CachedXPathAPI xpathAPI = new CachedXPathAPI();
72         Node JavaDoc foundNode = xpathAPI.selectSingleNode(document.getDocumentElement(),
73                                                    xpath);
74
75         if (foundNode != null) {
76
77             NamedNodeMap JavaDoc attrMap = foundNode.getAttributes();
78
79             for (int ctr = 0; ctr < attrMap.getLength(); ++ctr) {
80
81                 Attr JavaDoc attrNode = (Attr JavaDoc)attrMap.item(ctr);
82                 map.put(attrNode.getName(), attrNode.getValue());
83             }
84         }
85     }
86
87     public static boolean verifyAttributesPresent(Map JavaDoc attrMap,
88                                                   String JavaDoc[] attrNames,
89                                                   boolean allowOthers)
90                                            throws Exception JavaDoc {
91
92         boolean result = true;
93
94         if (attrNames != null) {
95
96             // First see if all of the expected attributes were actually found.
97
for (int ctr = 0; ctr < attrNames.length; ++ctr) {
98
99                 if (attrMap.get(attrNames[ctr]) == null) {
100                     result = false;
101                     throw new Exception JavaDoc("Expected attribute \"" +
102                                         attrNames[ctr] +
103                                         "\" was not found in the generated tag.");
104                 }
105             }
106
107             // Now, if no "extra" attributes are allowed, verify that all the
108
// attributes that were found were expected.
109
if (!allowOthers) {
110
111                 for (Iterator JavaDoc iter = attrMap.keySet().iterator();
112                      iter.hasNext();) {
113
114                     String JavaDoc key = (String JavaDoc)iter.next();
115                     boolean found = false;
116
117                     for (int ctr = 0; ctr < attrNames.length; ++ctr) {
118
119                         if (key.equals(attrNames[ctr])) {
120                             found = true;
121
122                             break;
123                         }
124                     }
125
126                     if (!found) {
127                         throw new Exception JavaDoc("Attribute \"" + key +
128                                             "\" was not " +
129                                             "an expected attribute in the " +
130                                             "generated tag.");
131                     }
132                 }
133             }
134         }
135
136         return (result);
137     }
138
139     public static boolean verifyAttributesNotPresent(Map JavaDoc attrMap,
140                                                      String JavaDoc[] attrNames)
141                                               throws Exception JavaDoc {
142
143         boolean result = true;
144
145         if (attrNames != null) {
146
147             for (int ctr = 0; ctr < attrNames.length; ++ctr) {
148
149                 if (attrMap.get(attrNames[ctr]) != null) {
150                     result = false;
151
152                     break;
153                 }
154             }
155         }
156
157         return (result);
158     }
159
160     public static void printNode(Node JavaDoc node) {
161         if (log.isDebugEnabled()) {
162             System.out.println("Node tree:");
163             printNode(node, 0);
164         }
165     }
166
167     public static void printNode(Node JavaDoc node, int level) {
168
169         if (node == null)
170             return;
171
172         String JavaDoc nodeName = node.getNodeName();
173         NodeList JavaDoc children = node.getChildNodes();
174
175         if (children != null) {
176
177             short nodeType = node.getNodeType();
178
179             if (nodeType == Node.TEXT_NODE) {
180
181                 String JavaDoc text = ((Text JavaDoc)node).getData();
182                 System.out.print(text);
183             } else {
184                 System.out.print(spaces.substring(0, level) + "<" + nodeName);
185
186                 NamedNodeMap JavaDoc nodeMap = node.getAttributes();
187                 if (nodeMap.getLength() > 0) {
188                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
189
190                     for (int ctr = 0; ctr < nodeMap.getLength(); ++ ctr) {
191                         Attr JavaDoc attrnode = (Attr JavaDoc) nodeMap.item(ctr);
192                         String JavaDoc name = attrnode.getName();
193                         String JavaDoc value = attrnode.getValue();
194
195                         sb.append(" " + name + "=\"" + value + "\"");
196                     }
197
198                     System.out.print(sb.toString());
199                 }
200                 
201                 System.out.println(">");
202             }
203             
204             for (int ctr = 0; ctr < children.getLength(); ++ctr) {
205
206                 Node JavaDoc child = children.item(ctr);
207                 printNode(child, level + 1);
208             }
209
210             if (nodeType != Node.TEXT_NODE)
211                 System.out.println(
212                         spaces.substring(0, level) + "</" + nodeName + ">");
213         } else
214             System.out.println(
215                     spaces.substring(0, level) + "<" + nodeName + "/>");
216     }
217 }
218
Popular Tags