KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > valueeditors > AbstractDialogBinaryValueEditor


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.valueeditors;
22
23
24 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
25 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
26 import org.apache.directory.ldapstudio.browser.core.jobs.CreateValuesJob;
27 import org.apache.directory.ldapstudio.browser.core.jobs.DeleteAttributesValueJob;
28 import org.apache.directory.ldapstudio.browser.core.jobs.ModifyValueJob;
29 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
30 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.IValue;
34 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
35 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
36 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
37
38
39 /**
40  *
41  * Abstract base class for value editors that handle binary values
42  * in a dialog.
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public abstract class AbstractDialogBinaryValueEditor extends AbstractDialogValueEditor
48 {
49
50     /**
51      * Creates a new instance of AbstractDialogBinaryValueEditor.
52      */

53     protected AbstractDialogBinaryValueEditor()
54     {
55     }
56
57
58     /**
59      * {@inheritDoc}
60      *
61      * This implementation of getDisplayValue just returns a note,
62      * that the value is binary and the size of the data.
63      */

64     public String JavaDoc getDisplayValue( IValue value )
65     {
66         if ( showRawValues() )
67         {
68             return getPrintableString( value );
69         }
70         else
71         {
72             if ( value == null )
73             {
74                 return "NULL";
75             }
76             else if ( value.isBinary() )
77             {
78                 byte[] data = value.getBinaryValue();
79                 return "Binary Data (" + data.length + " Bytes)";
80             }
81             else
82             {
83                 return "Invalid Data";
84             }
85         }
86     }
87
88
89     /**
90      * Helper method, returns a printable string if the value
91      * is binary.
92      *
93      * @param value the value
94      *
95      * @return the printable string
96      */

97     public static String JavaDoc getPrintableString( IValue value )
98     {
99         if ( value == null )
100         {
101             return "NULL";
102         }
103         else if ( value.isBinary() )
104         {
105             byte[] data = value.getBinaryValue();
106             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
107             for ( int i = 0; data != null && i < data.length && i < 128; i++ )
108             {
109                 if ( data[i] > 32 && data[i] < 127 )
110                     sb.append( ( char ) data[i] );
111                 else
112                     sb.append( '.' );
113             }
114             return sb.toString();
115         }
116         else if ( value.isString() )
117         {
118             return value.getStringValue();
119         }
120         else
121         {
122             return "NULL";
123         }
124     }
125
126
127     /**
128      * {@inheritDoc}
129      *
130      * This implementation returns IValue.EMPTY_BINARY_VALUE if
131      * the attribute is binary.
132      */

133     protected Object JavaDoc getEmptyRawValue( IAttribute attribute )
134     {
135         if ( attribute.isBinary() )
136         {
137             return IValue.EMPTY_BINARY_VALUE;
138         }
139         else
140         {
141             return null;
142         }
143     }
144
145
146     /**
147      * {@inheritDoc}
148      *
149      * This implementation returns the binary (byte[]) value
150      * of the given value.
151      */

152     public Object JavaDoc getRawValue( IValue value )
153     {
154         if ( value == null )
155         {
156             return null;
157         }
158         else if ( value.isString() )
159         {
160             return value.getBinaryValue();
161         }
162         else if ( value.isBinary() )
163         {
164             return value.getBinaryValue();
165         }
166         else
167         {
168             return null;
169         }
170     }
171
172
173     /**
174      * {@inheritDoc}
175      *
176      * This implementation returns the value itself if it is
177      * of type byte[] or a byte[] with the UTF-8 encoded string
178      * value if it is of type String.
179      */

180     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
181     {
182         if ( value == null )
183         {
184             return null;
185         }
186         else if ( value instanceof String JavaDoc )
187         {
188             return LdifUtils.utf8encode( ( String JavaDoc ) value );
189         }
190         else if ( value instanceof byte[] )
191         {
192             return value;
193         }
194         else
195         {
196             return null;
197         }
198     }
199
200
201     /**
202      * {@inheritDoc}
203      *
204      * This implementation always return the binary value
205      * as byte[].
206      */

207     public Object JavaDoc getStringOrBinaryValue( Object JavaDoc rawValue )
208     {
209         if ( rawValue == null )
210         {
211             return null;
212         }
213         else if ( rawValue instanceof byte[] )
214         {
215             return rawValue;
216         }
217         else
218         {
219             return null;
220         }
221     }
222
223
224     /**
225      * {@inheritDoc}
226      */

227     public final void createValue( IEntry entry, String JavaDoc attributeDescription, Object JavaDoc newRawValue )
228         throws ModelModificationException
229     {
230         if ( entry != null && attributeDescription != null && newRawValue != null && newRawValue instanceof byte[] )
231         {
232             if ( entry.getAttribute( attributeDescription ) != null )
233             {
234                 this.modify( entry.getAttribute( attributeDescription ), newRawValue );
235             }
236             else
237             {
238                 EventRegistry.suspendEventFireingInCurrentThread();
239                 IAttribute attribute = new Attribute( entry, attributeDescription );
240                 entry.addAttribute( attribute );
241                 EventRegistry.resumeEventFireingInCurrentThread();
242
243                 Object JavaDoc newValue;
244                 if ( entry.getConnection().getSchema().getAttributeTypeDescription( attributeDescription )
245                     .getSyntaxDescription().isString() )
246                 {
247                     newValue = LdifUtils.utf8decode( ( byte[] ) newRawValue );
248                 }
249                 else
250                 {
251                     newValue = ( byte[] ) newRawValue;
252                 }
253
254                 new CreateValuesJob( attribute, newValue ).execute();
255             }
256         }
257     }
258
259
260     private final void modify( IAttribute attribute, Object JavaDoc newRawValue ) throws ModelModificationException
261     {
262         if ( attribute != null && newRawValue != null && newRawValue instanceof byte[] )
263         {
264             if ( attribute.getValueSize() == 0 )
265             {
266                 byte[] newValue = ( byte[] ) newRawValue;
267                 new CreateValuesJob( attribute, newValue ).execute();
268             }
269             else if ( attribute.getValueSize() == 1 )
270             {
271                 this.modifyValue( attribute.getValues()[0], newRawValue );
272             }
273         }
274     }
275
276
277     /**
278      * {@inheritDoc}
279      */

280     public final void modifyValue( IValue oldValue, Object JavaDoc newRawValue ) throws ModelModificationException
281     {
282         if ( oldValue != null && newRawValue != null && newRawValue instanceof byte[] )
283         {
284             byte[] newValue = ( byte[] ) newRawValue;
285             IAttribute attribute = oldValue.getAttribute();
286             if ( !Utils.equals( oldValue.getBinaryValue(), newValue ) )
287             {
288                 if ( oldValue.isEmpty() )
289                 {
290                     EventRegistry.suspendEventFireingInCurrentThread();
291                     attribute.deleteEmptyValue();
292                     EventRegistry.resumeEventFireingInCurrentThread();
293                     new CreateValuesJob( attribute, newValue ).execute();
294                 }
295                 else
296                 {
297                     new ModifyValueJob( attribute, oldValue, newValue ).execute();
298                 }
299             }
300         }
301     }
302
303
304     /**
305      * {@inheritDoc}
306      */

307     public final void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException
308     {
309         if ( ah != null )
310         {
311             new DeleteAttributesValueJob( ah ).execute();
312         }
313     }
314
315
316     /**
317      * {@inheritDoc}
318      */

319     public final void deleteValue( IValue oldValue ) throws ModelModificationException
320     {
321         if ( oldValue != null )
322         {
323             new DeleteAttributesValueJob( oldValue ).execute();
324         }
325     }
326
327 }
328
Popular Tags