KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Replacement


1 /*
2  * Replacement.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Replacement.java,v 1.1.1.1 2002/09/24 16:09:21 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.REMatch;
25
26 public class Replacement extends Search
27 {
28     private Editor editor;
29
30     // This is the replacement string entered by the user. If we're working
31
// with regular expressions, this string may contain one or more
32
// occurrences of "\\", "\&", or "\D", where D is a digit from 1 to 9
33
// (inclusive).
34
private String JavaDoc replaceWith;
35
36     private boolean confirmChanges = true;
37     private int replacementCount;
38
39     public Replacement(Editor editor)
40     {
41         super();
42         this.editor = editor;
43     }
44
45     public final Editor getEditor()
46     {
47         return editor;
48     }
49
50     public final String JavaDoc getReplaceWith()
51     {
52         return replaceWith;
53     }
54
55     public final void setReplaceWith(String JavaDoc s)
56     {
57         replaceWith = s;
58     }
59
60     public final boolean confirmChanges()
61     {
62         return confirmChanges;
63     }
64
65     public final void setConfirmChanges(boolean b)
66     {
67         confirmChanges = b;
68     }
69
70     public final int getReplacementCount()
71     {
72         return replacementCount;
73     }
74
75     public void replaceOccurrence()
76     {
77         final Buffer buffer = editor.getBuffer();
78         try {
79             buffer.lockWrite();
80         }
81         catch (InterruptedException JavaDoc e) {
82             Log.error(e);
83             return;
84         }
85         try {
86             final Line dotLine = editor.getDotLine();
87             final int dotOffset = editor.getDotOffset();
88             if (isMultilinePattern()) {
89                 Debug.assertTrue(isRegularExpression());
90                 Debug.assertTrue(editor.getMark() != null);
91                 Debug.assertTrue(editor.getDot().isBefore(editor.getMark()));
92                 Debug.assertTrue(getMatch() != null);
93                 final Region region = getRegion();
94                 int end = -1;
95                 if (region != null) {
96                     // Restrict to selection.
97
end = buffer.getAbsoluteOffset(region.getEnd());
98                 }
99                 Region r = new Region(editor);
100                 editor.addUndoDeleteRegion(r);
101                 // Sets buffer modified flag.
102
r.delete();
103                 final String JavaDoc toBeReplaced = getMatch().toString();
104                 final String JavaDoc toBeInserted = getReplacementText(toBeReplaced);
105                 editor.addUndo(SimpleEdit.INSERT_STRING);
106                 buffer.insertString(editor.getDot(), toBeInserted);
107                 if (region != null) {
108                     Debug.assertTrue(end >= 0);
109                     end -= toBeReplaced.length();
110                     end += toBeInserted.length();
111                     Debug.assertTrue(end > buffer.getAbsoluteOffset(region.getBegin()));
112                     Position endPos = buffer.getPosition(end);
113                     Debug.assertTrue(endPos != null);
114                     region.setEnd(endPos);
115                 }
116                 editor.getBuffer().repaint();
117             } else {
118                 editor.addUndo(SimpleEdit.LINE_EDIT);
119                 editor.setMark(null);
120                 String JavaDoc head = dotLine.substring(0, dotOffset);
121                 String JavaDoc toBeReplaced;
122                 if (isRegularExpression()) {
123                     Debug.assertTrue(getMatch() != null);
124                     toBeReplaced = getMatch().toString();
125                 } else {
126                     toBeReplaced = dotLine.substring(dotOffset,
127                         dotOffset + getPatternLength());
128                 }
129                 String JavaDoc tail = dotLine.substring(dotOffset + toBeReplaced.length());
130                 String JavaDoc toBeInserted = getReplacementText(toBeReplaced);
131                 FastStringBuffer sb = new FastStringBuffer(head);
132                 sb.append(toBeInserted);
133                 sb.append(tail);
134                 dotLine.setText(sb.toString());
135                 Region region = getRegion();
136                 if (region != null && dotLine == region.getEndLine())
137                     if (dotOffset + toBeReplaced.length() <= region.getEndOffset())
138                         region.setEndOffset(region.getEndOffset() + toBeInserted.length() - toBeReplaced.length());
139                 editor.getDot().skip(toBeInserted.length());
140                 Editor.updateInAllEditors(dotLine);
141                 buffer.modified();
142             }
143             ++replacementCount;
144         }
145         finally {
146             buffer.unlockWrite();
147         }
148     }
149
150     // No editor, no region, no undo, does not set buffer's modified flag.
151
// Used by replace in files when not confirming changes or keeping
152
// modified buffers.
153
public void replaceOccurrence(Position pos)
154     {
155         final Line line = pos.getLine();
156         final int offset = pos.getOffset();
157         final String JavaDoc head = line.substring(0, offset);
158         String JavaDoc toBeReplaced;
159         if (isRegularExpression()) {
160             Debug.assertTrue(getMatch() != null);
161             toBeReplaced = getMatch().toString();
162         } else
163             toBeReplaced = line.substring(offset, offset + getPatternLength());
164         final String JavaDoc tail = line.substring(offset + toBeReplaced.length());
165         final String JavaDoc toBeInserted = getReplacementText(toBeReplaced);
166         FastStringBuffer sb = new FastStringBuffer(head);
167         sb.append(toBeInserted);
168         sb.append(tail);
169         line.setText(sb.toString());
170         pos.skip(toBeInserted.length());
171         ++replacementCount;
172     }
173
174     private String JavaDoc getReplacementText(String JavaDoc toBeReplaced)
175     {
176         String JavaDoc replacementText;
177         if (isRegularExpression()) {
178             // Perform regular expression variable substitution as necessary.
179
replacementText = substituteInto(getMatch(), replaceWith);
180         } else {
181             // We're not doing regular expressions. Use the string entered by
182
// the user, verbatim.
183
replacementText = replaceWith;
184         }
185         // Handle case conversion if appropriate.
186
if (ignoreCase() && toBeReplaced.length() > 0 && replacementText.length() > 0) {
187             if (Utilities.isUpperCase(toBeReplaced)) {
188                 // The string to be replaced is all upper case. Make the
189
// replacement all upper case too.
190
replacementText = replacementText.toUpperCase();
191             } else {
192                 char c = toBeReplaced.charAt(0);
193                 if (Character.isUpperCase(c)) {
194                     // The string to be replaced begins with an upper case
195
// letter. Make the replacement begin with an upper case
196
// letter too.
197
c = replacementText.charAt(0);
198                     if (Character.isLowerCase(c)) {
199                         FastStringBuffer sb = new FastStringBuffer(replacementText);
200                         sb.setCharAt(0, Character.toUpperCase(c));
201                         replacementText = sb.toString();
202                     }
203                 }
204             }
205         }
206         return replacementText;
207     }
208
209     private String JavaDoc substituteInto(REMatch match, String JavaDoc input)
210     {
211         FastStringBuffer sb = new FastStringBuffer();
212         int i;
213         for (i = 0; i < input.length()-1; i++) {
214             char c = input.charAt(i);
215             if (c == '\\') {
216                 c = input.charAt(++i);
217                 if (c == '&') {
218                     sb.append(match.toString());
219                 } else if (c >= '1' && c <= '9') {
220                     int val = Character.digit(c, 10);
221                     sb.append(match.toString(val));
222                 } else if (isMultilinePattern() && c == 'n') {
223                     sb.append('\n');
224                 } else {
225                      // Escape everything else.
226
sb.append(c);
227                 }
228             } else
229                 sb.append(c);
230         }
231
232         if (i < input.length())
233             sb.append(input.charAt(i));
234
235         return sb.toString();
236     }
237 }
238
Popular Tags