| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.iter; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.functions.UnparsedTextFunction; |
| 12 |
import net.sf.saxon.om.SequenceIterator; |
| 13 |
import net.sf.saxon.serialize.charcode.UTF16CharacterSet; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.value.StringValue; |
| 16 |
|
| 17 |
import java.util.function.IntPredicate; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.io.LineNumberReader; |
| 21 |
import java.net.URI; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract class TextLinesIterator implements SequenceIterator<StringValue> { |
| 28 |
|
| 29 |
protected LineNumberReader reader; |
| 30 |
protected IntPredicate checker; |
| 31 |
StringValue current = null; |
| 32 |
int position = 0; |
| 33 |
protected Location location; |
| 34 |
protected URI uri; |
| 35 |
|
| 36 |
protected TextLinesIterator() { |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public TextLinesIterator(LineNumberReader reader, Location location, URI uri, IntPredicate checker) throws XPathException { |
| 52 |
this.reader = reader; |
| 53 |
this.location = location; |
| 54 |
this.uri = uri; |
| 55 |
this.checker = checker; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public StringValue next() throws XPathException { |
| 67 |
if (position < 0) { |
| 68 |
|
| 69 |
close(); |
| 70 |
return null; |
| 71 |
} |
| 72 |
try { |
| 73 |
String s = reader.readLine(); |
| 74 |
if (s == null) { |
| 75 |
current = null; |
| 76 |
position = -1; |
| 77 |
close(); |
| 78 |
return null; |
| 79 |
} |
| 80 |
if (position == 0 && s.startsWith("\ufeff")) { |
| 81 |
|
| 82 |
s = s.substring(1); |
| 83 |
} |
| 84 |
checkLine(checker, s); |
| 85 |
current = new StringValue(s); |
| 86 |
position++; |
| 87 |
return current; |
| 88 |
} catch (IOException err) { |
| 89 |
close(); |
| 90 |
XPathException e = UnparsedTextFunction.handleIOError(uri, err, null); |
| 91 |
if (location != null) { |
| 92 |
e.setLocator(location); |
| 93 |
} |
| 94 |
throw e; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public void close() { |
| 105 |
try { |
| 106 |
reader.close(); |
| 107 |
} catch (IOException err) { |
| 108 |
|
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
private void checkLine(IntPredicate checker, String buffer) throws XPathException { |
| 114 |
for (int c = 0; c < buffer.length(); ) { |
| 115 |
int ch32 = buffer.charAt(c++); |
| 116 |
if (UTF16CharacterSet.isHighSurrogate(ch32)) { |
| 117 |
char low = buffer.charAt(c++); |
| 118 |
ch32 = UTF16CharacterSet.combinePair((char) ch32, low); |
| 119 |
} |
| 120 |
if (!checker.test(ch32)) { |
| 121 |
XPathException err = new XPathException("The unparsed-text file contains a character that is illegal in XML (line=" + |
| 122 |
position + " column=" + (c + 1) + " value=hex " + Integer.toHexString(ch32) + ')'); |
| 123 |
err.setErrorCode("FOUT1190"); |
| 124 |
err.setLocator(location); |
| 125 |
throw err; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|