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