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 java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27  import org.apache.directory.api.ldap.model.entry.DefaultEntry;
28  import org.apache.directory.api.ldap.model.entry.Entry;
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.apache.directory.api.ldap.model.ldif.LdifUtils;
31  import org.apache.directory.api.ldap.model.name.Rdn;
32  import org.apache.directory.api.ldap.model.schema.ObjectClassTypeEnum;
33  
34  
35  /**
36   * A bean used to encapsulate the literal String values of an ObjectClass
37   * definition found within an OpenLDAP schema configuration file.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class ObjectClassHolder extends SchemaElementImpl
42  {
43      /** The list of superiors */
44      private List<String> superiors = new ArrayList<String>();
45  
46      /** The list of mandatory attributes */
47      private List<String> must = new ArrayList<String>();
48  
49      /** The list of optional attributes */
50      private List<String> may = new ArrayList<String>();
51  
52      /** The ObjectClass type */
53      private ObjectClassTypeEnum classType = ObjectClassTypeEnum.STRUCTURAL;
54  
55  
56      /**
57       * Create an instance of ObjectClass element
58       * 
59       * @param OID the OjectClass OID
60       */
61      public ObjectClassHolder( String oid )
62      {
63          this.oid = oid;
64      }
65  
66  
67      /**
68       * Get the list of superior for this objectClass
69       * @return A list of all inherited objectClasses 
70       */
71      public List<String> getSuperiors()
72      {
73          return superiors;
74      }
75  
76  
77      /**
78       * Set the list of inherited objectClasses
79       * @param superiors The list of inherited objectClasses
80       */
81      public void setSuperiors( List<String> superiors )
82      {
83          this.superiors = superiors;
84      }
85  
86  
87      /**
88       * @return The list of mandatory attributes
89       */
90      public List<String> getMust()
91      {
92          return must;
93      }
94  
95  
96      /**
97       * Set the list of mandatory attributes
98       * @param must The list of mandatory attributes
99       */
100     public void setMust( List<String> must )
101     {
102         this.must = must;
103     }
104 
105 
106     /**
107      * @return The list of optional attributes
108      */
109     public List<String> getMay()
110     {
111         return may;
112     }
113 
114 
115     /**
116      * Set the list of optional attributes
117      * @param may The list of optional attributes
118      */
119     public void setMay( List<String> may )
120     {
121         this.may = may;
122     }
123 
124 
125     /**
126      * @return The objectClass type
127      */
128     public ObjectClassTypeEnum getClassType()
129     {
130         return classType;
131     }
132 
133 
134     /**
135      * Set the objectClass type. 
136      * @param classType The objectClass type. 
137      */
138     public void setClassType( ObjectClassTypeEnum classType )
139     {
140         this.classType = classType;
141     }
142 
143 
144     /**
145      * Convert this objectClass to a Ldif string
146      * 
147      * @param schemaName The name of the schema file containing this objectClass
148      * @return A ldif formatted string
149      * @throws org.apache.directory.api.ldap.model.exception.LdapException If something went wrong
150      */
151     public String toLdif( String schemaName ) throws LdapException
152     {
153         StringBuilder sb = new StringBuilder();
154 
155         sb.append( schemaToLdif( schemaName, "metaObjectClass" ) );
156 
157         // The superiors
158         if ( superiors.size() != 0 )
159         {
160             for ( String superior : superiors )
161             {
162                 sb.append( "m-supObjectClass: " ).append( superior ).append( '\n' );
163             }
164         }
165 
166         // The kind of class
167         if ( classType != ObjectClassTypeEnum.STRUCTURAL )
168         {
169             sb.append( "m-typeObjectClass: " ).append( classType ).append( '\n' );
170         }
171 
172         // The 'must'
173         if ( must.size() != 0 )
174         {
175             for ( String attr : must )
176             {
177                 sb.append( "m-must: " ).append( attr ).append( '\n' );
178             }
179         }
180 
181         // The 'may'
182         if ( may.size() != 0 )
183         {
184             for ( String attr : may )
185             {
186                 sb.append( "m-may: " ).append( attr ).append( '\n' );
187             }
188         }
189 
190         // The extensions
191         if ( extensions.size() != 0 )
192         {
193             extensionsToLdif( "m-extensionObjectClass" );
194         }
195 
196         return sb.toString();
197     }
198 
199 
200     /**
201      * @return a String representing this ObjectClass.
202      */
203     public String toString()
204     {
205         return getOid();
206     }
207 
208 
209     /**
210      * Transform a schema name to a Dn pointing to the correct position in the DIT
211      * 
212      * @param schemaName The schema name
213      * @return the Dn associated with this schema in the DIT
214      */
215     public String dnToLdif( String schemaName ) throws LdapException
216     {
217         StringBuilder sb = new StringBuilder();
218 
219         String dn = "m-oid=" + oid + ", " + SchemaConstants.OBJECT_CLASSES_PATH + ", cn="
220             + Rdn.escapeValue( schemaName ) + ", ou=schema";
221 
222         // First dump the Dn only
223         Entry entry = new DefaultEntry( dn );
224         sb.append( LdifUtils.convertToLdif( entry ) );
225 
226         return sb.toString();
227     }
228 }