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.HashSet;
24  import java.util.Set;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
28  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
29  import org.apache.directory.api.util.Chars;
30  import org.apache.directory.api.util.Strings;
31  
32  
33  /**
34   * A SyntaxChecker which verifies that a value is a DSEType according to 
35   * http://tools.ietf.org/id/draft-ietf-asid-ldapv3-attributes-03.txt, par 6.2.1.5 :
36   * <pre>
37   * &lt;DSEType&gt;    ::= '(' &lt;sp&gt;* &lt;DSEBit&gt; &lt;sp&gt;* &lt;DSEBitList&gt; ')'
38   * &lt;DSEBitList&gt; ::= '$' &lt;sp&gt;* &lt;DSEBit&gt; &lt;sp&gt;* &lt;DSEBitList&gt; | e      
39   * &lt;DSEBit&gt;     ::= 'root' | 'glue' | 'cp' | 'entry' | 'alias' | 'subr' |
40   *                  'nssr' | 'supr' | 'xr' | 'admPoint' | 'subentry' |
41   *                  'shadow' | 'zombie' | 'immSupr' | 'rhob' | 'sa'
42   * </pre>
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  @SuppressWarnings("serial")
47  public final class DseTypeSyntaxChecker extends SyntaxChecker
48  {
49      /** The DSE BITS keywords */
50      private static final String[] DSE_BITS_STRINGS =
51          {
52              "root", "glue", "cp", "entry", "alias", "subr",
53              "nssr", "supr", "xr", "admPoint", "subentry",
54              "shadow", "zombie", "immSupr", "rhob", "sa"
55      };
56  
57      /** The Set which contains the DESBits */
58      private static final Set<String> DSE_BITS = new HashSet<>();
59      
60      /**
61       * A static instance of DseTypeSyntaxChecker
62       */
63      public static final DseTypeSyntaxChecker INSTANCE = new DseTypeSyntaxChecker( SchemaConstants.DSE_TYPE_SYNTAX );
64      
65      /**
66       * A static Builder for this class
67       */
68      public static final class Builder extends SCBuilder<DseTypeSyntaxChecker>
69      {
70          /**
71           * The Builder constructor
72           */
73          private Builder()
74          {
75              super( SchemaConstants.DSE_TYPE_SYNTAX );
76          }
77          
78          
79          /**
80           * Create a new instance of DseTypeSyntaxChecker
81           * @return A new instance of DseTypeSyntaxChecker
82           */
83          @Override
84          public DseTypeSyntaxChecker build()
85          {
86              return new DseTypeSyntaxChecker( oid );
87          }
88      }
89  
90      
91      /** Initialization of the country set */
92      static
93      {
94          for ( String country : DSE_BITS_STRINGS )
95          {
96              DSE_BITS.add( country );
97          }
98      }
99  
100 
101     /**
102      * Creates a new instance of DSETypeSyntaxChecker.
103      *
104      * @param oid The OID to use for this SyntaxChecker
105      */
106     private DseTypeSyntaxChecker( String oid )
107     {
108         super( oid );
109     }
110 
111     
112     /**
113      * @return An instance of the Builder for this class
114      */
115     public static Builder builder()
116     {
117         return new Builder();
118     }
119 
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public boolean isValidSyntax( Object value )
126     {
127         String strValue;
128 
129         if ( value == null )
130         {
131             if ( LOG.isDebugEnabled() )
132             {
133                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
134             }
135             
136             return false;
137         }
138 
139         if ( value instanceof String )
140         {
141             strValue = ( String ) value;
142         }
143         else if ( value instanceof byte[] )
144         {
145             strValue = Strings.utf8ToString( ( byte[] ) value );
146         }
147         else
148         {
149             strValue = value.toString();
150         }
151 
152         // We must have at least '(cp)', '(xr)' or '(ca)'
153         if ( strValue.length() < 4 )
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 the opening and closing parenthesis
164         if ( ( strValue.charAt( 0 ) != '(' )
165             || ( strValue.charAt( strValue.length() - 1 ) != ')' ) )
166         {
167             if ( LOG.isDebugEnabled() )
168             {
169                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
170             }
171             
172             return false;
173         }
174 
175         Set<String> keywords = new HashSet<>();
176         int len = strValue.length() - 1;
177         boolean needKeyword = true;
178 
179         // 
180         for ( int i = 1; i < len; /* */)
181         {
182             // Skip spaces
183             while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
184             {
185                 i++;
186             }
187 
188             int pos = i;
189 
190             // Search for a keyword
191             while ( ( i < len ) && Chars.isAlphaASCII( strValue, pos ) )
192             {
193                 pos++;
194             }
195 
196             if ( pos == i )
197             {
198                 // No keyword : error
199                 if ( LOG.isDebugEnabled() )
200                 {
201                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
202                 }
203                 
204                 return false;
205             }
206 
207             String keyword = strValue.substring( i, pos );
208             i = pos;
209 
210             if ( !DSE_BITS.contains( keyword ) )
211             {
212                 // Unknown keyword
213                 if ( LOG.isDebugEnabled() )
214                 {
215                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
216                 }
217                 
218                 return false;
219             }
220 
221             // Check that the keyword has not been met
222             if ( keywords.contains( keyword ) )
223             {
224                 if ( LOG.isDebugEnabled() )
225                 {
226                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
227                 }
228                 
229                 return false;
230             }
231 
232             keywords.add( keyword );
233             needKeyword = false;
234 
235             // Skip spaces
236             while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
237             {
238                 i++;
239             }
240 
241             // Do we have another keyword ?
242             if ( ( i < len ) && ( strValue.charAt( i ) == '$' ) )
243             {
244                 // yes
245                 i++;
246                 needKeyword = true;
247                 continue;
248             }
249         }
250 
251         // We are done
252         if ( LOG.isDebugEnabled() )
253         {
254             if ( needKeyword )
255             {
256                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
257             }
258             else
259             {
260                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
261             }
262         }
263 
264         return !needKeyword;
265     }
266 }