View Javadoc

1   package org.catacomb.util;
2   
3   import org.catacomb.report.E;
4   
5   import java.util.zip.CRC32;
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   import java.util.ArrayList;
10  
11  import java.util.StringTokenizer;
12  
13  
14  public class StringUtil {
15  
16  
17  
18      public static String readableRegularize(String sin) {
19          String s = sin;
20  
21  
22          s = s.replaceAll("[\\(\\),;\\./\\\"\\\'\\\\\\[\\]]", "");
23          s = s.replaceAll("[:&\\.]", "-");
24          s = s.replaceAll("[=\\{\\}\\?\\<\\>]", "_");
25          s = s.replaceAll("%20", "");
26  
27          E.info("string regularization: ");
28          E.info("in=" + sin);
29          E.info("out=" + s);
30  
31  
32          return s;
33  
34      }
35  
36  
37      public static String regularize(String s) {
38          CRC32 crc = new CRC32();
39          crc.update(s.getBytes());
40          long lval = crc.getValue();
41          String sval = "" + lval;
42          return sval;
43      }
44  
45  
46      public static String lastCapitalized(String src) {
47          String ret = "";
48          Pattern pat = Pattern.compile(".*([A-Z][a-z]+)");
49  
50          Matcher matcher = pat.matcher(src);
51          if (matcher.find()) {
52              ret = matcher.group(1);
53          } else {
54              E.warning("no capitalized sections in " + src);
55          }
56          return ret;
57  
58      }
59  
60  
61  
62      public static void main(String[] argv) {
63          /*
64           String[] sa = {"VoltageDepTrans", "VoltageTrans", "Reader", "timePoint", "_Thing"};
65  
66          for (String s : sa) {
67             E.info("" + s + " gives " + lastCapitalized(s));
68          }
69          */
70  
71          String[] sa = {"cpts.org.eng.misc.thing1", "cpts.org.eng.misc.thing2",
72                         "cpts.org.eng.thing3", "cpts.org.neuro.nthing1", "cpts.org.neuro.nthing2",
73                         "cpts.org.newneuro.this.that.thingn"
74                        };
75  
76          StringTree stree = treeify(sa, "tst");
77          stree.print();
78      }
79  
80  
81      public static String[] nonTrivialComponents(String[] sa) {
82          ArrayList<String> al = new ArrayList<String>();
83          for (String s : sa) {
84              if (s != null) {
85                  s = s.trim();
86                  if (s.length() > 0) {
87                      al.add(s);
88                  }
89              }
90          }
91          return al.toArray(new String[0]);
92      }
93  
94  
95      public static StringTree treeify(String[] sa, String rtnm) {
96          StringTree root = new StringTree("", rtnm);
97          root.setExcluded();
98  
99          for (String s : sa) {
100             StringTokenizer st = new StringTokenizer(s, "\\.");
101             root.addFromTokens(st);
102         }
103 
104         // root.compress();
105 
106         return root;
107     }
108 
109     public static StringTree flatTreeify(String[] sa, String rtnm) {
110         StringTree root = new StringTree("", rtnm);
111         root.setExcluded();
112 
113         for (String s : sa) {
114             StringTokenizer st = new StringTokenizer(s, "\\.");
115             root.addFromTokens(st);
116         }
117 
118         root.compress();
119 
120         root.partialFlatten();
121 
122         return root;
123     }
124 
125 
126 
127 
128 
129     public static String[] copyArray(String[] sa) {
130         String[] ret = null;
131         if (sa != null) {
132             ret = new String[sa.length];
133             for (int i = 0; i < sa.length; i++) {
134                 ret[i] = sa[i];
135             }
136         }
137         return ret;
138     }
139 
140 
141     public static String semiHTMLize(String s) {
142         if (s == null) {
143             return "";
144         }
145 
146         StringTokenizer st = new StringTokenizer(s, "\n");
147         StringBuffer ret = new StringBuffer();
148         ret.append("<p>\n");
149         boolean jdp = false;
150         while (st.hasMoreTokens()) {
151             String sl = st.nextToken();
152             if (sl.trim().length() == 0) {
153                 if (jdp) {
154                     // just done a para beak;
155                 } else {
156                     jdp = true;
157                     ret.append("</p><p>\n");
158                 }
159 
160             } else {
161                 while (sl.startsWith(" ")) {
162                     ret.append("&nbsp;");
163                     sl = sl.substring(1, sl.length());
164                 }
165                 jdp = false;
166                 ret.append(sl);
167                 ret.append("<br>\n");
168             }
169         }
170         ret.append("</p>\n");
171         return ret.toString();
172     }
173 
174 
175     public static Double extractQuotedDoubleField(String sin, String fnm) {
176         String s = sin;
177         Double ret = null;
178         s = " " + s;
179         String ks = fnm + "=\"";
180         int iks = s.indexOf(ks);
181         if (iks > 0) {
182             int ike = s.indexOf("\"", iks+1);
183             if (ike > 0) {
184                 ret = new Double(s.substring(iks+1, ike));
185             }
186         }
187         E.info("got double field for " + fnm + " from " + s  + " as " + ret);
188         return ret;
189     }
190 
191 
192     public static Double extractDoubleField(String sin, String fnm) {
193         String s = sin;
194         Double ret = null;
195         s = " " + s + " ";
196         String ks = fnm + "=";
197         int iks = s.indexOf(ks);
198         if (iks > 0) {
199             int ike = s.indexOf(" ", iks+1);
200             if (ike > 0) {
201                 ret = new Double(s.substring(iks + ks.length(), ike));
202             }
203         }
204         //  E.info("got double field for " + fnm + " from " + s  + " as " + ret);
205         return ret;
206     }
207 
208 
209     public static String capitalize(String s) {
210         return s.substring(0,1).toUpperCase() + s.substring(1, s.length());
211     }
212 
213 
214     public final static int countLines(String stxt) {
215         int lineCount = 0;
216         for (int i = 0; i < stxt.length(); i++) {
217             char ch = stxt.charAt(i);
218             if (ch == '\n') {
219                 lineCount ++;
220             }
221         }
222         return lineCount;
223     }
224 
225 }