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.HashSet ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 31 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 32 import org.apache.directory.ldapstudio.browser.core.internal.model.AttributeComparator; 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.IEntry; 36 import org.apache.directory.ldapstudio.browser.core.model.IValue; 37 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter; 38 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 39 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 40 41 import org.eclipse.jface.resource.ImageDescriptor; 42 43 44 50 public class CopyEntryAsLdifAction extends CopyEntryAsAction 51 { 52 53 59 public CopyEntryAsLdifAction( int mode ) 60 { 61 super( "LDIF", mode ); 62 } 63 64 65 68 public ImageDescriptor getImageDescriptor() 69 { 70 if ( this.mode == MODE_DN_ONLY ) 71 { 72 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_LDIF ); 73 } 74 else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY ) 75 { 76 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_LDIF_SEARCHRESULT ); 77 } 78 else if ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) 79 { 80 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_LDIF_OPERATIONAL ); 81 } 82 else 83 { 84 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_LDIF_USER ); 85 } 86 } 87 88 89 92 public void serialializeEntries( IEntry[] entries, StringBuffer text ) 93 { 94 95 String lineSeparator = BrowserCorePlugin.getDefault().getPluginPreferences().getString( 96 BrowserCoreConstants.PREFERENCE_LDIF_LINE_SEPARATOR ); 97 98 Set returningAttributesSet = null; 99 if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY && getSelectedSearchResults().length > 0 100 && getSelectedEntries().length + getSelectedBookmarks().length + getSelectedSearches().length == 0 ) 101 { 102 returningAttributesSet = new HashSet ( Arrays.asList( getSelectedSearchResults()[0].getSearch() 103 .getReturningAttributes() ) ); 104 } 105 else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY && getSelectedSearches().length == 1 ) 106 { 107 returningAttributesSet = new HashSet ( Arrays.asList( getSelectedSearches()[0].getReturningAttributes() ) ); 108 } 109 110 for ( int e = 0; entries != null && e < entries.length; e++ ) 111 { 112 113 serializeDn( entries[e].getDn(), text ); 114 115 if ( this.mode != MODE_DN_ONLY ) 116 { 117 118 List valueList = new ArrayList (); 119 IAttribute[] attributes = entries[e].getAttributes(); 120 if ( attributes != null ) 121 { 122 for ( int i = 0; i < attributes.length; i++ ) 123 { 124 125 if ( returningAttributesSet != null 126 && !returningAttributesSet.contains( attributes[i].getType() ) ) 127 continue; 128 129 if ( attributes[i].isOperationalAttribute() && this.mode != MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) 130 continue; 131 132 IValue[] values = attributes[i].getValues(); 133 for ( int k = 0; k < values.length; k++ ) 134 { 135 valueList.add( values[k] ); 136 } 137 } 138 } 139 IValue[] values = ( IValue[] ) valueList.toArray( new IValue[valueList.size()] ); 140 141 AttributeComparator comparator = new AttributeComparator( entries[e] ); 142 Arrays.sort( values, comparator ); 143 144 for ( int i = 0; i < values.length; i++ ) 145 { 146 serializeValue( values[i], text ); 147 } 148 } 149 if ( e < entries.length ) 150 { 151 text.append( lineSeparator ); 152 } 153 } 154 } 155 156 157 165 protected void serializeValue( IValue value, StringBuffer text ) 166 { 167 text.append( ModelConverter.valueToLdifAttrValLine( value ).toFormattedString() ); 168 } 169 170 171 179 protected void serializeDn( DN dn, StringBuffer text ) 180 { 181 text.append( ModelConverter.dnToLdifDnLine( dn ).toFormattedString() ); 182 } 183 } 184 | Popular Tags |