View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/StringUtil.java,v 1.5 2004/10/17 07:02:30 hkollmann Exp $
3    * $Revision: 1.5 $
4    * $Date: 2004/10/17 07:02:30 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.util;
25  
26  
27  import java.util.StringTokenizer;
28  import java.util.Vector;
29  
30  
31  
32  
33  /***
34   * This utility-class provides convenience methods for parsing strings and
35   * generating certain data structures
36   *
37   * @author Joe Peer
38   */
39  public class StringUtil {
40  
41  	/***
42  	    * Method for parsing substring embedded by constant delimeters.
43  	    * <br>
44  	    * consider the following string s: ac_update_3_12
45  	    * <br>
46  	    * <pre>
47  	    *  getEmbeddedString(s, 0, '_') ==> "ac"
48  	    *  getEmbeddedString(s, 1, '_') ==> "update"
49  	    *  getEmbeddedString(s, 2, '_') ==> "3"
50  	    *  getEmbeddedString(s, 3, '_') ==> "12"
51  	    *  getEmbeddedString(s, 3, '_') ==> will throw a Runtime Exception
52  	    * </pre>
53  	    *
54  	    * @param str the string to parse
55  	    * @param afterDelims the delimiter occurence where to start to parse
56  	    * @param delim the delimiter string
57  	    * @return the substring contained between the <code>afterDelims</code> delimiter occurence
58  	    *         and the next one
59  	    */
60  	   public static String getEmbeddedString(String str,
61  	                                          int    afterDelims,
62  	                                          char   delim) {
63  	      int lastIndex = 0;
64  	
65  	      for (int i = 0; i < afterDelims; i++) {
66  	         lastIndex = str.indexOf(delim, lastIndex) + 1; // search end of cutting
67  	      }
68  	
69  	      int nextIndex = str.indexOf(delim, lastIndex); // end of cutting
70  	
71  	      if (nextIndex == -1) {
72  	         // new: 18.1.2001: support for IMAGE buttons #checkme: can we swith this off?
73  	         int dotIndex = str.lastIndexOf('.');
74  	         nextIndex = (dotIndex == -1) ? str.length()
75  	                                      : dotIndex;
76  	      }
77  	
78  	      return str.substring(lastIndex, nextIndex);
79  	   }
80  
81  	/***
82  	    *  Get the int value of the substring contained between
83  	    *  the <code>afterDelims</code> delimiter occurence and the next one
84  	    *
85  	    * @param str the string to parse
86  	    * @param afterDelims the delimiter occurence where to start to parse
87  	    * @param delim the delimiter string
88  	    * @return the int value of the substring contained between
89  	    *         the <code>afterDelims</code> delimiter occurence and the next one
90  	    */
91  	   public static int getEmbeddedStringAsInteger(String str,
92  	                                                int    afterDelims,
93  	                                                char   delim) {
94  	      return Integer.parseInt(getEmbeddedString(str, afterDelims, delim));
95  	   }
96  
97  	/***
98  	    * Method for parsing substring embedded by constant delimeters
99  	    * <br>
100 	    * Because getEmbeddedString() support "." for image button, this method do
101 	    * the the same, but ignore dots. It's a patch and must be revised in the
102 	    * next cleanup... #checkme
103 	    * <br>
104 	    * consider the following string s: English-001:param1, param2
105 	    *
106 	    * <pre>
107 	    *  getEmbeddedString(s, 0, '_') ==> ""
108 	    *  getEmbeddedString(s, 1, '_') ==> ""
109 	    *  getEmbeddedString(s, 2, '_') ==> ""
110 	    *  getEmbeddedString(s, 3, '_') ==> ""
111 	    *  getEmbeddedString(s, 3, '_') ==> will throw a Runtime Exception
112 	    * </pre>
113 	    *
114 	    * @param str the string to parse
115 	    * @param afterDelims the delimiter occurence where to start to parse
116 	    * @param delim the delimiter string
117 	    *
118 	    * @return the substring contained between the <code>afterDelims</code> delimiter occurence
119 	    *         and the next one
120 	    */
121 	   public static String getEmbeddedStringWithoutDots(String str,
122 	                                                     int    afterDelims,
123 	                                                     char   delim) {
124 	      int lastIndex = 0;
125 	
126 	      for (int i = 0; i < afterDelims; i++) {
127 	         lastIndex = str.indexOf(delim, lastIndex) + 1; // search end of cutting
128 	      }
129 	
130 	      int nextIndex = str.indexOf(delim, lastIndex); // end of cutting
131 	
132 	      if (nextIndex == -1) {
133 	         nextIndex = str.length();
134 	      }
135 	
136 	      return str.substring(lastIndex, nextIndex);
137 	   }
138 
139 	/***
140 	    * Get a Vector object containing all the tokens
141 	    * related to the input string, splitted using the input delimiter.
142 	    *
143 	    * @param str the string to split
144 	    * @param delimeter the delimiter string
145 	    *
146 	    * @return a Vector object containing all the tokens
147 	    *         related to the input string
148 	    */
149 	   public static Vector splitString(String str,
150 	                                    String delimeter) {
151 	      Vector          result = new Vector();
152 	      StringTokenizer st = new StringTokenizer(str, delimeter);
153 	
154 	      while (st.hasMoreTokens())
155 	         result.addElement(st.nextToken());
156 	
157 	      return result;
158 	   }
159    
160 }