001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.syntaxes;
021
022
023 import java.util.ArrayList;
024 import java.util.List;
025 import java.util.regex.Pattern;
026 import java.util.regex.PatternSyntaxException;
027
028 import org.apache.directory.shared.ldap.constants.SchemaConstants;
029 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
030 import org.apache.directory.shared.ldap.util.StringTools;
031
032
033 /**
034 * A SyntaxChecker which verifies that a value is a TelephoneNumber according to ITU
035 * recommendation E.123 (which is quite vague ...).
036 *
037 * A valid Telephone number respect more or less this syntax :
038 *
039 * " *[+]? *((\([0-9- ]+\))|[0-9- ]+)+"
040 *
041 * If needed, and to allow more syntaxes, a list of regexps has been added
042 * which can be initialized to other values
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 * @version $Rev$
046 */
047 public class TelephoneNumberSyntaxChecker extends AbstractSyntaxChecker
048 {
049 /** Other regexps to extend the initial one */
050 private List<String> regexps;
051
052 /** Other regexp to extend the initial one, compiled */
053 private List<Pattern> compiledREs;
054
055 /** The default pattern used to check a TelephoneNumber */
056 private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ]+\\))|[0-9- ]+)+$";
057
058 /** The compiled default pattern */
059 private Pattern defaultPattern = Pattern.compile( DEFAULT_REGEXP );
060
061 /** A flag set when only the default regexp should be tested */
062 protected boolean defaultMandatory = false;
063
064 /**
065 * Creates a new instance of TelephoneNumberSyntaxChecker.
066 */
067 public TelephoneNumberSyntaxChecker()
068 {
069 super( SchemaConstants.TELEPHONE_NUMBER_SYNTAX );
070 }
071
072 /**
073 * Creates a new instance of TelephoneNumberSyntaxChecker.
074 *
075 * @param oid The OID to associate with this SyntaxChecker
076 */
077 protected TelephoneNumberSyntaxChecker( String oid )
078 {
079 super( oid );
080 }
081
082 /**
083 * Add a new valid regexp for a Telephone number
084 * @param regexp The new regexp to check
085 */
086 public void addRegexp( String regexp )
087 {
088 if ( defaultMandatory )
089 {
090 return;
091 }
092
093 try
094 {
095 Pattern compiledRE = Pattern.compile( regexp );
096
097 if ( regexps == null )
098 {
099 regexps = new ArrayList<String>();
100 compiledREs = new ArrayList<Pattern>();
101 }
102
103 regexps.add( regexp );
104 compiledREs.add( compiledRE );
105 }
106 catch ( PatternSyntaxException pse )
107 {
108 return;
109 }
110 }
111
112
113 /**
114 * Set the defaut regular expression for the Telephone number
115 *
116 * @param regexp the default regular expression.
117 */
118 public void setDefaultRegexp( String regexp )
119 {
120 try
121 {
122 defaultPattern = Pattern.compile( regexp );
123
124 defaultMandatory = true;
125 regexps = null;
126 compiledREs = null;
127 }
128 catch ( PatternSyntaxException pse )
129 {
130 return;
131 }
132 }
133
134 /* (non-Javadoc)
135 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
136 */
137 public boolean isValidSyntax( Object value )
138 {
139 String strValue = null;
140
141 if ( value == null )
142 {
143 return false;
144 }
145
146 if ( value instanceof String )
147 {
148 strValue = ( String ) value;
149 }
150 else if ( value instanceof byte[] )
151 {
152 strValue = StringTools.utf8ToString( ( byte[] ) value );
153 }
154 else
155 {
156 strValue = value.toString();
157 }
158
159 if ( strValue.length() == 0 )
160 {
161 return false;
162 }
163
164 // We will use a regexp to check the TelephoneNumber.
165 if ( defaultMandatory )
166 {
167 // We have a unique regexp to check, the default one
168 return defaultPattern.matcher( strValue ).matches();
169 }
170 else
171 {
172 if ( defaultPattern.matcher( strValue ).matches() )
173 {
174 return true;
175 }
176 else
177 {
178 if ( compiledREs == null )
179 {
180 return false;
181 }
182
183 // The default is not enough, let's try
184 // the other regexps
185 for ( Pattern pattern:compiledREs )
186 {
187 if ( pattern.matcher( strValue ).matches() )
188 {
189 return true;
190 }
191 }
192
193 return false;
194 }
195 }
196 }
197 }