1 17 package org.apache.excalibur.xml.xpath; 18 19 import javax.xml.transform.dom.DOMSource ; 20 import javax.xml.transform.TransformerFactory ; 21 22 import org.apache.avalon.framework.component.Component; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 25 import com.icl.saxon.Context; 26 import com.icl.saxon.TransformerFactoryImpl; 27 import com.icl.saxon.tinytree.TinyBuilder; 28 import com.icl.saxon.expr.Expression; 29 import com.icl.saxon.expr.StandaloneContext; 30 import com.icl.saxon.expr.Value; 31 import com.icl.saxon.expr.XPathException; 32 import com.icl.saxon.expr.NodeSetValue; 33 import com.icl.saxon.om.DocumentInfo; 34 import com.icl.saxon.om.NamePool; 35 import com.icl.saxon.om.NodeInfo; 36 import com.icl.saxon.om.NodeEnumeration; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 56 public class Saxon6ProcessorImpl 57 extends AbstractProcessorImpl 58 implements XPathProcessor, Component, ThreadSafe 59 { 60 private static final TransformerFactory factory = new TransformerFactoryImpl(); 61 62 70 public boolean evaluateAsBoolean(Node contextNode, String str, PrefixResolver resolver) 71 { 72 try 73 { 74 Value value = evaluate(contextNode, str, resolver); 75 if (value == null) 76 { 77 return false; 78 } 79 80 return value.asBoolean(); 81 } 82 catch (final Exception e) 83 { 84 if (getLogger().isDebugEnabled()) { 85 getLogger().debug("Failed to evaluate '" + str + "'", e); 86 } 87 88 return false; 90 } 91 } 92 93 101 public Number evaluateAsNumber(Node contextNode, String str, PrefixResolver resolver) 102 { 103 try 104 { 105 Value value = evaluate(contextNode, str, resolver); 106 if (value == null) 107 { 108 return null; 109 } 110 111 return new Double (value.asNumber()); 112 } 113 catch (final Exception e) 114 { 115 if (getLogger().isDebugEnabled()) { 116 getLogger().debug("Failed to evaluate '" + str + "'", e); 117 } 118 119 return null; 121 } 122 } 123 124 132 public String evaluateAsString(Node contextNode, String str, PrefixResolver resolver) { 133 try 134 { 135 Value value = evaluate(contextNode, str, resolver); 136 if (value == null) 137 { 138 return null; 139 } 140 141 return value.asString(); 142 } 143 catch (final Exception e) 144 { 145 if (getLogger().isDebugEnabled()) { 146 getLogger().debug("Failed to evaluate '" + str + "'", e); 147 } 148 149 return null; 151 } 152 } 153 154 public Node selectSingleNode(Node contextNode, String str, PrefixResolver resolver) 155 { 156 try 157 { 158 Value value = evaluate(contextNode, str, resolver); 159 if (value == null || value.getDataType() != Value.NODESET) 160 { 161 return null; 162 } 163 164 return (Node )((NodeSetValue)value).getFirst(); 165 } 166 catch (final Exception e) 167 { 168 if (getLogger().isDebugEnabled()) { 169 getLogger().debug("Failed to evaluate '" + str + "'", e); 170 } 171 172 return null; 174 } 175 } 176 177 public NodeList selectNodeList(Node contextNode, String str, PrefixResolver resolver) 178 { 179 try 180 { 181 Value value = evaluate(contextNode, str, resolver); 182 if (value.getDataType() != Value.NODESET) 183 { 184 return null; 185 } 186 187 NodeSetValue nodeset = (NodeSetValue)value; 188 NodeEnumeration enumeration = nodeset.enumerate(); 189 Node [] nodes = new Node [nodeset.getCount()]; 190 for (int i = 0; i < nodes.length; i++) 191 { 192 nodes[i] = (Node )enumeration.nextElement(); 193 } 194 195 return new NodeListImpl(nodes); 196 } catch (final Exception e) { 197 if (getLogger().isDebugEnabled()) { 198 getLogger().debug("Failed to evaluate '" + str + "'", e); 199 } 200 201 return null; 203 } 204 } 205 206 private Value evaluate(Node contextNode, String str, PrefixResolver resolver) 207 { 208 try 209 { 210 if (!(contextNode instanceof NodeInfo)) 211 { 212 getLogger().debug("Input tree is not SAXON TinyTree, converting"); 213 214 DOMSource source = new DOMSource (contextNode); 215 TinyBuilder result = new TinyBuilder(); 216 factory.newTransformer().transform(source, result); 217 contextNode = (Node )result.getCurrentDocument(); 218 } 219 220 DocumentInfo doc = ((NodeInfo)contextNode).getDocumentRoot(); 221 NamePool pool = doc.getNamePool(); 222 if (pool == null) 223 { 224 pool = NamePool.getDefaultNamePool(); 225 doc.setNamePool(pool); 226 } 227 Expression expression = Expression.make(str, new Saxon6Context(pool, resolver)); 228 229 Context context = new Context(); 230 context.setContextNode((NodeInfo)contextNode); 231 context.setPosition(1); 232 context.setLast(1); 233 234 return expression.evaluate(context); 235 } 236 catch (final Exception e) 237 { 238 if (getLogger().isDebugEnabled()) { 239 getLogger().debug("Failed to evaluate '" + str + "'", e); 240 } 241 242 return null; 244 } 245 } 246 247 private class Saxon6Context extends StandaloneContext 248 { 249 private final PrefixResolver resolver; 250 251 public Saxon6Context(NamePool namePool, PrefixResolver resolver) 252 { 253 super(namePool); 254 this.resolver = resolver; 255 } 256 257 public String getURIForPrefix(String prefix) throws XPathException 258 { 259 return resolver.prefixToNamespace(prefix); 260 } 261 } 262 } 263 | Popular Tags |