001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.schema.converter;
021
022
023import org.apache.directory.api.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.entry.DefaultEntry;
025import org.apache.directory.api.ldap.model.entry.Entry;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.ldif.LdifUtils;
028import org.apache.directory.api.ldap.model.name.Rdn;
029import org.apache.directory.api.ldap.model.schema.UsageEnum;
030
031
032/**
033 * A bean used to hold the literal values of an AttributeType parsed out of an
034 * OpenLDAP schema configuration file.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class AttributeTypeHolder extends SchemaElementImpl
039{
040    /** A flag for single valued attributes. Default to false */
041    private boolean singleValue = false;
042
043    /** A flag for collective attribute. Default to false */
044    private boolean collective = false;
045
046    /** A flaf for immutable attribue. Default to false */
047    private boolean noUserModification = false;
048
049    /** The optional superior */
050    private String superior;
051
052    /** The equality matching rule */
053    private String equality;
054
055    /** The ordering matching rule */
056    private String ordering;
057
058    /** The substring matching rule */
059    private String substr;
060
061    /** The syntax this attribute respects */
062    private String syntax;
063
064    /** The optional length for this attribute */
065    private long oidLen = -1;
066
067    /** The attribute uase. Default to userApplication */
068    private UsageEnum usage = UsageEnum.USER_APPLICATIONS;
069
070
071    /**
072     * Create an instance of an attributeType
073     * 
074     * @param oid The attributeType's OID
075     */
076    public AttributeTypeHolder( String oid )
077    {
078        this.oid = oid;
079    }
080
081
082    /**
083     * Tells if the attribute is single-valued
084     * 
085     * @return true if the attribute is single-valued, false otherwise
086     */
087    public boolean isSingleValue()
088    {
089        return singleValue;
090    }
091
092
093    /**
094     * Set the attributeType singleValue flag
095     * 
096     * @param singleValue The value for this flag
097     */
098    public void setSingleValue( boolean singleValue )
099    {
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}