1 20 21 package org.apache.directory.ldapstudio.browser.ui.actions; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 32 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 33 import org.apache.directory.ldapstudio.browser.core.internal.model.AttributeComparator; 34 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 35 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 36 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 37 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 38 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 39 import org.apache.directory.ldapstudio.browser.core.model.IValue; 40 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 41 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 42 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 43 import org.eclipse.jface.resource.ImageDescriptor; 44 45 46 52 public class CopyEntryAsCsvAction extends CopyEntryAsAction 53 { 54 57 public static final int MODE_TABLE = 5; 58 59 60 66 public CopyEntryAsCsvAction( int mode ) 67 { 68 super( "CSV", mode ); 69 } 70 71 72 75 public ImageDescriptor getImageDescriptor() 76 { 77 if ( this.mode == MODE_DN_ONLY ) 78 { 79 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV ); 80 } 81 else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY ) 82 { 83 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV_SEARCHRESULT ); 84 } 85 else if ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) 86 { 87 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV_OPERATIONAL ); 88 } 89 else if ( this.mode == MODE_NORMAL ) 90 { 91 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV_USER ); 92 } 93 else if ( this.mode == MODE_TABLE ) 94 { 95 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_TABLE ); 96 } 97 else 98 { 99 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_CSV ); 100 } 101 } 102 103 104 107 public String getText() 108 { 109 if ( this.mode == MODE_TABLE ) 110 { 111 return "Copy Table"; 112 } 113 114 return super.getText(); 115 } 116 117 118 121 public boolean isEnabled() 122 { 123 if ( this.mode == MODE_TABLE ) 124 { 125 return getInput() != null && getInput() instanceof ISearch 126 && ( ( ISearch ) getInput() ).getSearchResults() != null 127 && ( ( ISearch ) getInput() ).getSearchResults().length > 0; 128 } 129 130 return super.isEnabled(); 131 } 132 133 134 137 public void run() 138 { 139 140 if ( this.mode == MODE_TABLE ) 141 { 142 if ( getInput() != null && getInput() instanceof ISearch 143 && ( ( ISearch ) getInput() ).getSearchResults() != null 144 && ( ( ISearch ) getInput() ).getSearchResults().length > 0 ) 145 { 146 List <IEntry> entryList = new ArrayList <IEntry>(); 147 ISearchResult[] results = ( ( ISearch ) getInput() ).getSearchResults(); 148 for ( int k = 0; k < results.length; k++ ) 149 { 150 entryList.add( results[k].getEntry() ); 151 } 152 IEntry[] entries = ( IEntry[] ) entryList.toArray( new IEntry[entryList.size()] ); 153 154 StringBuffer text = new StringBuffer (); 155 serialializeEntries( entries, text ); 156 copyToClipboard( text.toString() ); 157 } 158 } 159 else 160 { 161 super.run(); 162 } 163 164 } 165 166 167 170 public void serialializeEntries( IEntry[] entries, StringBuffer text ) 171 { 172 173 String attributeDelimiter = BrowserCommonActivator.getDefault().getPreferenceStore().getString( 174 BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_ATTRIBUTEDELIMITER ); 175 String valueDelimiter = BrowserCommonActivator.getDefault().getPreferenceStore().getString( 176 BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_VALUEDELIMITER ); 177 String quoteCharacter = BrowserCommonActivator.getDefault().getPreferenceStore().getString( 178 BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_QUOTECHARACTER ); 179 String lineSeparator = BrowserCommonActivator.getDefault().getPreferenceStore().getString( 180 BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_LINESEPARATOR ); 181 int binaryEncoding = BrowserCommonActivator.getDefault().getPreferenceStore().getInt( 182 BrowserCommonConstants.PREFERENCE_FORMAT_TABLE_BINARYENCODING ); 183 184 String [] returningAttributes = null; 185 if ( this.mode == MODE_DN_ONLY ) 186 { 187 returningAttributes = new String [0]; 188 } 189 else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY && getSelectedSearchResults().length > 0 190 && getSelectedEntries().length + getSelectedBookmarks().length + getSelectedSearches().length == 0 ) 191 { 192 returningAttributes = getSelectedSearchResults()[0].getSearch().getReturningAttributes(); 193 } 194 else if ( ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY || this.mode == MODE_TABLE ) 195 && getSelectedSearches().length == 1 ) 196 { 197 returningAttributes = getSelectedSearches()[0].getReturningAttributes(); 198 } 199 else if ( ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY || this.mode == MODE_TABLE ) 200 && ( getInput() instanceof ISearch ) ) 201 { 202 returningAttributes = ( ( ISearch ) ( getInput() ) ).getReturningAttributes(); 203 } 204 else 205 { 206 Map <String , IAttribute> attributeMap = new HashMap <String , IAttribute>(); 207 for ( int e = 0; entries != null && e < entries.length; e++ ) 208 { 209 IAttribute[] attributes = entries[e].getAttributes(); 210 for ( int a = 0; attributes != null && a < attributes.length; a++ ) 211 { 212 213 if ( attributes[a].isOperationalAttribute() && this.mode != MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) 214 continue; 215 216 if ( !attributeMap.containsKey( attributes[a].getDescription() ) ) 217 { 218 attributeMap.put( attributes[a].getDescription(), attributes[a] ); 219 } 220 } 221 } 222 IAttribute[] attributes = ( IAttribute[] ) attributeMap.values().toArray( 223 new IAttribute[attributeMap.size()] ); 224 225 if ( attributes.length > 0 ) 226 { 227 AttributeComparator comparator = new AttributeComparator( entries[0].getConnection() ); 228 Arrays.sort( attributes, comparator ); 229 } 230 231 returningAttributes = new String [attributes.length]; 232 for ( int i = 0; i < attributes.length; i++ ) 233 { 234 returningAttributes[i] = attributes[i].getDescription(); 235 } 236 } 237 238 if ( this.mode != MODE_TABLE 240 || BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean( 241 BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_DN ) ) 242 { 243 text.append( quoteCharacter ); 244 text.append( "DN" ); 245 text.append( quoteCharacter ); 246 text.append( attributeDelimiter ); 247 } 248 for ( int a = 0; returningAttributes != null && a < returningAttributes.length; a++ ) 249 { 250 text.append( quoteCharacter ); 251 text.append( returningAttributes[a] ); 252 text.append( quoteCharacter ); 253 if ( a + 1 < returningAttributes.length ) 254 { 255 text.append( attributeDelimiter ); 256 } 257 } 258 text.append( lineSeparator ); 259 260 for ( int e = 0; entries != null && e < entries.length; e++ ) 261 { 262 263 if ( this.mode != MODE_TABLE 264 || BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean( 265 BrowserUIConstants.PREFERENCE_SEARCHRESULTEDITOR_SHOW_DN ) ) 266 { 267 text.append( quoteCharacter ); 268 text.append( entries[e].getDn().toString() ); 269 text.append( quoteCharacter ); 270 text.append( attributeDelimiter ); 271 272 } 273 for ( int a = 0; returningAttributes != null && a < returningAttributes.length; a++ ) 274 { 275 276 AttributeComparator comparator = new AttributeComparator( entries[e] ); 277 AttributeHierarchy ah = entries[e].getAttributeWithSubtypes( returningAttributes[a] ); 278 if ( ah != null ) 279 { 280 281 StringBuffer valueSB = new StringBuffer (); 282 283 for ( Iterator it = ah.iterator(); it.hasNext(); ) 284 { 285 IAttribute attribute = ( IAttribute ) it.next(); 286 if ( attribute != null ) 287 { 288 289 IValue[] values = attribute.getValues(); 290 Arrays.sort( values, comparator ); 291 292 for ( int v = 0; v < values.length; v++ ) 293 { 294 String val = LdifUtils.getStringValue( values[v], binaryEncoding ); 295 valueSB.append( val ); 296 if ( v + 1 < values.length ) 297 { 298 valueSB.append( valueDelimiter ); 299 } 300 } 301 } 302 303 if ( it.hasNext() ) 304 { 305 valueSB.append( valueDelimiter ); 306 } 307 } 308 309 String value = valueSB.toString().replaceAll( quoteCharacter, quoteCharacter + quoteCharacter ); 310 text.append( quoteCharacter ); 311 text.append( value ); 312 text.append( quoteCharacter ); 313 314 } 315 316 342 if ( a + 1 < returningAttributes.length ) 343 { 344 text.append( attributeDelimiter ); 345 } 346 } 347 348 if ( e < entries.length ) 349 { 350 text.append( lineSeparator ); 351 } 352 } 353 } 354 } 355 | Popular Tags |