1 18 19 package org.apache.strutsel.taglib.utils; 20 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.xml.transform.TransformerException ; 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 ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.Text ; 34 35 36 public class DOMHelper { 37 38 protected static final String spaces = " "; 39 40 private static Log log = LogFactory.getLog(DOMHelper.class); 41 42 public static String getNodeText(org.w3c.dom.Document document, 43 String xpath) 44 throws TransformerException { 45 46 String 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 foundNode = xpathAPI.selectSingleNode(document, xpath); 58 59 if (foundNode == null) 60 result = ""; 61 else if (foundNode.getNodeType() == Node.TEXT_NODE) 62 result = ((Text )foundNode).getData(); 63 64 return (result); 65 } 66 67 public static void recordFoundAttributes(org.w3c.dom.Document document, 68 String xpath, Map map) 69 throws TransformerException { 70 71 CachedXPathAPI xpathAPI = new CachedXPathAPI(); 72 Node foundNode = xpathAPI.selectSingleNode(document.getDocumentElement(), 73 xpath); 74 75 if (foundNode != null) { 76 77 NamedNodeMap attrMap = foundNode.getAttributes(); 78 79 for (int ctr = 0; ctr < attrMap.getLength(); ++ctr) { 80 81 Attr attrNode = (Attr )attrMap.item(ctr); 82 map.put(attrNode.getName(), attrNode.getValue()); 83 } 84 } 85 } 86 87 public static boolean verifyAttributesPresent(Map attrMap, 88 String [] attrNames, 89 boolean allowOthers) 90 throws Exception { 91 92 boolean result = true; 93 94 if (attrNames != null) { 95 96 for (int ctr = 0; ctr < attrNames.length; ++ctr) { 98 99 if (attrMap.get(attrNames[ctr]) == null) { 100 result = false; 101 throw new Exception ("Expected attribute \"" + 102 attrNames[ctr] + 103 "\" was not found in the generated tag."); 104 } 105 } 106 107 if (!allowOthers) { 110 111 for (Iterator iter = attrMap.keySet().iterator(); 112 iter.hasNext();) { 113 114 String key = (String )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 ("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 attrMap, 140 String [] attrNames) 141 throws Exception { 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 node) { 161 if (log.isDebugEnabled()) { 162 System.out.println("Node tree:"); 163 printNode(node, 0); 164 } 165 } 166 167 public static void printNode(Node node, int level) { 168 169 if (node == null) 170 return; 171 172 String nodeName = node.getNodeName(); 173 NodeList children = node.getChildNodes(); 174 175 if (children != null) { 176 177 short nodeType = node.getNodeType(); 178 179 if (nodeType == Node.TEXT_NODE) { 180 181 String text = ((Text )node).getData(); 182 System.out.print(text); 183 } else { 184 System.out.print(spaces.substring(0, level) + "<" + nodeName); 185 186 NamedNodeMap nodeMap = node.getAttributes(); 187 if (nodeMap.getLength() > 0) { 188 StringBuffer sb = new StringBuffer (); 189 190 for (int ctr = 0; ctr < nodeMap.getLength(); ++ ctr) { 191 Attr attrnode = (Attr ) nodeMap.item(ctr); 192 String name = attrnode.getName(); 193 String 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 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 |