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.entry.Attribute;
24  import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
25  import org.apache.directory.api.ldap.model.entry.DefaultEntry;
26  import org.apache.directory.api.ldap.model.entry.Entry;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.ldif.LdifUtils;
29  import org.apache.directory.api.util.Strings;
30  
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  
37  /**
38   * An abstract SchemaElement implementation. It contains shared
39   * elements from AttributeType and ObjectClass, like obsolete, oid, 
40   * description, names and extensions (not implemented)
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public abstract class SchemaElementImpl implements SchemaElement
45  {
46      /** The schema element oid */
47      protected String oid;
48  
49      /** The schema element description */
50      protected String description;
51  
52      /** The list of names for this schemaElements */
53      protected List<String> names = new ArrayList<String>();
54  
55      /** The obsolete flag */
56      protected boolean obsolete = false;
57  
58      /** The optional list of extensions */
59      protected Map<String, List<String>> extensions = new HashMap<String, List<String>>();
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      public boolean isObsolete()
66      {
67          return obsolete;
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void setObsolete( boolean obsolete )
75      {
76          this.obsolete = obsolete;
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      public String getOid()
84      {
85          return oid;
86      }
87  
88  
89      /**
90       * {@inheritDoc}
91       */
92      public String getDescription()
93      {
94          return description;
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     public void setDescription( String description )
102     {
103         this.description = description;
104     }
105 
106 
107     /**
108      * @see SchemaElement#getNames()
109      */
110     public List<String> getNames()
111     {
112         return names;
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public void setNames( List<String> names )
120     {
121         this.names = names;
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public List<String> getExtension( String key )
129     {
130         return extensions.get( key );
131     }
132 
133 
134     /**
135      * {@inheritDoc}
136      */
137     public Map<String, List<String>> getExtensions()
138     {
139         return extensions;
140     }
141 
142 
143     /**
144      * {@inheritDoc}
145      */
146     public void setExtensions( Map<String, List<String>> extensions )
147     {
148         this.extensions = extensions;
149     }
150 
151 
152     /**
153      * @return The OID as a Ldif line
154      */
155     private String oidToLdif()
156     {
157         return "m-oid: " + oid + '\n';
158     }
159 
160 
161     /**
162      * @return the Names as Ldif lines
163      * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
164      */
165     private String nameToLdif() throws LdapException
166     {
167         if ( names.size() == 0 )
168         {
169             return "";
170         }
171         else
172         {
173             Entry entry = new DefaultEntry();
174             Attribute attribute = new DefaultAttribute( "m-name" );
175 
176             for ( String name : names )
177             {
178                 attribute.add( name );
179             }
180 
181             entry.put( attribute );
182 
183             return LdifUtils.convertAttributesToLdif( entry );
184         }
185     }
186 
187 
188     /**
189      * @return The description as a ldif line
190      * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
191      */
192     private String descToLdif() throws LdapException
193     {
194         if ( Strings.isEmpty( description ) )
195         {
196             return "";
197         }
198         else
199         {
200             Entry entry = new DefaultEntry();
201             Attribute attribute = new DefaultAttribute( "m-description", description );
202 
203             entry.put( attribute );
204 
205             return LdifUtils.convertAttributesToLdif( entry );
206         }
207     }
208 
209 
210     /**
211      * Transform a Schema Element to a LDIF String
212      *
213      * @param schemaName The schema element to transform
214      * @return The Schema Element as a ldif String
215      * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
216      */
217     public abstract String dnToLdif( String schemaName ) throws LdapException;
218 
219 
220     /**
221      * Return the extensions formated as Ldif lines
222      *
223      * @param id The attributeId : can be m-objectClassExtension or
224      * m-attributeTypeExtension
225      * @return The extensions formated as ldif lines
226      * @throws org.apache.directory.api.ldap.model.exception.LdapException If the conversion goes wrong
227      */
228     protected String extensionsToLdif( String id ) throws LdapException
229     {
230         StringBuilder sb = new StringBuilder();
231 
232         Entry entry = new DefaultEntry();
233         Attribute attribute = new DefaultAttribute( id );
234 
235         for ( String extension : extensions.keySet() )
236         {
237             attribute.add( extension );
238         }
239 
240         sb.append( LdifUtils.convertAttributesToLdif( entry ) );
241 
242         return sb.toString();
243     }
244 
245 
246     /**
247      * Transform a Schema to a LDIF formated String
248      *
249      * @param schemaName The schema to transform
250      * @param type The ObjectClass type
251      * @return A LDIF String representing the schema
252      * @throws org.apache.directory.api.ldap.model.exception.LdapException If the transformation can't be done
253      */
254     protected String schemaToLdif( String schemaName, String type ) throws LdapException
255     {
256         StringBuilder sb = new StringBuilder();
257 
258         // The Dn
259         sb.append( dnToLdif( schemaName ) );
260 
261         // ObjectClasses
262         sb.append( "objectclass: " ).append( type ).append( '\n' );
263         sb.append( "objectclass: metaTop\n" );
264         sb.append( "objectclass: top\n" );
265 
266         // The oid
267         sb.append( oidToLdif() );
268 
269         // The name
270         sb.append( nameToLdif() );
271 
272         // The desc
273         sb.append( descToLdif() );
274 
275         // The obsolete flag, only if "true"
276         if ( obsolete )
277         {
278             sb.append( "m-obsolete: TRUE\n" );
279         }
280 
281         return sb.toString();
282     }
283 }