001 /***************************************************************************************************
002 * MODULE DESCRIPTION
003 ****************************************************************************************************
004 *
005 * NAME: INSERT_FILE_NAME.java
006 * LANGUAGE: Java2
007 * DATE: INSERT_DATE
008 * AUTHOR: INSERT_AUTHOR, Jyväskylän yliopisto
009 *
010 ****************************************************************************************************
011 * COPYRIGHT (C) KIURU-PROJEKTIRYHMÄ
012 * Limited rights granted. Please refer to license
013 ****************************************************************************************************
014 *
015 ****************************************************************************************************
016 * UPDATES
017 ****************************************************************************************************
018 *
019 * 5.12.2002 Initial release
020 *
021 ****************************************************************************************************/
022 package kiurubeans;
023 import java.util.StringTokenizer;
024 /**
025 * A String tokenizer-like class that supports phrases. Delimiters inside
026 * phrases are ignored.
027 *
028 * @author Miika Nurminen
029 */
030 public class PhraseStringTokenizer implements java.util.Enumeration {
031 // Attributes
032 /** Remaining String to parse*/
033 private StringTokenizer st = null;
034 /** Phrase delimiters */
035 private String phraseDelim = null;
036 /** Token delimiters */
037 private String delim = null;
038
039 // Constructors
040
041 /**
042 * Creates a new instance of PhraseStringTokenizer
043 *
044 * @param str String to parse
045 * @param delim Delimiters used
046 * @param phraseDelim Phrase delimiters used
047 */
048 public PhraseStringTokenizer(String str, String delim, String phraseDelim) {
049 if (KiuruString.anyCharIndexOf(delim,phraseDelim)!=-1) throw
050 new IllegalArgumentException("Delims and phrasedelims must be separate!");
051 st = new StringTokenizer(str,delim+phraseDelim,true);
052 this.phraseDelim = phraseDelim;
053 this.delim = delim;
054 }
055
056 /** Tests if this enumeration contains more elements.
057 *
058 * @return <code>true</code> if and only if this enumeration object
059 * contains at least one more element to provide;
060 * <code>false</code> otherwise.
061 *
062 */
063 public boolean hasMoreElements() {
064 return st.hasMoreElements();
065 }
066
067 /** Returns the next element of this enumeration if this enumeration
068 * object has at least one more element to provide.
069 * For example: Let input = a|b|"c"|d
070 * Returns<br/>
071 * a<br/>
072 * b<br/>
073 * "c"<br/>
074 * d<br/>
075 * @return the next element of this enumeration.
076 */
077 public Object nextElement() {
078 return nextToken();
079 }
080
081 /** Returns the next element of this enumeration if this enumeration
082 * object has at least one more element to provide.
083 * For example: Let input = a|b|"c"|d
084 * Returns<br/>
085 * a<br/>
086 * b<br/>
087 * "c"<br/>
088 * d<br/>
089 * @return the next element of this enumeration.
090 */
091 public String nextToken() {
092 boolean phrasestate = false;
093 StringBuffer result = new StringBuffer(10);
094 String s = null;
095 do {
096 do {
097 if (st.hasMoreTokens()==false) return result.toString();
098 s = st.nextToken();
099 } while ( (KiuruString.anyCharIndexOf(s, delim)!=-1) && (phrasestate==false) );
100
101 result.append(s);
102 if (KiuruString.anyCharIndexOf(s, phraseDelim)!=-1) {
103 if (phrasestate) return result.toString();
104 else phrasestate=true;
105 }
106 } while ((phrasestate==true) || (st.hasMoreTokens()==false));
107 return result.toString();
108 }
109
110
111 /**
112 * Unit test.
113 * @param args command line arguments (not used).
114 */
115 public static void main(String[] args) {
116 String[] t = {"Test \"another ffdsf\" fdsf |dsfds",
117 "\"test\",fjdslfkjsda , , \",fdsf,fds fd\"fdsfds\" dfg",
118 " 'test' , fjdslfkjsda , , \",fdsf,fds fd\"fdsfds "};
119 for (int i=0; i<t.length; i++) {
120 PhraseStringTokenizer p = new PhraseStringTokenizer(t[i],", ","\"'");
121 System.out.println(t[i]);
122 while (p.hasMoreElements()) {
123 System.out.println(p.nextElement()+"<<");
124 }
125 System.out.println("<end>");
126 }
127 for (int i=0; i<t.length; i++) {
128 PhraseStringTokenizer p = new PhraseStringTokenizer(t[i],", ","");
129 System.out.println(t[i]);
130 while (p.hasMoreElements()) {
131 System.out.println(p.nextElement());
132 }
133 System.out.println("<end>");
134 }
135 for (int i=0; i<t.length; i++) {
136 PhraseStringTokenizer p = new PhraseStringTokenizer(t[i],"","\"");
137 System.out.println(t[i]);
138 while (p.hasMoreElements()) {
139 System.out.println(p.nextElement());
140 }
141 System.out.println("<end>");
142 }
143 }
144 }
145 /***************************************************************************************************
146 * COPYRIGHT (C) KIURU-PROJEKTIRYHMÄ
147 * Limited rights granted. Please refer to license
148 ****************************************************************************************************/