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.asn1.util.Oid;
024import org.apache.directory.api.i18n.I18n;
025import org.apache.directory.api.ldap.model.constants.SchemaConstants;
026import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * A SyntaxChecker which verifies that a value is a numeric oid and a length
032 * constraint according to RFC 4512.
033 * <p>
034 * From RFC 4512 :
035 * <pre>
036 * noidlen    = numericoid [ LCURLY len RCURLY ]
037 * numericoid = number 1*( DOT number )
038 * len        = number
039 * number     = DIGIT | ( LDIGIT 1*DIGIT )
040 * DIGIT      = %x30 | LDIGIT                  ; "0"-"9"
041 * LDIGIT     = %x31-39                        ; "1"-"9"
042 * DOT        = %x2E                           ; period (".")
043 * LCURLY  = %x7B                              ; left curly brace "{"
044 * RCURLY  = %x7D                              ; right curly brace "}"
045 * </pre>
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049@SuppressWarnings("serial")
050public final class OidLenSyntaxChecker extends SyntaxChecker
051{
052    /**
053     * A static instance of OidLenSyntaxChecker
054     */
055    public static final OidLenSyntaxChecker INSTANCE = 
056        new OidLenSyntaxChecker( SchemaConstants.OID_LEN_SYNTAX );
057    
058    /**
059     * A static Builder for this class
060     */
061    public static final class Builder extends SCBuilder<OidLenSyntaxChecker>
062    {
063        /**
064         * The Builder constructor
065         */
066        private Builder()
067        {
068            super( SchemaConstants.OID_LEN_SYNTAX );
069        }
070        
071        
072        /**
073         * Create a new instance of OidLenSyntaxChecker
074         * @return A new instance of OidLenSyntaxChecker
075         */
076        @Override
077        public OidLenSyntaxChecker build()
078        {
079            return new OidLenSyntaxChecker( oid );
080        }
081    }
082
083    
084    /**
085     * 
086     * Creates a new instance of OidLenSyntaxChecker.
087     *
088     */
089    private OidLenSyntaxChecker( String oid )
090    {
091        super( oid );
092    }
093
094    
095    /**
096     * @return An instance of the Builder for this class
097     */
098    public static Builder builder()
099    {
100        return new Builder();
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public boolean isValidSyntax( Object value )
109    {
110        String strValue;
111
112        if ( value == null )
113        {
114            if ( LOG.isDebugEnabled() )
115            {
116                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
117            }
118            
119            return false;
120        }
121
122        if ( value instanceof String )
123        {
124            strValue = ( String ) value;
125        }
126        else if ( value instanceof byte[] )
127        {
128            strValue = Strings.utf8ToString( ( byte[] ) value );
129        }
130        else
131        {
132            strValue = value.toString();
133        }
134
135        if ( strValue.length() == 0 )
136        {
137            if ( LOG.isDebugEnabled() )
138            {
139                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
140            }
141            
142            return false;
143        }
144
145        // We are looking at the first position of the len part
146        int pos = strValue.indexOf( '{' );
147
148        if ( pos < 0 )
149        {
150            // Not found ... but it may still be a valid OID
151            boolean result = Oid.isOid( strValue );
152
153            if ( LOG.isDebugEnabled() )
154            {
155                if ( result )
156                {
157                    LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
158                }
159                else
160                {
161                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
162                }
163            }
164
165            return result;
166        }
167        else
168        {
169            // we should have a len value. First check that the OID is valid
170            String oid = strValue.substring( 0, pos );
171
172            if ( !Oid.isOid( oid ) )
173            {
174                if ( LOG.isDebugEnabled() )
175                {
176                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
177                }
178                
179                return false;
180            }
181
182            String len = strValue.substring( pos );
183
184            // We must have a number and a '}' at the end
185            if ( len.charAt( len.length() - 1 ) != '}' )
186            {
187                // No final '}'
188                if ( LOG.isDebugEnabled() )
189                {
190                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
191                }
192                
193                return false;
194            }
195
196            for ( int i = 1; i < len.length() - 1; i++ )
197            {
198                switch ( len.charAt( i ) )
199                {
200                    case '0':
201                    case '1':
202                    case '2':
203                    case '3':
204                    case '4':
205                    case '5':
206                    case '6':
207                    case '7':
208                    case '8':
209                    case '9':
210                        break;
211
212                    default:
213                        if ( LOG.isDebugEnabled() )
214                        {
215                            LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
216                        }
217                        
218                        return false;
219                }
220            }
221
222            if ( ( len.charAt( 1 ) == '0' ) && len.length() > 3 )
223            {
224                // A number can't start with a '0' unless it's the only
225                // number
226                if ( LOG.isDebugEnabled() )
227                {
228                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
229                }
230                
231                return false;
232            }
233
234            if ( LOG.isDebugEnabled() )
235            {
236                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
237            }
238            
239            return true;
240        }
241    }
242}