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 org.apache.directory.shared.ldap.constants.SchemaConstants;
024 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
025 import org.apache.directory.shared.ldap.util.StringTools;
026
027
028 /**
029 * A SyntaxChecker which verifies that a value is a PostalAddress according to
030 * RFC 4517 :
031 *
032 * <postal-address> = <dstring> <dstring-list>
033 * <dstring-list> = "$" <dstring> <dstring-list> | e
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 * @version $Rev$
037 */
038 public class PostalAddressSyntaxChecker extends AbstractSyntaxChecker
039 {
040 /**
041 *
042 * Creates a new instance of PostalAddressSyntaxChecker.
043 *
044 */
045 public PostalAddressSyntaxChecker()
046 {
047 super( SchemaConstants.POSTAL_ADDRESS_SYNTAX );
048 }
049
050 /**
051 *
052 * Creates a new instance of PostalAddressSyntaxChecker.
053 *
054 * @param oid the oid to associate with this new SyntaxChecker
055 *
056 */
057 protected PostalAddressSyntaxChecker( String oid )
058 {
059 super( oid );
060 }
061
062
063 /* (non-Javadoc)
064 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
065 */
066 public boolean isValidSyntax( Object value )
067 {
068 String strValue = null;
069
070 if ( value == null )
071 {
072 return false;
073 }
074
075 if ( value instanceof String )
076 {
077 strValue = ( String ) value;
078 }
079 else if ( value instanceof byte[] )
080 {
081 strValue = StringTools.utf8ToString( ( byte[] ) value );
082 }
083 else
084 {
085 strValue = value.toString();
086 }
087
088 if ( strValue.length() == 0 )
089 {
090 return false;
091 }
092
093 // Search for the '$' separator
094 int dollar = strValue.indexOf( '$' );
095
096 if ( dollar == -1 )
097 {
098 // No '$' => only a dstring
099 return true;
100 }
101
102 int pos = 0;
103 do
104 {
105 // check that the element between each '$' is not empty
106 String address = strValue.substring( pos, dollar );
107
108 if ( StringTools.isEmpty( address ) )
109 {
110 return false;
111 }
112
113 pos = dollar + 1;
114
115 if ( pos == strValue.length() )
116 {
117 // we should not have a '$' at the end
118 return false;
119 }
120
121 dollar = strValue.indexOf( '$', pos );
122 } while ( dollar > -1 );
123
124 return true;
125 }
126 }