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     */
020    package org.apache.directory.shared.converter.schema;
021    
022    
023    import org.apache.directory.shared.ldap.constants.SchemaConstants;
024    import org.apache.directory.shared.ldap.entry.Entry;
025    import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
026    import org.apache.directory.shared.ldap.exception.LdapException;
027    import org.apache.directory.shared.ldap.ldif.LdifUtils;
028    import org.apache.directory.shared.ldap.name.DN;
029    import org.apache.directory.shared.ldap.name.RDN;
030    import org.apache.directory.shared.ldap.schema.UsageEnum;
031    
032    
033    /**
034     * A bean used to hold the literal values of an AttributeType parsed out of an
035     * OpenLDAP schema configuration file.
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 476875 $
039     */
040    public class AttributeTypeHolder extends SchemaElementImpl
041    {
042        /** A flag for single valued attributes. Default to false */
043        private boolean singleValue = false;
044    
045        /** A flag for collective attribute. Default to false */
046        private boolean collective = false;
047    
048        /** A flaf for immutable attribue. Default to false */
049        private boolean noUserModification = false;
050    
051        /** The optional superior */
052        private String superior;
053    
054        /** The equality matching rule */
055        private String equality;
056    
057        /** The ordering matching rule */
058        private String ordering;
059    
060        /** The substring matching rule */
061        private String substr;
062    
063        /** The syntax this attribute respects */
064        private String syntax;
065    
066        /** The optional length for this attribute */
067        private int oidLen = -1;
068    
069        /** The attribute uase. Default to userApplication */
070        private UsageEnum usage = UsageEnum.USER_APPLICATIONS;
071    
072    
073        // ------------------------------------------------------------------------
074        // C O N S T R U C T O R S
075        // ------------------------------------------------------------------------
076    
077        /**
078         * Create an instance of an attributeType
079         * 
080         * @param oid
081         *            The attributeType's OID
082         */
083        public AttributeTypeHolder( String oid )
084        {
085            this.oid = oid;
086        }
087    
088    
089        // ------------------------------------------------------------------------
090        // Accessors and mutators
091        // ------------------------------------------------------------------------
092    
093        /**
094         * Tells if the attribute is single-valued
095         * 
096         * @return true if the attribute is single-valued, false otherwise
097         */
098        public boolean isSingleValue()
099        {
100            return singleValue;
101        }
102    
103    
104        /**
105         * Set the attributeType singleValue flag
106         * 
107         * @param singleValue
108         *            The value for this flag
109         */
110        public void setSingleValue( boolean singleValue )
111        {
112            this.singleValue = singleValue;
113        }
114    
115    
116        /**
117         * Tells if the attributeType is collectove or not
118         * 
119         * @return True if the attributeType is collective, false otherwise
120         */
121        public boolean isCollective()
122        {
123            return collective;
124        }
125    
126    
127        /**
128         * Set the attributeType collective flag
129         * 
130         * @param collective
131         *            The value for this flag
132         */
133        public void setCollective( boolean collective )
134        {
135            this.collective = collective;
136        }
137    
138    
139        /**
140         * Tellse if the attributeType is mutable or not
141         * 
142         * @return True if the attributeType is immutable, false otherwise
143         */
144        public boolean isNoUserModification()
145        {
146            return noUserModification;
147        }
148    
149    
150        /**
151         * Set the attributeType noUserModification flag
152         * 
153         * @param noUserModification
154         *            The value for this flag
155         */
156        public void setNoUserModification( boolean noUserModification )
157        {
158            this.noUserModification = noUserModification;
159        }
160    
161    
162        /**
163         * Get the optional attributeType's superior
164         * 
165         * @return The attributeType's superior, if any
166         */
167        public String getSuperior()
168        {
169            return superior;
170        }
171    
172    
173        /**
174         * Set the attributeType's superior
175         * 
176         * @param superior
177         *            The attributeType's superior
178         */
179        public void setSuperior( String superior )
180        {
181            this.superior = superior;
182        }
183    
184    
185        /**
186         * Get the equality Matching Rule
187         * 
188         * @return The equality matchingRule
189         */
190        public String getEquality()
191        {
192            return equality;
193        }
194    
195    
196        /**
197         * Set the equality Matching Rule
198         * 
199         * @param equality
200         *            The equality Matching Rule
201         */
202        public void setEquality( String equality )
203        {
204            this.equality = equality;
205        }
206    
207    
208        /**
209         * Get the ordering Matching Rule
210         * 
211         * @return The ordering matchingRule
212         */
213        public String getOrdering()
214        {
215            return ordering;
216        }
217    
218    
219        /**
220         * Set the ordering Matching Rule
221         * 
222         * @param ordering
223         *            The ordering Matching Rule
224         */
225        public void setOrdering( String ordering )
226        {
227            this.ordering = ordering;
228        }
229    
230    
231        /**
232         * Get the substring Matching Rule
233         * 
234         * @return The substring matchingRule
235         */
236        public String getSubstr()
237        {
238            return substr;
239        }
240    
241    
242        /**
243         * Set the substring Matching Rule
244         * 
245         * @param substr
246         *            The substring Matching Rule
247         */
248        public void setSubstr( String substr )
249        {
250            this.substr = substr;
251        }
252    
253    
254        /**
255         * Get the attributeType's syntax
256         * 
257         * @return The attributeType's syntax
258         */
259        public String getSyntax()
260        {
261            return syntax;
262        }
263    
264    
265        /**
266         * Set the attributeType's syntax
267         * 
268         * @param syntax
269         *            The attributeType's syntax
270         */
271        public void setSyntax( String syntax )
272        {
273            this.syntax = syntax;
274        }
275    
276    
277        /**
278         * Get the attributeType's usage
279         * 
280         * @return The attributeType's usage
281         */
282        public UsageEnum getUsage()
283        {
284            return usage;
285        }
286    
287    
288        /**
289         * Set the attributeType's usage
290         * 
291         * @param usage
292         *            The attributeType's usage
293         */
294        public void setUsage( UsageEnum usage )
295        {
296            this.usage = usage;
297        }
298    
299    
300        /**
301         * Get the attributeType's syntax length
302         * 
303         * @return The attributeType's syntax length
304         */
305        public int getOidLen()
306        {
307            return oidLen;
308        }
309    
310    
311        /**
312         * Set the attributeType's syntax length
313         * 
314         * @param oidLen
315         *            The attributeType's syntax length
316         */
317        public void setOidLen( int oidLen )
318        {
319            this.oidLen = oidLen;
320        }
321    
322    
323        /**
324         * Convert this attributeType to a Ldif string
325         * 
326         * @param schemaName
327         *            The name of the schema file containing this attributeType
328         * @return A ldif formatted string
329         */
330        public String toLdif( String schemaName ) throws LdapException
331        {
332            StringBuilder sb = new StringBuilder();
333    
334            sb.append( schemaToLdif( schemaName, "metaAttributeType" ) );
335    
336            // The superior
337            if ( superior != null )
338            {
339                sb.append( "m-supAttributeType: " ).append( superior ).append( '\n' );
340            }
341    
342            // The equality matching rule
343            if ( equality != null )
344            {
345                sb.append( "m-equality: " ).append( equality ).append( '\n' );
346            }
347    
348            // The ordering matching rule
349            if ( ordering != null )
350            {
351                sb.append( "m-ordering: " ).append( ordering ).append( '\n' );
352            }
353    
354            // The substrings matching rule
355            if ( substr != null )
356            {
357                sb.append( "m-substr: " ).append( substr ).append( '\n' );
358            }
359    
360            // The value syntax
361            if ( syntax != null )
362            {
363                sb.append( "m-syntax: " ).append( syntax ).append(  '\n'  );
364    
365                if ( oidLen != -1 )
366                {
367                    sb.append( "m-length: " ).append( oidLen ).append( '\n' );
368                }
369            }
370    
371            // The single value flag
372            if ( singleValue )
373            {
374                sb.append( "m-singleValue: TRUE\n" );
375            }
376    
377            // The collective flag
378            if ( collective )
379            {
380                sb.append( "m-collective: TRUE\n" );
381            }
382    
383            // The not user modifiable flag
384            if ( noUserModification )
385            {
386                sb.append( "m-noUserModification: TRUE\n" );
387            }
388    
389            // The usage value
390            if ( usage != UsageEnum.USER_APPLICATIONS )
391            {
392                sb.append( "m-usage: " ).append( usage.render() ).append( '\n' );
393            }
394    
395            // The extensions
396            if ( extensions.size() != 0 )
397            {
398                extensionsToLdif( "m-extensionAttributeType" );
399            }
400    
401            return sb.toString();
402    
403        }
404    
405    
406        /**
407         * Return a String representing this AttributeType.
408         */
409        public String toString()
410        {
411            return getOid();
412        }
413    
414    
415        /* (non-Javadoc)
416         * @see org.apache.directory.shared.converter.schema.SchemaElementImpl#dnToLdif(java.lang.String)
417         */
418        public String dnToLdif( String schemaName ) throws LdapException
419        {
420            StringBuilder sb = new StringBuilder();
421    
422            String dn = "m-oid=" + oid + ", " + SchemaConstants.ATTRIBUTES_TYPE_PATH + ", cn=" + RDN.escapeValue( schemaName ) + ", ou=schema";
423    
424            // First dump the DN only
425            Entry entry = new DefaultClientEntry( new DN( dn ) );
426            sb.append( LdifUtils.convertEntryToLdif( entry ) );
427    
428            return sb.toString();
429        }
430    }