| 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.ma.map.MapItem; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.Sequence; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.SpecificFunctionType; |
| 18 |
import net.sf.saxon.value.BooleanValue; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
import net.sf.saxon.value.StringValue; |
| 21 |
|
| 22 |
import java.util.Map; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class ParseJsonFn extends JsonToXMLFn { |
| 30 |
|
| 31 |
public static OptionsParameter OPTION_DETAILS; |
| 32 |
static { |
| 33 |
SpecificFunctionType fallbackType = new SpecificFunctionType( |
| 34 |
new SequenceType[]{SequenceType.SINGLE_STRING}, SequenceType.SINGLE_STRING); |
| 35 |
OptionsParameter parseJsonOptions = new OptionsParameter(); |
| 36 |
parseJsonOptions.addAllowedOption("liberal", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE); |
| 37 |
parseJsonOptions.addAllowedOption("duplicates", SequenceType.SINGLE_STRING, new StringValue("use-first")); |
| 38 |
parseJsonOptions.setAllowedValues("duplicates", "FOJS0005", "reject", "use-first", "use-last"); |
| 39 |
parseJsonOptions.addAllowedOption("escape", SequenceType.SINGLE_BOOLEAN, BooleanValue.FALSE); |
| 40 |
parseJsonOptions.addAllowedOption("fallback", SequenceType.makeSequenceType(fallbackType, StaticProperty.EXACTLY_ONE), null); |
| 41 |
OPTION_DETAILS = parseJsonOptions; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@Override |
| 55 |
protected Item eval(String input, MapItem options, XPathContext context) throws XPathException { |
| 56 |
Map<String, Sequence<?>> checkedOptions = null; |
| 57 |
if (options != null) { |
| 58 |
checkedOptions = getDetails().optionDetails.processSuppliedOptions(options, context); |
| 59 |
} |
| 60 |
return parse(input, checkedOptions, context); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static Item parse(String input, Map<String, Sequence<?>> options, XPathContext context) throws XPathException { |
| 74 |
JsonParser parser = new JsonParser(); |
| 75 |
int flags = 0; |
| 76 |
if (options != null) { |
| 77 |
flags = JsonParser.getFlags(options, context, false); |
| 78 |
} |
| 79 |
JsonHandlerMap handler = new JsonHandlerMap(context, flags); |
| 80 |
if ((flags & JsonParser.DUPLICATES_RETAINED) != 0) { |
| 81 |
throw new XPathException("parse-json: duplicates=retain is not allowed", "FOJS0005"); |
| 82 |
} |
| 83 |
if ((flags & JsonParser.DUPLICATES_SPECIFIED) == 0) { |
| 84 |
flags |= JsonParser.DUPLICATES_FIRST; |
| 85 |
} |
| 86 |
if (options != null) { |
| 87 |
handler.setFallbackFunction(options, context); |
| 88 |
} |
| 89 |
parser.parse(input, flags, handler, context); |
| 90 |
return handler.getResult().head(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|