Quadcap Embeddable Server

com/quadcap/util/text/Text.java

Go to the documentation of this file.
00001 package com.quadcap.util.text; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.ArrayList; 00042 00043 /** 00044 * This class is yet another approach to string parsing. It's basically 00045 * an anchored glob-style match. 00046 * 00047 * @author Stan Bailes 00048 */ 00049 public class Text { 00050 /** 00051 * Extract strings matching glob patterns 00052 * 00053 * Examples: 00054 * sv[0,1,2] = extractN(w, "*(*)*") 00055 * sv[0-3] = extractN(w, "*:*:*:*"); 00056 */ 00057 00058 public static String[] extractN(String s, String p) { 00059 return extractN(s, p, '*'); 00060 } 00061 00062 public static String[] extractMatching(String s, 00063 String[] p, 00064 char d) { 00065 // ret[ret.length-1] = String.valueOf(idx) 00066 // where idx is the index of the matching pattern 00067 String[] ret = null; 00068 int i; 00069 for (i = 0; i < p.length; i++) { 00070 ret = extractN(s, p[i], d); 00071 if (ret != null) { 00072 break; 00073 } 00074 } 00075 if (ret != null) { 00076 String[] nret = new String[ret.length + 1]; 00077 System.arraycopy(ret, 0, nret, 0, ret.length); 00078 nret[ret.length] = String.valueOf(i); 00079 ret = nret; 00080 } 00081 return ret; 00082 } 00083 00084 public static String[] extractN(String s, String p, char d) { 00085 if (p == null || p.length() == 0) p = "" + d; 00086 if (p.charAt(0) != d) p = "" + d + p; 00087 if (p.charAt(p.length()-1) != d) p = p + d; 00088 00089 // count glob instances, that's the number of strings to return 00090 int cnt = 0; 00091 StringBuffer sb = new StringBuffer(); 00092 ArrayList list = new ArrayList(); 00093 for (int i = 0; i < p.length(); i++) { 00094 char c = p.charAt(i); 00095 if (c == d) { 00096 cnt++; 00097 list.add(sb.toString()); 00098 //msg("[" + sb + "]"); 00099 sb.setLength(0); 00100 } else { 00101 sb.append(c); 00102 } 00103 } 00104 list.add(sb.toString()); 00105 if (cnt < list.size()) cnt = list.size(); 00106 String[] lits = new String[cnt]; 00107 for (int i = 0; i < cnt; i++) { 00108 lits[i] = list.get(i).toString(); 00109 } 00110 String[] ret = new String[cnt-1]; 00111 int[] ixs = new int[cnt]; 00112 int lev = 0; 00113 int rlev = 0; 00114 for (int i = 0; lev >= 0 && lev < cnt; i++) { 00115 int pre = ixs[lev]; 00116 final int len = lits[lev].length(); 00117 int idx = len == 0 ? pre : s.indexOf(lits[lev], pre); 00118 if (idx < 0) { 00119 lev--; 00120 } else { 00121 if (len == 0) { 00122 ++lev; 00123 if (i == 0) { // first time through 00124 if (lev < cnt) { 00125 rlev--; 00126 ixs[lev] = idx; 00127 } 00128 } else if (lev == 1) { 00129 lev = -1; 00130 } else { 00131 ret[lev + rlev - 1] = s.substring(idx); 00132 } 00133 } else { 00134 ret[lev + rlev] = s.substring(pre, idx); 00135 ixs[lev] = idx + len; 00136 if (++lev < cnt) { 00137 ixs[lev] = idx + len; 00138 } 00139 } 00140 } 00141 } 00142 //#ifdef DEBUG 00143 // sb.setLength(0); 00144 // for (int i = 0; i < ret.length; i++) { 00145 // sb.append("\n[" + i + "] " + ret[i]); 00146 // } 00147 // sb.append("\n"); 00148 //msg("extract(" + s + ") [" + p + "]: " + sb); 00149 //#endif 00150 return lev < cnt ? null : ret; 00151 } 00152 00153 /** 00154 * Extract a substring 00155 * 00156 * example: 00157 * 00158 * extract(s, "*.gif", 0); 00159 * extract(s, "http://*index.html", 1); 00160 */ 00161 public static String extract(String s, String p, int n) { 00162 return extract(s, p, n, '*'); 00163 } 00164 00165 public static String extract(String s, String p, int n, char d) { 00166 String[] ret = extractN(s, p, d); 00167 if (ret == null) { 00168 return s; 00169 } else { 00170 return ret[n]; 00171 } 00172 } 00173 00174 }