LineElementsFactory.java
6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Decompiled with CFR 0_118.
*/
package com.day.util.diff;
import com.day.util.diff.Document;
import com.day.util.diff.DocumentSource;
import com.day.util.diff.ElementsFactory;
import com.day.util.diff.FileDocumentSource;
import java.io.*;
import java.util.ArrayList;
public class LineElementsFactory
implements ElementsFactory {
static final String CVS_ID = "$URL$ $Rev$ $Date$";
private static final int MAX_ELEMENTS = 100000;
private final Document.Element[] elements;
static /* synthetic */ Class class$com$day$util$diff$LineElementsFactory;
private LineElementsFactory(Document.Element[] elements) {
this.elements = elements;
}
public Document.Element[] getElements() {
return this.elements;
}
public static LineElementsFactory create(DocumentSource source, String text, boolean ignoreWs) {
try {
StringReader reader = new StringReader(text);
return LineElementsFactory.create(source, reader, ignoreWs);
}
catch (IOException e) {
throw new IllegalArgumentException(e.toString());
}
}
public static LineElementsFactory create(FileDocumentSource source, boolean ignoreWs) throws IOException {
return LineElementsFactory.create((DocumentSource)source, (String)null, ignoreWs);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static LineElementsFactory create(FileDocumentSource source, boolean ignoreWs, String charset) throws IOException {
Reader text = charset == null ? new FileReader(source.getFile()) : new InputStreamReader((InputStream)new FileInputStream(source.getFile()), charset);
try {
LineElementsFactory lineElementsFactory = LineElementsFactory.create((DocumentSource)source, text, ignoreWs);
return lineElementsFactory;
}
finally {
try {
text.close();
}
catch (IOException e) {}
}
}
public static LineElementsFactory create(DocumentSource source, Reader text, boolean ignoreWs) throws IOException {
Document.Element[] elements = LineElementsFactory.getElements(source, text, ignoreWs);
return new LineElementsFactory(elements);
}
private static Document.Element[] getElements(DocumentSource source, Reader r, boolean ignoreWS) throws IOException {
if (r == null) {
return new Document.Element[0];
}
ArrayList<Document.AnnotatedElement> lines = new ArrayList<Document.AnnotatedElement>();
char[] buffer = new char[8192];
int start = 0;
int pos = 0;
int end = r.read(buffer);
while (pos < end) {
char c;
if ((c = buffer[pos++]) == '\n') {
String line = new String(buffer, start, pos - start);
if (ignoreWS) {
lines.add(new IStringElement(source, line));
} else {
lines.add(new StringElement(source, line));
}
start = pos;
if (lines.size() == 100000) break;
}
if (pos != end) continue;
int len = end - start;
if (len == buffer.length) {
char[] newBuffer = new char[buffer.length * 2];
System.arraycopy(buffer, start, newBuffer, 0, len);
buffer = newBuffer;
} else if (len > 0) {
System.arraycopy(buffer, start, buffer, 0, len);
}
end = len;
start = 0;
pos = 0;
int read = r.read(buffer, end, buffer.length - end);
if (read < 0) break;
end += read;
}
if (start < end) {
String line = new String(buffer, start, end - start);
if (ignoreWS) {
lines.add(new IStringElement(source, line));
} else {
lines.add(new StringElement(source, line));
}
}
if (ignoreWS) {
return lines.toArray(new IStringElement[lines.size()]);
}
return lines.toArray(new StringElement[lines.size()]);
}
public static class IStringElement
implements Document.AnnotatedElement {
private final DocumentSource source;
private final String string;
private String stripped;
static final /* synthetic */ boolean $assertionsDisabled;
public DocumentSource getDocumentSource() {
return this.source;
}
public String getString() {
return this.string;
}
public IStringElement(DocumentSource source, String string) {
this.source = source;
this.string = string;
}
private String getStripped() {
if (this.stripped == null) {
StringBuffer buf = new StringBuffer(this.string.length());
for (int i = 0; i < this.string.length(); ++i) {
char c = this.string.charAt(i);
if (Character.isWhitespace(c)) continue;
buf.append(c);
}
this.stripped = buf.toString();
}
return this.stripped;
}
public int hashCode() {
return this.getStripped().hashCode();
}
public boolean equals(Object obj) {
if (!$assertionsDisabled && !(obj instanceof IStringElement)) {
throw new AssertionError();
}
return this.getStripped().equals(((IStringElement)obj).getStripped());
}
public String toString() {
return this.string;
}
static {
Class class_ = LineElementsFactory.class$com$day$util$diff$LineElementsFactory == null ? (LineElementsFactory.class$com$day$util$diff$LineElementsFactory = LineElementsFactory.class$("com.day.util.diff.LineElementsFactory")) : LineElementsFactory.class$com$day$util$diff$LineElementsFactory;
$assertionsDisabled = !class_.desiredAssertionStatus();
}
}
public static class StringElement
implements Document.AnnotatedElement {
private final DocumentSource source;
private final String string;
public StringElement(DocumentSource source, String string) {
this.source = source;
this.string = string;
}
public String getString() {
return this.string;
}
public DocumentSource getDocumentSource() {
return this.source;
}
public int hashCode() {
return this.string.hashCode();
}
public String toString() {
return this.string;
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof StringElement) {
return ((StringElement)obj).string.equals(this.string);
}
return false;
}
}
}