| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 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 |
|
| 65 |
final URI absoluteURI = UnparsedTextFunction.getAbsoluteURI(href, getRetainedStaticContext().getStaticBaseUriString(), context); |
| 66 |
return new UnparsedTextIterator(absoluteURI, context, encoding, null); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
|