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<String>();
54
55
56 protected boolean obsolete = false;
57
58
59 protected Map<String, List<String>> extensions = new HashMap<String, List<String>>();
60
61
62
63
64
65 public boolean isObsolete()
66 {
67 return obsolete;
68 }
69
70
71
72
73
74 public void setObsolete( boolean obsolete )
75 {
76 this.obsolete = obsolete;
77 }
78
79
80
81
82
83 public String getOid()
84 {
85 return oid;
86 }
87
88
89
90
91
92 public String getDescription()
93 {
94 return description;
95 }
96
97
98
99
100
101 public void setDescription( String description )
102 {
103 this.description = description;
104 }
105
106
107
108
109
110 public List<String> getNames()
111 {
112 return names;
113 }
114
115
116
117
118
119 public void setNames( List<String> names )
120 {
121 this.names = names;
122 }
123
124
125
126
127
128 public List<String> getExtension( String key )
129 {
130 return extensions.get( key );
131 }
132
133
134
135
136
137 public Map<String, List<String>> getExtensions()
138 {
139 return extensions;
140 }
141
142
143
144
145
146 public void setExtensions( Map<String, List<String>> extensions )
147 {
148 this.extensions = extensions;
149 }
150
151
152
153
154
155 private String oidToLdif()
156 {
157 return "m-oid: " + oid + '\n';
158 }
159
160
161
162
163
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
190
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
212
213
214
215
216
217 public abstract String dnToLdif( String schemaName ) throws LdapException;
218
219
220
221
222
223
224
225
226
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
248
249
250
251
252
253
254 protected String schemaToLdif( String schemaName, String type ) throws LdapException
255 {
256 StringBuilder sb = new StringBuilder();
257
258
259 sb.append( dnToLdif( schemaName ) );
260
261
262 sb.append( "objectclass: " ).append( type ).append( '\n' );
263 sb.append( "objectclass: metaTop\n" );
264 sb.append( "objectclass: top\n" );
265
266
267 sb.append( oidToLdif() );
268
269
270 sb.append( nameToLdif() );
271
272
273 sb.append( descToLdif() );
274
275
276 if ( obsolete )
277 {
278 sb.append( "m-obsolete: TRUE\n" );
279 }
280
281 return sb.toString();
282 }
283 }