1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $(date) The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 *******************************************************************************/
22 // This software is now distributed according to
23 // the Lesser Gnu Public License. Please see
24 // http://www.gnu.org/copyleft/lesser.txt for
26 // -- Happy Computing!
28 package com.stevesoft.pat;
30 import jalview.util.MessageManager;
32 import com.stevesoft.pat.wrap.StringWrap;
34 /** Internally used class. */
39 RegHolder prev = null;
43 * Internally used class.
53 CodeVal(int p, char c)
59 public String toString()
61 return "(" + pos + "," + code + ")";
66 * To use this class, first use either the getReplacer() method from Transformer
67 * or Regex. You can then use replaceAll, replaceFirst, etc. methods on the
68 * Replacer in the same way that you can from either of those two classes.
70 * The only potential difference between using the methods of Replacer to do the
71 * replacing is that Replacer remembers changes to the replacing object between
72 * calls to replaceAll, replaceFirst etc. For details, see the example file <a
73 * href="http://javaregex.com/code/trans3.java.html">trans3.java</a>.
75 * @see com.stevesoft.pat.Transformer
76 * @see com.stevesoft.pat.Regex
82 /** Instantiate a new Replacer. */
87 public StringLike replaceFirstRegion(String s, Regex r, int start, int end)
89 return replaceFirstRegion(new StringWrap(s), r, start, end);
93 * This method replaces the first occurence of the Regex in the String
94 * starting with position pos according to the Replacer rule of this object.
96 public StringLike replaceFirstRegion(StringLike s, Regex r, int start,
102 return dorep(s, start, end);
105 public StringLike replaceFirst(StringLike s)
107 return replaceFirstRegion(s, 0, s.length());
110 public StringLike replaceFirstFrom(StringLike s, int start)
112 return replaceFirstRegion(s, start, s.length());
115 public StringLike replaceFirstRegion(StringLike s, int start, int end)
118 return dorep(s, start, end);
121 RegHolder rh = new RegHolder();
123 public StringLike replaceAllRegion(String s, Regex r, int start, int end)
125 return replaceAllRegion(new StringWrap(s), r, start, end);
129 * This method replaces all occurences of the Regex in the String starting
130 * with postition pos according to the Replacer rule of this object.
132 public StringLike replaceAllRegion(StringLike s, Regex r, int start,
139 return dorep(s, start, end);
142 public StringLike replaceAll(StringLike s)
144 return replaceAllRegion(s, 0, s.length());
147 public StringLike replaceAllFrom(StringLike s, int start)
149 return replaceAllRegion(s, start, s.length());
152 public StringLike replaceAllRegion(StringLike s, int start, int end)
155 return dorep(s, start, end);
158 public String replaceAll(String s)
160 return replaceAllRegion(new StringWrap(s), 0, s.length()).toString();
163 public String replaceAllFrom(String s, int start)
165 return replaceAllRegion(new StringWrap(s), start, s.length())
169 public String replaceAllRegion(String s, int start, int end)
172 return dorep(new StringWrap(s), start, end).toString();
175 final public boolean isSpecial(ReplaceRule x)
179 if (x instanceof SpecialRule
180 || (x instanceof RuleHolder && ((RuleHolder) x).held instanceof SpecialRule))
189 final public void apply1(RegRes rr)
196 final StringLike dorep(StringLike s, int start, int end)
199 want_more_text = false;
203 throw new NullPointerException(
205 .getString("exception.replace_null_regex_pointer"));
207 if (rh.me._search(s, start, end))
209 int rmn = rh.me.matchedTo();
210 if (rh.me.charsMatched() == 0 && !isSpecial(rh.me.getReplaceRule()))
218 for (int i = rmn; !want_more_text && rh.me._search(s, i, end); i = rmn)
220 rmn = rh.me.matchedTo();
221 if (rh.me.charsMatched() == 0)
223 if (!isSpecial(rh.me.getReplaceRule()))
233 ret = ret == null ? s : ret;
238 StringBufferLike sb = null;
240 StringLike src = null;
245 * This method allows you to apply the results of several matches in a
246 * sequence to modify a String of text. Each call in the sequence must operate
247 * on the same piece of text and the matchedFrom() of each RegRes given to
248 * this method must be greater in value than the preceeding RegRes's
251 public void apply(RegRes r, ReplaceRule rp)
253 if (rp == null || (rp.next == null && rp instanceof AmpersandRule))
261 src = r.getStringLike();
265 sb = new StringBufferLike(src.newStringBufferLike());
267 int rmf = r.matchedFrom();
268 for (int ii = pos; ii < rmf; ii++)
270 sb.append(src.charAt(ii));
273 for (ReplaceRule x = rp; x != null; x = x.next)
276 if (x instanceof SpecialRule)
278 if (x instanceof WantMoreTextReplaceRule && want_more_text_enable)
280 want_more_text = true;
282 else if (x instanceof PushRule)
284 RegHolder rh2 = new RegHolder();
285 rh2.me = ((PushRule) x).NewRule;
289 else if (x instanceof PopRule)
296 else if (x instanceof ChangeRule)
298 rh.me = ((ChangeRule) x).NewRule;
309 boolean want_more_text = false, want_more_text_enable = false;
311 public boolean WantMoreText()
313 return want_more_text;
317 * Another form of apply, it is the same as apply(r,r.getReplaceRule()).
319 public void apply(Regex r)
321 apply(r, r.getReplaceRule());
325 * This finishes the replacement, appending the right() part of the last
326 * RegRes given to substitute(RegRes). After this method is called, the
327 * Replace object is reset to perform another substitution. If no RegRes
328 * objects with a true didMatch are applied, this returns null.
330 public StringLike finish()
336 // sb.append(src.substring(pos,src.length()));
337 int s_end = src.length();
338 for (int ii = pos; ii < s_end; ii++)
340 sb.append(src.charAt(ii));
345 StringLike retstr = sb.toStringLike();
350 int lastMatchedTo = 0;
352 public Object clone()
354 Replacer r = new Replacer();
359 r.lastMatchedTo = lastMatchedTo;
360 r.want_more_text = want_more_text;
361 r.want_more_text_enable = want_more_text_enable;
367 public int lastMatchedTo()
369 return lastMatchedTo;
372 public Regex getRegex()
377 public void setSource(StringLike sl)
382 public void setBuffer(StringBufferLike sbl)
387 public void setPos(int pos)