1 20 package org.apache.directory.ldapstudio.aciitemeditor.model; 21 22 23 import java.text.ParseException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import javax.naming.Name ; 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 50 public class UserClassWrapper 51 { 52 53 54 public static final Map <Class , String > classToIdentifierMap; 55 static 56 { 57 Map <Class , String > map = new HashMap <Class , String >(); 58 map.put( UserClass.AllUsers.class, "allUsers" ); map.put( UserClass.ThisEntry.class, "thisEntry" ); map.put( UserClass.Name.class, "name" ); map.put( UserClass.UserGroup.class, "userGroup" ); map.put( UserClass.Subtree.class, "subtree" ); classToIdentifierMap = Collections.unmodifiableMap( map ); 64 } 65 66 67 public static final Map <Class , String > classToDisplayMap; 68 static 69 { 70 Map <Class , String > map = new HashMap <Class , String >(); 71 map.put( UserClass.AllUsers.class, Messages.getString( "UserClassWrapper.userClass.allUsers.label" ) ); map.put( UserClass.ThisEntry.class, Messages.getString( "UserClassWrapper.userClass.thisEntry.label" ) ); map.put( UserClass.Name.class, Messages.getString( "UserClassWrapper.userClass.name.label" ) ); map.put( UserClass.UserGroup.class, Messages.getString( "UserClassWrapper.userClass.userGroup.label" ) ); map.put( UserClass.Subtree.class, Messages.getString( "UserClassWrapper.userClass.subtree.label" ) ); classToDisplayMap = Collections.unmodifiableMap( map ); 77 } 78 79 80 private static final String DUMMY = "{ identificationTag \"id1\", precedence 1, authenticationLevel simple, " + "itemOrUserFirst userFirst: { userClasses { #identifier# #values# }, " + "userPermissions { { protectedItems { entry }, grantsAndDenials { grantRead } } } } }"; 84 85 private final Class clazz; 86 87 88 private List <String > values; 89 90 91 private String valuePrefix; 92 93 94 private String valueSuffix; 95 96 97 private AbstractDialogStringValueEditor valueEditor; 98 99 100 108 public UserClassWrapper( Class clazz, String valuePrefix, String 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 <String >(); 117 } 118 119 120 129 public UserClass getUserClass() throws ParseException 130 { 131 String flatValue = getFlatValue(); 132 String spec = DUMMY; 133 spec = spec.replaceAll( "#identifier#", getIdentifier() ); spec = spec.replaceAll( "#values#", flatValue ); ACIItemParser parser = new ACIItemParser( null ); 136 UserFirstACIItem aci = null; 137 try 138 { 139 aci = ( UserFirstACIItem ) parser.parse( spec ); 140 } 141 catch ( ParseException e ) 142 { 143 String msg = NLS.bind( 144 Messages.getString( "UserClassWrapper.error.message" ), new String [] { getIdentifier(), flatValue } ); throw new ParseException ( msg, 0 ); 146 } 147 UserClass userClass = ( UserClass ) aci.getUserClasses().iterator().next(); 148 return userClass; 149 } 150 151 152 157 public void setUserClass( UserClass userClass ) 158 { 159 assert userClass.getClass() == getClazz(); 160 161 values.clear(); 163 164 if ( userClass.getClass() == UserClass.Name.class ) 167 { 168 UserClass.Name name = ( UserClass.Name ) userClass; 169 Set <Name > jndiNames = name.getNames(); 170 for ( Name 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 <Name > jndiNames = userGrops.getNames(); 179 for ( Name 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 <SubtreeSpecification> subtreeSpecifications = subtree.getSubtreeSpecifications(); 188 for ( SubtreeSpecification subtreeSpecification : subtreeSpecifications ) 189 { 190 StringBuffer buffer = new StringBuffer (); 191 subtreeSpecification.printToBuffer( buffer ); 192 String s = buffer.toString(); 193 values.add( s ); 194 } 195 } 196 } 197 198 199 204 public String toString() 205 { 206 String flatValue = getFlatValue(); 207 if ( flatValue.length() > 0 ) 208 { 209 flatValue = flatValue.replace( '\r', ' ' ); 210 flatValue = flatValue.replace( '\n', ' ' ); 211 flatValue = ": " + flatValue; if ( flatValue.length() > 40 ) 213 { 214 String temp = flatValue; 215 flatValue = temp.substring( 0, 20 ); 216 flatValue = flatValue + "..."; flatValue = flatValue + temp.substring( temp.length() - 20, temp.length() ); 218 } 219 } 220 221 return getDisplayName() + " " + flatValue; } 223 224 225 230 private String getFlatValue() 231 { 232 if ( valueEditor == null || values.isEmpty() ) 233 { 234 return ""; } 236 237 StringBuffer sb = new StringBuffer (); 238 sb.append( "{ " ); for ( Iterator <String > it = values.iterator(); it.hasNext(); ) 240 { 241 sb.append( valuePrefix ); 242 String value = it.next(); 243 sb.append( value ); 244 sb.append( valueSuffix ); 245 if ( it.hasNext() ) 246 { 247 sb.append( ", " ); } 249 } 250 sb.append( " }" ); return sb.toString(); 252 } 253 254 255 260 public List <String > getValues() 261 { 262 return values; 263 } 264 265 266 271 public String getDisplayName() 272 { 273 return classToDisplayMap.get( clazz ); 274 } 275 276 277 282 public String getIdentifier() 283 { 284 return classToIdentifierMap.get( clazz ); 285 } 286 287 288 293 public Class getClazz() 294 { 295 return clazz; 296 } 297 298 299 304 public boolean isEditable() 305 { 306 return valueEditor != null; 307 } 308 309 310 315 public AbstractDialogStringValueEditor getValueEditor() 316 { 317 return valueEditor; 318 } 319 320 } 321 | Popular Tags |