Debian Codegraph @

net/sf/saxon/ma/json/JsonDoc.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.ma.json;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.SystemFunction;
13 import net.sf.saxon.functions.UnparsedTextFunction;
14 import net.sf.saxon.ma.map.MapItem;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.trans.Err;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.value.EmptySequence;
20 import net.sf.saxon.z.IntSetPredicate;
21
22 import java.util.function.IntPredicate;
23
24 import java.io.Reader;
25 import java.net.URI;
26 import java.util.Map;
27
28 /**
29 * Implements the json-to-xml function defined in XSLT 3.0.
30 */
31
32 public class JsonDoc extends SystemFunction {
33
34 /**
35 * Evaluate the expression
36 *
37 * @param context the dynamic evaluation context
38 * @param arguments the values of the arguments, supplied as SequenceIterators
39 * @return the result of the evaluation, in the form of a SequenceIterator
40 * @throws net.sf.saxon.trans.XPathException
41 * if a dynamic error occurs during the evaluation of the expression
42 */
43 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
44
45 Item arg0 = arguments[0].head();
46 if (arg0 == null) {
47 return EmptySequence.getInstance();
48 }
49 String href = arg0.getStringValue();
50
51 final Configuration config = context.getConfiguration();
52 IntPredicate checker = IntSetPredicate.ALWAYS_TRUE; // allow non-XML characters - bug 3911
53
54 // Use the URI machinery to validate and resolve the URIs
55
56 URI absoluteURI = UnparsedTextFunction.getAbsoluteURI(href, getStaticBaseUriString(), context);
57
58 String encoding = "UTF-8"; // for now
59
60 Reader reader;
61 try {
62 reader = context.getController().getUnparsedTextURIResolver().resolve(absoluteURI, encoding, config);
63 } catch (XPathException err) {
64 err.maybeSetErrorCode("FOUT1170");
65 throw err;
66 }
67 CharSequence content;
68 try {
69 content = UnparsedTextFunction.readFile(checker, reader);
70 } catch (java.io.UnsupportedEncodingException encErr) {
71 XPathException e = new XPathException("Unknown encoding " + Err.wrap(encoding), encErr);
72 e.setErrorCode("FOUT1190");
73 throw e;
74 } catch (java.io.IOException ioErr) {
75 // System.err.println("ProxyHost: " + System.getProperty("http.proxyHost"));
76 // System.err.println("ProxyPort: " + System.getProperty("http.proxyPort"));
77 throw UnparsedTextFunction.handleIOError(absoluteURI, ioErr, context);
78 }
79
80 Map<String, Sequence<?>> checkedOptions;
81 if (getArity() == 2) {
82 MapItem options = (MapItem) arguments[1].head();
83 checkedOptions = getDetails().optionDetails.processSuppliedOptions(options, context);
84 } else {
85 checkedOptions = ParseJsonFn.OPTION_DETAILS.getDefaultOptions();
86 }
87 Item result = ParseJsonFn.parse(content.toString(), checkedOptions, context);
88 return result == null ? EmptySequence.getInstance() : result;
89 }
90
91
92 }
93
94 // Copyright (c) 2018 Saxonica Limited.