1 17 package org.apache.excalibur.xml.xpath; 18 19 import org.apache.avalon.framework.thread.ThreadSafe; 20 import org.apache.avalon.framework.component.Component; 21 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import net.sf.saxon.om.NodeInfo; 25 import net.sf.saxon.om.SequenceIterator; 26 import net.sf.saxon.om.Item; 27 import net.sf.saxon.xpath.XPathException; 28 import net.sf.saxon.xpath.StandaloneContext; 29 import net.sf.saxon.TransformerFactoryImpl; 30 import net.sf.saxon.expr.Expression; 31 import net.sf.saxon.expr.XPathContext; 32 import net.sf.saxon.expr.ExpressionTool; 33 import net.sf.saxon.tinytree.TinyBuilder; 34 import net.sf.saxon.value.Type; 35 import net.sf.saxon.value.BooleanValue; 36 import net.sf.saxon.value.DoubleValue; 37 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.dom.DOMSource ; 40 import java.util.ArrayList ; 41 42 58 public class Saxon7ProcessorImpl 59 extends AbstractProcessorImpl 60 implements XPathProcessor, Component, ThreadSafe 61 { 62 private static final TransformerFactory factory = new TransformerFactoryImpl(); 63 64 72 public boolean evaluateAsBoolean(Node contextNode, String str, PrefixResolver resolver) 73 { 74 try 75 { 76 Item item = evaluateSingle(contextNode, str, resolver); 77 if (item == null) 78 { 79 return false; 80 } 81 82 if (item.getItemType() == Type.BOOLEAN) 83 { 84 return ((BooleanValue)item).getValue(); 85 } 86 87 return Boolean.valueOf(item.getStringValue()).booleanValue(); 88 } 89 catch (final Exception e) 90 { 91 if (getLogger().isDebugEnabled()) { 92 getLogger().debug("Failed to evaluate '" + str + "'", e); 93 } 94 95 return false; 97 } 98 } 99 100 108 public Number evaluateAsNumber(Node contextNode, String str, PrefixResolver resolver) 109 { 110 try 111 { 112 Item item = evaluateSingle(contextNode, str, resolver); 113 if (item == null) 114 { 115 return null; 116 } 117 118 if (item.getItemType() == Type.NUMBER) 119 { 120 return new Double (((DoubleValue)item).getValue()); 121 } 122 123 return Double.valueOf(item.getStringValue()); 124 } 125 catch (final Exception e) 126 { 127 if (getLogger().isDebugEnabled()) { 128 getLogger().debug("Failed to evaluate '" + str + "'", e); 129 } 130 131 return null; 133 } 134 } 135 136 144 public String evaluateAsString(Node contextNode, String str, PrefixResolver resolver) { 145 try 146 { 147 Item item = evaluateSingle(contextNode, str, resolver); 148 if (item == null) 149 { 150 return null; 151 } 152 153 return item.getStringValue(); 154 } 155 catch (final Exception e) 156 { 157 if (getLogger().isDebugEnabled()) { 158 getLogger().debug("Failed to evaluate '" + str + "'", e); 159 } 160 161 return null; 163 } 164 } 165 166 public Node selectSingleNode(Node contextNode, String str, PrefixResolver resolver) 167 { 168 try 169 { 170 Item item = evaluateSingle(contextNode, str, resolver); 171 172 return (Node )item; 173 } 174 catch (final Exception e) 175 { 176 if (getLogger().isDebugEnabled()) { 177 getLogger().debug("Failed to evaluate '" + str + "'", e); 178 } 179 180 return null; 182 } 183 } 184 185 public NodeList selectNodeList(Node contextNode, String str, PrefixResolver resolver) 186 { 187 try 188 { 189 SequenceIterator iterator = evaluate(contextNode, str, resolver); 190 ArrayList nodes = new ArrayList (); 191 while (iterator.hasNext()) 192 { 193 Node node = (Node )iterator.current(); 194 nodes.add(node); 195 } 196 197 return new NodeListImpl((Node [])nodes.toArray()); 198 } catch (final Exception e) { 199 if (getLogger().isDebugEnabled()) { 200 getLogger().debug("Failed to evaluate '" + str + "'", e); 201 } 202 203 return null; 205 } 206 } 207 208 private Item evaluateSingle(Node contextNode, String str, PrefixResolver resolver) 209 { 210 try 211 { 212 SequenceIterator iterator = evaluate(contextNode, str, resolver); 213 if (iterator == null) 214 { 215 return null; 216 } 217 218 return iterator.current(); 219 } 220 catch (final Exception e) 221 { 222 if (getLogger().isDebugEnabled()) { 223 getLogger().debug("Failed to evaluate '" + str + "'", e); 224 } 225 226 return null; 228 } 229 } 230 231 private SequenceIterator evaluate(Node contextNode, String str, PrefixResolver resolver) 232 { 233 try 234 { 235 if (!(contextNode instanceof NodeInfo)) 236 { 237 getLogger().debug("Input tree is not SAXON TinyTree, converting"); 238 DOMSource source = new DOMSource (contextNode); 239 TinyBuilder result = new TinyBuilder(); 240 factory.newTransformer().transform(source, result); 241 contextNode = (Node )result.getCurrentDocument(); 242 } 243 244 Expression expression = ExpressionTool.make( 245 str, new Saxon7Context((NodeInfo)contextNode, resolver)); 246 XPathContext context = new XPathContext((NodeInfo)contextNode); 247 return expression.iterate(context); 248 } 249 catch (final Exception e) 250 { 251 if (getLogger().isDebugEnabled()) { 252 getLogger().debug("Failed to evaluate '" + str + "'", e); 253 } 254 255 return null; 257 } 258 } 259 260 261 private class Saxon7Context extends StandaloneContext 262 { 263 private final PrefixResolver resolver; 264 265 public Saxon7Context(NodeInfo node, PrefixResolver resolver) 266 { 267 super(node); 268 this.resolver = resolver; 269 } 270 271 public String getURIForPrefix(String prefix) throws XPathException 272 { 273 return resolver.prefixToNamespace(prefix); 274 } 275 } 276 } 277 | Popular Tags |