KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sqlmagic > tinysql > UtilString


1 /*
2  * This class provides string manipulation methods.
3  *
4  * $Author: davis $
5  * $Date: 2004/12/18 21:23:31 $
6  * $Revision: 1.1 $
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * Revision History:
23  *
24  * Written by Davis Swan in February, 2004.
25  */

26 package com.sqlmagic.tinysql;
27
28 import java.text.*;
29 import java.util.*;
30 import java.lang.*;
31 import java.sql.Types JavaDoc;
32
33 public class UtilString
34 {
35 /*
36  * Is this a quoted string?
37  */

38    public static boolean isQuotedString(String JavaDoc inputString)
39    {
40       String JavaDoc trimString;
41       int trimLength;
42       if ( inputString == (String JavaDoc)null ) return false;
43       trimString = inputString.trim();
44       trimLength = trimString.length();
45       if ( trimString.length() == 0 ) return false;
46       if ( ( trimString.charAt(0) == '\'' &
47              trimString.charAt(trimLength - 1) == '\'' ) |
48            ( trimString.charAt(0) == '"' &
49              trimString.charAt(trimLength - 1) == '"' ) )
50       {
51          return true;
52       }
53       return false;
54    }
55 /*
56  * Remove enclosing quotes from a string.
57  */

58    public static String JavaDoc removeQuotes(String JavaDoc inputString)
59    {
60       String JavaDoc trimString;
61       int trimLength;
62       if ( inputString == (String JavaDoc)null ) return inputString;
63       trimString = inputString.trim();
64       trimLength = trimString.length();
65       if ( trimString.length() == 0 ) return inputString;
66       if ( ( trimString.charAt(0) == '\'' &
67              trimString.charAt(trimLength - 1) == '\'' ) |
68            ( trimString.charAt(0) == '"' &
69              trimString.charAt(trimLength - 1) == '"' ) )
70       {
71          return trimString.substring(1,trimString.length() - 1);
72       }
73       return inputString;
74    }
75 /*
76  * Convert a string to a double or return a default value.
77  */

78    public static double doubleValue(String JavaDoc inputString)
79    {
80       return doubleValue(inputString,Double.MIN_VALUE);
81    }
82    public static double doubleValue(String JavaDoc inputString,double defaultValue )
83    {
84       try
85       {
86          return Double.parseDouble(inputString);
87       } catch (Exception JavaDoc e) {
88          return defaultValue;
89       }
90    }
91 /*
92  * The following method replaces all occurrences of oldString with newString
93  * in the inputString. This function can be replaced with the native
94  * String method replaceAll in JDK 1.4 and above but is provide to support
95  * earlier versions of the JRE.
96  */

97    public static String JavaDoc replaceAll(String JavaDoc inputString,String JavaDoc oldString,
98       String JavaDoc newString)
99    {
100       StringBuffer JavaDoc outputString = new StringBuffer JavaDoc(100);
101       int startIndex=0,nextIndex;
102       while ( inputString.substring(startIndex).indexOf(oldString) > -1 )
103       {
104          nextIndex = startIndex + inputString.substring(startIndex).indexOf(oldString);
105          if ( nextIndex > startIndex )
106          {
107             outputString.append(inputString.substring(startIndex,nextIndex));
108          }
109          outputString.append(newString);
110          startIndex = nextIndex + oldString.length();
111       }
112       if ( startIndex <= inputString.length() - 1 )
113       {
114          outputString.append(inputString.substring(startIndex));
115       }
116       return outputString.toString();
117    }
118 /*
119  * Convert a string to an int or return a default value.
120  */

121    public static int intValue(String JavaDoc inputString,int defaultValue )
122    {
123       try
124       {
125          return Integer.parseInt(inputString);
126       } catch (Exception JavaDoc e) {
127          return defaultValue;
128       }
129    }
130 /*
131  * This method formats an action Hashtable for display.
132  */

133    public static String JavaDoc actionToString(Hashtable displayAction)
134    {
135       StringBuffer JavaDoc displayBuffer = new StringBuffer JavaDoc();
136       String JavaDoc displayType,tableName,columnContext;
137       tinySQLWhere displayWhere;
138       tsColumn createColumn;
139       boolean groupBy=false,orderBy=false;
140       int i;
141       Vector displayTables,displayColumns,columnDefs,displayValues,
142       displayContext;
143       displayType = (String JavaDoc)displayAction.get("TYPE");
144       displayBuffer.append(displayType + " ");
145       displayWhere = (tinySQLWhere)null;
146       displayContext = (Vector)null;
147       displayColumns = (Vector)null;
148       if ( displayType.equals("SELECT") )
149       {
150          displayTables = (Vector)displayAction.get("TABLES");
151          displayColumns = (Vector)displayAction.get("COLUMNS");
152          displayContext = (Vector)displayAction.get("CONTEXT");
153          displayWhere = (tinySQLWhere)displayAction.get("WHERE");
154          for ( i = 0; i < displayColumns.size(); i++ )
155          {
156             columnContext = (String JavaDoc)displayContext.elementAt(i);
157             if ( columnContext.equals("GROUP") )
158             {
159                groupBy = true;
160                continue;
161             } else if ( columnContext.equals("ORDER") ) {
162                orderBy = true;
163                continue;
164             }
165             if ( i > 0 ) displayBuffer.append(",");
166             displayBuffer.append((String JavaDoc)displayColumns.elementAt(i));
167          }
168          displayBuffer.append(" FROM " );
169          for ( i = 0; i < displayTables.size(); i++ )
170          {
171             if ( i > 0 ) displayBuffer.append(",");
172             displayBuffer.append((String JavaDoc)displayTables.elementAt(i));
173          }
174       } else if ( displayType.equals("DROP_TABLE") ) {
175          tableName = (String JavaDoc)displayAction.get("TABLE");
176          displayBuffer.append(tableName);
177       } else if ( displayType.equals("CREATE_TABLE") ) {
178          tableName = (String JavaDoc)displayAction.get("TABLE");
179          displayBuffer.append(tableName + " (");
180          columnDefs = (Vector)displayAction.get("COLUMN_DEF");
181          for ( i = 0; i < columnDefs.size(); i++ )
182          {
183             if ( i > 0 ) displayBuffer.append(",");
184             createColumn = (tsColumn)columnDefs.elementAt(i);
185             displayBuffer.append(createColumn.name + " " + createColumn.type
186             + "( " + createColumn.size + "," + createColumn.decimalPlaces
187             + ")");
188          }
189          displayBuffer.append(")");
190       } else if ( displayType.equals("INSERT") ) {
191          tableName = (String JavaDoc)displayAction.get("TABLE");
192          displayBuffer.append("INTO " + tableName + "(");
193          displayColumns = (Vector)displayAction.get("COLUMNS");
194          for ( i = 0; i < displayColumns.size(); i++ )
195          {
196             if ( i > 0 ) displayBuffer.append(",");
197             displayBuffer.append((String JavaDoc)displayColumns.elementAt(i));
198          }
199          displayBuffer.append(") VALUES (");
200          displayValues = (Vector)displayAction.get("VALUES");
201          for ( i = 0; i < displayValues.size(); i++ )
202          {
203             if ( i > 0 ) displayBuffer.append(",");
204             displayBuffer.append((String JavaDoc)displayValues.elementAt(i));
205          }
206          displayBuffer.append(")");
207       } else if ( displayType.equals("UPDATE") ) {
208          tableName = (String JavaDoc)displayAction.get("TABLE");
209          displayBuffer.append(tableName + " SET ");
210          displayColumns = (Vector)displayAction.get("COLUMNS");
211          displayValues = (Vector)displayAction.get("VALUES");
212          displayWhere = (tinySQLWhere)displayAction.get("WHERE");
213          for ( i = 0; i < displayColumns.size(); i++ )
214          {
215             if ( i > 0 ) displayBuffer.append(",");
216             displayBuffer.append((String JavaDoc)displayColumns.elementAt(i)
217             + "=" + (String JavaDoc)displayValues.elementAt(i));
218          }
219       } else if ( displayType.equals("DELETE") ) {
220          tableName = (String JavaDoc)displayAction.get("TABLE");
221          displayBuffer.append(" FROM " + tableName);
222          displayWhere = (tinySQLWhere)displayAction.get("WHERE");
223       }
224       if ( displayWhere != (tinySQLWhere)null )
225       {
226          displayBuffer.append(displayWhere.toString());
227       }
228       if ( groupBy )
229       {
230          displayBuffer.append(" GROUP BY ");
231          for ( i = 0; i < displayColumns.size(); i++ )
232          {
233             columnContext = (String JavaDoc)displayContext.elementAt(i);
234             if ( !columnContext.equals("GROUP") ) continue;
235             if ( !displayBuffer.toString().endsWith(" GROUP BY ") )
236                displayBuffer.append(",");
237             displayBuffer.append((String JavaDoc)displayColumns.elementAt(i));
238          }
239       }
240       if ( orderBy )
241       {
242          displayBuffer.append(" ORDER BY ");
243          for ( i = 0; i < displayColumns.size(); i++ )
244          {
245             columnContext = (String JavaDoc)displayContext.elementAt(i);
246             if ( !columnContext.equals("ORDER") ) continue;
247             if ( !displayBuffer.toString().endsWith(" ORDER BY ") )
248                displayBuffer.append(",");
249             displayBuffer.append((String JavaDoc)displayColumns.elementAt(i));
250          }
251       }
252       return displayBuffer.toString();
253    }
254 /*
255  * Find the input table alias in the list provided and return the table name.
256  */

257    public static String JavaDoc findTableForAlias(String JavaDoc inputAlias,Vector tableList)
258                               throws tinySQLException
259    {
260       int i,aliasAt;
261       String JavaDoc tableAndAlias;
262       tableAndAlias = findTableAlias(inputAlias,tableList);
263       aliasAt = tableAndAlias.indexOf("->");
264       return tableAndAlias.substring(0,aliasAt);
265    }
266 /*
267  * Find the input table alias in the list provided and return the table name
268  * and alias in the form tableName=tableAlias.
269  */

270    public static String JavaDoc findTableAlias(String JavaDoc inputAlias,Vector tableList)
271                               throws tinySQLException
272    {
273       int i,aliasAt;
274       String JavaDoc tableAndAlias,tableName,tableAlias;
275       for ( i = 0; i < tableList.size(); i++ )
276       {
277          tableAndAlias = (String JavaDoc)tableList.elementAt(i);
278          aliasAt = tableAndAlias.indexOf("->");
279          tableName = tableAndAlias.substring(0,aliasAt);
280          tableAlias = tableAndAlias.substring(aliasAt + 2);
281          if ( inputAlias.equals(tableAlias) )
282          {
283             return tableAndAlias;
284          }
285       }
286       throw new tinySQLException("Unable to identify table alias "
287       + inputAlias);
288    }
289 /*
290  * Determine a type for the input string.
291  */

292    public static int getValueType (String JavaDoc inputValue)
293    {
294       double doubleValue,floorValue;
295       long intValue;
296       if ( inputValue.startsWith("\"") |
297            inputValue.startsWith("'") )
298          return Types.CHAR;
299       try
300       {
301          doubleValue = Double.parseDouble(inputValue.trim());
302          floorValue = Math.floor(doubleValue);
303          if ( floorValue == doubleValue )
304             return Types.INTEGER;
305          else
306             return Types.FLOAT;
307       } catch (Exception JavaDoc e) {
308          return Types.CHAR;
309       }
310    }
311 }
312
Popular Tags