KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > utils > ModelConverter


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
21 package org.apache.directory.ldapstudio.browser.core.utils;
22
23
24 /**
25  * Utilities to convert between models
26  */

27 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
28 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
29 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry;
30 import org.apache.directory.ldapstudio.browser.core.internal.model.Value;
31 import org.apache.directory.ldapstudio.browser.core.model.DN;
32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
33 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
34 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
35 import org.apache.directory.ldapstudio.browser.core.model.IValue;
36 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
37 import org.apache.directory.ldapstudio.browser.core.model.NameException;
38 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
39 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
40 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeRecord;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
44 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifChangeTypeLine;
45 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifCommentLine;
46 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifControlLine;
47 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine;
48 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine;
49
50
51 public class ModelConverter
52 {
53
54
55     /**
56      * Converts the given {@link LdifContentRecord} to an {@link DummyEntry}.
57      *
58      * @param ldifContentRecord the ldif content record to convert
59      * @param connection the connection
60      *
61      * @return the resulting dummy entry
62      *
63      * @throws ModelModificationException the model modification exception
64      * @throws NameException the name exception
65      */

66     public static DummyEntry ldifContentRecordToEntry( LdifContentRecord ldifContentRecord, IConnection connection )
67         throws NameException, ModelModificationException
68     {
69         return createIntern( ldifContentRecord, connection );
70     }
71
72
73     /**
74      * Converts the given {@link LdifChangeAddRecord} to an {@link DummyEntry}.
75      *
76      * @param ldifChangeAddRecord the ldif change add record to convert
77      * @param connection the connection
78      *
79      * @return the resulting dummy entry
80      *
81      * @throws ModelModificationException the model modification exception
82      * @throws NameException the name exception
83      */

84     public static DummyEntry ldifChangeAddRecordToEntry( LdifChangeAddRecord ldifChangeAddRecord, IConnection connection )
85         throws NameException, ModelModificationException
86     {
87         return createIntern( ldifChangeAddRecord, connection );
88     }
89
90
91     /**
92      * Creates an {@link DummyEntry} from the given {@link LdifRecord}.
93      *
94      * @param connection the connection
95      * @param ldifRecord the ldif record
96      *
97      * @return the dummy entry
98      *
99      * @throws ModelModificationException the model modification exception
100      * @throws NameException the name exception
101      */

102     private static DummyEntry createIntern( LdifRecord ldifRecord, IConnection connection ) throws NameException,
103         ModelModificationException
104     {
105         LdifPart[] parts = ldifRecord.getParts();
106
107         EventRegistry.suspendEventFireingInCurrentThread();
108
109         DummyEntry entry = new DummyEntry( new DN( ldifRecord.getDnLine().getValueAsString() ), connection );
110
111         for ( int i = 0; i < parts.length; i++ )
112         {
113             if ( parts[i] instanceof LdifAttrValLine )
114             {
115                 LdifAttrValLine line = ( LdifAttrValLine ) parts[i];
116                 String JavaDoc attributeName = line.getUnfoldedAttributeDescription();
117                 Object JavaDoc value = line.getValueAsObject();
118                 IAttribute attribute = entry.getAttribute( attributeName );
119                 if ( attribute == null )
120                 {
121                     attribute = new Attribute( entry, attributeName );
122                     entry.addAttribute( attribute );
123                 }
124                 attribute.addValue( new Value( attribute, value ) );
125             }
126             else if ( !( parts[i] instanceof LdifDnLine ) && !( parts[i] instanceof LdifSepLine ) )
127             {
128                 String JavaDoc name = parts[i].toRawString();
129                 name = name.replaceAll( "\n", "" );
130                 name = name.replaceAll( "\r", "" );
131                 IAttribute attribute = new Attribute( entry, name );
132                 attribute.addValue( new Value( attribute, parts[i] ) );
133                 entry.addAttribute( attribute );
134                 // IAttribute attribute = entry.getAttribute("");
135
// if(attribute == null) {
136
// attribute = new Attribute(entry, "");
137
// entry.addAttribute(attribute, null);
138
// }
139
// attribute.addValue(new Value(attribute, parts[i]), null);
140
}
141         }
142
143         EventRegistry.resumeEventFireingInCurrentThread();
144
145         return entry;
146     }
147
148
149     public static LdifChangeAddRecord entryToLdifChangeAddRecord( IEntry entry )
150     {
151
152         boolean mustCreateChangeTypeLine = true;
153         IAttribute[] attributes = entry.getAttributes();
154         for ( int i = 0; i < attributes.length; i++ )
155         {
156             IValue[] values = attributes[i].getValues();
157             for ( int ii = 0; ii < values.length; ii++ )
158             {
159                 IValue value = values[ii];
160                 if ( value.getRawValue() instanceof LdifPart )
161                 {
162                     mustCreateChangeTypeLine = false;
163                 }
164             }
165         }
166
167         // LdifChangeAddRecord record =
168
// LdifChangeAddRecord.create(entry.getDn().toString());
169
LdifChangeAddRecord record = new LdifChangeAddRecord( LdifDnLine.create( entry.getDn().toString() ) );
170         if ( mustCreateChangeTypeLine )
171         {
172             addControls( record, entry );
173             record.setChangeType( LdifChangeTypeLine.createAdd() );
174         }
175
176         for ( int i = 0; i < attributes.length; i++ )
177         {
178             String JavaDoc name = attributes[i].getDescription();
179             IValue[] values = attributes[i].getValues();
180             for ( int ii = 0; ii < values.length; ii++ )
181             {
182                 IValue value = values[ii];
183                 if ( value.getRawValue() instanceof LdifPart )
184                 {
185                     LdifPart part = ( LdifPart ) value.getRawValue();
186                     if ( part instanceof LdifChangeTypeLine )
187                     {
188                         record.setChangeType( ( LdifChangeTypeLine ) part );
189                     }
190                     else if ( part instanceof LdifCommentLine )
191                     {
192                         record.addComment( ( LdifCommentLine ) part );
193                     }
194                     else if ( part instanceof LdifControlLine )
195                     {
196                         record.addControl( ( LdifControlLine ) part );
197                     }
198                 }
199                 else if ( value.isString() )
200                 {
201                     record.addAttrVal( LdifAttrValLine.create( name, value.getStringValue() ) );
202                 }
203                 else
204                 {
205                     record.addAttrVal( LdifAttrValLine.create( name, value.getBinaryValue() ) );
206                 }
207             }
208         }
209
210         record.finish( LdifSepLine.create() );
211
212         return record;
213     }
214
215
216     public static LdifContentRecord entryToLdifContentRecord( IEntry entry )
217     {
218
219         LdifContentRecord record = LdifContentRecord.create( entry.getDn().toString() );
220
221         IAttribute[] attributes = entry.getAttributes();
222         for ( int i = 0; i < attributes.length; i++ )
223         {
224             String JavaDoc name = attributes[i].getDescription();
225             IValue[] values = attributes[i].getValues();
226             for ( int ii = 0; ii < values.length; ii++ )
227             {
228                 IValue value = values[ii];
229                 if ( value.getRawValue() instanceof LdifPart )
230                 {
231                     LdifPart part = ( LdifPart ) value.getRawValue();
232                     if ( part instanceof LdifCommentLine )
233                     {
234                         record.addComment( ( LdifCommentLine ) part );
235                     }
236                 }
237                 else if ( value.isString() )
238                 {
239                     record.addAttrVal( LdifAttrValLine.create( name, value.getStringValue() ) );
240                 }
241                 else
242                 {
243                     record.addAttrVal( LdifAttrValLine.create( name, value.getBinaryValue() ) );
244                 }
245             }
246         }
247
248         record.finish( LdifSepLine.create() );
249
250         return record;
251     }
252
253
254     public static LdifAttrValLine valueToLdifAttrValLine( IValue value )
255     {
256
257         LdifAttrValLine line;
258         if ( value.isString() )
259         {
260             line = LdifAttrValLine.create( value.getAttribute().getDescription(), value.getStringValue() );
261         }
262         else
263         {
264             line = LdifAttrValLine.create( value.getAttribute().getDescription(), value.getBinaryValue() );
265         }
266         return line;
267     }
268
269
270     public static IValue ldifAttrValLineToValue( LdifAttrValLine line, IEntry entry )
271     {
272         try
273         {
274             IAttribute attribute = new Attribute( entry, line.getUnfoldedAttributeDescription() );
275             IValue value = new Value( attribute, line.getValueAsObject() );
276             return value;
277         }
278         catch ( Exception JavaDoc e )
279         {
280             return null;
281         }
282     }
283
284
285     public static LdifDnLine dnToLdifDnLine( DN dn )
286     {
287         LdifDnLine line = LdifDnLine.create( dn.toString() );
288         return line;
289     }
290
291
292     public static void addControls( LdifChangeRecord cr, IEntry entry )
293     {
294         if ( entry.isReferral() )
295         {
296             cr.addControl( LdifControlLine.create( IConnection.CONTROL_MANAGEDSAIT, null, ( String JavaDoc ) null ) );
297         }
298     }
299
300 }
301
Popular Tags