KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > model > UserClassWrapper


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 package org.apache.directory.ldapstudio.aciitemeditor.model;
21
22
23 import java.text.ParseException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.naming.Name JavaDoc;
34
35 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
36 import org.apache.directory.shared.ldap.aci.ACIItemParser;
37 import org.apache.directory.shared.ldap.aci.UserClass;
38 import org.apache.directory.shared.ldap.aci.UserFirstACIItem;
39 import org.apache.directory.shared.ldap.subtree.SubtreeSpecification;
40 import org.eclipse.osgi.util.NLS;
41
42
43 /**
44  * The UserClassWrapper is used as input for the table viewer.
45  * The user class values are always stored as raw string values.
46  *
47  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
48  * @version $Rev$, $Date$
49  */

50 public class UserClassWrapper
51 {
52
53     /** This map contains all possible user class identifiers */
54     public static final Map JavaDoc<Class JavaDoc, String JavaDoc> classToIdentifierMap;
55     static
56     {
57         Map JavaDoc<Class JavaDoc, String JavaDoc> map = new HashMap JavaDoc<Class JavaDoc, String JavaDoc>();
58         map.put( UserClass.AllUsers.class, "allUsers" ); //$NON-NLS-1$
59
map.put( UserClass.ThisEntry.class, "thisEntry" ); //$NON-NLS-1$
60
map.put( UserClass.Name.class, "name" ); //$NON-NLS-1$
61
map.put( UserClass.UserGroup.class, "userGroup" ); //$NON-NLS-1$
62
map.put( UserClass.Subtree.class, "subtree" ); //$NON-NLS-1$
63
classToIdentifierMap = Collections.unmodifiableMap( map );
64     }
65
66     /** This map contains all user class display values */
67     public static final Map JavaDoc<Class JavaDoc, String JavaDoc> classToDisplayMap;
68     static
69     {
70         Map JavaDoc<Class JavaDoc, String JavaDoc> map = new HashMap JavaDoc<Class JavaDoc, String JavaDoc>();
71         map.put( UserClass.AllUsers.class, Messages.getString( "UserClassWrapper.userClass.allUsers.label" ) ); //$NON-NLS-1$
72
map.put( UserClass.ThisEntry.class, Messages.getString( "UserClassWrapper.userClass.thisEntry.label" ) ); //$NON-NLS-1$
73
map.put( UserClass.Name.class, Messages.getString( "UserClassWrapper.userClass.name.label" ) ); //$NON-NLS-1$
74
map.put( UserClass.UserGroup.class, Messages.getString( "UserClassWrapper.userClass.userGroup.label" ) ); //$NON-NLS-1$
75
map.put( UserClass.Subtree.class, Messages.getString( "UserClassWrapper.userClass.subtree.label" ) ); //$NON-NLS-1$
76
classToDisplayMap = Collections.unmodifiableMap( map );
77     }
78
79     /** A dummy ACI to check syntax of the userClassValue. */
80     private static final String JavaDoc DUMMY = "{ identificationTag \"id1\", precedence 1, authenticationLevel simple, " //$NON-NLS-1$
81
+ "itemOrUserFirst userFirst: { userClasses { #identifier# #values# }, " //$NON-NLS-1$
82
+ "userPermissions { { protectedItems { entry }, grantsAndDenials { grantRead } } } } }"; //$NON-NLS-1$
83

84     /** The class of the user class, never null. */
85     private final Class JavaDoc clazz;
86
87     /** The user class values, may be empty. */
88     private List JavaDoc<String JavaDoc> values;
89
90     /** The value prefix, prepended to the value. */
91     private String JavaDoc valuePrefix;
92
93     /** The value suffix, appended to the value. */
94     private String JavaDoc valueSuffix;
95
96     /** The value editor, null means no value. */
97     private AbstractDialogStringValueEditor valueEditor;
98
99
100     /**
101      * Creates a new instance of UserClassWrapper.
102      *
103      * @param clazz the java class of the UserClass
104      * @param valuePrefix the identifier
105      * @param valueSuffix the dislpay name
106      * @param valueEditor the value editor
107      */

108     public UserClassWrapper( Class JavaDoc clazz, String JavaDoc valuePrefix, String JavaDoc valueSuffix,
109         AbstractDialogStringValueEditor valueEditor )
110     {
111         this.clazz = clazz;
112         this.valuePrefix = valuePrefix;
113         this.valueSuffix = valueSuffix;
114         this.valueEditor = valueEditor;
115
116         this.values = new ArrayList JavaDoc<String JavaDoc>();
117     }
118
119
120     /**
121      * Creates a new user class object. Therefore it uses the
122      * dummy ACI, injects the user class and its value, parses
123      * the ACI and extracts the user class from the parsed bean.
124      *
125      * @return the parsed user class
126      *
127      * @throws ParseException if parsing fails
128      */

129     public UserClass getUserClass() throws ParseException JavaDoc
130     {
131         String JavaDoc flatValue = getFlatValue();
132         String JavaDoc spec = DUMMY;
133         spec = spec.replaceAll( "#identifier#", getIdentifier() ); //$NON-NLS-1$
134
spec = spec.replaceAll( "#values#", flatValue ); //$NON-NLS-1$
135
ACIItemParser parser = new ACIItemParser( null );
136         UserFirstACIItem aci = null;
137         try
138         {
139             aci = ( UserFirstACIItem ) parser.parse( spec );
140         }
141         catch ( ParseException JavaDoc e )
142         {
143             String JavaDoc msg = NLS.bind(
144                 Messages.getString( "UserClassWrapper.error.message" ), new String JavaDoc[] { getIdentifier(), flatValue } ); //$NON-NLS-1$
145
throw new ParseException JavaDoc( msg, 0 );
146         }
147         UserClass userClass = ( UserClass ) aci.getUserClasses().iterator().next();
148         return userClass;
149     }
150
151
152     /**
153      * Sets the user class.
154      *
155      * @param userClass the user class
156      */

157     public void setUserClass( UserClass userClass )
158     {
159         assert userClass.getClass() == getClazz();
160
161         // first clear values
162
values.clear();
163
164         // switch on userClass type
165
// no value in UserClass.AllUsers and UserClass.ThisEntry
166
if ( userClass.getClass() == UserClass.Name.class )
167         {
168             UserClass.Name name = ( UserClass.Name ) userClass;
169             Set JavaDoc<Name JavaDoc> jndiNames = name.getNames();
170             for ( Name JavaDoc jndiName : jndiNames )
171             {
172                 values.add( jndiName.toString() );
173             }
174         }
175         else if ( userClass.getClass() == UserClass.UserGroup.class )
176         {
177             UserClass.UserGroup userGrops = ( UserClass.UserGroup ) userClass;
178             Set JavaDoc<Name JavaDoc> jndiNames = userGrops.getNames();
179             for ( Name JavaDoc jndiName : jndiNames )
180             {
181                 values.add( jndiName.toString() );
182             }
183         }
184         else if ( userClass.getClass() == UserClass.Subtree.class )
185         {
186             UserClass.Subtree subtree = ( UserClass.Subtree ) userClass;
187             Collection JavaDoc<SubtreeSpecification> subtreeSpecifications = subtree.getSubtreeSpecifications();
188             for ( SubtreeSpecification subtreeSpecification : subtreeSpecifications )
189             {
190                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
191                 subtreeSpecification.printToBuffer( buffer );
192                 String JavaDoc s = buffer.toString();
193                 values.add( s );
194             }
195         }
196     }
197
198
199     /**
200      * Returns a user-friedly string, displayed in the table.
201      *
202      * @return the string
203      */

204     public String JavaDoc toString()
205     {
206         String JavaDoc flatValue = getFlatValue();
207         if ( flatValue.length() > 0 )
208         {
209             flatValue = flatValue.replace( '\r', ' ' );
210             flatValue = flatValue.replace( '\n', ' ' );
211             flatValue = ": " + flatValue; //$NON-NLS-1$
212
if ( flatValue.length() > 40 )
213             {
214                 String JavaDoc temp = flatValue;
215                 flatValue = temp.substring( 0, 20 );
216                 flatValue = flatValue + "..."; //$NON-NLS-1$
217
flatValue = flatValue + temp.substring( temp.length() - 20, temp.length() );
218             }
219         }
220
221         return getDisplayName() + " " + flatValue; //$NON-NLS-1$
222
}
223
224
225     /**
226      * Returns the flat value.
227      *
228      * @return the flat value
229      */

230     private String JavaDoc getFlatValue()
231     {
232         if ( valueEditor == null || values.isEmpty() )
233         {
234             return ""; //$NON-NLS-1$
235
}
236
237         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
238         sb.append( "{ " ); //$NON-NLS-1$
239
for ( Iterator JavaDoc<String JavaDoc> it = values.iterator(); it.hasNext(); )
240         {
241             sb.append( valuePrefix );
242             String JavaDoc value = it.next();
243             sb.append( value );
244             sb.append( valueSuffix );
245             if ( it.hasNext() )
246             {
247                 sb.append( ", " ); //$NON-NLS-1$
248
}
249         }
250         sb.append( " }" ); //$NON-NLS-1$
251
return sb.toString();
252     }
253
254
255     /**
256      * Returns the list of values, may be modified.
257      *
258      * @return the modifyable list of values.
259      */

260     public List JavaDoc<String JavaDoc> getValues()
261     {
262         return values;
263     }
264
265
266     /**
267      * Gets the display name.
268      *
269      * @return the display name
270      */

271     public String JavaDoc getDisplayName()
272     {
273         return classToDisplayMap.get( clazz );
274     }
275
276
277     /**
278      * Gets the identifier.
279      *
280      * @return the identifier
281      */

282     public String JavaDoc getIdentifier()
283     {
284         return classToIdentifierMap.get( clazz );
285     }
286
287
288     /**
289      * Returns the class of the user class.
290      *
291      * @return the class of the user class.
292      */

293     public Class JavaDoc getClazz()
294     {
295         return clazz;
296     }
297
298
299     /**
300      * Checks if is editable.
301      *
302      * @return true, if is editable
303      */

304     public boolean isEditable()
305     {
306         return valueEditor != null;
307     }
308
309
310     /**
311      * Gets the value editor.
312      *
313      * @return the value editor, may be null.
314      */

315     public AbstractDialogStringValueEditor getValueEditor()
316     {
317         return valueEditor;
318     }
319
320 }
321
Popular Tags