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.StringWrap;
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
53 * <a 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,
70 return replaceFirstRegion(new StringWrap(s), r, start, end);
74 * This method replaces the first occurence of the Regex in the String
75 * starting with position pos according to the Replacer rule of this object.
77 public StringLike replaceFirstRegion(StringLike s, Regex r, int start,
83 return dorep(s, start, end);
86 public StringLike replaceFirst(StringLike s)
88 return replaceFirstRegion(s, 0, s.length());
91 public StringLike replaceFirstFrom(StringLike s, int start)
93 return replaceFirstRegion(s, start, s.length());
96 public StringLike replaceFirstRegion(StringLike s, int start, int end)
99 return dorep(s, start, end);
102 RegHolder rh = new RegHolder();
104 public StringLike replaceAllRegion(String s, Regex r, int start, int end)
106 return replaceAllRegion(new StringWrap(s), r, start, end);
110 * This method replaces all occurences of the Regex in the String starting
111 * with postition pos according to the Replacer rule of this object.
113 public StringLike replaceAllRegion(StringLike s, Regex r, int start,
120 return dorep(s, start, end);
123 public StringLike replaceAll(StringLike s)
125 return replaceAllRegion(s, 0, s.length());
128 public StringLike replaceAllFrom(StringLike s, int start)
130 return replaceAllRegion(s, start, s.length());
133 public StringLike replaceAllRegion(StringLike s, int start, int end)
136 return dorep(s, start, end);
139 public String replaceAll(String s)
141 return replaceAllRegion(new StringWrap(s), 0, s.length()).toString();
144 public String replaceAllFrom(String s, int start)
146 return replaceAllRegion(new StringWrap(s), start, s.length())
150 public String replaceAllRegion(String s, int start, int end)
153 return dorep(new StringWrap(s), start, end).toString();
156 final public boolean isSpecial(ReplaceRule x)
160 if (x instanceof SpecialRule || (x instanceof RuleHolder
161 && ((RuleHolder) x).held instanceof SpecialRule))
170 final public void apply1(RegRes rr)
177 final StringLike dorep(StringLike s, int start, int end)
180 want_more_text = false;
184 throw new NullPointerException(MessageManager
185 .getString("exception.replace_null_regex_pointer"));
187 if (rh.me._search(s, start, end))
189 int rmn = rh.me.matchedTo();
190 if (rh.me.charsMatched() == 0 && !isSpecial(rh.me.getReplaceRule()))
198 for (int i = rmn; !want_more_text
199 && rh.me._search(s, i, end); i = rmn)
201 rmn = rh.me.matchedTo();
202 if (rh.me.charsMatched() == 0)
204 if (!isSpecial(rh.me.getReplaceRule()))
214 ret = ret == null ? s : ret;
219 StringBufferLike sb = null;
221 StringLike src = null;
226 * This method allows you to apply the results of several matches in a
227 * sequence to modify a String of text. Each call in the sequence must operate
228 * on the same piece of text and the matchedFrom() of each RegRes given to
229 * this method must be greater in value than the preceeding RegRes's
232 public void apply(RegRes r, ReplaceRule rp)
234 if (rp == null || (rp.next == null && rp instanceof AmpersandRule))
242 src = r.getStringLike();
246 sb = new StringBufferLike(src.newStringBufferLike());
248 int rmf = r.matchedFrom();
249 for (int ii = pos; ii < rmf; ii++)
251 sb.append(src.charAt(ii));
254 for (ReplaceRule x = rp; x != null; x = x.next)
257 if (x instanceof SpecialRule)
259 if (x instanceof WantMoreTextReplaceRule && want_more_text_enable)
261 want_more_text = true;
263 else if (x instanceof PushRule)
265 RegHolder rh2 = new RegHolder();
266 rh2.me = ((PushRule) x).NewRule;
270 else if (x instanceof PopRule)
277 else if (x instanceof ChangeRule)
279 rh.me = ((ChangeRule) x).NewRule;
290 boolean want_more_text = false, want_more_text_enable = false;
292 public boolean WantMoreText()
294 return want_more_text;
298 * Another form of apply, it is the same as apply(r,r.getReplaceRule()).
300 public void apply(Regex r)
302 apply(r, r.getReplaceRule());
306 * This finishes the replacement, appending the right() part of the last
307 * RegRes given to substitute(RegRes). After this method is called, the
308 * Replace object is reset to perform another substitution. If no RegRes
309 * objects with a true didMatch are applied, this returns null.
311 public StringLike finish()
317 // sb.append(src.substring(pos,src.length()));
318 int s_end = src.length();
319 for (int ii = pos; ii < s_end; ii++)
321 sb.append(src.charAt(ii));
326 StringLike retstr = sb.toStringLike();
331 int lastMatchedTo = 0;
333 public Object clone()
335 Replacer r = new Replacer();
340 r.lastMatchedTo = lastMatchedTo;
341 r.want_more_text = want_more_text;
342 r.want_more_text_enable = want_more_text_enable;
348 public int lastMatchedTo()
350 return lastMatchedTo;
353 public Regex getRegex()
358 public void setSource(StringLike sl)
363 public void setBuffer(StringBufferLike sbl)
368 public void setPos(int pos)