Permalink
Browse files

Fix issue #108 as suggested

  • Loading branch information...
ndw committed Jul 25, 2013
1 parent 84996dc commit 0d910cd591f484046c9f369a24bd621c5e411165
Showing with 77 additions and 72 deletions.
  1. +36 −34 src/com/xmlcalabash/library/XQuery.java
  2. +41 −38 src/com/xmlcalabash/library/XSLT.java
@@ -110,45 +110,47 @@ public void run() throws SaxonApiException {
config.setCollectionURIResolver(new CollectionResolver(runtime, defaultCollection, collectionResolver));
- Processor qtproc = runtime.getProcessor();
- XQueryCompiler xqcomp = qtproc.newXQueryCompiler();
- xqcomp.setBaseURI(root.getBaseURI());
- XQueryExecutable xqexec = xqcomp.compile(queryString);
- XQueryEvaluator xqeval = xqexec.load();
- if (document != null) {
- xqeval.setContextItem(document);
- }
-
- for (QName name : params.keySet()) {
- RuntimeValue v = params.get(name);
- if (runtime.getAllowGeneralExpressions()) {
- xqeval.setExternalVariable(name, v.getValue());
- } else {
- xqeval.setExternalVariable(name, new XdmAtomicValue(v.getString()));
+ try {
+ Processor qtproc = runtime.getProcessor();
+ XQueryCompiler xqcomp = qtproc.newXQueryCompiler();
+ xqcomp.setBaseURI(root.getBaseURI());
+ XQueryExecutable xqexec = xqcomp.compile(queryString);
+ XQueryEvaluator xqeval = xqexec.load();
+ if (document != null) {
+ xqeval.setContextItem(document);
}
- }
+ for (QName name : params.keySet()) {
+ RuntimeValue v = params.get(name);
+ if (runtime.getAllowGeneralExpressions()) {
+ xqeval.setExternalVariable(name, v.getValue());
+ } else {
+ xqeval.setExternalVariable(name, new XdmAtomicValue(v.getString()));
+ }
- Iterator<XdmItem> iter = xqeval.iterator();
- while (iter.hasNext()) {
- XdmItem item = iter.next();
- if (item.isAtomicValue()) {
- throw new XProcException(step.getNode(), "Not expecting atomic values back from XQuery!");
}
- XdmNode node = (XdmNode) item;
-
- if (node.getNodeKind() != XdmNodeKind.DOCUMENT) {
- // Make a document for this node...is this the right thing to do?
- TreeWriter treeWriter = new TreeWriter(runtime);
- treeWriter.startDocument(step.getNode().getBaseURI());
- treeWriter.addSubtree(node);
- treeWriter.endDocument();
- node = treeWriter.getResult();
+
+ Iterator<XdmItem> iter = xqeval.iterator();
+ while (iter.hasNext()) {
+ XdmItem item = iter.next();
+ if (item.isAtomicValue()) {
+ throw new XProcException(step.getNode(), "Not expecting atomic values back from XQuery!");
+ }
+ XdmNode node = (XdmNode) item;
+
+ if (node.getNodeKind() != XdmNodeKind.DOCUMENT) {
+ // Make a document for this node...is this the right thing to do?
+ TreeWriter treeWriter = new TreeWriter(runtime);
+ treeWriter.startDocument(step.getNode().getBaseURI());
+ treeWriter.addSubtree(node);
+ treeWriter.endDocument();
+ node = treeWriter.getResult();
+ }
+
+ result.write(node);
}
-
- result.write(node);
+ } finally {
+ config.setCollectionURIResolver(collectionResolver);
}
-
- config.setCollectionURIResolver(collectionResolver);
}
}
@@ -192,51 +192,54 @@ public void run() throws SaxonApiException {
config.setOutputURIResolver(new OutputResolver());
config.setCollectionURIResolver(new CollectionResolver(runtime, defaultCollection, collectionResolver));
- XsltCompiler compiler = runtime.getProcessor().newXsltCompiler();
- compiler.setSchemaAware(processor.isSchemaAware());
- XsltExecutable exec = compiler.compile(stylesheet.asSource());
- XsltTransformer transformer = exec.load();
-
- for (QName name : params.keySet()) {
- RuntimeValue v = params.get(name);
- if (runtime.getAllowGeneralExpressions()) {
- transformer.setParameter(name, v.getValue());
- } else {
- transformer.setParameter(name, new XdmAtomicValue(v.getString()));
- }
- }
+ XdmDestination result = null;
+ try {
+ XsltCompiler compiler = runtime.getProcessor().newXsltCompiler();
+ compiler.setSchemaAware(processor.isSchemaAware());
+ XsltExecutable exec = compiler.compile(stylesheet.asSource());
+ XsltTransformer transformer = exec.load();
- if (document != null) {
- transformer.setInitialContextNode(document);
- }
- transformer.setMessageListener(new CatchMessages());
- XdmDestination result = new XdmDestination();
- transformer.setDestination(result);
+ for (QName name : params.keySet()) {
+ RuntimeValue v = params.get(name);
+ if (runtime.getAllowGeneralExpressions()) {
+ transformer.setParameter(name, v.getValue());
+ } else {
+ transformer.setParameter(name, new XdmAtomicValue(v.getString()));
+ }
+ }
- if (initialMode != null) {
- transformer.setInitialMode(initialMode);
- }
+ if (document != null) {
+ transformer.setInitialContextNode(document);
+ }
+ transformer.setMessageListener(new CatchMessages());
+ result = new XdmDestination();
+ transformer.setDestination(result);
- if (templateName != null) {
- transformer.setInitialTemplate(templateName);
- }
+ if (initialMode != null) {
+ transformer.setInitialMode(initialMode);
+ }
- if (outputBaseURI != null) {
- transformer.setBaseOutputURI(outputBaseURI);
- // The following hack works around https://saxonica.plan.io/issues/1724
- try {
- result.setBaseURI(new URI(outputBaseURI));
- } catch (URISyntaxException use) {
- // whatever
+ if (templateName != null) {
+ transformer.setInitialTemplate(templateName);
}
- }
- transformer.setSchemaValidationMode(ValidationMode.DEFAULT);
- transformer.getUnderlyingController().setUnparsedTextURIResolver(unparsedTextURIResolver);
- transformer.transform();
+ if (outputBaseURI != null) {
+ transformer.setBaseOutputURI(outputBaseURI);
+ // The following hack works around https://saxonica.plan.io/issues/1724
+ try {
+ result.setBaseURI(new URI(outputBaseURI));
+ } catch (URISyntaxException use) {
+ // whatever
+ }
+ }
- config.setOutputURIResolver(uriResolver);
- config.setCollectionURIResolver(collectionResolver);
+ transformer.setSchemaValidationMode(ValidationMode.DEFAULT);
+ transformer.getUnderlyingController().setUnparsedTextURIResolver(unparsedTextURIResolver);
+ transformer.transform();
+ } finally {
+ config.setOutputURIResolver(uriResolver);
+ config.setCollectionURIResolver(collectionResolver);
+ }
XdmNode xformed = result.getXdmNode();

0 comments on commit 0d910cd

Please sign in to comment.