JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / unused / com / stevesoft / pat / RegexTokenizer.java
1 //
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
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat;
9
10 import java.util.*;
11
12 /**
13  Shareware: package pat
14  <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
15  */
16 /**
17  * The RegexTokenizer is similar to the StringTokenizer class provided with
18  * java, but allows one to tokenize using regular expressions, rather than a
19  * simple list of characters. Tokens are any strings between the supplied
20  * regular expression, as well as any backreferences (things in parenthesis)
21  * contained within the regular expression.
22  */
23 public class RegexTokenizer implements Enumeration
24 {
25   String toParse;
26
27   Regex r;
28
29   int count = 0;
30
31   Vector v = new Vector();
32
33   Vector vi = new Vector();
34
35   int pos = 0;
36
37   int offset = 1;
38
39   void getMore()
40   {
41     String s = r.right();
42     if (r.searchFrom(toParse, pos))
43     {
44       v.addElement(r.left().substring(pos));
45       vi.addElement(new Integer(r.matchFrom() + r.charsMatched()));
46       for (int i = 0; i < r.numSubs(); i++)
47       {
48         if (r.substring() != null)
49         {
50           v.addElement(r.substring(i + offset));
51           vi.addElement(new Integer(r.matchFrom(i + offset)
52                   + r.charsMatched(i + offset)));
53         }
54       }
55       pos = r.matchFrom() + r.charsMatched();
56     }
57     else if (s != null)
58     {
59       v.addElement(s);
60     }
61   }
62
63   /** Initialize the tokenizer with a string of text and a pattern */
64   public RegexTokenizer(String txt, String ptrn)
65   {
66     toParse = txt;
67     r = new Regex(ptrn);
68     offset = Regex.BackRefOffset;
69     getMore();
70   }
71
72   /** Initialize the tokenizer with a Regex object. */
73   public RegexTokenizer(String txt, Regex r)
74   {
75     toParse = txt;
76     this.r = r;
77     offset = Regex.BackRefOffset;
78     getMore();
79   }
80
81   /**
82    * This should always be cast to a String, as in StringTokenizer, and as in
83    * StringTokenizer one can do this by calling nextString().
84    */
85   public Object nextElement()
86   {
87     if (count >= v.size())
88     {
89       getMore();
90     }
91     return v.elementAt(count++);
92   }
93
94   /** This is the equivalent (String)nextElement(). */
95   public String nextToken()
96   {
97     return (String) nextElement();
98   }
99
100   /**
101    * This asks for the next token, and changes the pattern being used at the
102    * same time.
103    */
104   public String nextToken(String newpat)
105   {
106     try
107     {
108       r.compile(newpat);
109     } catch (RegSyntax r_)
110     {
111     }
112     return nextToken(r);
113   }
114
115   /**
116    * This asks for the next token, and changes the pattern being used at the
117    * same time.
118    */
119   public String nextToken(Regex nr)
120   {
121     r = nr;
122     if (vi.size() > count)
123     {
124       pos = ((Integer) vi.elementAt(count)).intValue();
125       v.setSize(count);
126       vi.setSize(count);
127     }
128     getMore();
129     return nextToken();
130   }
131
132   /** Tells whether there are more tokens in the pattern. */
133   public boolean hasMoreElements()
134   {
135     if (count >= v.size())
136     {
137       getMore();
138     }
139     return count < v.size();
140   }
141
142   /**
143    * Tells whether there are more tokens in the pattern, but in the fashion of
144    * StringTokenizer.
145    */
146   public boolean hasMoreTokens()
147   {
148     return hasMoreElements();
149   }
150
151   /** Determines the # of remaining tokens */
152   public int countTokens()
153   {
154     int _count = count;
155     while (hasMoreTokens())
156     {
157       nextToken();
158     }
159     count = _count;
160     return v.size() - count;
161   }
162
163   /** Returns all tokens in the String */
164   public String[] allTokens()
165   {
166     countTokens();
167     String[] ret = new String[v.size()];
168     v.copyInto(ret);
169     return ret;
170   }
171 };