1    package com.instantbank.common.utilcomponents;
2    
3    import java.util.StringTokenizer;
4    import java.util.ArrayList;
5    
6    /**
7     * Various general algorithms implemented as static methods.
8     *
9     * @author InstantBank (Rodrigo Lopez)
10    */
11   
12   public class UtilOnJdk {
13     /**
14      * A verification is OK.
15      */
16     public static final int OK = 1;
17   
18     /**
19      * A void name has been detected.
20      */
21     public static final int VOID_NAME = 2;
22   
23     /**
24      * An ill formed name has been detected.
25      */
26     public static final int BAD_SYNTAX_NAME = 3;
27   
28     /**
29      * Row separator
30      */
31     public static final char ROW_SEPARATOR = '\u00BB';
32   
33     /**
34      * Value separator
35      */
36     public static final char VALUE_SEPARATOR = '\u00B1';
37   
38   
39     /**
40      * Validates if a String is a valid name in the system.
41      * The name can contain white spaces and they are converted to exactly
42      * one occurrence between non void name parts. Letters are also converted
43      * to low case. A name part can contain
44      * any character excepting: white spaces, control characters, doublequotes,
45      * singlequotes, backquotes and backslash.
46      *
47      * @param s The string to be validated.
48      * @return  <ul>
49      *   <li>An Integer object if the name is invalid
50      *   <ul>
51      *     <li>{@link #VOID_NAME} if s has no visible characters.
52      *     <li>{@link #BAD_SYNTAX_NAME} if the name is ill formed.
53      *   </ul>
54      *   <li>The converted string if the name is valid.
55      * </ul>
56      */
57     public static Object isValidName(String s) {
58       StringTokenizer stk = new StringTokenizer(s);
59       StringBuffer sb = new StringBuffer();
60       String tok;
61       if(stk.hasMoreTokens()) {
62         tok = stk.nextToken().toLowerCase();
63         if(isValidNamePart(tok)) {
64           sb.append(tok);
65         }
66         else {
67           return new Integer(BAD_SYNTAX_NAME);
68         }
69       }
70       else {
71         return new Integer(VOID_NAME);
72       }
73   
74       while(stk.hasMoreTokens()) {
75         sb.append(' ');
76         tok = stk.nextToken().toLowerCase();
77         if(isValidNamePart(tok)) {
78           sb.append(tok);
79         }
80         else {
81           return new Integer(BAD_SYNTAX_NAME);
82         }
83       }
84   
85       return sb.toString();
86     }
87   
88   
89     /**
90      * Decides if a nonblank String can be part of a name in the system.
91      * invalid characters are: doublequote, singlequote, backquote, backslash.
92      *
93      * @param s The string to be validated.
94      * @return True if valid, false if non valid.
95      */
96     public static boolean isValidNamePart(String s) {
97       char c;
98       for(int i = 0; i < s.length(); i++) {
99         c = s.charAt(i);
100        if(c < 32 || c > 126) {  //not a "viewable normal" ascii.
101          return false;
102        }
103  
104        if(c == '"' || c == '\'' || c == '`' || c == '\\') {
105          return false;
106        }
107      }
108      return true;
109    }
110  
111  
112    /**
113     * Starting from a String, produces a new String where all internal
114     * white space sequences have length = 1 and all letters are in lowercase.
115     *
116     * @param s The starting String.
117     * @return The new String with all inner white spaces sequences' length = 1.
118     */
119    public static String squeeze(String s) {
120      return squeeze(s, ' ');
121    }
122  
123  
124    /**
125     * Starting from a String, produces a new String where each internal
126     * white space sequence is replaced by one occurrence of the
127     * "whiteReplacement" parameter and all letters are in lowercase.
128     *
129     * @param s The starting String.
130     * @param whiteReplacement The character that replaces white spaces
131     * sequences.
132     * @return The new String with all inner white spaces sequences replaced
133     * by the whiteReplacement character.
134     */
135    public static String squeeze(String s, char whiteReplacement) {
136      StringTokenizer stk = new StringTokenizer(s);
137      StringBuffer sb = new StringBuffer();
138      if(stk.hasMoreTokens()) {
139        sb.append(stk.nextToken().toLowerCase());
140      }
141      else {
142        return "";
143      }
144  
145      while(stk.hasMoreTokens()) {
146        sb.append(whiteReplacement);
147        sb.append(stk.nextToken().toLowerCase());
148      }
149  
150      return sb.toString();
151    }
152  
153  
154    /**
155     * Serialize an ArrayList to a String. The ArrayList contains arrays
156     * of String.
157     *
158     * @param rowset The set of rows to be serialized
159     * @return A String where rows are separated by the ROW_SEPARATOR
160     * non printable character and values in a row are separated by the
161     * VALUE_SEPARATOR non printable character.
162     */
163    public static String serializeRowSet(ArrayList rowset) {
164  
165      if(rowset.size() == 0) {
166        return "";
167      }
168  
169      StringBuffer sb = new StringBuffer();
170  
171      for(int i = 0; i < rowset.size(); i++) {
172  
173        String[] row = (String[])rowset.get(i);
174        for(int k = 0; k < row.length; k++) {
175          sb.append(row[k]);
176          if(k + 1 < row.length) {
177            sb.append(VALUE_SEPARATOR);
178          }
179        }
180  
181        if(i + 1 < rowset.size()) {
182          sb.append(ROW_SEPARATOR);
183        }
184      }
185      return sb.toString();
186    }
187  
188  }
189