1:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49:
50:
51:
56: public final class Pattern implements Serializable
57: {
58: private static final long serialVersionUID = 5073258162644648461L;
59:
60: public static final int CANON_EQ = 128;
61: public static final int CASE_INSENSITIVE = 2;
62: public static final int COMMENTS = 4;
63: public static final int DOTALL = 32;
64: public static final int MULTILINE = 8;
65: public static final int UNICODE_CASE = 64;
66: public static final int UNIX_LINES = 1;
67:
68: private final String regex;
69: private final int flags;
70:
71: private final RE re;
72:
73: private Pattern (String regex, int flags)
74: throws PatternSyntaxException
75: {
76: this.regex = regex;
77: this.flags = flags;
78:
79: RESyntax syntax = RESyntax.RE_SYNTAX_JAVA_1_4;
80: int gnuFlags = 0;
81: gnuFlags |= RE.REG_ICASE_USASCII;
82: if ((flags & CASE_INSENSITIVE) != 0)
83: gnuFlags |= RE.REG_ICASE;
84: if ((flags & MULTILINE) != 0)
85: {
86: gnuFlags |= RE.REG_MULTILINE;
87: syntax = new RESyntax(syntax);
88: syntax.setLineSeparator(null);
89: }
90: if ((flags & DOTALL) != 0)
91: gnuFlags |= RE.REG_DOT_NEWLINE;
92: if ((flags & UNICODE_CASE) != 0)
93: gnuFlags &= ~RE.REG_ICASE_USASCII;
94:
95:
96:
97: if ((flags & UNIX_LINES) != 0)
98: {
99:
100: syntax = new RESyntax(syntax);
101: syntax.setLineSeparator("\n");
102: }
103:
104: if ((flags & COMMENTS) != 0)
105: {
106: gnuFlags |= RE.REG_X_COMMENTS;
107: }
108:
109: try
110: {
111: this.re = new RE(regex, gnuFlags, syntax);
112: }
113: catch (REException e)
114: {
115: PatternSyntaxException pse;
116: pse = new PatternSyntaxException(e.getMessage(),
117: regex, e.getPosition());
118: pse.initCause(e);
119: throw pse;
120: }
121: }
122:
123:
124: RE getRE()
125: {
126: return re;
127: }
128:
129:
134: public static Pattern compile (String regex)
135: throws PatternSyntaxException
136: {
137: return compile(regex, 0);
138: }
139:
140:
148: public static Pattern compile (String regex, int flags)
149: throws PatternSyntaxException
150: {
151:
152: if ((flags & ~0xEF) != 0)
153: throw new IllegalArgumentException ();
154:
155: return new Pattern (regex, flags);
156: }
157:
158: public int flags ()
159: {
160: return this.flags;
161: }
162:
163:
169: public static boolean matches (String regex, CharSequence input)
170: {
171: return compile(regex).matcher(input).matches();
172: }
173:
174:
177: public Matcher matcher (CharSequence input)
178: {
179: return new Matcher(this, input);
180: }
181:
182:
185: public String[] split (CharSequence input)
186: {
187: return split(input, 0);
188: }
189:
190:
194: public String[] split (CharSequence input, int limit)
195: {
196: Matcher matcher = new Matcher(this, input);
197: ArrayList<String> list = new ArrayList<String>();
198: int empties = 0;
199: int count = 0;
200: int start = 0;
201: int end;
202: boolean matched = matcher.find();
203:
204: while (matched && (limit <= 0 || count < limit - 1))
205: {
206: ++count;
207: end = matcher.start();
208: if (start == end)
209: empties++;
210: else
211: {
212: while (empties > 0)
213: {
214: list.add("");
215: empties--;
216: }
217:
218: String text = input.subSequence(start, end).toString();
219: list.add(text);
220: }
221: start = matcher.end();
222: matched = matcher.find();
223: }
224:
225:
226: if (!matched && count == 0)
227: return new String[] { input.toString() };
228:
229:
230: boolean emptyLast = (start == input.length());
231:
232:
233: if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast))
234: {
235: if (limit > list.size())
236: {
237: int max = limit - list.size();
238: empties = (empties > max) ? max : empties;
239: }
240: while (empties > 0)
241: {
242: list.add("");
243: empties--;
244: }
245: }
246:
247:
248: if (limit != 0 || (limit == 0 && !emptyLast))
249: {
250: String t = input.subSequence(start, input.length()).toString();
251: if ("".equals(t) && limit == 0)
252: { }
253: else
254: list.add(t);
255: }
256:
257: return list.toArray(new String[list.size()]);
258: }
259:
260: public String pattern ()
261: {
262: return regex;
263: }
264:
265:
273: public static String quote(String str)
274: {
275: int eInd = str.indexOf("\\E");
276: if (eInd < 0)
277: {
278:
279: return "\\Q" + str + "\\E";
280: }
281:
282: CPStringBuilder sb = new CPStringBuilder(str.length() + 16);
283: sb.append("\\Q");
284:
285: int pos = 0;
286: do
287: {
288:
289:
290: sb.append(str.substring(pos, eInd))
291: .append("\\E" + "\\\\" + "E" + "\\Q");
292: pos = eInd + 2;
293: } while ((eInd = str.indexOf("\\E", pos)) >= 0);
294:
295: sb.append(str.substring(pos, str.length()))
296: .append("\\E");
297: return sb.toString();
298: }
299:
300:
305: public String toString()
306: {
307: return regex;
308: }
309: }