Debian Codegraph @

net/sf/saxon/ma/json/JsonToXMLFn.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.expr.StaticProperty;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.functions.OptionsParameter;
13 import net.sf.saxon.functions.SystemFunction;
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.XPathException;
18 import net.sf.saxon.type.SpecificFunctionType;
19 import net.sf.saxon.value.BooleanValue;
20 import net.sf.saxon.value.EmptySequence;
21 import net.sf.saxon.value.SequenceType;
22
23 import java.util.Map;
24
25 /**
26 * Implements the json-to-xml function defined in XSLT 3.0.
27 */
28
29 public class JsonToXMLFn extends SystemFunction {
30
31 public static OptionsParameter OPTION_DETAILS;
32
33 static{
34 SpecificFunctionType fallbackType = new SpecificFunctionType(
35 new SequenceType[]{SequenceType.SINGLE_STRING}, SequenceType.SINGLE_STRING);
36 OptionsParameter jsonToXmlOptions = new OptionsParameter();
37 jsonToXmlOptions.addAllowedOption("liberal", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE);
38 jsonToXmlOptions.addAllowedOption("duplicates", SequenceType.SINGLE_STRING, null);
39 jsonToXmlOptions.setAllowedValues("duplicates", "FOJS0005", "reject", "use-first", "retain");
40 jsonToXmlOptions.addAllowedOption("validate", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE);
41 jsonToXmlOptions.addAllowedOption("escape", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE);
42 jsonToXmlOptions.addAllowedOption("fallback", SequenceType.makeSequenceType(fallbackType, StaticProperty.EXACTLY_ONE), null);
43 OPTION_DETAILS = jsonToXmlOptions;
44 }
45
46 /**
47 * Evaluate the expression
48 *
49 * @param context the dynamic evaluation context
50 * @param arguments the values of the arguments, supplied as SequenceIterators
51 * @return the result of the evaluation, in the form of a SequenceIterator
52 * @throws net.sf.saxon.trans.XPathException
53 * if a dynamic error occurs during the evaluation of the expression
54 */
55 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
56 Item arg0 = arguments[0].head();
57 if (arg0 == null) {
58 return EmptySequence.getInstance();
59 }
60 String input = arg0.getStringValue();
61 MapItem options = null;
62 if (getArity() == 2) {
63 options = (MapItem) arguments[1].head();
64 }
65 Item result = eval(input, options, context);
66 return result == null ? EmptySequence.getInstance() : result;
67 }
68
69
70 /**
71 * Parse the JSON string according to supplied options
72 *
73 * @param input JSON input string
74 * @param options options for the conversion as a map of xs:string : value pairs
75 * @param context XPath evaluation context
76 * @return the result of the parsing, as an XML element
77 * @throws XPathException if the syntax of the input is incorrect
78 */
79 protected Item eval(String input, MapItem options, XPathContext context) throws XPathException {
80 JsonParser parser = new JsonParser();
81 int flags = 0;
82 Map<String, Sequence<?>> checkedOptions = null;
83 if (options != null) {
84 checkedOptions = getDetails().optionDetails.processSuppliedOptions(options, context);
85 flags = JsonParser.getFlags(checkedOptions, context, true);
86 if ((flags & JsonParser.DUPLICATES_LAST) != 0) {
87 throw new XPathException("json-to-xml: duplicates=use-last is not allowed", "FOJS0005");
88 }
89 if ((flags & JsonParser.DUPLICATES_SPECIFIED) == 0) {
90 if ((flags & JsonParser.VALIDATE) != 0) {
91 flags |= JsonParser.DUPLICATES_REJECTED;
92 } else {
93 flags |= JsonParser.DUPLICATES_RETAINED;
94 }
95 }
96 } else {
97 flags = JsonParser.DUPLICATES_RETAINED;
98 }
99 JsonHandlerXML handler = new JsonHandlerXML(context, getStaticBaseUriString(), flags);
100 if (options != null) {
101 handler.setFallbackFunction(checkedOptions, context);
102 }
103 parser.parse(input, flags, handler, context);
104 return handler.getResult();
105 }
106
107
108
109 }
110
111 // Copyright (c) 2011-2014 Saxonica Limited.