Debian Codegraph @

net/sf/saxon/lib/OutputURIResolverWrapper.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.lib;
9
10 import net.sf.saxon.event.CloseNotifier;
11 import net.sf.saxon.event.PipelineConfiguration;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.s9api.Action;
15 import net.sf.saxon.serialize.SerializationProperties;
16 import net.sf.saxon.trans.UncheckedXPathException;
17 import net.sf.saxon.trans.XPathException;
18
19 import javax.xml.transform.Result;
20 import javax.xml.transform.TransformerException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25 /**
26 * This class is an implementation of the {@link ResultDocumentResolver} interface
27 * that wraps a supplied {@link OutputURIResolver}. It is provided to give backwards
28 * compatibility for applications that use the old {@code OutputURIResolver} interface.
29 */
30
31 public class OutputURIResolverWrapper implements ResultDocumentResolver {
32
33 private OutputURIResolver outputURIResolver;
34
35 public OutputURIResolverWrapper(OutputURIResolver resolver) {
36 this.outputURIResolver = resolver;
37 }
38
39 /**
40 * Saxon calls this method when an {@code xsl:result-document} instruction
41 * with an {@code href} attribute is evaluated
42 * @param context the dynamic evaluation context
43 * @param href the effective value of the href attribute
44 * @param baseUri the base output URI of the transformation (typically, the
45 * destination of the principal output
46 * @param properties
47 * @return a new instance of the {@code ResultDocumentInstance} class;
48 * which Saxon will then call for further information.
49 */
50
51 public Receiver resolve(
52 XPathContext context, String href, String baseUri, SerializationProperties properties)
53 throws XPathException {
54 OutputURIResolver r2 = outputURIResolver.newInstance();
55
56 try {
57 Result result = r2.resolve(href, baseUri);
58 Action onClose = () -> {
59 try {
60 r2.close(result);
61 } catch (TransformerException te) {
62 throw new UncheckedXPathException(XPathException.makeXPathException(te));
63 }
64 };
65 Receiver out;
66 if (result instanceof Receiver) {
67 out = (Receiver)result;
68 } else {
69 SerializerFactory factory = context.getConfiguration().getSerializerFactory();
70 PipelineConfiguration pipe = context.getController().makePipelineConfiguration();
71 pipe.setXPathContext(context);
72 out = factory.getReceiver(result, properties, pipe);
73 }
74 List<Action> actions = new ArrayList<>();
75 actions.add(onClose);
76 return new CloseNotifier(out, actions);
77 } catch (TransformerException e) {
78 throw XPathException.makeXPathException(e);
79 }
80 }
81
82 public OutputURIResolver getOutputURIResolver() {
83 return outputURIResolver;
84 }
85
86 }
87