1 50 51 package org.openlaszlo.iv.flash.xml.apache; 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 57 import java.io.FileInputStream ; 58 import java.io.FileNotFoundException ; 59 import java.util.Properties ; 60 61 import org.apache.xerces.parsers.DOMParser; 62 import org.apache.xpath.*; 63 import org.apache.xml.utils.*; 64 import org.apache.xpath.objects.*; 65 import org.w3c.dom.*; 66 import org.w3c.dom.traversal.*; 67 import org.xml.sax.SAXException ; 68 import org.xml.sax.InputSource ; 69 70 import javax.xml.parsers.*; 71 import javax.xml.transform.*; 72 73 import org.openlaszlo.iv.flash.api.*; 74 import org.openlaszlo.iv.flash.url.*; 75 76 import org.openlaszlo.iv.flash.cache.*; 77 78 import org.xml.sax.EntityResolver ; 79 import org.xml.sax.InputSource ; 80 81 88 public class XPathHelper { 89 90 private static Hashtable xpaths = new Hashtable(); 91 private static Hashtable prefix_resolvers = new Hashtable(); 92 93 public synchronized static PrefixResolver getPrefixResolver( Node node ) { 94 if( prefix_resolvers.size() > 300 ) prefix_resolvers.clear(); 95 96 PrefixResolver pr = (PrefixResolver) prefix_resolvers.get(node); 97 if( pr == null ) { 98 pr = new PrefixResolverDefault( 103 (node.getNodeType() == Node.DOCUMENT_NODE)? 104 ((Document) node).getDocumentElement() : 105 node 106 ); 107 prefix_resolvers.put(node, pr); 108 } 109 110 return pr; 111 } 112 113 124 public synchronized static XPath getXPath( Node node, String expr ) throws TransformerException { 125 if( xpaths.size() > 300 ) xpaths.clear(); 126 127 XPath xpath = (XPath) xpaths.get( expr ); 128 if( xpath == null ) { 129 xpath = new XPath(expr, null, getPrefixResolver(node), XPath.SELECT, null); 130 xpaths.put( expr, xpath ); 131 } 132 return xpath; 133 } 134 135 144 public static XObject evalXPath( XPathContext xpathContext, Node node, String expr ) 145 throws TransformerException 146 { 147 return evalXPath( xpathContext, node, getXPath(node, expr) ); 148 } 149 150 159 public static XObject evalXPath( XPathContext xpathContext, Node node, XPath xpath ) 160 throws TransformerException 161 { 162 int ctxtNode = xpathContext.getDTMHandleFromNode(node); 165 166 return xpath.execute(xpathContext, ctxtNode, getPrefixResolver(node)); 167 } 168 169 178 public static NodeList selectNodeList( XPathContext xpathContext, Node node, String expr ) 179 throws TransformerException 180 { 181 return getNodeList( evalXPath(xpathContext, node, expr) ); 182 } 183 184 193 public static NodeList selectNodeList( XPathContext xpathContext, Node node, XPath xpath ) 194 throws TransformerException 195 { 196 return getNodeList( evalXPath(xpathContext, node, xpath) ); 197 } 198 199 208 public static Node selectSingleNode( XPathContext xpathContext, Node node, String expr ) 209 throws TransformerException 210 { 211 NodeList nl = selectNodeList(xpathContext, node, expr); 212 if( nl.getLength() > 0 ) return nl.item(0); 213 return null; 214 } 215 216 225 public static Node selectSingleNode( XPathContext xpathContext, Node node, XPath xpath ) 226 throws TransformerException 227 { 228 NodeList nl = selectNodeList(xpathContext, node, xpath); 229 if( nl.getLength() > 0 ) return nl.item(0); 230 return null; 231 } 232 233 240 public static NodeList getNodeList( XObject xo ) throws TransformerException { 241 return xo.nodelist(); 242 } 243 244 250 public static String getNodeData( Node node ) { 251 switch( node.getNodeType() ) { 252 case Node.DOCUMENT_FRAGMENT_NODE: 253 case Node.DOCUMENT_NODE: 254 case Node.ELEMENT_NODE: { 255 257 Node child = node.getFirstChild(); 258 if( child != null ) return getNodeData( child ); 259 } 260 break; 261 case Node.TEXT_NODE: 262 case Node.CDATA_SECTION_NODE: 263 return node.getNodeValue(); 264 case Node.ATTRIBUTE_NODE: 265 return node.getNodeValue(); 266 case Node.PROCESSING_INSTRUCTION_NODE : 267 break; 268 default: 269 break; 270 } 271 return ""; 272 } 273 274 public static String getXObjectData( XObject xo ) { 275 switch( xo.getType() ) { 276 case XObject.CLASS_UNKNOWN: 277 return null; 278 case XObject.CLASS_BOOLEAN: 279 case XObject.CLASS_STRING: 280 case XObject.CLASS_NULL: 281 case XObject.CLASS_NUMBER: 282 case XObject.CLASS_UNRESOLVEDVARIABLE: 283 case XObject.CLASS_RTREEFRAG: 284 return xo.toString(); 285 case XObject.CLASS_NODESET: 287 try { 288 NodeList nl = xo.nodelist(); 289 StringBuffer sb = new StringBuffer (); 290 int l = nl.getLength(); 291 if( l == 0 ) return null; 292 for( int i=0; i<l; i++ ) { 293 Node node = nl.item(i); 294 sb.append(getNodeData(node)); 295 } 296 return sb.toString(); 297 } catch( javax.xml.transform.TransformerException e ) { 298 } 299 } 300 return null; 301 } 303 304 public static void getNodeData(Node node, StringBuffer buf) { 305 switch( node.getNodeType() ) { 306 case Node.DOCUMENT_FRAGMENT_NODE: 307 case Node.DOCUMENT_NODE: 308 case Node.ELEMENT_NODE: 309 { 310 for( Node child = node.getFirstChild(); null != child; 311 child = child.getNextSibling() ) { 312 buf.append('<'); 313 buf.append(node.getNodeName()); 314 buf.append('>'); 315 getNodeData(child, buf); 316 buf.append("</"); 317 buf.append(node.getNodeName()); 318 buf.append('>'); 319 } 320 } 321 break; 322 case Node.TEXT_NODE : 323 case Node.CDATA_SECTION_NODE : 324 buf.append(node.getNodeValue()); 325 break; 326 case Node.ATTRIBUTE_NODE : 327 buf.append(node.getNodeValue()); 328 break; 329 case Node.PROCESSING_INSTRUCTION_NODE : 330 break; 332 default : 333 break; 335 } 336 } 337 } 338 339 | Popular Tags |