View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
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      //- Class Variables
47  
48      public static final Pattern RE_BLANK_LINE = Pattern.compile("^//s*$");
49  
50  
51      //------------------------------------------------------------
52      //- Class Functions
53  
54  
55      //------------------------------------------------------------
56      //- Instance Variables
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      //- Constructors
67  
68      public Replace(ReplaceControl pControl, TargetFile pFile) {
69          vControl = pControl;
70          vFile = pFile;
71      }
72  
73  
74      //------------------------------------------------------------
75      //- Accessors
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      //- Settors
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     //- Private/Protected Utility Functions
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                 // System.out.println("Changed: [" + line + "]");
151                 // System.out.println("To:      [" + candidate + "]");
152                 tfline.setText(candidate);
153             }
154         }
155     }
156 
157     protected void replace() {
158         // getFile().backup();
159         getFile().saveContent(getNewLines());
160     }
161 
162 
163     //------------------------------------------------------------
164     //- Public Interface Functions
165 
166     public void run() {
167         search();
168         if (getWasChanged()) {
169             replace();
170         }
171     }
172 
173 
174     //------------------------------------------------------------
175     //- Class Interface Functions
176 
177 
178     //------------------------------------------------------------
179     //- Inner Classes
180 
181 
182 
183     //------------------------------------------------------------
184     //- Main
185 
186 
187 
188 }