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 org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026import org.apache.directory.api.util.Strings;
027
028
029/**
030 * A SyntaxChecker which verifies that a value is an OtherMailbox according to 
031 * RFC 4517 :
032 * <pre>
033 * OtherMailbox = mailbox-type DOLLAR mailbox
034 * mailbox-type = PrintableString
035 * mailbox      = IA5String
036 * </pre>
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039@SuppressWarnings("serial")
040public final class OtherMailboxSyntaxChecker extends SyntaxChecker
041{
042    /**
043     * A static instance of OtherMailboxSyntaxChecker
044     */
045    public static final OtherMailboxSyntaxChecker INSTANCE = 
046        new OtherMailboxSyntaxChecker( SchemaConstants.OTHER_MAILBOX_SYNTAX );
047    
048    /**
049     * A static Builder for this class
050     */
051    public static final class Builder extends SCBuilder<OtherMailboxSyntaxChecker>
052    {
053        /**
054         * The Builder constructor
055         */
056        private Builder()
057        {
058            super( SchemaConstants.OTHER_MAILBOX_SYNTAX );
059        }
060        
061        
062        /**
063         * Create a new instance of OtherMailboxSyntaxChecker
064         * @return A new instance of OtherMailboxSyntaxChecker
065         */
066        @Override
067        public OtherMailboxSyntaxChecker build()
068        {
069            return new OtherMailboxSyntaxChecker( oid );
070        }
071    }
072
073    
074    /**
075     * Creates a new instance of OtherMailboxSyntaxChecker.
076     */
077    private OtherMailboxSyntaxChecker( 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        String strValue;
099
100        if ( value == null )
101        {
102            if ( LOG.isDebugEnabled() )
103            {
104                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
105            }
106            
107            return false;
108        }
109
110        if ( value instanceof String )
111        {
112            strValue = ( String ) value;
113        }
114        else if ( value instanceof byte[] )
115        {
116            strValue = Strings.utf8ToString( ( byte[] ) value );
117        }
118        else
119        {
120            strValue = value.toString();
121        }
122
123        if ( strValue.length() == 0 )
124        {
125            if ( LOG.isDebugEnabled() )
126            {
127                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
128            }
129            
130            return false;
131        }
132
133        // Search for the '$' separator
134        int dollar = strValue.indexOf( '$' );
135
136        if ( dollar == -1 )
137        {
138            // No '$' => error
139            if ( LOG.isDebugEnabled() )
140            {
141                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
142            }
143            
144            return false;
145        }
146
147        String mailboxType = strValue.substring( 0, dollar );
148
149        String mailbox = ( dollar < strValue.length() - 1 )
150            ? strValue.substring( dollar + 1 ) : "";
151
152        // The mailbox should not contains a '$'
153        if ( mailbox.indexOf( '$' ) != -1 )
154        {
155            if ( LOG.isDebugEnabled() )
156            {
157                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
158            }
159            
160            return false;
161        }
162
163        // Check that the mailboxType is a PrintableString
164        if ( !Strings.isPrintableString( mailboxType ) )
165        {
166            if ( LOG.isDebugEnabled() )
167            {
168                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
169            }
170            
171            return false;
172        }
173
174        // Check that the mailbox is an IA5String
175        boolean result = Strings.isIA5String( mailbox );
176
177        if ( LOG.isDebugEnabled() )
178        {
179            if ( result )
180            {
181                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
182            }
183            else
184            {
185                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
186            }
187        }
188
189        return result;
190    }
191}