KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > actions > CopyEntryAsLdifAction


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.ui.actions;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
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 /**
45  * This Action copies entry(ies) as LDIF.
46  *
47  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
48  * @version $Rev$, $Date$
49  */

50 public class CopyEntryAsLdifAction extends CopyEntryAsAction
51 {
52
53     /**
54      * Creates a new instance of CopyEntryAsLdifAction.
55      *
56      * @param mode
57      * the copy Mode
58      */

59     public CopyEntryAsLdifAction( int mode )
60     {
61         super( "LDIF", mode );
62     }
63
64
65     /**
66      * {@inheritDoc}
67      */

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     /**
90      * {@inheritDoc}
91      */

92     public void serialializeEntries( IEntry[] entries, StringBuffer JavaDoc text )
93     {
94
95         String JavaDoc lineSeparator = BrowserCorePlugin.getDefault().getPluginPreferences().getString(
96             BrowserCoreConstants.PREFERENCE_LDIF_LINE_SEPARATOR );
97
98         Set JavaDoc 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 JavaDoc( 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 JavaDoc( 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 JavaDoc valueList = new ArrayList JavaDoc();
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     /**
158      * Serializes a Value.
159      *
160      * @param value
161      * the Value to serialize
162      * @param text
163      * the StringBuffer to serialize to
164      */

165     protected void serializeValue( IValue value, StringBuffer JavaDoc text )
166     {
167         text.append( ModelConverter.valueToLdifAttrValLine( value ).toFormattedString() );
168     }
169
170
171     /**
172      * Serialize a DN.
173      *
174      * @param dn
175      * the DN to serialize
176      * @param text
177      * the StringBuffer to serialize to
178      */

179     protected void serializeDn( DN dn, StringBuffer JavaDoc text )
180     {
181         text.append( ModelConverter.dnToLdifDnLine( dn ).toFormattedString() );
182     }
183 }
184
Popular Tags