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 Telex Number according to 
030     * RFC 4517 :
031     * 
032     * telex-number  = actual-number DOLLAR country-code DOLLAR answerback
033     * actual-number = PrintableString
034     * country-code  = PrintableString
035     * answerback    = PrintableString
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$
039     */
040    public class TelexNumberSyntaxChecker extends AbstractSyntaxChecker
041    {
042        /**
043         * 
044         * Creates a new instance of TelexNumberSyntaxChecker.
045         *
046         */
047        public TelexNumberSyntaxChecker()
048        {
049            super( SchemaConstants.TELEX_NUMBER_SYNTAX );
050        }
051        
052        /**
053         * 
054         * Creates a new instance of TelexNumberSyntaxChecker.
055         * 
056         * @param oid the oid to associate with this new SyntaxChecker
057         *
058         */
059        protected TelexNumberSyntaxChecker( String oid )
060        {
061            super( oid );
062        }
063        
064        
065        /* (non-Javadoc)
066         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
067         */
068        public boolean isValidSyntax( Object value )
069        {
070            String strValue = null;
071    
072            if ( value == null )
073            {
074                return false;
075            }
076            
077            if ( value instanceof String )
078            {
079                strValue = ( String ) value;
080            }
081            else if ( value instanceof byte[] )
082            {
083                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
084            }
085            else
086            {
087                strValue = value.toString();
088            }
089    
090            if ( strValue.length() == 0 )
091            {
092                return false;
093            }
094    
095            // Search for the first '$' separator
096            int dollar = strValue.indexOf( '$' );
097            
098            // We must have one, and not on first position
099            if ( dollar <= 0 )
100            {
101                // No '$' => error
102                return false;
103            }
104            
105            String actualNumber = strValue.substring( 0, dollar );
106            
107            // The actualNumber must not be empty
108            if ( actualNumber.length() == 0 )
109            {
110                return false;
111            }
112            
113            // The actual number should be a PrintableString 
114            if ( ! StringTools.isPrintableString( actualNumber ) )
115            {
116                return false;
117            }
118            
119            // Search for the second separator
120            int dollar2 = strValue.indexOf( '$', dollar + 1 );
121                
122            // We must have one
123            if ( dollar2 == -1 )
124            {
125                // No '$' => error
126                return false;
127            }
128    
129            String countryCode = strValue.substring( dollar + 1, dollar2 );
130            
131            // The countryCode must not be empty
132            if ( countryCode.length() == 0 )
133            {
134                return false;
135            }
136            
137            // The country Code should be a PrintableString 
138            if ( ! StringTools.isPrintableString( countryCode ) )
139            {
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                return false;
148            }
149            
150            String answerBack = strValue.substring( dollar2 + 1 );
151            
152            // The answerBack should be a PrintableString 
153            if ( ! StringTools.isPrintableString( answerBack ) )
154            {
155                return false;
156            }
157            
158            // Check that the mailboxType is a PrintableString
159            return StringTools.isPrintableString( answerBack );
160        }
161    }