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 */ 020package org.apache.directory.api.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.api.i18n.I18n; 024import org.apache.directory.api.ldap.model.constants.SchemaConstants; 025import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 026import org.apache.directory.api.util.Strings; 027 028 029/** 030 * A SyntaxChecker which verifies that a value is a Telex Number according to 031 * RFC 4517 : 032 * <pre> 033 * telex-number = actual-number DOLLAR country-code DOLLAR answerback 034 * actual-number = PrintableString 035 * country-code = PrintableString 036 * answerback = PrintableString 037 * </pre> 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 */ 040@SuppressWarnings("serial") 041public final class TelexNumberSyntaxChecker extends SyntaxChecker 042{ 043 /** 044 * A static instance of TelexNumberSyntaxChecker 045 */ 046 public static final TelexNumberSyntaxChecker INSTANCE = 047 new TelexNumberSyntaxChecker( SchemaConstants.TELEX_NUMBER_SYNTAX ); 048 049 /** 050 * A static Builder for this class 051 */ 052 public static final class Builder extends SCBuilder<TelexNumberSyntaxChecker> 053 { 054 /** 055 * The Builder constructor 056 */ 057 private Builder() 058 { 059 super( SchemaConstants.TELEX_NUMBER_SYNTAX ); 060 } 061 062 063 /** 064 * Create a new instance of TelexNumberSyntaxChecker 065 * @return A new instance of TelexNumberSyntaxChecker 066 */ 067 @Override 068 public TelexNumberSyntaxChecker build() 069 { 070 return new TelexNumberSyntaxChecker( oid ); 071 } 072 } 073 074 075 /** 076 * Creates a new instance of TelexNumberSyntaxChecker. 077 * 078 * @param oid the child's OID 079 */ 080 private TelexNumberSyntaxChecker( String oid ) 081 { 082 super( oid ); 083 } 084 085 086 /** 087 * @return An instance of the Builder for this class 088 */ 089 public static Builder builder() 090 { 091 return new Builder(); 092 } 093 094 095 /** 096 * {@inheritDoc} 097 */ 098 @Override 099 public boolean isValidSyntax( Object value ) 100 { 101 String strValue; 102 103 if ( value == null ) 104 { 105 if ( LOG.isDebugEnabled() ) 106 { 107 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 108 } 109 110 return false; 111 } 112 113 if ( value instanceof String ) 114 { 115 strValue = ( String ) value; 116 } 117 else if ( value instanceof byte[] ) 118 { 119 strValue = Strings.utf8ToString( ( byte[] ) value ); 120 } 121 else 122 { 123 strValue = value.toString(); 124 } 125 126 if ( strValue.length() == 0 ) 127 { 128 if ( LOG.isDebugEnabled() ) 129 { 130 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 131 } 132 133 return false; 134 } 135 136 // Search for the first '$' separator 137 int dollar = strValue.indexOf( '$' ); 138 139 // We must have one, and not on first position 140 if ( dollar <= 0 ) 141 { 142 // No '$' => error 143 if ( LOG.isDebugEnabled() ) 144 { 145 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 146 } 147 148 return false; 149 } 150 151 String actualNumber = strValue.substring( 0, dollar ); 152 153 // The actualNumber must not be empty 154 if ( actualNumber.length() == 0 ) 155 { 156 if ( LOG.isDebugEnabled() ) 157 { 158 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 159 } 160 161 return false; 162 } 163 164 // The actual number should be a PrintableString 165 if ( !Strings.isPrintableString( actualNumber ) ) 166 { 167 if ( LOG.isDebugEnabled() ) 168 { 169 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 170 } 171 172 return false; 173 } 174 175 // Search for the second separator 176 int dollar2 = strValue.indexOf( '$', dollar + 1 ); 177 178 // We must have one 179 if ( dollar2 == -1 ) 180 { 181 // No '$' => error 182 if ( LOG.isDebugEnabled() ) 183 { 184 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 185 } 186 187 return false; 188 } 189 190 String countryCode = strValue.substring( dollar + 1, dollar2 ); 191 192 // The countryCode must not be empty 193 if ( countryCode.length() == 0 ) 194 { 195 if ( LOG.isDebugEnabled() ) 196 { 197 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 198 } 199 200 return false; 201 } 202 203 // The country Code should be a PrintableString 204 if ( !Strings.isPrintableString( countryCode ) ) 205 { 206 if ( LOG.isDebugEnabled() ) 207 { 208 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 209 } 210 211 return false; 212 } 213 214 // Now, check for the answerBack 215 if ( dollar2 + 1 == strValue.length() ) 216 { 217 // The last string should not be null 218 if ( LOG.isDebugEnabled() ) 219 { 220 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 221 } 222 223 return false; 224 } 225 226 String answerBack = strValue.substring( dollar2 + 1 ); 227 228 // The answerBack should be a PrintableString 229 if ( !Strings.isPrintableString( answerBack ) ) 230 { 231 if ( LOG.isDebugEnabled() ) 232 { 233 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 234 } 235 236 return false; 237 } 238 239 // Check that the mailboxType is a PrintableString 240 boolean result = Strings.isPrintableString( answerBack ); 241 242 if ( LOG.isDebugEnabled() ) 243 { 244 if ( result ) 245 { 246 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 247 } 248 else 249 { 250 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 251 } 252 } 253 254 return result; 255 } 256}