/
CheckErrorAlgorithm075.java
162 lines (146 loc) · 5.34 KB
/
CheckErrorAlgorithm075.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
/*
* 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.a07x.a075;
import java.util.Collection;
import java.util.List;
import org.wikipediacleaner.api.check.CheckErrorResult;
import org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithmBase;
import org.wikipediacleaner.api.data.PageElementListItem;
import org.wikipediacleaner.api.data.analysis.PageAnalysis;
/**
* Algorithm for analyzing error 75 of check wikipedia project.
* Error 75: Indented list
*/
public class CheckErrorAlgorithm075 extends CheckErrorAlgorithmBase {
public CheckErrorAlgorithm075() {
super("Indented list");
}
/**
* 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;
}
if (!analysis.getPage().isArticle()) {
return false;
}
// Analyze each list item
List<PageElementListItem> listItems = analysis.getListItems();
if (listItems == null) {
return false;
}
boolean result = false;
for (int index = 0; index < listItems.size(); index++) {
result |= analyzeListItem(analysis, errors, listItems, index);
}
return result;
}
/**
* Analyze a list item to check if errors are present.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param listItems List items
* @param initialIndex Index of the initial item to analyze.
* @return Flag indicating if the error was found.
*/
public boolean analyzeListItem(
PageAnalysis analysis,
Collection<CheckErrorResult> errors,
List<PageElementListItem> listItems,
int initialIndex) {
// Start with bullet or number list items
PageElementListItem initialItem = listItems.get(initialIndex);
String initialIndicators = initialItem.getIndicators();
if (!initialIndicators.startsWith("*") && !initialIndicators.startsWith("#")) {
return false;
}
if (analysis.comments().isAt(initialItem.getBeginIndex())) {
return false;
}
// Check following list items
int lastIndex = initialItem.getEndIndex();
int currentIndex = initialIndex + 1;
boolean result = false;
String contents = analysis.getContents();
boolean emptyLine = false;
while (currentIndex < listItems.size()) {
// Check that following list item is close to the previous one
while ((lastIndex < contents.length()) && (contents.charAt(lastIndex) == '\n')) {
lastIndex++;
}
emptyLine |= lastIndex > listItems.get(currentIndex - 1).getEndIndex() + 1;
PageElementListItem currentItem = listItems.get(currentIndex);
if (currentItem.getBeginIndex() > lastIndex) {
return result;
}
// Check what indicators are used
String currentIndicators = currentItem.getIndicators();
int incorrectIndicators = 0;
while ((incorrectIndicators < currentIndicators.length()) &&
(currentIndicators.charAt(incorrectIndicators) == ':')) {
incorrectIndicators++;
}
if ((incorrectIndicators < 1) || (incorrectIndicators >= currentIndicators.length())) {
return result;
}
if (analysis.comments().isAt(currentItem.getBeginIndex())) {
return result;
}
// Report error
if ("*#".indexOf(currentIndicators.charAt(incorrectIndicators)) >= 0) {
if (errors == null) {
return true;
}
result = true;
int beginIndex = currentItem.getBeginIndex();
int endIndex = currentItem.getEndIndex();
CheckErrorResult errorResult = createCheckErrorResult(analysis, beginIndex, endIndex);
boolean automatic =
!emptyLine &&
(incorrectIndicators <= initialIndicators.length());
int maxLengthRetrieved = Math.min(initialIndicators.length(), incorrectIndicators);
String replacement =
initialIndicators.substring(0, maxLengthRetrieved) +
contents.substring(beginIndex + maxLengthRetrieved, endIndex);
String text =
initialIndicators.substring(0, maxLengthRetrieved) +
currentIndicators.substring(maxLengthRetrieved) +
" ...";
errorResult.addReplacement(replacement, text, automatic);
errors.add(errorResult);
} else {
return result;
}
lastIndex = currentItem.getEndIndex();
currentIndex++;
}
return result;
}
/**
* Automatic fixing of all the errors in the page.
*
* @param analysis Page analysis.
* @return Page contents after fix.
*/
@Override
protected String internalAutomaticFix(PageAnalysis analysis) {
if (!analysis.getPage().isArticle()) {
return analysis.getContents();
}
return fixUsingAutomaticReplacement(analysis);
}
}