| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
public class JsonDoc extends SystemFunction { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
URI absoluteURI = UnparsedTextFunction.getAbsoluteURI(href, getStaticBaseUriString(), context); |
| 57 |
|
| 58 |
String encoding = "UTF-8"; |
| 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 |
|
| 76 |
|
| 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 |
|