KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > text > Text


1 package com.quadcap.util.text;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.ArrayList JavaDoc;
42
43 /**
44  * This class is yet another approach to string parsing. It's basically
45  * an anchored glob-style match.
46  *
47  * @author Stan Bailes
48  */

49 public class Text {
50     /**
51      * Extract strings matching glob patterns
52      *
53      * Examples:
54      * sv[0,1,2] = extractN(w, "*(*)*")
55      * sv[0-3] = extractN(w, "*:*:*:*");
56      */

57      
58     public static String JavaDoc[] extractN(String JavaDoc s, String JavaDoc p) {
59         return extractN(s, p, '*');
60     }
61
62     public static String JavaDoc[] extractMatching(String JavaDoc s,
63                                            String JavaDoc[] p,
64                                            char d) {
65         // ret[ret.length-1] = String.valueOf(idx)
66
// where idx is the index of the matching pattern
67
String JavaDoc[] ret = null;
68         int i;
69         for (i = 0; i < p.length; i++) {
70             ret = extractN(s, p[i], d);
71             if (ret != null) {
72                 break;
73             }
74         }
75         if (ret != null) {
76             String JavaDoc[] nret = new String JavaDoc[ret.length + 1];
77             System.arraycopy(ret, 0, nret, 0, ret.length);
78             nret[ret.length] = String.valueOf(i);
79             ret = nret;
80         }
81         return ret;
82     }
83
84    public static String JavaDoc[] extractN(String JavaDoc s, String JavaDoc p, char d) {
85         if (p == null || p.length() == 0) p = "" + d;
86         if (p.charAt(0) != d) p = "" + d + p;
87         if (p.charAt(p.length()-1) != d) p = p + d;
88         
89         // count glob instances, that's the number of strings to return
90
int cnt = 0;
91         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
92         ArrayList JavaDoc list = new ArrayList JavaDoc();
93         for (int i = 0; i < p.length(); i++) {
94             char c = p.charAt(i);
95             if (c == d) {
96                 cnt++;
97                 list.add(sb.toString());
98                 //msg("[" + sb + "]");
99
sb.setLength(0);
100             } else {
101                 sb.append(c);
102             }
103         }
104         list.add(sb.toString());
105         if (cnt < list.size()) cnt = list.size();
106         String JavaDoc[] lits = new String JavaDoc[cnt];
107         for (int i = 0; i < cnt; i++) {
108             lits[i] = list.get(i).toString();
109         }
110         String JavaDoc[] ret = new String JavaDoc[cnt-1];
111         int[] ixs = new int[cnt];
112         int lev = 0;
113         int rlev = 0;
114         for (int i = 0; lev >= 0 && lev < cnt; i++) {
115             int pre = ixs[lev];
116             final int len = lits[lev].length();
117             int idx = len == 0 ? pre : s.indexOf(lits[lev], pre);
118             if (idx < 0) {
119                 lev--;
120             } else {
121                 if (len == 0) {
122                     ++lev;
123                     if (i == 0) { // first time through
124
if (lev < cnt) {
125                             rlev--;
126                             ixs[lev] = idx;
127                         }
128                     } else if (lev == 1) {
129                         lev = -1;
130                     } else {
131                         ret[lev + rlev - 1] = s.substring(idx);
132                     }
133                 } else {
134                     ret[lev + rlev] = s.substring(pre, idx);
135                     ixs[lev] = idx + len;
136                     if (++lev < cnt) {
137                         ixs[lev] = idx + len;
138                     }
139                 }
140             }
141         }
142         //#ifdef DEBUG
143
// sb.setLength(0);
144
// for (int i = 0; i < ret.length; i++) {
145
// sb.append("\n[" + i + "] " + ret[i]);
146
// }
147
// sb.append("\n");
148
//msg("extract(" + s + ") [" + p + "]: " + sb);
149
//#endif
150
return lev < cnt ? null : ret;
151     }
152
153     /**
154      * Extract a substring
155      *
156      * example:
157      *
158      * extract(s, "*.gif", 0);
159      * extract(s, "http://*index.html", 1);
160      */

161     public static String JavaDoc extract(String JavaDoc s, String JavaDoc p, int n) {
162         return extract(s, p, n, '*');
163     }
164                 
165     public static String JavaDoc extract(String JavaDoc s, String JavaDoc p, int n, char d) {
166         String JavaDoc[] ret = extractN(s, p, d);
167         if (ret == null) {
168             return s;
169         } else {
170             return ret[n];
171         }
172     }
173
174 }
175
Popular Tags