Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: postgresql/util/PGtokenizer.java


1   package postgresql.util;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   /**
7    * This class is used to tokenize the text output of postgres.
8    *
9    * <p>It's mainly used by the geometric classes, but is useful in parsing any
10   * output from custom data types output from postgresql.
11   *
12   * @see postgresql.geometric.PGbox
13   * @see postgresql.geometric.PGcircle
14   * @see postgresql.geometric.PGlseg
15   * @see postgresql.geometric.PGpath
16   * @see postgresql.geometric.PGpoint
17   * @see postgresql.geometric.PGpolygon
18   */
19  public class PGtokenizer
20  {
21    // Our tokens
22    protected Vector  tokens;
23    
24    /**
25     * Create a tokeniser.
26     *
27     * <p>We could have used StringTokenizer to do this, however, we needed to
28     * handle nesting of '(' ')' '[' ']' '&lt;' and '&gt;' as these are used
29     * by the geometric data types.
30     *
31     * @param string containing tokens
32     * @param delim single character to split the tokens
33     */
34    public PGtokenizer(String string,char delim)
35    {
36      tokenize(string,delim);
37    }
38    
39    /**
40     * This resets this tokenizer with a new string and/or delimiter.
41     *
42     * @param string containing tokens
43     * @param delim single character to split the tokens
44     */
45    public int tokenize(String string,char delim)
46    {
47      tokens = new Vector();
48      
49      // nest holds how many levels we are in the current token.
50      // if this is > 0 then we don't split a token when delim is matched.
51      //
52      // The Geometric datatypes use this, because often a type may have others
53      // (usualls PGpoint) imbedded within a token.
54      //
55      // Peter 1998 Jan 6 - Added < and > to the nesting rules
56      int nest=0,p,s;
57      
58      for(p=0,s=0;p<string.length();p++) {
59        char c = string.charAt(p);
60        
61        // increase nesting if an open character is found
62        if(c == '(' || c == '[' || c == '<')
63    nest++;
64        
65        // decrease nesting if a close character is found
66        if(c == ')' || c == ']' || c == '>')
67    nest--;
68        
69        if(nest==0 && c==delim) {
70    tokens.addElement(string.substring(s,p));
71    s=p+1; // +1 to skip the delimiter
72        }
73        
74      }
75      
76      // Don't forget the last token ;-)
77      if(s<string.length())
78        tokens.addElement(string.substring(s));
79      
80      return tokens.size();
81    }
82    
83    /**
84     * @return the number of tokens available
85     */
86    public int getSize()
87    {
88      return tokens.size();
89    }
90    
91    /**
92     * @param n Token number ( 0 ... getSize()-1 )
93     * @return The token value
94     */
95    public String getToken(int n)
96    {
97      return (String)tokens.elementAt(n);
98    }
99    
100   /**
101    * This returns a new tokenizer based on one of our tokens.
102    *
103    * The geometric datatypes use this to process nested tokens (usually
104    * PGpoint).
105    *
106    * @param n Token number ( 0 ... getSize()-1 )
107    * @param delim The delimiter to use
108    * @return A new instance of PGtokenizer based on the token
109    */
110   public PGtokenizer tokenizeToken(int n,char delim)
111   {
112     return new PGtokenizer(getToken(n),delim);
113   }
114   
115   /**
116    * This removes the lead/trailing strings from a string
117    * @param s Source string
118    * @param l Leading string to remove
119    * @param t Trailing string to remove
120    * @return String without the lead/trailing strings
121    */
122   public static String remove(String s,String l,String t)
123   {
124     if(s.startsWith(l))  s = s.substring(l.length());
125     if(s.endsWith(t))  s = s.substring(0,s.length()-t.length());
126     return s;
127   }
128   
129   /**
130    * This removes the lead/trailing strings from all tokens
131    * @param l Leading string to remove
132    * @param t Trailing string to remove
133    */
134   public void remove(String l,String t)
135   {
136     for(int i=0;i<tokens.size();i++) {
137       tokens.setElementAt(remove((String)tokens.elementAt(i),l,t),i);
138     }
139   }
140   
141   /**
142    * Removes ( and ) from the beginning and end of a string
143    * @param s String to remove from
144    * @return String without the ( or )
145    */
146   public static String removePara(String s)
147   {
148     return remove(s,"(",")");
149   }
150   
151   /**
152    * Removes ( and ) from the beginning and end of all tokens
153    * @return String without the ( or )
154    */
155   public void removePara()
156   {
157     remove("(",")");
158   }
159   
160   /**
161    * Removes [ and ] from the beginning and end of a string
162    * @param s String to remove from
163    * @return String without the [ or ]
164    */
165   public static String removeBox(String s)
166   {
167     return remove(s,"[","]");
168   }
169   
170   /**
171    * Removes [ and ] from the beginning and end of all tokens
172    * @return String without the [ or ]
173    */
174   public void removeBox()
175   {
176     remove("[","]");
177   }
178   
179   /**
180    * Removes &lt; and &gt; from the beginning and end of a string
181    * @param s String to remove from
182    * @return String without the &lt; or &gt;
183    */
184   public static String removeAngle(String s)
185   {
186     return remove(s,"<",">");
187   }
188   
189   /**
190    * Removes &lt; and &gt; from the beginning and end of all tokens
191    * @return String without the &lt; or &gt;
192    */
193   public void removeAngle()
194   {
195     remove("<",">");
196   }
197 }