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