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, eCopyOfUuidSyntaxCheckerither 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.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.csn.Csn;
026import org.apache.directory.api.ldap.model.csn.InvalidCSNException;
027import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
028
029
030/**
031 * An CSN syntax checker.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035@SuppressWarnings("serial")
036public final class CsnSyntaxChecker extends SyntaxChecker
037{
038    /**
039     * A static instance of CsnSyntaxChecker
040     */
041    public static final CsnSyntaxChecker INSTANCE = new CsnSyntaxChecker( SchemaConstants.CSN_SYNTAX );
042    
043    /**
044     * A static Builder for this class
045     */
046    public static final class Builder extends SCBuilder<CsnSyntaxChecker>
047    {
048        /**
049         * The Builder constructor
050         */
051        private Builder()
052        {
053            super( SchemaConstants.CSN_SYNTAX );
054        }
055        
056        
057        /**
058         * Create a new instance of CsnSyntaxChecker
059         * @return A new instance of CsnSyntaxChecker
060         */
061        @Override
062        public CsnSyntaxChecker build()
063        {
064            return new CsnSyntaxChecker( oid );
065        }
066    }
067
068    
069    /**
070     * Creates a new instance of CsnSyntaxChecker.
071     * 
072     * @param oid The OID to use for this SyntaxChecker
073     */
074    private CsnSyntaxChecker( String oid )
075    {
076        super( oid );
077    }
078
079    
080    /**
081     * @return An instance of the Builder for this class
082     */
083    public static Builder builder()
084    {
085        return new Builder();
086    }
087
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public boolean isValidSyntax( Object value )
094    {
095        if ( value == null )
096        {
097            if ( LOG.isDebugEnabled() )
098            {
099                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
100            }
101            
102            return false;
103        }
104
105        if ( !( value instanceof String ) )
106        {
107            if ( LOG.isDebugEnabled() )
108            {
109                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
110            }
111            
112            return false;
113        }
114
115        String csnStr = ( String ) value;
116
117        // It must be a valid CSN : try to create a new one.
118        try
119        {
120            boolean result = Csn.isValid( csnStr );
121
122            if ( LOG.isDebugEnabled() )
123            {
124                if ( result )
125                {
126                    LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
127                }
128                else
129                {
130                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
131                }
132            }
133
134            return result;
135        }
136        catch ( InvalidCSNException icsne )
137        {
138            if ( LOG.isDebugEnabled() )
139            {
140                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
141            }
142            
143            return false;
144        }
145    }
146}