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.util.UUID;
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.SyntaxChecker;
28  import org.apache.directory.api.util.Strings;
29  
30  
31  /**
32   * An UUID syntax checker.
33   * <pre>
34   * UUID ::= OCTET STRING (SIZE(16)) -- constrained to an UUID [RFC4122]
35   * </pre>
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  @SuppressWarnings("serial")
39  public final class UuidSyntaxChecker extends SyntaxChecker
40  {
41      /**
42       * A static instance of UuidSyntaxChecker
43       */
44      public static final UuidSyntaxChecker INSTANCE = new UuidSyntaxChecker( SchemaConstants.UUID_SYNTAX );
45      
46      /**
47       * A static Builder for this class
48       */
49      public static final class Builder extends SCBuilder<UuidSyntaxChecker>
50      {
51          /**
52           * The Builder constructor
53           */
54          private Builder()
55          {
56              super( SchemaConstants.UUID_SYNTAX );
57          }
58          
59          
60          /**
61           * Create a new instance of UuidSyntaxChecker
62           * @return A new instance of UuidSyntaxChecker
63           */
64          @Override
65          public UuidSyntaxChecker build()
66          {
67              return new UuidSyntaxChecker( oid );
68          }
69      }
70  
71      
72      /**
73       * Creates a new instance of UUIDSyntaxChecker.
74       * 
75       * @param oid The OID to use for this SyntaxChecker
76       */
77      private UuidSyntaxChecker( String oid )
78      {
79          super( oid );
80      }
81  
82      
83      /**
84       * @return An instance of the Builder for this class
85       */
86      public static Builder builder()
87      {
88          return new Builder();
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public boolean isValidSyntax( Object value )
97      {
98          if ( value == null )
99          {
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 }