Debian Codegraph @

net/sf/saxon/tree/iter/TextLinesIterator.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.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 * An iterator that iterates over a file line by line. It is abstract because it does not supply the
25 * getAnother() method
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 * Create a TextLinesIterator over a given reader
42 *
43 * @param reader the reader that reads the file
44 * @param checker checks that the characters in the file are legal XML characters
45 * @param location the location of the instruction being executed, for diagnostics. May be null.
46 * @param uri the URI of the file being read, for diagnostics
47 * @throws net.sf.saxon.trans.XPathException
48 * if a dynamic error occurs
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 // public TextLinesIterator(File file, String encoding) throws IOException {
59 // this.file = file;
60 // this.encoding = encoding;
61 // this.reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file), encoding));
62 // this.checker = Name11Checker.getInstance();
63 // }
64
65 /*@Nullable*/
66 public StringValue next() throws XPathException {
67 if (position < 0) {
68 // input already exhausted
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 // remove any BOM found at start of file
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 // } catch (Exception err) {
96 // XPathException e = new XPathException(err.getMessage(), "XPST0001");
97 // if (location != null) {
98 // e.setLocator(location);
99 // }
100 // throw e;
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, /*@NotNull*/ 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