View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * A syntax checker which checks to see if an attributeType's type is either: 
31   * <ul>
32   *   <li>userApplications</li>
33   *   <lidirectoryOperation</li>
34   *   <lidistributedOperation</li>
35   *   <lidSAOperation</li>
36   * </ul>
37  .* 
38   * The case is NOT ignored.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  @SuppressWarnings("serial")
43  public final class AttributeTypeUsageSyntaxChecker extends SyntaxChecker
44  {
45      /**
46       * A static instance of AttributeTypeUsageSyntaxChecker
47       */
48      public static final AttributeTypeUsageSyntaxChecker INSTANCE = 
49          new AttributeTypeUsageSyntaxChecker( SchemaConstants.ATTRIBUTE_TYPE_USAGE_SYNTAX );
50  
51      /**
52       * A static Builder for this class
53       */
54      public static final class Builder extends SCBuilder<AttributeTypeUsageSyntaxChecker>
55      {
56          /**
57           * The Builder constructor
58           */
59          private Builder()
60          {
61              super( SchemaConstants.ATTRIBUTE_TYPE_USAGE_SYNTAX );
62          }
63          
64          
65          /**
66           * Create a new instance of AttributeTypeUsageSyntaxChecker
67           * @return A new instance of AttributeTypeUsageSyntaxChecker
68           */
69          @Override
70          public AttributeTypeUsageSyntaxChecker build()
71          {
72              return new AttributeTypeUsageSyntaxChecker( oid );
73          }
74      }
75  
76      
77      /**
78       * Creates a new instance of AttributeTypeUsageSyntaxChecker.
79       * 
80       * @param oid The OID to use for this SyntaxChecker
81       */
82      private AttributeTypeUsageSyntaxChecker( String oid )
83      {
84          super( oid );
85      }
86  
87  
88      /**
89       * @return An instance of the Builder for this class
90       */
91      public static Builder builder()
92      {
93          return new Builder();
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public boolean isValidSyntax( Object value )
102     {
103         String strValue;
104 
105         if ( value == null )
106         {
107             if ( LOG.isDebugEnabled() )
108             {
109                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
110             }
111             
112             return false;
113         }
114 
115         if ( value instanceof String )
116         {
117             strValue = ( String ) value;
118         }
119         else if ( value instanceof byte[] )
120         {
121             strValue = Strings.utf8ToString( ( byte[] ) value );
122         }
123         else
124         {
125             strValue = value.toString();
126         }
127 
128         switch ( strValue )
129         {
130             case "dSAOperation" :
131             case "directoryOperation" :
132             case "distributedOperation" :
133             case "userApplications" :
134                 if ( LOG.isDebugEnabled() )
135                 {
136                     LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
137                 }
138                 
139                 return true;
140 
141             default :
142                 if ( LOG.isDebugEnabled() )
143                 {
144                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
145                 }
146                 
147                 return false;
148         }
149     }
150 }