1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43:
44: import ;
45: import ;
46:
47:
48:
53: public final class Pattern implements Serializable
54: {
55: private static final long serialVersionUID = 5073258162644648461L;
56:
57: public static final int CANON_EQ = 128;
58: public static final int CASE_INSENSITIVE = 2;
59: public static final int COMMENTS = 4;
60: public static final int DOTALL = 32;
61: public static final int MULTILINE = 8;
62: public static final int UNICODE_CASE = 64;
63: public static final int UNIX_LINES = 1;
64:
65: private final String regex;
66: private final int flags;
67:
68: private final RE re;
69:
70: private Pattern (String regex, int flags)
71: throws PatternSyntaxException
72: {
73: this.regex = regex;
74: this.flags = flags;
75:
76: int gnuFlags = 0;
77: if ((flags & CASE_INSENSITIVE) != 0)
78: gnuFlags |= RE.REG_ICASE;
79: if ((flags & MULTILINE) != 0)
80: gnuFlags |= RE.REG_MULTILINE;
81: if ((flags & DOTALL) != 0)
82: gnuFlags |= RE.REG_DOT_NEWLINE;
83:
84:
85:
86:
87: RESyntax syntax = RESyntax.RE_SYNTAX_JAVA_1_4;
88: if ((flags & UNIX_LINES) != 0)
89: {
90:
91: syntax = new RESyntax(syntax);
92: syntax.setLineSeparator("\n");
93: }
94:
95: if ((flags & COMMENTS) != 0)
96: {
97:
98: }
99:
100: try
101: {
102: this.re = new RE(regex, gnuFlags, syntax);
103: }
104: catch (REException e)
105: {
106: PatternSyntaxException pse;
107: pse = new PatternSyntaxException(e.getMessage(),
108: regex, e.getPosition());
109: pse.initCause(e);
110: throw pse;
111: }
112: }
113:
114:
115: RE getRE()
116: {
117: return re;
118: }
119:
120:
125: public static Pattern compile (String regex)
126: throws PatternSyntaxException
127: {
128: return compile(regex, 0);
129: }
130:
131:
139: public static Pattern compile (String regex, int flags)
140: throws PatternSyntaxException
141: {
142:
143: if ((flags & ~0xEF) != 0)
144: throw new IllegalArgumentException ();
145:
146: return new Pattern (regex, flags);
147: }
148:
149: public int flags ()
150: {
151: return this.flags;
152: }
153:
154:
160: public static boolean matches (String regex, CharSequence input)
161: {
162: return compile(regex).matcher(input).matches();
163: }
164:
165:
168: public Matcher matcher (CharSequence input)
169: {
170: return new Matcher(this, input);
171: }
172:
173:
176: public String[] split (CharSequence input)
177: {
178: return split(input, 0);
179: }
180:
181:
185: public String[] split (CharSequence input, int limit)
186: {
187: Matcher matcher = new Matcher(this, input);
188: ArrayList list = new ArrayList();
189: int empties = 0;
190: int count = 0;
191: int start = 0;
192: int end;
193: boolean matched = matcher.find();
194:
195: while (matched && (limit <= 0 || count < limit - 1))
196: {
197: ++count;
198: end = matcher.start();
199: if (start == end)
200: empties++;
201: else
202: {
203: while (empties > 0)
204: {
205: list.add("");
206: empties--;
207: }
208:
209: String text = input.subSequence(start, end).toString();
210: list.add(text);
211: }
212: start = matcher.end();
213: matched = matcher.find();
214: }
215:
216:
217: if (!matched && count == 0)
218: return new String[] { input.toString() };
219:
220:
221: boolean emptyLast = (start == input.length());
222:
223:
224: if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast))
225: {
226: if (limit > list.size())
227: {
228: int max = limit - list.size();
229: empties = (empties > max) ? max : empties;
230: }
231: while (empties > 0)
232: {
233: list.add("");
234: empties--;
235: }
236: }
237:
238:
239: if (limit != 0 || (limit == 0 && !emptyLast))
240: {
241: String t = input.subSequence(start, input.length()).toString();
242: if ("".equals(t) && limit == 0)
243: ;
244: else
245: list.add(t);
246: }
247:
248: String[] output = new String [list.size()];
249: list.toArray(output);
250: return output;
251: }
252:
253: public String pattern ()
254: {
255: return regex;
256: }
257: }