|
| 1 | +package graphql.parser; |
| 2 | + |
| 3 | +import graphql.Assert; |
| 4 | +import graphql.Internal; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.LineNumberReader; |
| 8 | +import java.io.Reader; |
| 9 | +import java.io.StringReader; |
| 10 | +import java.io.UncheckedIOException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +/** |
| 15 | + * This reader allows you to read N number readers and combine them as one logical reader |
| 16 | + * however you can then map back to the underlying readers in terms of their source name |
| 17 | + * and the relative lines numbers. |
| 18 | + * |
| 19 | + * It can also track all data in memory if you want to have all of the previous read data in |
| 20 | + * place at some point in time. |
| 21 | + */ |
| 22 | +@Internal |
| 23 | +public class MultiSourceReader extends Reader { |
| 24 | + |
| 25 | + private final List<SourcePart> sourceParts; |
| 26 | + private final StringBuilder data = new StringBuilder(); |
| 27 | + private int currentIndex = 0; |
| 28 | + private int overallLineNumber = 0; |
| 29 | + private final boolean trackData; |
| 30 | + |
| 31 | + |
| 32 | + private MultiSourceReader(Builder builder) { |
| 33 | + this.sourceParts = builder.sourceParts; |
| 34 | + this.trackData = builder.trackData; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public int read(char[] cbuf, int off, int len) throws IOException { |
| 39 | + while (true) { |
| 40 | + synchronized (this) { |
| 41 | + if (currentIndex >= sourceParts.size()) { |
| 42 | + return -1; |
| 43 | + } |
| 44 | + SourcePart sourcePart = sourceParts.get(currentIndex); |
| 45 | + int read = sourcePart.lineReader.read(cbuf, off, len); |
| 46 | + overallLineNumber = calcLineNumber(); |
| 47 | + if (read == -1) { |
| 48 | + currentIndex++; |
| 49 | + } else { |
| 50 | + trackData(cbuf, off, read); |
| 51 | + return read; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private void trackData(char[] cbuf, int off, int len) { |
| 58 | + if (trackData) { |
| 59 | + data.append(cbuf, off, len); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private int calcLineNumber() { |
| 64 | + int linenumber = 0; |
| 65 | + for (SourcePart sourcePart : sourceParts) { |
| 66 | + linenumber += sourcePart.lineReader.getLineNumber(); |
| 67 | + } |
| 68 | + return linenumber; |
| 69 | + } |
| 70 | + |
| 71 | + public static class SourceAndLine { |
| 72 | + private String sourceName = null; |
| 73 | + private int line = 0; |
| 74 | + |
| 75 | + public String getSourceName() { |
| 76 | + return sourceName; |
| 77 | + } |
| 78 | + |
| 79 | + public int getLine() { |
| 80 | + return line; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public SourceAndLine getSourceAndLineFromOverallLine(int overallLineNumber) { |
| 85 | + SourceAndLine sourceAndLine = new SourceAndLine(); |
| 86 | + if (sourceParts.isEmpty()) { |
| 87 | + return sourceAndLine; |
| 88 | + } |
| 89 | + SourcePart currentPart; |
| 90 | + if (currentIndex >= sourceParts.size()) { |
| 91 | + currentPart = sourceParts.get(sourceParts.size() - 1); |
| 92 | + } else { |
| 93 | + currentPart = sourceParts.get(currentIndex); |
| 94 | + } |
| 95 | + int page = 0; |
| 96 | + int previousPage; |
| 97 | + for (SourcePart sourcePart : sourceParts) { |
| 98 | + sourceAndLine.sourceName = sourcePart.sourceName; |
| 99 | + if (sourcePart == currentPart) { |
| 100 | + // we cant go any further |
| 101 | + int offset = currentPart.lineReader.getLineNumber(); |
| 102 | + previousPage = page; |
| 103 | + page += offset; |
| 104 | + if (page > overallLineNumber) { |
| 105 | + sourceAndLine.line = overallLineNumber - previousPage; |
| 106 | + } else { |
| 107 | + sourceAndLine.line = page; |
| 108 | + } |
| 109 | + return sourceAndLine; |
| 110 | + } else { |
| 111 | + previousPage = page; |
| 112 | + page += sourcePart.lineReader.getLineNumber(); |
| 113 | + if (page > overallLineNumber) { |
| 114 | + sourceAndLine.line = overallLineNumber - previousPage; |
| 115 | + return sourceAndLine; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + sourceAndLine.line = overallLineNumber - page; |
| 120 | + return sourceAndLine; |
| 121 | + } |
| 122 | + |
| 123 | + public int getLineNumber() { |
| 124 | + synchronized (this) { |
| 125 | + if (sourceParts.isEmpty()) { |
| 126 | + return 0; |
| 127 | + } |
| 128 | + if (currentIndex >= sourceParts.size()) { |
| 129 | + return sourceParts.get(sourceParts.size() - 1).lineReader.getLineNumber(); |
| 130 | + } |
| 131 | + return sourceParts.get(currentIndex).lineReader.getLineNumber(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public String getSourceName() { |
| 136 | + synchronized (this) { |
| 137 | + if (sourceParts.isEmpty()) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + if (currentIndex >= sourceParts.size()) { |
| 141 | + return sourceParts.get(sourceParts.size() - 1).sourceName; |
| 142 | + } |
| 143 | + return sourceParts.get(currentIndex).sourceName; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public int getOverallLineNumber() { |
| 148 | + return overallLineNumber; |
| 149 | + } |
| 150 | + |
| 151 | + public List<String> getData() { |
| 152 | + LineNumberReader reader = new LineNumberReader(new StringReader(data.toString())); |
| 153 | + List<String> lines = new ArrayList<>(); |
| 154 | + while (true) { |
| 155 | + try { |
| 156 | + String line = reader.readLine(); |
| 157 | + if (line == null) { |
| 158 | + return lines; |
| 159 | + } |
| 160 | + lines.add(line); |
| 161 | + } catch (IOException e) { |
| 162 | + throw new UncheckedIOException(e); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void close() throws IOException { |
| 169 | + synchronized (this) { |
| 170 | + for (SourcePart sourcePart : sourceParts) { |
| 171 | + if (!sourcePart.closed) { |
| 172 | + sourcePart.lineReader.close(); |
| 173 | + sourcePart.closed = true; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static class SourcePart { |
| 180 | + String sourceName; |
| 181 | + LineNumberReader lineReader; |
| 182 | + boolean closed; |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + public static Builder newMultiSourceLineReader() { |
| 187 | + return new Builder(); |
| 188 | + } |
| 189 | + |
| 190 | + public static class Builder { |
| 191 | + List<SourcePart> sourceParts = new ArrayList<>(); |
| 192 | + boolean trackData = true; |
| 193 | + |
| 194 | + public Builder reader(Reader reader, String sourceName) { |
| 195 | + SourcePart sourcePart = new SourcePart(); |
| 196 | + sourcePart.lineReader = new LineNumberReader(Assert.assertNotNull(reader)); |
| 197 | + sourcePart.sourceName = sourceName; |
| 198 | + sourcePart.closed = false; |
| 199 | + sourceParts.add(sourcePart); |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + public Builder string(String input, String sourceName) { |
| 204 | + SourcePart sourcePart = new SourcePart(); |
| 205 | + sourcePart.lineReader = new LineNumberReader(new StringReader(input)); |
| 206 | + sourcePart.sourceName = sourceName; |
| 207 | + sourcePart.closed = false; |
| 208 | + sourceParts.add(sourcePart); |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + public Builder trackData(boolean trackData) { |
| 213 | + this.trackData = trackData; |
| 214 | + return this; |
| 215 | + |
| 216 | + } |
| 217 | + |
| 218 | + public MultiSourceReader build() { |
| 219 | + return new MultiSourceReader(this); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments