Debian Codegraph @

net/sf/saxon/functions/UnparsedTextLines.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.functions;
9
10 import net.sf.saxon.expr.Callable;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.lib.Feature;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.SequenceIterator;
15 import net.sf.saxon.om.SequenceTool;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.EmptyIterator;
18 import net.sf.saxon.tree.iter.UnparsedTextIterator;
19 import net.sf.saxon.value.StringValue;
20
21 import java.io.LineNumberReader;
22 import java.io.StringReader;
23 import java.net.URI;
24
25
26 public class UnparsedTextLines extends UnparsedTextFunction implements Callable {
27
28 /**
29 * Evaluate the expression
30 *
31 * @param context the dynamic evaluation context
32 * @param arguments the values of the arguments, supplied as SequenceIterators
33 * @return the result of the evaluation, in the form of a SequenceIterator
34 * @throws net.sf.saxon.trans.XPathException
35 * if a dynamic error occurs during the evaluation of the expression
36 */
37 public Sequence<StringValue> call(XPathContext context, Sequence[] arguments) throws XPathException {
38 StringValue hrefVal = (StringValue) arguments[0].head();
39 String encoding = getArity() == 2 ? arguments[1].head().getStringValue() : null;
40 try {
41 return SequenceTool.toLazySequence(evalUnparsedTextLines(hrefVal, encoding, context));
42 } catch (XPathException e) {
43 if (getArity() == 2 && e.getErrorCodeLocalPart().equals("FOUT1200")) {
44 e.setErrorCode("FOUT1190");
45 }
46 throw e;
47 }
48 }
49
50 private SequenceIterator<StringValue> evalUnparsedTextLines(StringValue hrefVal, String encoding, XPathContext context) throws XPathException {
51 if (hrefVal == null) {
52 return EmptyIterator.emptyIterator();
53 }
54 String href = hrefVal.getStringValue();
55 boolean stable = context.getConfiguration().getBooleanProperty(Feature.STABLE_UNPARSED_TEXT);
56 if (stable) {
57 // if results have to be stable, the text has to be read into memory and cached
58 StringValue content = UnparsedText.evalUnparsedText(hrefVal, getStaticBaseUriString(), encoding, context);
59 assert content != null;
60 URI abs = UnparsedTextFunction.getAbsoluteURI(href, getStaticBaseUriString(), context);
61 LineNumberReader reader = new LineNumberReader(new StringReader(content.getStringValue()));
62 return new UnparsedTextIterator(reader, abs, context, encoding);
63 } else {
64 // with unstable results, we avoid reading the whole file into memory
65 final URI absoluteURI = UnparsedTextFunction.getAbsoluteURI(href, getRetainedStaticContext().getStaticBaseUriString(), context);
66 return new UnparsedTextIterator(absoluteURI, context, encoding, null);
67 }
68 }
69
70 }
71
72 // Copyright (c) 2012-2018 Saxonica Limited.