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.util.UUID;
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.util.Strings;
029
030
031/**
032 * An UUID syntax checker.
033 * <pre>
034 * UUID ::= OCTET STRING (SIZE(16)) -- constrained to an UUID [RFC4122]
035 * </pre>
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038@SuppressWarnings("serial")
039public final class UuidSyntaxChecker extends SyntaxChecker
040{
041    /**
042     * A static instance of UuidSyntaxChecker
043     */
044    public static final UuidSyntaxChecker INSTANCE = new UuidSyntaxChecker( SchemaConstants.UUID_SYNTAX );
045    
046    /**
047     * A static Builder for this class
048     */
049    public static final class Builder extends SCBuilder<UuidSyntaxChecker>
050    {
051        /**
052         * The Builder constructor
053         */
054        private Builder()
055        {
056            super( SchemaConstants.UUID_SYNTAX );
057        }
058        
059        
060        /**
061         * Create a new instance of UuidSyntaxChecker
062         * @return A new instance of UuidSyntaxChecker
063         */
064        @Override
065        public UuidSyntaxChecker build()
066        {
067            return new UuidSyntaxChecker( oid );
068        }
069    }
070
071    
072    /**
073     * Creates a new instance of UUIDSyntaxChecker.
074     * 
075     * @param oid The OID to use for this SyntaxChecker
076     */
077    private UuidSyntaxChecker( String oid )
078    {
079        super( oid );
080    }
081
082    
083    /**
084     * @return An instance of the Builder for this class
085     */
086    public static Builder builder()
087    {
088        return new Builder();
089    }
090
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public boolean isValidSyntax( Object value )
097    {
098        if ( value == null )
099        {
100            if ( LOG.isDebugEnabled() )
101            {
102                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
103            }
104            
105            return false;
106        }
107
108        if ( value instanceof UUID )
109        {
110            return true;
111        }
112
113        if ( !( value instanceof String ) )
114        {
115            if ( LOG.isDebugEnabled() )
116            {
117                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
118            }
119            
120            return false;
121        }
122
123        return Strings.isValidUuid( ( String ) value );
124    }
125}