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.shared.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.shared.ldap.model.constants.SchemaConstants; 024import org.apache.directory.shared.ldap.model.schema.SyntaxChecker; 025import org.apache.directory.shared.util.Strings; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029 030/** 031 * A SyntaxChecker which verifies that a value is a Telex Number according to 032 * RFC 4517 : 033 * 034 * telex-number = actual-number DOLLAR country-code DOLLAR answerback 035 * actual-number = PrintableString 036 * country-code = PrintableString 037 * answerback = PrintableString 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 */ 041@SuppressWarnings("serial") 042public class TelexNumberSyntaxChecker extends SyntaxChecker 043{ 044 /** A logger for this class */ 045 private static final Logger LOG = LoggerFactory.getLogger( TelexNumberSyntaxChecker.class ); 046 047 /** 048 * Creates a new instance of TelexNumberSyntaxChecker. 049 */ 050 public TelexNumberSyntaxChecker() 051 { 052 super( SchemaConstants.TELEX_NUMBER_SYNTAX ); 053 } 054 055 056 /** 057 * {@inheritDoc} 058 */ 059 public boolean isValidSyntax( Object value ) 060 { 061 String strValue = null; 062 063 if ( value == null ) 064 { 065 LOG.debug( "Syntax invalid for 'null'" ); 066 return false; 067 } 068 069 if ( value instanceof String ) 070 { 071 strValue = ( String ) value; 072 } 073 else if ( value instanceof byte[] ) 074 { 075 strValue = Strings.utf8ToString((byte[]) value); 076 } 077 else 078 { 079 strValue = value.toString(); 080 } 081 082 if ( strValue.length() == 0 ) 083 { 084 LOG.debug( "Syntax invalid for '{}'", value ); 085 return false; 086 } 087 088 // Search for the first '$' separator 089 int dollar = strValue.indexOf( '$' ); 090 091 // We must have one, and not on first position 092 if ( dollar <= 0 ) 093 { 094 // No '$' => error 095 LOG.debug( "Syntax invalid for '{}'", value ); 096 return false; 097 } 098 099 String actualNumber = strValue.substring( 0, dollar ); 100 101 // The actualNumber must not be empty 102 if ( actualNumber.length() == 0 ) 103 { 104 LOG.debug( "Syntax invalid for '{}'", value ); 105 return false; 106 } 107 108 // The actual number should be a PrintableString 109 if ( ! Strings.isPrintableString(actualNumber) ) 110 { 111 LOG.debug( "Syntax invalid for '{}'", value ); 112 return false; 113 } 114 115 // Search for the second separator 116 int dollar2 = strValue.indexOf( '$', dollar + 1 ); 117 118 // We must have one 119 if ( dollar2 == -1 ) 120 { 121 // No '$' => error 122 LOG.debug( "Syntax invalid for '{}'", value ); 123 return false; 124 } 125 126 String countryCode = strValue.substring( dollar + 1, dollar2 ); 127 128 // The countryCode must not be empty 129 if ( countryCode.length() == 0 ) 130 { 131 LOG.debug( "Syntax invalid for '{}'", value ); 132 return false; 133 } 134 135 // The country Code should be a PrintableString 136 if ( ! Strings.isPrintableString(countryCode) ) 137 { 138 LOG.debug( "Syntax invalid for '{}'", value ); 139 return false; 140 } 141 142 // Now, check for the answerBack 143 if ( dollar2 + 1 == strValue.length() ) 144 { 145 // The last string should not be null 146 LOG.debug( "Syntax invalid for '{}'", value ); 147 return false; 148 } 149 150 String answerBack = strValue.substring( dollar2 + 1 ); 151 152 // The answerBack should be a PrintableString 153 if ( ! Strings.isPrintableString(answerBack) ) 154 { 155 LOG.debug( "Syntax invalid for '{}'", value ); 156 return false; 157 } 158 159 // Check that the mailboxType is a PrintableString 160 boolean result = Strings.isPrintableString(answerBack); 161 162 if ( result ) 163 { 164 LOG.debug( "Syntax valid for '{}'", value ); 165 } 166 else 167 { 168 LOG.debug( "Syntax invalid for '{}'", value ); 169 } 170 171 return result; 172 } 173}