1 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.instruct.UserFunction; 5 import net.sf.saxon.om.*; 6 import net.sf.saxon.query.DynamicQueryContext; 7 import net.sf.saxon.query.QueryResult; 8 import net.sf.saxon.query.StaticQueryContext; 9 import net.sf.saxon.query.XQueryExpression; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.value.IntegerValue; 12 import net.sf.saxon.value.Value; 13 import net.sf.saxon.xpath.XPathEvaluator; 14 15 import javax.xml.transform.*; 16 import javax.xml.transform.dom.DOMResult ; 17 import javax.xml.transform.dom.DOMSource ; 18 import javax.xml.transform.stream.StreamResult ; 19 import javax.xml.transform.stream.StreamSource ; 20 import java.io.File ; 21 import java.io.FileReader ; 22 import java.io.IOException ; 23 import java.math.BigDecimal ; 24 import java.util.Properties ; 25 26 27 30 public class QueryAPIExamples { 31 32 35 private QueryAPIExamples() { 36 } 37 38 41 public static void main(String argv[]) { 42 43 String test = "all"; 44 if (argv.length > 0) { 45 test = argv[0]; 46 } 47 48 if (test.equals("all") || test.equals("toStreamResult")) { 49 System.out.println("\n\n==== toStreamResult ===="); 50 51 try { 52 exampleToStreamResult(); 53 } catch (XPathException ex) { 54 handleException(ex); 55 } 56 } 57 58 if (test.equals("all") || test.equals("toSingleton")) { 59 System.out.println("\n\n==== toSingleton ===="); 60 61 try { 62 exampleToSingleton(); 63 } catch (XPathException ex) { 64 handleException(ex); 65 } 66 } 67 68 if (test.equals("all") || test.equals("toDOM")) { 69 System.out.println("\n\n==== toDOM ===="); 70 71 try { 72 exampleToDOM(); 73 } catch (Exception ex) { 74 handleException(ex); 75 } 76 } 77 78 if (test.equals("all") || test.equals("toSequence")) { 79 System.out.println("\n\n==== toSequence ===="); 80 81 try { 82 exampleToSequence(); 83 } catch (XPathException ex) { 84 handleException(ex); 85 } 86 } 87 88 if (test.equals("all") || test.equals("toSerializedSequence")) { 89 System.out.println("\n\n==== toSerializedSequence ===="); 90 91 try { 92 exampleToSerializedSequence(); 93 } catch (XPathException ex) { 94 handleException(ex); 95 } 96 } 97 98 if (test.equals("all") || test.equals("toWrappedSequence")) { 99 System.out.println("\n\n==== toWrappedSequence ===="); 100 101 try { 102 exampleToWrappedSequence(); 103 } catch (XPathException ex) { 104 handleException(ex); 105 } 106 } 107 108 if (test.equals("all") || test.equals("toHTMLFile")) { 109 System.out.println("\n\n==== toHTMLFile ===="); 110 111 try { 112 exampleToHTMLFile(); 113 } catch (XPathException ex) { 114 handleException(ex); 115 } catch (IOException ex) { 116 System.err.println("Problem reading/writing files. Check that the current directory is the 'samples' directory"); 117 handleException(ex); 118 } 119 } 120 121 if (test.equals("all") || test.equals("withParam")) { 122 System.out.println("\n\n==== withParam ===="); 123 124 try { 125 exampleWithParam(); 126 } catch (XPathException ex) { 127 handleException(ex); 128 } 129 } 130 131 if (test.equals("all") || test.equals("pipeline")) { 132 System.out.println("\n\n==== pipeline ===="); 133 134 try { 135 examplePipeline(); 136 } catch (XPathException ex) { 137 handleException(ex); 138 } 139 } 140 141 if (test.equals("all") || test.equals("directFunction")) { 142 System.out.println("\n\n==== directFunction ===="); 143 144 try { 145 exampleDirectFunction(); 146 } catch (XPathException ex) { 147 handleException(ex); 148 } 149 } 150 151 152 } 153 154 158 159 public static void exampleToStreamResult() throws XPathException { 160 final Configuration config = new Configuration(); 161 final StaticQueryContext sqc = new StaticQueryContext(config); 162 final XQueryExpression exp = sqc.compileQuery("<a b='c'>{5+2}</a>"); 163 164 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 165 final Properties props = new Properties (); 166 props.setProperty(OutputKeys.METHOD, "xml"); 167 props.setProperty(OutputKeys.INDENT, "yes"); 168 exp.run(dynamicContext, new StreamResult (System.out), props); 169 } 170 171 175 176 public static void exampleToSingleton() throws XPathException { 177 final Configuration config = new Configuration(); 178 final StaticQueryContext sqc = new StaticQueryContext(config); 179 final XQueryExpression exp = sqc.compileQuery("avg(for $i in 1 to 10 return $i*$i)"); 180 181 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 182 final BigDecimal result = (BigDecimal )exp.evaluateSingle(dynamicContext); 183 System.out.println(result); 184 185 } 186 187 191 192 public static void exampleToDOM() throws TransformerException, TransformerConfigurationException { 193 final Configuration config = new Configuration(); 194 final StaticQueryContext sqc = new StaticQueryContext(config); 195 final XQueryExpression exp = sqc.compileQuery("<a xmlns='http://a/uri' xmlns:a='another.uri'>text</a>"); 196 197 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 198 DOMResult result = new DOMResult (); 199 exp.run(dynamicContext, result, new Properties ()); 200 201 203 Transformer identity = TransformerFactory.newInstance().newTransformer(); 204 identity.transform(new DOMSource (result.getNode()), new StreamResult (System.out)); 205 206 } 207 208 213 214 public static void exampleToSequence() throws XPathException { 215 final Configuration config = new Configuration(); 216 final StaticQueryContext sqc = new StaticQueryContext(config); 217 final XQueryExpression exp = sqc.compileQuery("for $i in 1 to 10 return ($i * $i)"); 218 219 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 220 final SequenceIterator iter = exp.iterator(dynamicContext); 221 while (true) { 222 Item item = iter.next(); 223 if (item == null) { 224 break; 225 } 226 System.out.println(item.getStringValue()); 227 } 228 229 } 230 231 236 237 public static void exampleToSerializedSequence() throws XPathException { 238 final Configuration config = new Configuration(); 239 final StaticQueryContext sqc = new StaticQueryContext(config); 240 final XQueryExpression exp = sqc.compileQuery("<doc><chap><a>3</a></chap></doc>//a, <b>4</b>, attribute c {5}, 19"); 241 Properties props = new Properties (); 242 props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 243 244 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 245 final SequenceIterator iter = exp.iterator(dynamicContext); 246 QueryResult.serializeSequence(iter, config, System.out, props); 247 } 248 249 255 256 public static void exampleToWrappedSequence() throws XPathException { 257 final Configuration config = new Configuration(); 258 final StaticQueryContext sqc = new StaticQueryContext(config); 259 final XQueryExpression exp = sqc.compileQuery("<doc><chap><a>3</a></chap></doc>//a, <b>4</b>, attribute c {5}, 19"); 260 Properties props = new Properties (); 261 props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 262 props.setProperty(OutputKeys.INDENT, "yes"); 263 264 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 265 final SequenceIterator iter = exp.iterator(dynamicContext); 266 final DocumentInfo doc = QueryResult.wrap(iter, config); 267 QueryResult.serialize(doc, new StreamResult (System.out), props, config); 268 } 269 270 271 276 277 public static void exampleToHTMLFile() throws XPathException, IOException { 278 final Configuration config = new Configuration(); 279 final StaticQueryContext sqc = new StaticQueryContext(config); 280 final XQueryExpression exp = sqc.compileQuery(new FileReader ("query/books-to-html.xq")); 281 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 282 dynamicContext.setContextNode(sqc.buildDocument(new StreamSource ("data/books.xml"))); 283 final Properties props = new Properties (); 284 props.setProperty(OutputKeys.METHOD, "html"); 285 props.setProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD HTML 4.01 Transitional//EN"); 286 exp.run(dynamicContext, new StreamResult (new File ("booklist.html")), props); 287 } 288 289 295 296 public static void exampleWithParam() throws XPathException { 297 final Configuration config = new Configuration(); 298 final StaticQueryContext sqc = new StaticQueryContext(config); 299 final XQueryExpression exp = sqc.compileQuery("declare variable $in as xs:integer external;" + 300 "$in * $in"); 301 302 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 303 dynamicContext.setParameter("in", new Long (17)); 304 final Long result = (Long )exp.evaluateSingle(dynamicContext); 305 System.out.println("17 * 17 = " + result); 306 307 } 308 309 313 314 public static void examplePipeline() throws XPathException { 315 final Configuration config = new Configuration(); 316 317 final StaticQueryContext sqc1 = new StaticQueryContext(config); 319 final XQueryExpression exp1 = sqc1.compileQuery("declare variable $in as xs:integer external;" + 320 "document{ <a>{$in * $in}</a> }"); 321 322 final StaticQueryContext sqc2 = new StaticQueryContext(config); 324 final XQueryExpression exp2 = sqc2.compileQuery("/a + 5"); 325 326 final DynamicQueryContext dynamicContext = new DynamicQueryContext(config); 328 dynamicContext.setParameter("in", new Long (3)); 329 final NodeInfo doc = (NodeInfo)exp1.evaluateSingle(dynamicContext); 330 331 dynamicContext.clearParameters(); 333 dynamicContext.setContextNode(doc); 334 final Object result = exp2.evaluateSingle(dynamicContext); 335 System.out.println("3*3 + 5 = " + result); 336 } 338 339 344 345 public static void exampleDirectFunction() throws XPathException { 346 final StaticQueryContext sqc = new StaticQueryContext(new Configuration()); 347 final XQueryExpression exp1 = sqc.compileQuery("declare namespace f='f.ns';" + 348 "declare function f:t1($v1 as xs:integer, $v2 as xdt:untypedAtomic*) { " + 349 " $v1 div $v2" + 350 "};" + 351 "10"); 352 353 final UserFunction fn1 = exp1.getStaticContext().getUserDefinedFunction("f.ns", "t1", 2); 354 final Controller controller = exp1.getController(); 355 final Value[] arglist = new Value[2]; 356 arglist[0] = new IntegerValue(10); 357 for (int i = 3; i < 10; i++) { 358 arglist[1] = new IntegerValue(i); 359 final ValueRepresentation result = fn1.call(arglist, controller); 360 System.out.println(arglist[0] + " div " + arglist[1] + " = " + result); 361 } 362 } 363 364 369 private static void handleException(Exception ex) { 370 System.out.println("EXCEPTION: " + ex); 371 ex.printStackTrace(); 372 } 373 374 } 375 376 | Popular Tags |