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.aci;
21  
22  
23  import java.text.ParseException;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27  import org.apache.directory.api.ldap.model.schema.SchemaManager;
28  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
29  import org.apache.directory.api.util.Strings;
30  
31  
32  /**
33   * A SyntaxChecker which verifies that a value is a valid ACIItem.
34   * 
35   *  
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  @SuppressWarnings("serial")
39  public final class ACIItemSyntaxChecker extends SyntaxChecker
40  {
41      /** An instance of ACI Item Checker */
42      private transient ACIItemChecker aciItemChecker;
43      
44      /**
45       * A static instance of ACIItemSyntaxChecker
46       */
47      public static final ACIItemSyntaxChecker INSTANCE = 
48          new ACIItemSyntaxChecker( SchemaConstants.ACI_ITEM_SYNTAX, null );
49      
50      /**
51       * A static Builder for this class
52       */
53      public static final class Builder extends SCBuilder<ACIItemSyntaxChecker>
54      {
55          /** The schemaManager to use */
56          private SchemaManager schemaManager;
57          
58          /**
59           * The Builder constructor
60           */
61          private Builder()
62          {
63              super( SchemaConstants.ACI_ITEM_SYNTAX );
64          }
65          
66          
67          public Builder setSchemaManager( SchemaManager schemaManager )
68          {
69              this.schemaManager = schemaManager;
70              
71              return this;
72          }
73          
74          /**
75           * Create a new instance of ACIItemSyntaxChecker
76           * @return A new instance of ACIItemSyntaxChecker
77           */
78          @Override
79          public ACIItemSyntaxChecker build()
80          {
81              return new ACIItemSyntaxChecker( oid, schemaManager );
82          }
83      }
84  
85  
86      /**
87       * Creates a new instance of ACIItemSyntaxChecker
88       * 
89       * @param oid The OID to use for this SyntaxChecker
90       * @param schemaManager The SchemaManager instance
91       */
92      private ACIItemSyntaxChecker( String oid, SchemaManager schemaManager )
93      {
94          super( oid );
95          aciItemChecker = new ACIItemChecker( schemaManager );
96      }
97  
98      
99      /**
100      * @return An instance of the Builder for this class
101      */
102     public static Builder builder()
103     {
104         return new Builder();
105     }
106 
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public boolean isValidSyntax( Object value )
113     {
114         String strValue;
115 
116         if ( value == null )
117         {
118             if ( LOG.isDebugEnabled() )
119             {
120                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
121             }
122             
123             return false;
124         }
125 
126         if ( value instanceof String )
127         {
128             strValue = ( String ) value;
129         }
130         else if ( value instanceof byte[] )
131         {
132             strValue = Strings.utf8ToString( ( byte[] ) value );
133         }
134         else
135         {
136             strValue = value.toString();
137         }
138 
139         if ( strValue.length() == 0 )
140         {
141             if ( LOG.isDebugEnabled() )
142             {
143                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
144             }
145             
146             return false;
147         }
148 
149         try
150         {
151             synchronized ( aciItemChecker )
152             {
153                 aciItemChecker.parse( strValue );
154             }
155 
156             if ( LOG.isDebugEnabled() )
157             {
158                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
159             }
160             
161             return true;
162         }
163         catch ( ParseException pe )
164         {
165             if ( LOG.isDebugEnabled() )
166             {
167                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
168             }
169             
170             return false;
171         }
172     }
173 }