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 java.text.ParseException;
24  
25  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
26  import org.apache.directory.api.ldap.model.schema.SchemaManager;
27  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28  import org.apache.directory.api.ldap.model.subtree.SubtreeSpecificationChecker;
29  import org.apache.directory.api.util.Strings;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A SyntaxChecker which verifies that a value is a subtree specification.
36   * 
37   * It has been removed in RFC 4517
38   *  
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  @SuppressWarnings("serial")
42  public class SubtreeSpecificationSyntaxChecker extends SyntaxChecker
43  {
44      /** A logger for this class */
45      private static final Logger LOG = LoggerFactory.getLogger( SubtreeSpecificationSyntaxChecker.class );
46  
47      /** The associated checker */
48      private SubtreeSpecificationChecker subtreeSpecificationChecker;
49  
50  
51      /**
52       * Creates an instance of SubtreeSpecificationSyntaxChecker
53       */
54      public SubtreeSpecificationSyntaxChecker()
55      {
56          super( SchemaConstants.SUBTREE_SPECIFICATION_SYNTAX );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public boolean isValidSyntax( Object value )
64      {
65          String strValue = null;
66  
67          if ( value == null )
68          {
69              LOG.debug( "Syntax invalid for 'null'" );
70              return false;
71          }
72  
73          if ( value instanceof String )
74          {
75              strValue = ( String ) value;
76          }
77          else if ( value instanceof byte[] )
78          {
79              strValue = Strings.utf8ToString( ( byte[] ) value );
80          }
81          else
82          {
83              strValue = value.toString();
84          }
85  
86          if ( strValue.length() == 0 )
87          {
88              LOG.debug( "Syntax invalid for '{}'", value );
89              return false;
90          }
91  
92          try
93          {
94              synchronized ( subtreeSpecificationChecker )
95              {
96                  subtreeSpecificationChecker.parse( strValue );
97              }
98  
99              LOG.debug( "Syntax valid for '{}'", value );
100             return true;
101         }
102         catch ( ParseException pe )
103         {
104             LOG.debug( "Syntax invalid for '{}'", value );
105             return false;
106         }
107     }
108 
109 
110     /**
111      * {@inheritDoc}
112      */
113     public void setSchemaManager( SchemaManager schemaManager )
114     {
115         subtreeSpecificationChecker = new SubtreeSpecificationChecker( schemaManager );
116     }
117 }