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.shared.converter.schema;
021
022
023import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
024import org.apache.directory.shared.ldap.model.entry.DefaultEntry;
025import org.apache.directory.shared.ldap.model.entry.Entry;
026import org.apache.directory.shared.ldap.model.exception.LdapException;
027import org.apache.directory.shared.ldap.model.ldif.LdifUtils;
028import org.apache.directory.shared.ldap.model.name.Rdn;
029import org.apache.directory.shared.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 int 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 int 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( int 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.shared.ldap.model.exception.LdapException If something went wrong
308     */
309    public String toLdif( String schemaName ) throws LdapException
310    {
311        StringBuilder sb = new StringBuilder();
312
313        sb.append( schemaToLdif( schemaName, "metaAttributeType" ) );
314
315        // The superior
316        if ( superior != null )
317        {
318            sb.append( "m-supAttributeType: " ).append( superior ).append( '\n' );
319        }
320
321        // The equality matching rule
322        if ( equality != null )
323        {
324            sb.append( "m-equality: " ).append( equality ).append( '\n' );
325        }
326
327        // The ordering matching rule
328        if ( ordering != null )
329        {
330            sb.append( "m-ordering: " ).append( ordering ).append( '\n' );
331        }
332
333        // The substrings matching rule
334        if ( substr != null )
335        {
336            sb.append( "m-substr: " ).append( substr ).append( '\n' );
337        }
338
339        // The value syntax
340        if ( syntax != null )
341        {
342            sb.append( "m-syntax: " ).append( syntax ).append( '\n' );
343
344            if ( oidLen != -1 )
345            {
346                sb.append( "m-length: " ).append( oidLen ).append( '\n' );
347            }
348        }
349
350        // The single value flag
351        if ( singleValue )
352        {
353            sb.append( "m-singleValue: TRUE\n" );
354        }
355
356        // The collective flag
357        if ( collective )
358        {
359            sb.append( "m-collective: TRUE\n" );
360        }
361
362        // The not user modifiable flag
363        if ( noUserModification )
364        {
365            sb.append( "m-noUserModification: TRUE\n" );
366        }
367
368        // The usage value
369        if ( usage != UsageEnum.USER_APPLICATIONS )
370        {
371            sb.append( "m-usage: " ).append( usage.render() ).append( '\n' );
372        }
373
374        // The extensions
375        if ( extensions.size() != 0 )
376        {
377            extensionsToLdif( "m-extensionAttributeType" );
378        }
379
380        return sb.toString();
381
382    }
383
384
385    /**
386     * @return a String representing this AttributeType.
387     */
388    public String toString()
389    {
390        return getOid();
391    }
392
393
394    /**
395     * Transform a schema name to a Dn pointing to the correct position in the DIT
396     * 
397     * @param schemaName The schema name
398     * @return the Dn associated with this schema in the DIT
399     */
400    public String dnToLdif( String schemaName ) throws LdapException
401    {
402        StringBuilder sb = new StringBuilder();
403
404        String dn = "m-oid=" + oid + ", " + SchemaConstants.ATTRIBUTE_TYPES_PATH + ", cn="
405            + Rdn.escapeValue(schemaName) + ", ou=schema";
406
407        // First dump the Dn only
408        Entry entry = new DefaultEntry( dn );
409        sb.append( LdifUtils.convertToLdif(entry) );
410
411        return sb.toString();
412    }
413}