001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.propertyeditors; 018 019import java.beans.PropertyEditorSupport; 020import java.util.regex.Pattern; 021 022/** 023 * Editor for {@code java.util.regex.Pattern}, to directly populate a Pattern property. 024 * Expects the same syntax as Pattern's {@code compile} method. 025 * 026 * @author Juergen Hoeller 027 * @since 2.0.1 028 * @see java.util.regex.Pattern 029 * @see java.util.regex.Pattern#compile(String) 030 */ 031public class PatternEditor extends PropertyEditorSupport { 032 033 private final int flags; 034 035 036 /** 037 * Create a new PatternEditor with default settings. 038 */ 039 public PatternEditor() { 040 this.flags = 0; 041 } 042 043 /** 044 * Create a new PatternEditor with the given settings. 045 * @param flags the {@code java.util.regex.Pattern} flags to apply 046 * @see java.util.regex.Pattern#compile(String, int) 047 * @see java.util.regex.Pattern#CASE_INSENSITIVE 048 * @see java.util.regex.Pattern#MULTILINE 049 * @see java.util.regex.Pattern#DOTALL 050 * @see java.util.regex.Pattern#UNICODE_CASE 051 * @see java.util.regex.Pattern#CANON_EQ 052 */ 053 public PatternEditor(int flags) { 054 this.flags = flags; 055 } 056 057 058 @Override 059 public void setAsText(String text) { 060 setValue(text != null ? Pattern.compile(text, this.flags) : null); 061 } 062 063 @Override 064 public String getAsText() { 065 Pattern value = (Pattern) getValue(); 066 return (value != null ? value.pattern() : ""); 067 } 068 069}