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: * @author Juergen Hoeller
27: * @since 2.0.1
28: * @see java.util.regex.Pattern
29: * @see java.util.regex.Pattern#compile(String)
30: */
31: public class PatternEditor extends PropertyEditorSupport {
32:
33: private final int flags;
34:
35: /**
36: * Create a new PatternEditor with default settings.
37: */
38: public PatternEditor() {
39: this .flags = 0;
40: }
41:
42: /**
43: * Create a new PatternEditor with the given settings.
44: * @param flags the <code>java.util.regex.Pattern</code> flags to apply
45: * @see java.util.regex.Pattern#compile(String, int)
46: * @see java.util.regex.Pattern#CASE_INSENSITIVE
47: * @see java.util.regex.Pattern#MULTILINE
48: * @see java.util.regex.Pattern#DOTALL
49: * @see java.util.regex.Pattern#UNICODE_CASE
50: * @see java.util.regex.Pattern#CANON_EQ
51: */
52: public PatternEditor(int flags) {
53: this .flags = flags;
54: }
55:
56: public void setAsText(String text) {
57: setValue(text != null ? Pattern.compile(text, this .flags)
58: : null);
59: }
60:
61: public String getAsText() {
62: Pattern value = (Pattern) getValue();
63: return (value != null ? value.pattern() : "");
64: }
65:
66: }
|