1 20 21 package org.apache.directory.ldapstudio.browser.common.dnd; 22 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.DataInputStream ; 27 import java.io.DataOutputStream ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 33 import org.apache.directory.ldapstudio.browser.core.model.DN; 34 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 35 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 36 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 37 import org.apache.directory.ldapstudio.browser.core.model.IValue; 38 import org.eclipse.swt.dnd.ByteArrayTransfer; 39 import org.eclipse.swt.dnd.Transfer; 40 import org.eclipse.swt.dnd.TransferData; 41 42 43 49 public class ValuesTransfer extends ByteArrayTransfer 50 { 51 52 53 private static final String TYPENAME = "org.apache.directory.ldapstudio.browser.value"; 54 55 56 private static final int TYPEID = registerType( TYPENAME ); 57 58 59 private static ValuesTransfer instance = new ValuesTransfer(); 60 61 62 67 public static ValuesTransfer getInstance() 68 { 69 return instance; 70 } 71 72 73 76 private ValuesTransfer() 77 { 78 } 79 80 81 89 public void javaToNative( Object object, TransferData transferData ) 90 { 91 if ( object == null || !( object instanceof IValue[] ) ) 92 { 93 return; 94 } 95 96 if ( isSupportedType( transferData ) ) 97 { 98 IValue[] values = ( IValue[] ) object; 99 try 100 { 101 ByteArrayOutputStream out = new ByteArrayOutputStream (); 102 DataOutputStream writeOut = new DataOutputStream ( out ); 103 104 for ( int i = 0; i < values.length; i++ ) 105 { 106 byte[] connectionName = values[i].getAttribute().getEntry().getConnection().getName().getBytes(); 107 writeOut.writeInt( connectionName.length ); 108 writeOut.write( connectionName ); 109 byte[] dn = values[i].getAttribute().getEntry().getDn().toString().getBytes(); 110 writeOut.writeInt( dn.length ); 111 writeOut.write( dn ); 112 byte[] attributeName = values[i].getAttribute().getDescription().getBytes(); 113 writeOut.writeInt( attributeName.length ); 114 writeOut.write( attributeName ); 115 if ( values[i].isString() ) 116 { 117 byte[] value = values[i].getStringValue().getBytes(); 118 writeOut.writeBoolean( true ); 119 writeOut.writeInt( value.length ); 120 writeOut.write( value ); 121 } 122 else if ( values[i].isBinary() ) 123 { 124 byte[] value = values[i].getBinaryValue(); 125 writeOut.writeBoolean( false ); 126 writeOut.writeInt( value.length ); 127 writeOut.write( value ); 128 } 129 } 130 131 byte[] buffer = out.toByteArray(); 132 writeOut.close(); 133 134 super.javaToNative( buffer, transferData ); 135 136 } 137 catch ( IOException e ) 138 { 139 } 140 } 141 } 142 143 144 151 public Object nativeToJava( TransferData transferData ) 152 { 153 try 154 { 155 if ( isSupportedType( transferData ) ) 156 { 157 byte[] buffer = ( byte[] ) super.nativeToJava( transferData ); 158 if ( buffer == null ) 159 { 160 return null; 161 } 162 163 List <IValue> valueList = new ArrayList <IValue>(); 164 try 165 { 166 ByteArrayInputStream in = new ByteArrayInputStream ( buffer ); 167 DataInputStream readIn = new DataInputStream ( in ); 168 169 do 170 { 171 IConnection connection = null; 172 if ( readIn.available() > 1 ) 173 { 174 int size = readIn.readInt(); 175 byte[] connectionName = new byte[size]; 176 readIn.read( connectionName ); 177 connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection( 178 new String ( connectionName ) ); 179 } 180 181 IEntry entry = null; 182 if ( readIn.available() > 1 && connection != null ) 183 { 184 int size = readIn.readInt(); 185 byte[] dn = new byte[size]; 186 readIn.read( dn ); 187 entry = connection.getEntryFromCache( new DN( new String ( dn ) ) ); 188 } 189 else 190 { 191 return null; 192 } 193 194 IAttribute attribute = null; 195 if ( readIn.available() > 1 && entry != null ) 196 { 197 int size = readIn.readInt(); 198 byte[] attributeName = new byte[size]; 199 readIn.read( attributeName ); 200 attribute = entry.getAttribute( new String ( attributeName ) ); 201 } 202 else 203 { 204 return null; 205 } 206 207 IValue value = null; 208 if ( readIn.available() > 1 && attribute != null ) 209 { 210 boolean isString = readIn.readBoolean(); 211 int size = readIn.readInt(); 212 byte[] val = new byte[size]; 213 readIn.read( val ); 214 String test = new String ( val ); 215 216 IValue[] values = attribute.getValues(); 217 for ( int i = 0; i < values.length; i++ ) 218 { 219 if ( isString && values[i].isString() && test.equals( values[i].getStringValue() ) ) 220 { 221 value = values[i]; 222 break; 223 } 224 else if ( !isString && values[i].isBinary() 225 && test.equals( new String ( values[i].getBinaryValue() ) ) ) 226 { 227 value = values[i]; 228 break; 229 } 230 } 231 } 232 else 233 { 234 return null; 235 } 236 237 if ( value != null ) 238 { 239 valueList.add( value ); 240 } 241 } 242 while ( readIn.available() > 1 ); 243 244 readIn.close(); 245 } 246 catch ( IOException ex ) 247 { 248 return null; 249 } 250 251 return valueList.isEmpty() ? null : valueList.toArray( new IValue[valueList.size()] ); 252 } 253 254 } 255 catch ( Exception e ) 256 { 257 e.printStackTrace(); 258 } 259 260 return null; 261 262 } 263 264 265 268 protected String [] getTypeNames() 269 { 270 return new String [] 271 { TYPENAME }; 272 } 273 274 275 278 protected int[] getTypeIds() 279 { 280 return new int[] 281 { TYPEID }; 282 } 283 284 } | Popular Tags |