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 package mk.fsgrep.match;
26
27 import java.util.*;
28
29 import java.util.regex.*;
30
31 import mk.fsgrep.util.FileLine;
32 import mk.fsgrep.util.ReplaceQuery;
33 import mk.fsgrep.util.ReplaceQueryFactory;
34 import mk.fsgrep.util.ReplaceQueryResult;
35 import mk.fsgrep.util.TargetFile;
36
37 /***
38 * Search for a regular expression in a file.
39 *
40 * @author Murali Krishnan
41 *
42 */
43 public class Replace implements Runnable {
44
45
46
47
48 public static final Pattern RE_BLANK_LINE = Pattern.compile("^//s*$");
49
50
51
52
53
54
55
56
57
58 private ReplaceControl vControl = null;
59 private TargetFile vFile = null;
60 private List<FileLine> vNewLines = new ArrayList<FileLine>();
61 private boolean vWasChanged = false;
62
63
64
65
66
67
68 public Replace(ReplaceControl pControl, TargetFile pFile) {
69 vControl = pControl;
70 vFile = pFile;
71 }
72
73
74
75
76
77 protected TargetFile getFile() {
78 return vFile;
79 }
80
81 protected ReplaceControl getControl() {
82 return vControl;
83 }
84
85 protected List<FileLine> getNewLines() {
86 return vNewLines;
87 }
88
89 protected boolean getWasChanged() {
90 return vWasChanged;
91 }
92
93
94
95
96
97
98 protected void setNewLines(List<FileLine> val) {
99 vNewLines = val;
100 }
101
102 protected void setWasChanged(boolean val) {
103 vWasChanged = val;
104 }
105
106
107
108
109
110 protected void search() {
111 TargetFile.LineReader reader = getFile().createLineReader();
112
113 setNewLines(reader.getAllLines());
114
115 while (reader.hasMoreLines()) {
116 process(reader.nextLine());
117 }
118 }
119
120
121 protected void process(FileLine tfline) {
122 int lineNum = tfline.getNumber();
123 String line = tfline.getText();
124
125 if (getControl().getPattern().matcher(line).find()) {
126 String candidate = getControl().getPattern().matcher(line).replaceAll(getControl().getReplacement());
127 boolean substituteThis;
128
129 if (getControl().getDoContinue() && getControl().getDoConfirm()) {
130 ReplaceQuery replaceQuery = ReplaceQueryFactory.create();
131 ReplaceQueryResult response = replaceQuery.query(getFile().getName(), String.valueOf(lineNum), line, candidate);
132
133 substituteThis = response.doFixThis();
134
135 if (response.doFixAll()) {
136 getControl().setDoConfirm(false);
137 }
138
139 if (response.doSkipAll()) {
140 getControl().setDoContinue(false);
141 getControl().setDoConfirm(false);
142 }
143 } else {
144 substituteThis = (getControl().getDoContinue() && !getControl().getDoConfirm());
145 }
146
147 if (getControl().getDoContinue() && substituteThis) {
148 setWasChanged(true);
149 getControl().getReplaceCount().increment();
150
151
152 tfline.setText(candidate);
153 }
154 }
155 }
156
157 protected void replace() {
158
159 getFile().saveContent(getNewLines());
160 }
161
162
163
164
165
166 public void run() {
167 search();
168 if (getWasChanged()) {
169 replace();
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 }