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 java.text.ParseException;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.constants.SchemaConstants;
027import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
028import org.apache.directory.api.ldap.model.schema.parsers.LdapSyntaxDescriptionSchemaParser;
029import org.apache.directory.api.util.Strings;
030
031
032/**
033 * A SyntaxChecker which verifies that a value follows the
034 * LDAP syntax description syntax according to RFC 4512, par 4.2.2:
035 * 
036 * <pre>
037 * SyntaxDescription = LPAREN WSP
038 *    numericoid                 ; object identifier
039 *    [ SP "DESC" SP qdstring ]  ; description
040 *    extensions WSP RPAREN      ; extensions
041 * </pre>
042 * 
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045@SuppressWarnings("serial")
046public final class LdapSyntaxDescriptionSyntaxChecker extends SyntaxChecker
047{
048    /** The schema parser used to parse the LdapSyntax description Syntax */
049    private transient LdapSyntaxDescriptionSchemaParser schemaParser = new LdapSyntaxDescriptionSchemaParser();
050    
051    /**
052     * A static instance of LdapSyntaxDescriptionSyntaxChecker
053     */
054    public static final LdapSyntaxDescriptionSyntaxChecker INSTANCE = 
055        new LdapSyntaxDescriptionSyntaxChecker( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
056    
057    /**
058     * A static Builder for this class
059     */
060    public static final class Builder extends SCBuilder<LdapSyntaxDescriptionSyntaxChecker>
061    {
062        /**
063         * The Builder constructor
064         */
065        private Builder()
066        {
067            super( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
068        }
069        
070        
071        /**
072         * Create a new instance of LdapSyntaxDescriptionSyntaxChecker
073         * @return A new instance of LdapSyntaxDescriptionSyntaxChecker
074         */
075        @Override
076        public LdapSyntaxDescriptionSyntaxChecker build()
077        {
078            return new LdapSyntaxDescriptionSyntaxChecker( oid );
079        }
080    }
081
082    
083    /**
084     * Creates a new instance of LdapSyntaxDescriptionSyntaxChecker.
085     *
086     * @param oid The OID to use for this SyntaxChecker
087     */
088    private LdapSyntaxDescriptionSyntaxChecker( String oid )
089    {
090        super( oid );
091    }
092
093    
094    /**
095     * @return An instance of the Builder for this class
096     */
097    public static Builder builder()
098    {
099        return new Builder();
100    }
101
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public boolean isValidSyntax( Object value )
108    {
109        String strValue;
110
111        if ( value == null )
112        {
113            if ( LOG.isDebugEnabled() )
114            {
115                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
116            }
117            
118            return false;
119        }
120
121        if ( value instanceof String )
122        {
123            strValue = ( String ) value;
124        }
125        else if ( value instanceof byte[] )
126        {
127            strValue = Strings.utf8ToString( ( byte[] ) value );
128        }
129        else
130        {
131            strValue = value.toString();
132        }
133
134        try
135        {
136            schemaParser.parseLdapSyntaxDescription( strValue );
137
138            if ( LOG.isDebugEnabled() )
139            {
140                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
141            }
142
143            return true;
144        }
145        catch ( ParseException pe )
146        {
147            if ( LOG.isDebugEnabled() )
148            {
149                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
150            }
151            
152            return false;
153        }
154    }
155}