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.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.naming.NamingException ; 32 import javax.naming.directory.Attribute ; 33 34 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor; 35 import org.apache.directory.shared.ldap.aci.ACIItemParser; 36 import org.apache.directory.shared.ldap.aci.ItemFirstACIItem; 37 import org.apache.directory.shared.ldap.aci.ProtectedItem; 38 import org.eclipse.osgi.util.NLS; 39 40 41 48 public class ProtectedItemWrapper 49 { 50 51 public static final Map <Class , String > classToIdentifierMap; 52 static 53 { 54 Map <Class , String > map = new HashMap <Class , String >(); 55 map.put( ProtectedItem.Entry.class, "entry" ); map.put( ProtectedItem.AllUserAttributeTypes.class, "allUserAttributeTypes" ); map.put( ProtectedItem.AttributeType.class, "attributeType" ); map.put( ProtectedItem.AllAttributeValues.class, "allAttributeValues" ); map.put( ProtectedItem.AllUserAttributeTypesAndValues.class, "allUserAttributeTypesAndValues" ); map.put( ProtectedItem.AttributeValue.class, "attributeValue" ); map.put( ProtectedItem.SelfValue.class, "selfValue" ); map.put( ProtectedItem.RangeOfValues.class, "rangeOfValues" ); map.put( ProtectedItem.MaxValueCount.class, "maxValueCount" ); map.put( ProtectedItem.MaxImmSub.class, "maxImmSub" ); map.put( ProtectedItem.RestrictedBy.class, "restrictedBy" ); map.put( ProtectedItem.Classes.class, "classes" ); classToIdentifierMap = Collections.unmodifiableMap( map ); 68 } 69 70 71 public static final Map <Class , String > classToDisplayMap; 72 static 73 { 74 Map <Class , String > map = new HashMap <Class , String >(); 75 map.put( ProtectedItem.Entry.class, Messages.getString( "ProtectedItemWrapper.protectedItem.entry.label" ) ); map.put( ProtectedItem.AllUserAttributeTypes.class, Messages 77 .getString( "ProtectedItemWrapper.protectedItem.allUserAttributeTypes.label" ) ); map.put( ProtectedItem.AttributeType.class, Messages 79 .getString( "ProtectedItemWrapper.protectedItem.attributeType.label" ) ); map.put( ProtectedItem.AllAttributeValues.class, Messages 81 .getString( "ProtectedItemWrapper.protectedItem.allAttributeValues.label" ) ); map.put( ProtectedItem.AllUserAttributeTypesAndValues.class, Messages 83 .getString( "ProtectedItemWrapper.protectedItem.allUserAttributeTypesAndValues.label" ) ); map.put( ProtectedItem.AttributeValue.class, Messages 85 .getString( "ProtectedItemWrapper.protectedItem.attributeValue.label" ) ); map.put( ProtectedItem.SelfValue.class, Messages 87 .getString( "ProtectedItemWrapper.protectedItem.selfValue.label" ) ); map.put( ProtectedItem.RangeOfValues.class, Messages 89 .getString( "ProtectedItemWrapper.protectedItem.rangeOfValues.label" ) ); map.put( ProtectedItem.MaxValueCount.class, Messages 91 .getString( "ProtectedItemWrapper.protectedItem.maxValueCount.label" ) ); map.put( ProtectedItem.MaxImmSub.class, Messages 93 .getString( "ProtectedItemWrapper.protectedItem.maxImmSub.label" ) ); map.put( ProtectedItem.RestrictedBy.class, Messages 95 .getString( "ProtectedItemWrapper.protectedItem.restrictedBy.label" ) ); map.put( ProtectedItem.Classes.class, Messages.getString( "ProtectedItemWrapper.protectedItem.classes.label" ) ); classToDisplayMap = Collections.unmodifiableMap( map ); 98 } 99 100 101 private static final String DUMMY = "{ identificationTag \"id1\", precedence 1, authenticationLevel simple, " + "itemOrUserFirst itemFirst: { protectedItems { #identifier# #values# }, " + "itemPermissions { { userClasses { allUsers }, grantsAndDenials { grantRead } } } } }"; 105 106 private final Class clazz; 107 108 109 private List <String > values; 110 111 112 private String valuePrefix; 113 114 115 private String valueSuffix; 116 117 118 private AbstractDialogStringValueEditor valueEditor; 119 120 121 private boolean isMultivalued; 122 123 124 133 public ProtectedItemWrapper( Class clazz, boolean isMultivalued, String valuePrefix, String valueSuffix, 134 AbstractDialogStringValueEditor valueEditor ) 135 { 136 this.clazz = clazz; 137 this.isMultivalued = isMultivalued; 138 this.valuePrefix = valuePrefix; 139 this.valueSuffix = valueSuffix; 140 this.valueEditor = valueEditor; 141 142 this.values = new ArrayList <String >(); 143 } 144 145 146 155 public ProtectedItem getProtectedItem() throws ParseException 156 { 157 String flatValue = getFlatValue(); 158 String spec = DUMMY; 159 spec = spec.replaceAll( "#identifier#", getIdentifier() ); spec = spec.replaceAll( "#values#", flatValue ); ACIItemParser parser = new ACIItemParser( null ); 162 ItemFirstACIItem aci = null; 163 try 164 { 165 aci = ( ItemFirstACIItem ) parser.parse( spec ); 166 } 167 catch ( ParseException e ) 168 { 169 170 String msg = NLS 171 .bind( 172 Messages.getString( "ProtectedItemWrapper.error.message" ), new String [] { getIdentifier(), flatValue } ); throw new ParseException ( msg, 0 ); 174 } 175 ProtectedItem item = ( ProtectedItem ) aci.getProtectedItems().iterator().next(); 176 return item; 177 } 178 179 180 185 public void setProtectedItem( ProtectedItem item ) 186 { 187 assert item.getClass() == getClazz(); 188 189 values.clear(); 191 192 if ( item.getClass() == ProtectedItem.AttributeType.class ) 195 { 196 ProtectedItem.AttributeType at = ( ProtectedItem.AttributeType ) item; 197 for ( Iterator it = at.iterator(); it.hasNext(); ) 198 { 199 values.add( it.next().toString() ); 200 } 201 } 202 else if ( item.getClass() == ProtectedItem.AllAttributeValues.class ) 203 { 204 ProtectedItem.AllAttributeValues aav = ( ProtectedItem.AllAttributeValues ) item; 205 for ( Iterator it = aav.iterator(); it.hasNext(); ) 206 { 207 values.add( it.next().toString() ); 208 } 209 } 210 else if ( item.getClass() == ProtectedItem.AttributeValue.class ) 211 { 212 ProtectedItem.AttributeValue av = ( ProtectedItem.AttributeValue ) item; 213 for ( Iterator it = av.iterator(); it.hasNext(); ) 214 { 215 Attribute attribute = ( Attribute ) it.next(); 216 try 217 { 218 values.add( attribute.getID() + "=" + attribute.get() ); } 220 catch ( NamingException e ) 221 { 222 } 223 } 224 } 225 else if ( item.getClass() == ProtectedItem.SelfValue.class ) 226 { 227 ProtectedItem.SelfValue sv = ( ProtectedItem.SelfValue ) item; 228 for ( Iterator it = sv.iterator(); it.hasNext(); ) 229 { 230 values.add( it.next().toString() ); 231 } 232 } 233 else if ( item.getClass() == ProtectedItem.RangeOfValues.class ) 234 { 235 ProtectedItem.RangeOfValues rov = ( ProtectedItem.RangeOfValues ) item; 236 StringBuffer buffer = new StringBuffer (); 237 rov.getFilter().printToBuffer( buffer ); 238 values.add( buffer.toString() ); 239 } 240 else if ( item.getClass() == ProtectedItem.MaxValueCount.class ) 241 { 242 ProtectedItem.MaxValueCount mvc = ( ProtectedItem.MaxValueCount ) item; 243 for ( Iterator it = mvc.iterator(); it.hasNext(); ) 244 { 245 ProtectedItem.MaxValueCountItem mvci = ( ProtectedItem.MaxValueCountItem ) it.next(); 246 StringBuffer buffer = new StringBuffer (); 247 mvci.printToBuffer( buffer ); 248 values.add( buffer.toString() ); 249 } 250 } 251 else if ( item.getClass() == ProtectedItem.MaxImmSub.class ) 252 { 253 ProtectedItem.MaxImmSub mis = ( ProtectedItem.MaxImmSub ) item; 254 values.add( Integer.toString( mis.getValue() ) ); 255 } 256 else if ( item.getClass() == ProtectedItem.RestrictedBy.class ) 257 { 258 ProtectedItem.RestrictedBy rb = ( ProtectedItem.RestrictedBy ) item; 259 for ( Iterator it = rb.iterator(); it.hasNext(); ) 260 { 261 ProtectedItem.RestrictedByItem rbi = ( ProtectedItem.RestrictedByItem ) it.next(); 262 StringBuffer buffer = new StringBuffer (); 263 rbi.printToBuffer( buffer ); 264 values.add( buffer.toString() ); 265 } 266 } 267 else if ( item.getClass() == ProtectedItem.Classes.class ) 268 { 269 ProtectedItem.Classes classes = ( ProtectedItem.Classes ) item; 270 StringBuffer buffer = new StringBuffer (); 271 classes.getClasses().printRefinementToBuffer( buffer ); 272 values.add( buffer.toString() ); 273 } 274 275 } 276 277 278 283 public String toString() 284 { 285 String flatValue = getFlatValue(); 286 if ( flatValue.length() > 0 ) 287 { 288 flatValue = flatValue.replace( '\r', ' ' ); 289 flatValue = flatValue.replace( '\n', ' ' ); 290 flatValue = ": " + flatValue; if ( flatValue.length() > 40 ) 292 { 293 String temp = flatValue; 294 flatValue = temp.substring( 0, 20 ); 295 flatValue = flatValue + "..."; flatValue = flatValue + temp.substring( temp.length() - 20, temp.length() ); 297 } 298 } 299 300 return getDisplayName() + " " + flatValue; } 302 303 304 309 private String getFlatValue() 310 { 311 if ( valueEditor == null || values.isEmpty() ) 312 { 313 return ""; } 315 316 StringBuffer sb = new StringBuffer (); 317 if ( isMultivalued() ) 318 { 319 sb.append( "{ " ); } 321 for ( Iterator <String > it = values.iterator(); it.hasNext(); ) 322 { 323 sb.append( valuePrefix ); 324 String value = it.next(); 325 sb.append( value ); 326 sb.append( valueSuffix ); 327 if ( it.hasNext() ) 328 { 329 sb.append( ", " ); } 331 } 332 if ( isMultivalued() ) 333 { 334 sb.append( " }" ); } 336 return sb.toString(); 337 } 338 339 340 345 public List <String > getValues() 346 { 347 return values; 348 } 349 350 351 356 public String getDisplayName() 357 { 358 return classToDisplayMap.get( clazz ); 359 } 360 361 362 367 public String getIdentifier() 368 { 369 return classToIdentifierMap.get( clazz ); 370 } 371 372 373 378 public Class getClazz() 379 { 380 return clazz; 381 } 382 383 384 389 public boolean isEditable() 390 { 391 return valueEditor != null; 392 } 393 394 395 400 public AbstractDialogStringValueEditor getValueEditor() 401 { 402 return valueEditor; 403 } 404 405 406 411 public boolean isMultivalued() 412 { 413 return isMultivalued; 414 } 415 } 416 | Popular Tags |