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.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
025import org.apache.directory.api.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    /**
049     * Creates a new instance of TelexNumberSyntaxChecker.
050     */
051    public TelexNumberSyntaxChecker()
052    {
053        super( SchemaConstants.TELEX_NUMBER_SYNTAX );
054    }
055
056
057    /**
058     * {@inheritDoc}
059     */
060    public boolean isValidSyntax( Object value )
061    {
062        String strValue = null;
063
064        if ( value == null )
065        {
066            LOG.debug( "Syntax invalid for 'null'" );
067            return false;
068        }
069
070        if ( value instanceof String )
071        {
072            strValue = ( String ) value;
073        }
074        else if ( value instanceof byte[] )
075        {
076            strValue = Strings.utf8ToString( ( byte[] ) value );
077        }
078        else
079        {
080            strValue = value.toString();
081        }
082
083        if ( strValue.length() == 0 )
084        {
085            LOG.debug( "Syntax invalid for '{}'", value );
086            return false;
087        }
088
089        // Search for the first '$' separator
090        int dollar = strValue.indexOf( '$' );
091
092        // We must have one, and not on first position
093        if ( dollar <= 0 )
094        {
095            // No '$' => error
096            LOG.debug( "Syntax invalid for '{}'", value );
097            return false;
098        }
099
100        String actualNumber = strValue.substring( 0, dollar );
101
102        // The actualNumber must not be empty
103        if ( actualNumber.length() == 0 )
104        {
105            LOG.debug( "Syntax invalid for '{}'", value );
106            return false;
107        }
108
109        // The actual number should be a PrintableString 
110        if ( !Strings.isPrintableString( actualNumber ) )
111        {
112            LOG.debug( "Syntax invalid for '{}'", value );
113            return false;
114        }
115
116        // Search for the second separator
117        int dollar2 = strValue.indexOf( '$', dollar + 1 );
118
119        // We must have one
120        if ( dollar2 == -1 )
121        {
122            // No '$' => error
123            LOG.debug( "Syntax invalid for '{}'", value );
124            return false;
125        }
126
127        String countryCode = strValue.substring( dollar + 1, dollar2 );
128
129        // The countryCode must not be empty
130        if ( countryCode.length() == 0 )
131        {
132            LOG.debug( "Syntax invalid for '{}'", value );
133            return false;
134        }
135
136        // The country Code should be a PrintableString 
137        if ( !Strings.isPrintableString( countryCode ) )
138        {
139            LOG.debug( "Syntax invalid for '{}'", value );
140            return false;
141        }
142
143        // Now, check for the answerBack
144        if ( dollar2 + 1 == strValue.length() )
145        {
146            // The last string should not be null
147            LOG.debug( "Syntax invalid for '{}'", value );
148            return false;
149        }
150
151        String answerBack = strValue.substring( dollar2 + 1 );
152
153        // The answerBack should be a PrintableString 
154        if ( !Strings.isPrintableString( answerBack ) )
155        {
156            LOG.debug( "Syntax invalid for '{}'", value );
157            return false;
158        }
159
160        // Check that the mailboxType is a PrintableString
161        boolean result = Strings.isPrintableString( answerBack );
162
163        if ( result )
164        {
165            LOG.debug( "Syntax valid for '{}'", value );
166        }
167        else
168        {
169            LOG.debug( "Syntax invalid for '{}'", value );
170        }
171
172        return result;
173    }
174}