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.SchemaManager;
028import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
029import org.apache.directory.api.ldap.model.subtree.SubtreeSpecificationChecker;
030import org.apache.directory.api.util.Strings;
031
032
033/**
034 * A SyntaxChecker which verifies that a value is a subtree specification.
035 * <p>
036 * It has been removed in RFC 4517
037 *  
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040@SuppressWarnings("serial")
041public final class SubtreeSpecificationSyntaxChecker extends SyntaxChecker
042{
043    /** The associated checker */
044    private transient SubtreeSpecificationChecker subtreeSpecificationChecker;
045    
046    /**
047     * A static instance of SubtreeSpecificationSyntaxChecker
048     */
049    public static final SubtreeSpecificationSyntaxChecker INSTANCE = 
050        new SubtreeSpecificationSyntaxChecker( SchemaConstants.SUBTREE_SPECIFICATION_SYNTAX, null );
051    
052    /**
053     * A static Builder for this class
054     */
055    public static final class Builder extends SCBuilder<SubtreeSpecificationSyntaxChecker>
056    {
057        /** The schemaManager */
058        private SchemaManager schemaManager;
059        
060        /**
061         * The Builder constructor
062         */
063        private Builder()
064        {
065            super( SchemaConstants.SUBTREE_SPECIFICATION_SYNTAX );
066        }
067        
068        
069        public Builder setSchemaManager( SchemaManager schemaManager )
070        {
071            this.schemaManager = schemaManager;
072                
073            return this;
074        }
075        
076        
077        /**
078         * Create a new instance of SubtreeSpecificationSyntaxChecker
079         * @return A new instance of SubtreeSpecificationSyntaxChecker
080         */
081        @Override
082        public SubtreeSpecificationSyntaxChecker build()
083        {
084            return new SubtreeSpecificationSyntaxChecker( oid, schemaManager );
085        }
086    }
087    
088    /**
089     * Creates an instance of SubtreeSpecificationSyntaxChecker
090     * 
091     * @param oid The OID to use for this SyntaxChecker
092     * @param schemaManager the SchemaManager instance
093     */
094    private SubtreeSpecificationSyntaxChecker( String oid, SchemaManager schemaManager )
095    {
096        super( oid );
097        subtreeSpecificationChecker = new SubtreeSpecificationChecker( schemaManager );
098    }
099
100    
101    /**
102     * @return An instance of the Builder for this class
103     */
104    public static Builder builder()
105    {
106        return new Builder();
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public boolean isValidSyntax( Object value )
115    {
116        String strValue;
117
118        if ( value == null )
119        {
120            if ( LOG.isDebugEnabled() )
121            {
122                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
123            }
124            
125            return false;
126        }
127
128        if ( value instanceof String )
129        {
130            strValue = ( String ) value;
131        }
132        else if ( value instanceof byte[] )
133        {
134            strValue = Strings.utf8ToString( ( byte[] ) value );
135        }
136        else
137        {
138            strValue = value.toString();
139        }
140
141        if ( strValue.length() == 0 )
142        {
143            if ( LOG.isDebugEnabled() )
144            {
145                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
146            }
147            
148            return false;
149        }
150
151        try
152        {
153            synchronized ( subtreeSpecificationChecker )
154            {
155                subtreeSpecificationChecker.parse( strValue );
156            }
157
158            if ( LOG.isDebugEnabled() )
159            {
160                LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
161            }
162            
163            return true;
164        }
165        catch ( ParseException pe )
166        {
167            if ( LOG.isDebugEnabled() )
168            {
169                LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
170            }
171            
172            return false;
173        }
174    }
175}