1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41
42
43
44 public abstract class SchemaElementImpl implements SchemaElement
45 {
46
47 protected String oid;
48
49
50 protected String description;
51
52
53 protected List<String> names = new ArrayList<>();
54
55
56 protected boolean obsolete = false;
57
58
59 protected Map<String, List<String>> extensions = new HashMap<>();
60
61
62
63
64
65 @Override
66 public boolean isObsolete()
67 {
68 return obsolete;
69 }
70
71
72
73
74
75 @Override
76 public void setObsolete( boolean obsolete )
77 {
78 this.obsolete = obsolete;
79 }
80
81
82
83
84
85 @Override
86 public String getOid()
87 {
88 return oid;
89 }
90
91
92
93
94
95 @Override
96 public String getDescription()
97 {
98 return description;
99 }
100
101
102
103
104
105 @Override
106 public void setDescription( String description )
107 {
108 this.description = description;
109 }
110
111
112
113
114
115 @Override
116 public List<String> getNames()
117 {
118 return names;
119 }
120
121
122
123
124
125 @Override
126 public void setNames( List<String> names )
127 {
128 this.names = names;
129 }
130
131
132
133
134
135 @Override
136 public List<String> getExtension( String key )
137 {
138 return extensions.get( key );
139 }
140
141
142
143
144
145 @Override
146 public Map<String, List<String>> getExtensions()
147 {
148 return extensions;
149 }
150
151
152
153
154
155 @Override
156 public void setExtensions( Map<String, List<String>> extensions )
157 {
158 this.extensions = extensions;
159 }
160
161
162
163
164
165 private String oidToLdif()
166 {
167 return "m-oid: " + oid + '\n';
168 }
169
170
171
172
173
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
200
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
222
223
224
225
226
227 public abstract String dnToLdif( String schemaName ) throws LdapException;
228
229
230
231
232
233
234
235
236
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
258
259
260
261
262
263
264 protected String schemaToLdif( String schemaName, String type ) throws LdapException
265 {
266 StringBuilder sb = new StringBuilder();
267
268
269 sb.append( dnToLdif( schemaName ) );
270
271
272 sb.append( "objectclass: " ).append( type ).append( '\n' );
273 sb.append( "objectclass: metaTop\n" );
274 sb.append( "objectclass: top\n" );
275
276
277 sb.append( oidToLdif() );
278
279
280 sb.append( nameToLdif() );
281
282
283 sb.append( descToLdif() );
284
285
286 if ( obsolete )
287 {
288 sb.append( "m-obsolete: TRUE\n" );
289 }
290
291 return sb.toString();
292 }
293 }