/* * GetValue.java * * Created on 13 November 2006, 15:13 * go fetch some xpath value from * and xml file * */ package resolve; import net.sf.saxon.om.NamespaceConstant; import net.sf.saxon.om.NodeInfo; import net.sf.saxon.xpath.XPathEvaluator; import org.xml.sax.InputSource; import javax.xml.namespace.QName; import javax.xml.transform.sax.SAXSource; import javax.xml.xpath.*; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.util.Iterator; import java.util.List; /** * * @author dpawson */ public class GetValue implements XPathVariableResolver{ /** *Filename of input document - Set by constructor, read by all methods **/ private String filename=""; /** *The compiled source document **/ NodeInfo builtDoc=null; /** * XpathExpression to be used **/ //XPath xpe = null; XPath xpe=null; /** *Debug value - Higher value, more output **/ private int debug=6; /** Constructor Creates a new instance of GetValue * Set the filename as the working document * **/ public GetValue(String filename) { this.filename=filename; this.builtDoc = buildSrc(filename); } /** *Build the source document into Saxon specific format **/ public NodeInfo buildSrc(String inputDocument){ InputSource is=null;; //Following is specific to Saxon: should be in a properties file System.setProperty("javax.xml.xpath.XPathFactory:"+ NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl"); XPathFactory xpf =null; try { xpf= XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); } catch (XPathFactoryConfigurationException ex) { System.err.println("Unable to create Xpath factory. Quitting\n"+ ex.toString()); ex.printStackTrace(); } this.xpe = xpf.newXPath(); if (debug > 6){ System.err.println("Loaded xpath provider "+xpe.getClass()); } try { is = new InputSource(new File(inputDocument).toURL().toString()); } catch (MalformedURLException ex) { System.err.println("Input file exception" + ex.toString()); ex.printStackTrace(); System.exit(2); } SAXSource ss = new SAXSource(is); NodeInfo doc =null; try { doc = ((XPathEvaluator)xpe).setSource(ss); } catch (net.sf.saxon.trans.XPathException ex) { System.err.println("Xpath exception, quitting. \n"+ ex.toString()); ex.printStackTrace(); } return doc; } /** *Resolve the xpath expression against the input document **/ public String [] resolveExpression(String xpathExpression){ xpe.setXPathVariableResolver(this); XPathExpression exp=null; String [] res= null; List matchedLines=null; try { exp = xpe.compile(xpathExpression); } catch (XPathExpressionException ex) { System.err.println("Invalid xpath expression "+ xpathExpression); ex.printStackTrace(); } try { matchedLines = (List)exp.evaluate(builtDoc, XPathConstants.NODESET); if (debug > 2){ System.err.println(matchedLines.size()+" matches"); } } catch (XPathExpressionException ex) { System.err.println("Invalid xpath expression: "+ xpathExpression); ex.printStackTrace(); } if (matchedLines != null){ int i = 0; Iterator iter = matchedLines.iterator(); res = new String[matchedLines.size()]; NodeInfo item = null; while ( iter.hasNext()){ item = (NodeInfo)iter.next(); res[i]=item.getStringValue(); i++; } } return res; } /** * This class serves as a variable resolver. The only variable used is $word. * @param qName the name of the variable required * @return the current value of the variable */ public Object resolveVariable(QName qName) { if (qName.getLocalPart().equals("word")) { return ""; } else { return null; } } public static void main(String []args){ GetValue m = new GetValue("in.xml"); String exp="//attendee-list/name[1]"; String names [] = m.resolveExpression(exp); for (int i = 0; i < names.length; i++){ System.out.println( names[i]); } } }