2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
10 import jalview.util.MessageManager;
\r
12 import com.stevesoft.pat.wrap.*;
\r
14 /** Internally used class. */
\r
19 RegHolder prev = null;
\r
23 * Internally used class.
\r
33 CodeVal(int p, char c)
\r
39 public String toString()
\r
41 return "(" + pos + "," + code + ")";
\r
46 * To use this class, first use either the getReplacer() method from Transformer
\r
47 * or Regex. You can then use replaceAll, replaceFirst, etc. methods on the
\r
48 * Replacer in the same way that you can from either of those two classes.
\r
50 * The only potential difference between using the methods of Replacer to do the
\r
51 * replacing is that Replacer remembers changes to the replacing object between
\r
52 * calls to replaceAll, replaceFirst etc. For details, see the example file <a
\r
53 * href="http://javaregex.com/code/trans3.java.html">trans3.java</a>.
\r
55 * @see com.stevesoft.pat.Transformer
\r
56 * @see com.stevesoft.pat.Regex
\r
58 public class Replacer
\r
62 /** Instantiate a new Replacer. */
\r
67 public StringLike replaceFirstRegion(String s, Regex r, int start, int end)
\r
69 return replaceFirstRegion(new StringWrap(s), r, start, end);
\r
73 * This method replaces the first occurence of the Regex in the String
\r
74 * starting with position pos according to the Replacer rule of this object.
\r
76 public StringLike replaceFirstRegion(StringLike s, Regex r, int start,
\r
82 return dorep(s, start, end);
\r
85 public StringLike replaceFirst(StringLike s)
\r
87 return replaceFirstRegion(s, 0, s.length());
\r
90 public StringLike replaceFirstFrom(StringLike s, int start)
\r
92 return replaceFirstRegion(s, start, s.length());
\r
95 public StringLike replaceFirstRegion(StringLike s, int start, int end)
\r
98 return dorep(s, start, end);
\r
101 RegHolder rh = new RegHolder();
\r
103 public StringLike replaceAllRegion(String s, Regex r, int start, int end)
\r
105 return replaceAllRegion(new StringWrap(s), r, start, end);
\r
109 * This method replaces all occurences of the Regex in the String starting
\r
110 * with postition pos according to the Replacer rule of this object.
\r
112 public StringLike replaceAllRegion(StringLike s, Regex r, int start,
\r
119 return dorep(s, start, end);
\r
122 public StringLike replaceAll(StringLike s)
\r
124 return replaceAllRegion(s, 0, s.length());
\r
127 public StringLike replaceAllFrom(StringLike s, int start)
\r
129 return replaceAllRegion(s, start, s.length());
\r
132 public StringLike replaceAllRegion(StringLike s, int start, int end)
\r
135 return dorep(s, start, end);
\r
138 public String replaceAll(String s)
\r
140 return replaceAllRegion(new StringWrap(s), 0, s.length()).toString();
\r
143 public String replaceAllFrom(String s, int start)
\r
145 return replaceAllRegion(new StringWrap(s), start, s.length())
\r
149 public String replaceAllRegion(String s, int start, int end)
\r
152 return dorep(new StringWrap(s), start, end).toString();
\r
155 final public boolean isSpecial(ReplaceRule x)
\r
159 if (x instanceof SpecialRule
\r
160 || (x instanceof RuleHolder && ((RuleHolder) x).held instanceof SpecialRule))
\r
169 final public void apply1(RegRes rr)
\r
171 rr.charsMatched_++;
\r
173 rr.charsMatched_--;
\r
176 final StringLike dorep(StringLike s, int start, int end)
\r
178 StringLike ret = s;
\r
179 want_more_text = false;
\r
183 throw new NullPointerException(MessageManager.getString("exception.replace_null_regex_pointer"));
\r
185 if (rh.me._search(s, start, end))
\r
187 int rmn = rh.me.matchedTo();
\r
188 if (rh.me.charsMatched() == 0 && !isSpecial(rh.me.getReplaceRule()))
\r
196 for (int i = rmn; !want_more_text && rh.me._search(s, i, end); i = rmn)
\r
198 rmn = rh.me.matchedTo();
\r
199 if (rh.me.charsMatched() == 0)
\r
201 if (!isSpecial(rh.me.getReplaceRule()))
\r
211 ret = ret == null ? s : ret;
\r
216 StringBufferLike sb = null;
\r
218 StringLike src = null;
\r
223 * This method allows you to apply the results of several matches in a
\r
224 * sequence to modify a String of text. Each call in the sequence must operate
\r
225 * on the same piece of text and the matchedFrom() of each RegRes given to
\r
226 * this method must be greater in value than the preceeding RegRes's
\r
227 * matchedTo() value.
\r
229 public void apply(RegRes r, ReplaceRule rp)
\r
231 if (rp == null || (rp.next == null && rp instanceof AmpersandRule))
\r
239 src = r.getStringLike();
\r
243 sb = new StringBufferLike(src.newStringBufferLike());
\r
245 int rmf = r.matchedFrom();
\r
246 for (int ii = pos; ii < rmf; ii++)
\r
248 sb.appendChar(src.charAt(ii));
\r
251 for (ReplaceRule x = rp; x != null; x = x.next)
\r
254 if (x instanceof SpecialRule)
\r
256 if (x instanceof WantMoreTextReplaceRule && want_more_text_enable)
\r
258 want_more_text = true;
\r
260 else if (x instanceof PushRule)
\r
262 RegHolder rh2 = new RegHolder();
\r
263 rh2.me = ((PushRule) x).NewRule;
\r
267 else if (x instanceof PopRule)
\r
269 if (rh.prev != null)
\r
274 else if (x instanceof ChangeRule)
\r
276 rh.me = ((ChangeRule) x).NewRule;
\r
280 if (!want_more_text)
\r
282 pos = r.matchedTo();
\r
287 boolean want_more_text = false, want_more_text_enable = false;
\r
289 public boolean WantMoreText()
\r
291 return want_more_text;
\r
295 * Another form of apply, it is the same as apply(r,r.getReplaceRule()).
\r
297 public void apply(Regex r)
\r
299 apply(r, r.getReplaceRule());
\r
303 * This finishes the replacement, appending the right() part of the last
\r
304 * RegRes given to substitute(RegRes). After this method is called, the
\r
305 * Replace object is reset to perform another substitution. If no RegRes
\r
306 * objects with a true didMatch are applied, this returns null.
\r
308 public StringLike finish()
\r
314 // sb.append(src.substring(pos,src.length()));
\r
315 int s_end = src.length();
\r
316 for (int ii = pos; ii < s_end; ii++)
\r
318 sb.appendChar(src.charAt(ii));
\r
321 lastMatchedTo = pos;
\r
323 StringLike retstr = sb.toStringLike();
\r
328 int lastMatchedTo = 0;
\r
330 public Object clone()
\r
332 Replacer r = new Replacer();
\r
337 r.lastMatchedTo = lastMatchedTo;
\r
338 r.want_more_text = want_more_text;
\r
339 r.want_more_text_enable = want_more_text_enable;
\r
341 r.rh.prev = rh.prev;
\r
345 public int lastMatchedTo()
\r
347 return lastMatchedTo;
\r
350 public Regex getRegex()
\r
355 public void setSource(StringLike sl)
\r
360 public void setBuffer(StringBufferLike sbl)
\r
365 public void setPos(int pos)
\r