01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.beans.propertyeditors;
18:
19: import java.beans.PropertyEditorSupport;
20: import java.util.regex.Pattern;
21:
22: /**
23: * Editor for <code>java.util.regex.Pattern</code>, to directly populate a Pattern property.
24: * Expects the same syntax as Pattern's <code>compile</code> method.
25: *
26: * <p>Since <code>java.util.regex.Pattern</code> is only available on JDK 1.4 or higher,
27: * this editor is only available on JDK 1.4 or higher as well.
28: *
29: * @author Juergen Hoeller
30: * @since 2.0.1
31: * @see java.util.regex.Pattern
32: * @see java.util.regex.Pattern#compile(String)
33: */
34: public class PatternEditor extends PropertyEditorSupport {
35:
36: private final int flags;
37:
38: /**
39: * Create a new PatternEditor with default settings.
40: */
41: public PatternEditor() {
42: this .flags = 0;
43: }
44:
45: /**
46: * Create a new PatternEditor with the given settings.
47: * @param flags the <code>java.util.regex.Pattern</code> flags to apply
48: * @see java.util.regex.Pattern#compile(String, int)
49: * @see java.util.regex.Pattern#CASE_INSENSITIVE
50: * @see java.util.regex.Pattern#MULTILINE
51: * @see java.util.regex.Pattern#DOTALL
52: * @see java.util.regex.Pattern#UNICODE_CASE
53: * @see java.util.regex.Pattern#CANON_EQ
54: */
55: public PatternEditor(int flags) {
56: this .flags = flags;
57: }
58:
59: public void setAsText(String text) {
60: setValue(text != null ? Pattern.compile(text, this .flags)
61: : null);
62: }
63:
64: public String getAsText() {
65: Pattern value = (Pattern) getValue();
66: return (value != null ? value.pattern() : "");
67: }
68:
69: }
|