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 com.stevesoft.pat.wrap.*;
\r
12 /** Internally used class. */
\r
16 RegHolder prev = null;
\r
19 /** Internally used class.
\r
26 CodeVal(int p, char c)
\r
32 public String toString()
\r
34 return "(" + pos + "," + code + ")";
\r
39 To use this class, first use either the getReplacer() method from
\r
40 Transformer or Regex. You can then use replaceAll, replaceFirst,
\r
41 etc. methods on the Replacer in the same way that you can from
\r
42 either of those two classes.
\r
44 The only potential difference between using the methods of
\r
45 Replacer to do the replacing is that Replacer remembers changes
\r
46 to the replacing object between calls to replaceAll, replaceFirst
\r
47 etc. For details, see the example file
\r
48 <a href="http://javaregex.com/code/trans3.java.html">trans3.java</a>.
\r
49 @see com.stevesoft.pat.Transformer
\r
50 @see com.stevesoft.pat.Regex
\r
52 public class Replacer
\r
56 /** Instantiate a new Replacer. */
\r
60 public StringLike replaceFirstRegion(String s, Regex r,
\r
63 return replaceFirstRegion(new StringWrap(s), r, start, end);
\r
66 /** This method replaces the first occurence of the Regex in the
\r
67 String starting with position pos
\r
68 according to the Replacer rule of this object. */
\r
69 public StringLike replaceFirstRegion(StringLike s, Regex r,
\r
75 return dorep(s, start, end);
\r
78 public StringLike replaceFirst(StringLike s)
\r
80 return replaceFirstRegion(s, 0, s.length());
\r
83 public StringLike replaceFirstFrom(StringLike s, int start)
\r
85 return replaceFirstRegion(s, start, s.length());
\r
88 public StringLike replaceFirstRegion(StringLike s, int start, int end)
\r
91 return dorep(s, start, end);
\r
94 RegHolder rh = new RegHolder();
\r
96 public StringLike replaceAllRegion(String s, Regex r,
\r
99 return replaceAllRegion(new StringWrap(s), r, start, end);
\r
102 /** This method replaces all occurences of the Regex in the
\r
103 String starting with postition pos
\r
104 according to the Replacer rule of this object. */
\r
105 public StringLike replaceAllRegion(StringLike s, Regex r,
\r
106 int start, int end)
\r
112 return dorep(s, start, end);
\r
115 public StringLike replaceAll(StringLike s)
\r
117 return replaceAllRegion(s, 0, s.length());
\r
120 public StringLike replaceAllFrom(StringLike s, int start)
\r
122 return replaceAllRegion(s, start, s.length());
\r
125 public StringLike replaceAllRegion(StringLike s, int start, int end)
\r
128 return dorep(s, start, end);
\r
131 public String replaceAll(String s)
\r
133 return replaceAllRegion(new StringWrap(s), 0, s.length()).toString();
\r
136 public String replaceAllFrom(String s, int start)
\r
138 return replaceAllRegion(new StringWrap(s), start, s.length()).toString();
\r
141 public String replaceAllRegion(String s, int start, int end)
\r
144 return dorep(new StringWrap(s), start, end).toString();
\r
147 final public boolean isSpecial(ReplaceRule x)
\r
151 if (x instanceof SpecialRule
\r
153 (x instanceof RuleHolder && ( (RuleHolder) x).held instanceof SpecialRule))
\r
162 final public void apply1(RegRes rr)
\r
164 rr.charsMatched_++;
\r
166 rr.charsMatched_--;
\r
169 final StringLike dorep(StringLike s, int start, int end)
\r
171 StringLike ret = s;
\r
172 want_more_text = false;
\r
176 throw new NullPointerException("Replacer has null Regex pointer");
\r
178 if (rh.me._search(s, start, end))
\r
180 int rmn = rh.me.matchedTo();
\r
181 if (rh.me.charsMatched() == 0 && !isSpecial(rh.me.getReplaceRule()))
\r
190 !want_more_text && rh.me._search(s, i, end); i = rmn)
\r
192 rmn = rh.me.matchedTo();
\r
193 if (rh.me.charsMatched() == 0)
\r
195 if (!isSpecial(rh.me.getReplaceRule()))
\r
205 ret = ret == null ? s : ret;
\r
210 StringBufferLike sb = null;
\r
211 StringLike src = null;
\r
213 /** This method allows you to apply the results of several
\r
214 matches in a sequence to modify a String of text. Each
\r
215 call in the sequence must operate on the same piece of
\r
216 text and the matchedFrom() of each RegRes given to this
\r
217 method must be greater in value than the preceeding
\r
218 RegRes's matchedTo() value.
\r
220 public void apply(RegRes r, ReplaceRule rp)
\r
222 if (rp == null || (rp.next == null && rp instanceof AmpersandRule))
\r
230 src = r.getStringLike();
\r
234 sb = new StringBufferLike(src.newStringBufferLike());
\r
236 int rmf = r.matchedFrom();
\r
237 for (int ii = pos; ii < rmf; ii++)
\r
239 sb.append(src.charAt(ii));
\r
242 for (ReplaceRule x = rp; x != null; x = x.next)
\r
245 if (x instanceof SpecialRule)
\r
247 if (x instanceof WantMoreTextReplaceRule
\r
248 && want_more_text_enable)
\r
250 want_more_text = true;
\r
252 else if (x instanceof PushRule)
\r
254 RegHolder rh2 = new RegHolder();
\r
255 rh2.me = ( (PushRule) x).NewRule;
\r
259 else if (x instanceof PopRule)
\r
261 if (rh.prev != null)
\r
266 else if (x instanceof ChangeRule)
\r
268 rh.me = ( (ChangeRule) x).NewRule;
\r
272 if (!want_more_text)
\r
274 pos = r.matchedTo();
\r
279 boolean want_more_text = false, want_more_text_enable = false;
\r
280 public boolean WantMoreText()
\r
282 return want_more_text;
\r
285 /** Another form of apply, it is the same as
\r
286 apply(r,r.getReplaceRule()). */
\r
287 public void apply(Regex r)
\r
289 apply(r, r.getReplaceRule());
\r
292 /** This finishes the replacement, appending the right() part of
\r
293 the last RegRes given to substitute(RegRes). After this method
\r
294 is called, the Replace object is reset to perform another
\r
295 substitution. If no RegRes objects with a true didMatch are
\r
296 applied, this returns null. */
\r
297 public StringLike finish()
\r
303 //sb.append(src.substring(pos,src.length()));
\r
304 int s_end = src.length();
\r
305 for (int ii = pos; ii < s_end; ii++)
\r
307 sb.append(src.charAt(ii));
\r
310 lastMatchedTo = pos;
\r
312 StringLike retstr = sb.toStringLike();
\r
317 int lastMatchedTo = 0;
\r
318 public Object clone()
\r
320 Replacer r = new Replacer();
\r
325 r.lastMatchedTo = lastMatchedTo;
\r
326 r.want_more_text = want_more_text;
\r
327 r.want_more_text_enable = want_more_text_enable;
\r
329 r.rh.prev = rh.prev;
\r
333 public int lastMatchedTo()
\r
335 return lastMatchedTo;
\r
338 public Regex getRegex()
\r
343 public void setSource(StringLike sl)
\r
348 public void setBuffer(StringBufferLike sbl)
\r
353 public void setPos(int pos)
\r