KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > util > StringQuoteTokenizer


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller 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 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.util;
27
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31
32 /**
33     class StringQuoteTokenizer
34     acts like the java.util.StringTokenizer, but treats as a single token
35     multiple tokens enclosed in quotation marks
36
37     default behaviour: trats " as quoting character
38 **/

39
40 public class StringQuoteTokenizer
41 {
42     private StringTokenizer JavaDoc st;
43     private boolean inQuote=false;
44     private String JavaDoc origDelim;
45   private int escapeType;
46
47   public static final int NO_ESCAPE=0;
48   public static final int ESCAPE_ALWAYS=1;
49   public static final int ESCAPE_IN_QUOTES=2;
50
51     public StringQuoteTokenizer(String JavaDoc str,String JavaDoc delim)
52     {
53     init(str,delim,NO_ESCAPE);
54     }
55     public StringQuoteTokenizer(String JavaDoc str,String JavaDoc delim, int escapeType)
56     {
57     init(str,delim,escapeType);
58     }
59
60     StringQuoteTokenizer(String JavaDoc str)
61     {
62     init(str," \t\n\r\f",NO_ESCAPE);
63     }
64
65   private void init(String JavaDoc str, String JavaDoc delim, int escape){
66         st=new StringTokenizer JavaDoc(str,delim);
67         origDelim=delim;
68     escapeType=escape;
69   }
70
71     public boolean hasMoreElements(){
72             return hasMoreTokens();
73     }
74
75     public boolean hasMoreTokens(){
76         return st.hasMoreTokens();
77     }
78
79     public Object JavaDoc nextElement(){
80         return (Object JavaDoc)nextToken();
81     }
82
83     public String JavaDoc nextToken(){
84         String JavaDoc token=st.nextToken(origDelim);
85     //System.err.println("Orig t='"+token+"'");
86
inQuote=false;
87     boolean quoted=false;
88         if(token.startsWith("\"")){
89         //System.err.println("in quoteg t='"+token+"'");
90
inQuote=true;
91       quoted=true;
92             token=token.substring(1);
93       //loop until it reach another "
94
//but a \" is not OK
95
if(token.endsWith("\"") && !token.endsWith("\\\""))
96             {
97                 inQuote=false;
98                 token=token.substring(0,token.length()-1);
99             }
100         }
101         //loop until it reachs a token with only a <"> or of the form <abc">
102
String JavaDoc token2=token;
103         if(inQuote)
104         {
105         //System.err.println("inquote 2t='"+token+"'");
106
boolean ok=false;
107       while(!ok)
108       {
109             try
110         {
111                 token2+=st.nextToken("\"");
112             //System.err.println("token 222t='"+token2+"'");
113
if(!token2.endsWith("\\\"") || escapeType==NO_ESCAPE){
114             ok=true;
115             st.nextToken(origDelim);
116           }
117
118             }
119         catch(NoSuchElementException JavaDoc e){
120           break;
121         }
122       }
123         }
124     return token2;
125     }
126
127     /* test main*/
128     public static void main(String JavaDoc[] argv){
129         String JavaDoc test="12 sdf asdf \"opo opo opo \" sadf ' sdf s \"wererw\" 122 \"uno due\"";
130         System.out.println("'"+test+"'");
131         StringQuoteTokenizer sqt=new StringQuoteTokenizer(test);
132         while(sqt.hasMoreTokens()){
133             System.out.println("> '"+sqt.nextToken()+"'");
134         }
135     }
136 }
Popular Tags