| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|