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.schema.converter;
21  
22  
23  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.entry.DefaultEntry;
25  import org.apache.directory.api.ldap.model.entry.Entry;
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.ldif.LdifUtils;
28  import org.apache.directory.api.ldap.model.name.Rdn;
29  import org.apache.directory.api.ldap.model.schema.UsageEnum;
30  
31  
32  /**
33   * A bean used to hold the literal values of an AttributeType parsed out of an
34   * OpenLDAP schema configuration file.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class AttributeTypeHolder extends SchemaElementImpl
39  {
40      /** A flag for single valued attributes. Default to false */
41      private boolean singleValue = false;
42  
43      /** A flag for collective attribute. Default to false */
44      private boolean collective = false;
45  
46      /** A flaf for immutable attribue. Default to false */
47      private boolean noUserModification = false;
48  
49      /** The optional superior */
50      private String superior;
51  
52      /** The equality matching rule */
53      private String equality;
54  
55      /** The ordering matching rule */
56      private String ordering;
57  
58      /** The substring matching rule */
59      private String substr;
60  
61      /** The syntax this attribute respects */
62      private String syntax;
63  
64      /** The optional length for this attribute */
65      private long oidLen = -1;
66  
67      /** The attribute uase. Default to userApplication */
68      private UsageEnum usage = UsageEnum.USER_APPLICATIONS;
69  
70  
71      /**
72       * Create an instance of an attributeType
73       * 
74       * @param oid The attributeType's OID
75       */
76      public AttributeTypeHolder( String oid )
77      {
78          this.oid = oid;
79      }
80  
81  
82      /**
83       * Tells if the attribute is single-valued
84       * 
85       * @return true if the attribute is single-valued, false otherwise
86       */
87      public boolean isSingleValue()
88      {
89          return singleValue;
90      }
91  
92  
93      /**
94       * Set the attributeType singleValue flag
95       * 
96       * @param singleValue The value for this flag
97       */
98      public void setSingleValue( boolean singleValue )
99      {
100         this.singleValue = singleValue;
101     }
102 
103 
104     /**
105      * Tells if the attributeType is collectove or not
106      * 
107      * @return True if the attributeType is collective, false otherwise
108      */
109     public boolean isCollective()
110     {
111         return collective;
112     }
113 
114 
115     /**
116      * Set the attributeType collective flag
117      * 
118      * @param collective The value for this flag
119      */
120     public void setCollective( boolean collective )
121     {
122         this.collective = collective;
123     }
124 
125 
126     /**
127      * Tells if the attributeType is mutable or not
128      * 
129      * @return True if the attributeType is immutable, false otherwise
130      */
131     public boolean isNoUserModification()
132     {
133         return noUserModification;
134     }
135 
136 
137     /**
138      * Set the attributeType noUserModification flag
139      * 
140      * @param noUserModification The value for this flag
141      */
142     public void setNoUserModification( boolean noUserModification )
143     {
144         this.noUserModification = noUserModification;
145     }
146 
147 
148     /**
149      * Get the optional attributeType's superior
150      * 
151      * @return The attributeType's superior, if any
152      */
153     public String getSuperior()
154     {
155         return superior;
156     }
157 
158 
159     /**
160      * Set the attributeType's superior
161      * 
162      * @param superior The attributeType's superior
163      */
164     public void setSuperior( String superior )
165     {
166         this.superior = superior;
167     }
168 
169 
170     /**
171      * Get the equality Matching Rule
172      * 
173      * @return The equality matchingRule
174      */
175     public String getEquality()
176     {
177         return equality;
178     }
179 
180 
181     /**
182      * Set the equality Matching Rule
183      * 
184      * @param equality The equality Matching Rule
185      */
186     public void setEquality( String equality )
187     {
188         this.equality = equality;
189     }
190 
191 
192     /**
193      * Get the ordering Matching Rule
194      * 
195      * @return The ordering matchingRule
196      */
197     public String getOrdering()
198     {
199         return ordering;
200     }
201 
202 
203     /**
204      * Set the ordering Matching Rule
205      * 
206      * @param ordering The ordering Matching Rule
207      */
208     public void setOrdering( String ordering )
209     {
210         this.ordering = ordering;
211     }
212 
213 
214     /**
215      * Get the substring Matching Rule
216      * 
217      * @return The substring matchingRule
218      */
219     public String getSubstr()
220     {
221         return substr;
222     }
223 
224 
225     /**
226      * Set the substring Matching Rule
227      * 
228      * @param substr The substring Matching Rule
229      */
230     public void setSubstr( String substr )
231     {
232         this.substr = substr;
233     }
234 
235 
236     /**
237      * Get the attributeType's syntax
238      * 
239      * @return The attributeType's syntax
240      */
241     public String getSyntax()
242     {
243         return syntax;
244     }
245 
246 
247     /**
248      * Set the attributeType's syntax
249      * 
250      * @param syntax The attributeType's syntax
251      */
252     public void setSyntax( String syntax )
253     {
254         this.syntax = syntax;
255     }
256 
257 
258     /**
259      * Get the attributeType's usage
260      * 
261      * @return The attributeType's usage
262      */
263     public UsageEnum getUsage()
264     {
265         return usage;
266     }
267 
268 
269     /**
270      * Set the attributeType's usage
271      * 
272      * @param usage The attributeType's usage
273      */
274     public void setUsage( UsageEnum usage )
275     {
276         this.usage = usage;
277     }
278 
279 
280     /**
281      * Get the attributeType's syntax length
282      * 
283      * @return The attributeType's syntax length
284      */
285     public long getOidLen()
286     {
287         return oidLen;
288     }
289 
290 
291     /**
292      * Set the attributeType's syntax length
293      * 
294      * @param oidLen The attributeType's syntax length
295      */
296     public void setOidLen( long oidLen )
297     {
298         this.oidLen = oidLen;
299     }
300 
301 
302     /**
303      * Convert this attributeType to a Ldif string
304      * 
305      * @param schemaName The name of the schema file containing this attributeType
306      * @return A ldif formatted string
307      * @throws org.apache.directory.api.ldap.model.exception.LdapException If something went wrong
308      */
309     @Override
310     public String toLdif( String schemaName ) throws LdapException
311     {
312         StringBuilder sb = new StringBuilder();
313 
314         sb.append( schemaToLdif( schemaName, "metaAttributeType" ) );
315 
316         // The superior
317         if ( superior != null )
318         {
319             sb.append( "m-supAttributeType: " ).append( superior ).append( '\n' );
320         }
321 
322         // The equality matching rule
323         if ( equality != null )
324         {
325             sb.append( "m-equality: " ).append( equality ).append( '\n' );
326         }
327 
328         // The ordering matching rule
329         if ( ordering != null )
330         {
331             sb.append( "m-ordering: " ).append( ordering ).append( '\n' );
332         }
333 
334         // The substrings matching rule
335         if ( substr != null )
336         {
337             sb.append( "m-substr: " ).append( substr ).append( '\n' );
338         }
339 
340         // The value syntax
341         if ( syntax != null )
342         {
343             sb.append( "m-syntax: " ).append( syntax ).append( '\n' );
344 
345             if ( oidLen != -1 )
346             {
347                 sb.append( "m-length: " ).append( oidLen ).append( '\n' );
348             }
349         }
350 
351         // The single value flag
352         if ( singleValue )
353         {
354             sb.append( "m-singleValue: TRUE\n" );
355         }
356 
357         // The collective flag
358         if ( collective )
359         {
360             sb.append( "m-collective: TRUE\n" );
361         }
362 
363         // The not user modifiable flag
364         if ( noUserModification )
365         {
366             sb.append( "m-noUserModification: TRUE\n" );
367         }
368 
369         // The usage value
370         if ( usage != UsageEnum.USER_APPLICATIONS )
371         {
372             sb.append( "m-usage: " ).append( usage.toString() ).append( '\n' );
373         }
374 
375         // The extensions
376         if ( extensions.size() != 0 )
377         {
378             extensionsToLdif( "m-extensionAttributeType" );
379         }
380 
381         return sb.toString();
382 
383     }
384 
385 
386     /**
387      * @return a String representing this AttributeType.
388      */
389     @Override
390     public String toString()
391     {
392         return getOid();
393     }
394 
395 
396     /**
397      * Transform a schema name to a Dn pointing to the correct position in the DIT
398      * 
399      * @param schemaName The schema name
400      * @return the Dn associated with this schema in the DIT
401      */
402     @Override
403     public String dnToLdif( String schemaName ) throws LdapException
404     {
405         StringBuilder sb = new StringBuilder();
406 
407         String dn = "m-oid=" + oid + ", " + SchemaConstants.ATTRIBUTE_TYPES_PATH + ", cn="
408             + Rdn.escapeValue( schemaName ) + ", ou=schema";
409 
410         // First dump the Dn only
411         Entry entry = new DefaultEntry( dn );
412         sb.append( LdifUtils.convertToLdif( entry ) );
413 
414         return sb.toString();
415     }
416 }