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 com.stevesoft.pat.wrap.*;
12 /** Internally used class. */
17 RegHolder prev = null;
21 * Internally used class.
31 CodeVal(int p, char c)
37 public String toString()
39 return "(" + pos + "," + code + ")";
44 * To use this class, first use either the getReplacer() method from Transformer
45 * or Regex. You can then use replaceAll, replaceFirst, etc. methods on the
46 * Replacer in the same way that you can from either of those two classes.
48 * The only potential difference between using the methods of Replacer to do the
49 * replacing is that Replacer remembers changes to the replacing object between
50 * calls to replaceAll, replaceFirst etc. For details, see the example file <a
51 * href="http://javaregex.com/code/trans3.java.html">trans3.java</a>.
53 * @see com.stevesoft.pat.Transformer
54 * @see com.stevesoft.pat.Regex
60 /** Instantiate a new Replacer. */
65 public StringLike replaceFirstRegion(String s, Regex r, int start, int end)
67 return replaceFirstRegion(new StringWrap(s), r, start, end);
71 * This method replaces the first occurence of the Regex in the String
72 * starting with position pos according to the Replacer rule of this object.
74 public StringLike replaceFirstRegion(StringLike s, Regex r, int start,
80 return dorep(s, start, end);
83 public StringLike replaceFirst(StringLike s)
85 return replaceFirstRegion(s, 0, s.length());
88 public StringLike replaceFirstFrom(StringLike s, int start)
90 return replaceFirstRegion(s, start, s.length());
93 public StringLike replaceFirstRegion(StringLike s, int start, int end)
96 return dorep(s, start, end);
99 RegHolder rh = new RegHolder();
101 public StringLike replaceAllRegion(String s, Regex r, int start, int end)
103 return replaceAllRegion(new StringWrap(s), r, start, end);
107 * This method replaces all occurences of the Regex in the String starting
108 * with postition pos according to the Replacer rule of this object.
110 public StringLike replaceAllRegion(StringLike s, Regex r, int start,
117 return dorep(s, start, end);
120 public StringLike replaceAll(StringLike s)
122 return replaceAllRegion(s, 0, s.length());
125 public StringLike replaceAllFrom(StringLike s, int start)
127 return replaceAllRegion(s, start, s.length());
130 public StringLike replaceAllRegion(StringLike s, int start, int end)
133 return dorep(s, start, end);
136 public String replaceAll(String s)
138 return replaceAllRegion(new StringWrap(s), 0, s.length()).toString();
141 public String replaceAllFrom(String s, int start)
143 return replaceAllRegion(new StringWrap(s), start, s.length())
147 public String replaceAllRegion(String s, int start, int end)
150 return dorep(new StringWrap(s), start, end).toString();
153 final public boolean isSpecial(ReplaceRule x)
157 if (x instanceof SpecialRule
158 || (x instanceof RuleHolder && ((RuleHolder) x).held instanceof SpecialRule))
167 final public void apply1(RegRes rr)
174 final StringLike dorep(StringLike s, int start, int end)
177 want_more_text = false;
181 throw new NullPointerException("Replacer has null Regex pointer");
183 if (rh.me._search(s, start, end))
185 int rmn = rh.me.matchedTo();
186 if (rh.me.charsMatched() == 0 && !isSpecial(rh.me.getReplaceRule()))
194 for (int i = rmn; !want_more_text && rh.me._search(s, i, end); i = rmn)
196 rmn = rh.me.matchedTo();
197 if (rh.me.charsMatched() == 0)
199 if (!isSpecial(rh.me.getReplaceRule()))
209 ret = ret == null ? s : ret;
214 StringBufferLike sb = null;
216 StringLike src = null;
221 * This method allows you to apply the results of several matches in a
222 * sequence to modify a String of text. Each call in the sequence must operate
223 * on the same piece of text and the matchedFrom() of each RegRes given to
224 * this method must be greater in value than the preceeding RegRes's
227 public void apply(RegRes r, ReplaceRule rp)
229 if (rp == null || (rp.next == null && rp instanceof AmpersandRule))
237 src = r.getStringLike();
241 sb = new StringBufferLike(src.newStringBufferLike());
243 int rmf = r.matchedFrom();
244 for (int ii = pos; ii < rmf; ii++)
246 sb.append(src.charAt(ii));
249 for (ReplaceRule x = rp; x != null; x = x.next)
252 if (x instanceof SpecialRule)
254 if (x instanceof WantMoreTextReplaceRule && want_more_text_enable)
256 want_more_text = true;
258 else if (x instanceof PushRule)
260 RegHolder rh2 = new RegHolder();
261 rh2.me = ((PushRule) x).NewRule;
265 else if (x instanceof PopRule)
272 else if (x instanceof ChangeRule)
274 rh.me = ((ChangeRule) x).NewRule;
285 boolean want_more_text = false, want_more_text_enable = false;
287 public boolean WantMoreText()
289 return want_more_text;
293 * Another form of apply, it is the same as apply(r,r.getReplaceRule()).
295 public void apply(Regex r)
297 apply(r, r.getReplaceRule());
301 * This finishes the replacement, appending the right() part of the last
302 * RegRes given to substitute(RegRes). After this method is called, the
303 * Replace object is reset to perform another substitution. If no RegRes
304 * objects with a true didMatch are applied, this returns null.
306 public StringLike finish()
312 // sb.append(src.substring(pos,src.length()));
313 int s_end = src.length();
314 for (int ii = pos; ii < s_end; ii++)
316 sb.append(src.charAt(ii));
321 StringLike retstr = sb.toStringLike();
326 int lastMatchedTo = 0;
328 public Object clone()
330 Replacer r = new Replacer();
335 r.lastMatchedTo = lastMatchedTo;
336 r.want_more_text = want_more_text;
337 r.want_more_text_enable = want_more_text_enable;
343 public int lastMatchedTo()
345 return lastMatchedTo;
348 public Regex getRegex()
353 public void setSource(StringLike sl)
358 public void setBuffer(StringBufferLike sbl)
363 public void setPos(int pos)