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 *    https://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     * A static instance of LdapSyntaxDescriptionSyntaxChecker
052     */
053    public static final LdapSyntaxDescriptionSyntaxChecker INSTANCE = 
054        new LdapSyntaxDescriptionSyntaxChecker( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
055    
056    /**
057     * A static Builder for this class
058     */
059    public static final class Builder extends SCBuilder<LdapSyntaxDescriptionSyntaxChecker>
060    {
061        /**
062         * The Builder constructor
063         */
064        private Builder()
065        {
066            super( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
067        }
068        
069        
070        /**
071         * Create a new instance of LdapSyntaxDescriptionSyntaxChecker
072         * @return A new instance of LdapSyntaxDescriptionSyntaxChecker
073         */
074        @Override
075        public LdapSyntaxDescriptionSyntaxChecker build()
076        {
077            return new LdapSyntaxDescriptionSyntaxChecker( oid );
078        }
079    }
080
081    
082    /**
083     * Creates a new instance of LdapSyntaxDescriptionSyntaxChecker.
084     *
085     * @param oid The OID to use for this SyntaxChecker
086     */
087    private LdapSyntaxDescriptionSyntaxChecker( String oid )
088    {
089        super( oid );
090    }
091
092    
093    /**
094     * @return An instance of the Builder for this class
095     */
096    public static Builder builder()
097    {
098        return new Builder();
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public boolean isValidSyntax( Object value )
107    {
108        String strValue;
109
110        if ( value == null )
111        {
112            if ( LOG.isDebugEnabled() )
113            {
114                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
115            }
116            
117            return false;
118        }
119
120        if ( value instanceof String )
121        {
122            strValue = ( String ) value;
123        }
124        else if ( value instanceof byte[] )
125        {
126            strValue = Strings.utf8ToString( ( byte[] ) value );
127        }
128        else
129        {
130            strValue = value.toString();
131        }
132
133        try
134        {
135            schemaParser.parse( strValue );
136
137            if ( LOG.isDebugEnabled() )
138            {
139                LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
140            }
141
142            return true;
143        }
144        catch ( ParseException pe )
145        {
146            if ( LOG.isDebugEnabled() )
147            {
148                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
149            }
150            
151            return false;
152        }
153    }
154}