1 57 59 import net.sf.saxon.om.NamespaceConstant; 60 import net.sf.saxon.xpath.XPathEvaluator; 61 import org.jdom.input.SAXBuilder; 62 import org.w3c.dom.Document ; 63 import org.xml.sax.InputSource ; 64 65 import javax.xml.namespace.NamespaceContext ; 66 import javax.xml.namespace.QName ; 67 import javax.xml.parsers.DocumentBuilderFactory ; 68 import javax.xml.transform.OutputKeys ; 69 import javax.xml.transform.Transformer ; 70 import javax.xml.transform.TransformerFactory ; 71 import javax.xml.transform.stream.StreamSource ; 72 import javax.xml.xpath.*; 73 import java.io.File ; 74 import java.io.FileInputStream ; 75 import java.util.Iterator ; 76 import java.util.List ; 77 78 79 101 public class ApplyXPathJAXP { 102 protected String filename = null; 103 protected String xpathExpressionStr = null; 104 protected String objectModel = null; 105 106 109 public void doMain(String [] args) 110 throws Exception { 111 filename = args[0]; 112 xpathExpressionStr = args[1]; 113 String om = "saxon"; 114 if (args.length > 2) { 115 om = args[2]; 116 } 117 if (om.equals("saxon")) { 118 objectModel = NamespaceConstant.OBJECT_MODEL_SAXON; 119 } else if (om.equals("dom")) { 120 objectModel = XPathConstants.DOM_OBJECT_MODEL; 121 } else if (om.equals("jdom")) { 122 objectModel = NamespaceConstant.OBJECT_MODEL_JDOM; 123 } else if (om.equals("xom")) { 124 objectModel = NamespaceConstant.OBJECT_MODEL_XOM; 125 } else { 126 System.err.println("Unknown object model " + args[2]); 127 return; 128 } 129 130 if ((filename != null) && (filename.length() > 0) 131 && (xpathExpressionStr != null) && (xpathExpressionStr.length() > 0)) { 132 System.out.println("Loading classes, parsing " + filename + ", and setting up serializer"); 136 137 Object document = null; 138 if (objectModel.equals(NamespaceConstant.OBJECT_MODEL_SAXON)) { 139 document = new XPathEvaluator().setSource(new StreamSource (new File (filename))); 140 141 } else if (objectModel.equals(XPathConstants.DOM_OBJECT_MODEL)) { 142 InputSource in = new InputSource (new FileInputStream (filename)); 143 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 144 dfactory.setNamespaceAware(true); 145 Document doc = dfactory.newDocumentBuilder().parse(in); 146 document = doc; 147 148 } else if (objectModel.equals(NamespaceConstant.OBJECT_MODEL_JDOM)) { 149 InputSource in = new InputSource (new FileInputStream (filename)); 150 SAXBuilder builder = new SAXBuilder(); 151 document = builder.build(in); 152 153 } else if (objectModel.equals(NamespaceConstant.OBJECT_MODEL_XOM)) { 154 nu.xom.Builder builder = new nu.xom.Builder(); 155 document = builder.build(new FileInputStream (filename)); 156 } 157 158 Transformer serializer = TransformerFactory.newInstance().newTransformer(); 160 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 161 162 System.out.println("Querying " + om + " using " + xpathExpressionStr); 164 165 System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON, 167 "net.sf.saxon.xpath.XPathFactoryImpl"); 168 System.setProperty("javax.xml.xpath.XPathFactory:"+XPathConstants.DOM_OBJECT_MODEL, 169 "net.sf.saxon.xpath.XPathFactoryImpl"); 170 System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_JDOM, 171 "net.sf.saxon.xpath.XPathFactoryImpl"); 172 System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_XOM, 173 "net.sf.saxon.xpath.XPathFactoryImpl"); 174 175 XPathFactory xpathFactory = XPathFactory.newInstance(objectModel); 178 XPath xpath = xpathFactory.newXPath(); 179 180 xpath.setNamespaceContext( 182 new NamespaceContext () { 183 public String getNamespaceURI(String s) { 184 if (s.equals("f")) { 185 return "http://localhost/f"; 186 } else { 187 return null; 188 } 189 } 190 191 public String getPrefix(String s) { return null; } 192 193 public Iterator getPrefixes(String s) { return null; } 194 } 195 ); 196 197 199 final QName f_var = new QName ("http://localhost/f", "pi"); 200 xpath.setXPathVariableResolver( 201 new XPathVariableResolver() { 202 public Object resolveVariable(QName qName) { 203 if (qName.equals(f_var)) { 204 return new Double (Math.PI); 205 } else { 206 return null; 207 } 208 } 209 } 210 ); 211 212 214 final XPathFunction sqrt = new XPathFunction() { 215 public Object evaluate(List list) throws XPathFunctionException { 216 Object arg = list.get(0); 217 if (!(arg instanceof Double )) { 218 throw new XPathFunctionException("f:sqrt() expects an xs:double argument"); 219 } 220 return new Double (Math.sqrt(((Double )arg).doubleValue())); 221 } 222 }; 223 224 final QName f_sqrt = new QName ("http://localhost/f", "sqrt"); 225 xpath.setXPathFunctionResolver( 226 new XPathFunctionResolver() { 227 public XPathFunction resolveFunction(QName qName, int arity) { 228 if (qName.equals(f_sqrt) && arity==1) { 229 return sqrt; 230 } else { 231 return null; 232 } 233 } 234 } 235 ); 236 237 238 XPathExpression xpathExpression = xpath.compile(xpathExpressionStr); 240 241 242 String resultString = xpathExpression.evaluate(document); 244 245 System.out.println("<output>"); 247 System.out.println("String Value => " + resultString); 248 System.out.println("</output>"); 249 } else { 250 System.out.println("Bad input args: " + filename + ", " + xpathExpressionStr); 251 } 252 } 253 254 255 258 public static void main(String [] args) 259 throws Exception { 260 if (args.length != 2 && args.length != 3) { 261 System.out.println("java ApplyXPathJAXP filename.xml xpath [saxon|dom|jdom|xom]\n" 262 + "Reads filename.xml and applies the xpath; prints the String value of result."); 263 return; 264 } 265 266 ApplyXPathJAXP app = new ApplyXPathJAXP(); 267 app.doMain(args); 268 } 269 270 } 272 | Popular Tags |