/
CheckErrorAlgorithm028.java
284 lines (255 loc) · 9.05 KB
/
CheckErrorAlgorithm028.java
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* WPCleaner: A tool to help on Wikipedia maintenance tasks.
* Copyright (C) 2013 Nicolas Vervelle
*
* See README.txt file for licensing information.
*/
package org.wikipediacleaner.api.check.algorithm.a0xx.a02x.a028;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.wikipediacleaner.api.algorithm.AlgorithmParameter;
import org.wikipediacleaner.api.algorithm.AlgorithmParameterElement;
import org.wikipediacleaner.api.check.CheckErrorResult;
import org.wikipediacleaner.api.check.CheckErrorResult.ErrorLevel;
import org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase;
import org.wikipediacleaner.api.configuration.WPCConfiguration;
import org.wikipediacleaner.api.data.PageElementTag;
import org.wikipediacleaner.api.data.PageElementTemplate;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
import org.wikipediacleaner.api.data.contents.tag.HtmlTagType;
import org.wikipediacleaner.api.data.contents.tag.WikiTagType;
import org.wikipediacleaner.i18n.GT;
/**
* Algorithm for analyzing error 28 of check wikipedia project.
* Error 28: Table not correct end
*/
public class CheckErrorAlgorithm028 extends CheckErrorAlgorithmBase {
public CheckErrorAlgorithm028() {
super("Table not correct end");
}
/**
* Analyze a page to check if errors are present.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param onlyAutomatic True if analysis could be restricted to errors automatically fixed.
* @return Flag indicating if the error was found.
*/
@Override
public boolean analyze(
PageAnalysis analysis,
Collection<CheckErrorResult> errors, boolean onlyAutomatic) {
if (analysis == null) {
return false;
}
// Retrieve list of table starts
List<TableElement> starts = getTableStarts(analysis);
if (starts.isEmpty()) {
return false;
}
// Retrieve list of table ends and pair them
List<TableElement> ends = getTableEnds(analysis);
for (TableElement end : ends) {
TableElement pair = null;
for (TableElement start : starts) {
if (!start.hasMatch && (start.endIndex < end.beginIndex)) {
pair = start;
}
}
if (pair != null) {
pair.hasMatch = true;
end.hasMatch = true;
}
}
// Check that every start has a matching end
boolean result = false;
for (TableElement start : starts) {
if (!start.hasMatch) {
result = true;
}
}
if (!result || (errors == null)) {
return result;
}
// Mark errors
starts.addAll(ends);
Collections.sort(starts);
for (TableElement element : starts) {
CheckErrorResult errorResult = createCheckErrorResult(
analysis, element.beginIndex, element.endIndex,
(element.begin && !element.hasMatch) ? ErrorLevel.ERROR : ErrorLevel.CORRECT);
errors.add(errorResult);
}
return result;
}
/**
* Construct list of table starts.
*
* @param analysis Page analysis.
* @return List of table starts.
*/
private List<TableElement> getTableStarts(PageAnalysis analysis) {
List<TableElement> list = new ArrayList<>();
// Find tables beginning by {|
String contents = analysis.getContents();
int index = contents.indexOf("{|");
while (index >= 0) {
if ((index == 0) || (contents.charAt(index - 1) != '{')) {
if (shouldCount(analysis, index)) {
list.add(new TableElement(index, index + 2, true));
}
}
index = contents.indexOf("{|", index + 1);
}
// Find tables beginning by <table>
List<PageElementTag> tags = analysis.getTags(HtmlTagType.TABLE);
for (PageElementTag tag : tags) {
if (!tag.isFullTag() && !tag.isEndTag()) {
if (shouldCount(analysis, index)) {
list.add(new TableElement(tag.getBeginIndex(), tag.getEndIndex(), true));
}
}
}
Collections.sort(list);
return list;
}
/**
* Construct list of table ends.
*
* @param analysis Page analysis.
* @return List of table ends.
*/
private List<TableElement> getTableEnds(PageAnalysis analysis) {
List<TableElement> list = new ArrayList<>();
// Find tables ending by |}
String contents = analysis.getContents();
int index = contents.indexOf("|}");
while (index >= 0) {
if (shouldCount(analysis, index)) {
PageElementTemplate template = analysis.isInTemplate(index);
if ((template == null) ||
(template.getEndIndex() != index + 3)) {
list.add(new TableElement(index, index + 2, false));
}
}
index = contents.indexOf("|}", index + 1);
}
// Find tables ending by </table>
List<PageElementTag> tags = analysis.getTags(HtmlTagType.TABLE);
for (PageElementTag tag : tags) {
if (!tag.isFullTag() && tag.isEndTag()) {
if (shouldCount(analysis, index)) {
list.add(new TableElement(tag.getBeginIndex(), tag.getEndIndex(), false));
}
}
}
// Find tables ending by a template
if (!templateNames.isEmpty()) {
List<PageElementTemplate> templates = analysis.getTemplates();
for (PageElementTemplate template : templates) {
if (templateNames.contains(template.getTemplateName())) {
list.add(new TableElement(template.getBeginIndex(), template.getEndIndex(), false));
}
}
}
Collections.sort(list);
return list;
}
/**
* @param analysis Page analysis.
* @param index Current index.
* @return True if this place should count for the detection.
*/
private boolean shouldCount(PageAnalysis analysis, int index) {
if ((analysis.getSurroundingTag(HtmlTagType.CODE, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.CHEM, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.MATH, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.MATH_CHEM, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.NOWIKI, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.PRE, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.SOURCE, index) != null) ||
(analysis.getSurroundingTag(WikiTagType.SYNTAXHIGHLIGHT, index) != null)) {
return false;
}
if (analysis.comments().isAt(index)) {
return false;
}
return true;
}
/* ====================================================================== */
/* PARAMETERS */
/* ====================================================================== */
/** Templates that can replace the end of a table */
private static final String PARAMETER_TEMPLATES = "templates";
/**
* Initialize settings for the algorithm.
*
* @see org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase#initializeSettings()
*/
@Override
protected void initializeSettings() {
String tmp = getSpecificProperty(PARAMETER_TEMPLATES, true, true, true);
templateNames.clear();
if (tmp != null) {
List<String> tmpList = WPCConfiguration.convertPropertyToStringList(tmp);
if (tmpList != null) {
for (String tmpName : tmpList) {
tmpName = tmpName.trim();
if (tmpName.startsWith("{{")) {
tmpName = tmpName.substring(2);
if (tmpName.endsWith("}}")) {
tmpName = tmpName.substring(0, tmpName.length() - 2);
}
}
tmpName = tmpName.trim();
if (tmpName.length() > 0) {
templateNames.add(tmpName);
}
}
}
}
}
/** Templates that can replace the end of a table */
private final Set<String> templateNames = new HashSet<>();
/**
* Build the list of parameters for this algorithm.
*/
@Override
protected void addParameters() {
super.addParameters();
addParameter(new AlgorithmParameter(
PARAMETER_TEMPLATES,
GT._T("Templates that can replace the end of a table"),
new AlgorithmParameterElement(
"template name",
GT._T("Template that can replace the end of a table")),
true));
}
/**
* POJO for storing table begin and end.
*/
private static class TableElement implements Comparable<TableElement> {
public final int beginIndex;
public final int endIndex;
public final boolean begin;
public boolean hasMatch;
TableElement(int beginIndex, int endIndex, boolean begin) {
this.beginIndex = beginIndex;
this.endIndex = endIndex;
this.begin = begin;
}
/**
* @param o
* @return
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(TableElement o) {
return beginIndex - o.beginIndex;
}
}
}