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