1 23 24 package org.apache.slide.content; 25 26 import java.io.Serializable ; 27 import java.lang.reflect.Method ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.Vector ; 34 35 import org.apache.slide.common.Domain; 36 import org.apache.slide.common.ObjectValidationFailedException; 37 import org.apache.slide.security.NodePermission; 38 import org.apache.slide.util.EmptyEnumeration; 39 import org.apache.slide.util.Messages; 40 import org.jdom.Namespace; 41 42 47 public final class NodeProperty implements Serializable , Cloneable { 48 49 50 52 53 public static final String DEFAULT_NAMESPACE = "DAV:"; 54 public static final String SLIDE_NAMESPACE = 55 "http://jakarta.apache.org/slide/"; 56 57 protected static final String I_STANDARDLIVEPROPERTIESCLASS = "standardLivePropertiesClass"; 58 protected static final String I_STANDARDLIVEPROPERTIESCLASS_DEFAULT = "org.apache.slide.webdav.util.resourcekind.AbstractResourceKind"; 59 60 63 public static Set allLiveProperties; 64 65 68 public static Set allProtectedProperties; 69 70 73 public static Set allComputedProperties; 74 75 static { 76 try { 77 if (Domain.isInitialized()) { 78 Class slpc = Class.forName( Domain.getParameter(I_STANDARDLIVEPROPERTIESCLASS, I_STANDARDLIVEPROPERTIESCLASS_DEFAULT) ); 79 Method lp = slpc.getMethod( "getAllLiveProperties", new Class []{} ); 80 allLiveProperties = (Set )lp.invoke( null, new Object []{} ); Method pp = slpc.getMethod( "getAllProtectedProperties", new Class []{} ); 82 allProtectedProperties = (Set )pp.invoke( null, new Object []{} ); Method cp = slpc.getMethod( "getAllComputedProperties", new Class []{} ); 84 allComputedProperties = (Set )cp.invoke( null, new Object []{} ); } 86 } 87 catch( Exception x ) { 88 Domain.warn( "Loading of standard live properties class failed: "+x.getMessage() ); 89 } 90 91 if( allLiveProperties == null ) allLiveProperties = Collections.EMPTY_SET; 92 if( allProtectedProperties == null ) allProtectedProperties = Collections.EMPTY_SET; 93 if( allComputedProperties == null ) allComputedProperties = Collections.EMPTY_SET; 94 } 95 97 98 104 public NodeProperty(String name, Object value) { 105 setName(name); 106 setValue(value); 107 this.namespace = DEFAULT_NAMESPACE; 108 this.type = new String (); 109 this.kind = determineKind( namespace, name ); 110 } 111 112 113 120 public NodeProperty(String name, Object value, String namespace) { 121 this(name, value); 122 setNamespace(namespace); 123 this.kind = determineKind( namespace, name ); 124 } 125 126 127 135 public NodeProperty(String name, Object value, String namespace, String type) { 136 this(name, value); 137 setNamespace(namespace); 138 this.type = type; 139 this.kind = determineKind( namespace, name ); 140 } 141 142 143 150 public NodeProperty(String name, Object value, boolean protectedProperty) { 151 this(name, value); 152 if( protectedProperty ) 153 this.kind = Kind.PROTECTED; 154 else 155 this.kind = determineKind( DEFAULT_NAMESPACE, name ); 156 } 157 158 159 168 public NodeProperty(String name, Object value, String namespace, 169 String type, boolean protectedProperty) { 170 this(name, value, namespace); 171 setType(type); 172 if( protectedProperty ) 173 setKind( Kind.PROTECTED ); 174 else 175 this.kind = determineKind( namespace, name ); 176 } 177 178 179 180 182 183 186 private String name; 187 188 189 192 private String namespace; 193 194 195 198 private Object value; 199 200 201 206 private String type; 207 208 209 210 private Kind kind = Kind.DEAD; 211 212 213 216 private Vector permissions = null; 217 218 219 221 224 private static Kind determineKind( String namespace, String name ) { 225 Kind result = Kind.DEAD; 226 227 if( DEFAULT_NAMESPACE.equals(namespace) ) { 228 if( allComputedProperties.contains(name) ) 229 result = Kind.COMPUTED; 230 else if( allProtectedProperties.contains(name) ) 231 result = Kind.PROTECTED; 232 else if( allLiveProperties.contains(name) ) 233 result = Kind.LIVE; 234 } 235 return result; 236 } 237 238 243 public boolean isDeadProperty() { 244 return( (this.kind == Kind.DEAD) ); 245 } 246 247 248 253 public boolean isComputed() { 254 return( (this.kind == Kind.COMPUTED) ); 255 } 256 257 258 263 public boolean isProtected() { 264 return( 265 (this.kind == Kind.PROTECTED) || (this.kind == Kind.COMPUTED) ); 266 } 267 268 269 274 public boolean isLiveProperty() { 275 return( 276 (this.kind == Kind.LIVE) || (this.kind == Kind.PROTECTED) || (this.kind == Kind.COMPUTED) ); 277 } 278 279 280 285 public String getName() { 286 return this.name; 287 } 288 289 290 295 void setName(String name) { 296 if (name == null) { 297 this.name = new String (); 298 } else { 299 this.name = name; 300 } 301 } 302 303 304 309 public String getNamespace() { 310 return this.namespace; 311 } 312 313 314 319 void setNamespace(String namespace) { 320 if (namespace == null) { 321 this.namespace = ""; 322 } else { 323 this.namespace = namespace; 324 } 325 } 326 327 328 333 public Object getValue() { 334 return value; 335 } 336 337 338 343 void setValue(Object value) { 344 if (value == null) { 345 this.value = new String (); 346 } else { 347 this.value = value; 348 } 349 } 350 351 352 357 public String getType() { 358 return type; 359 } 360 361 362 367 void setType(String type) { 368 if (type == null) { 369 this.type = new String (); 370 } else { 371 this.type = type; 372 } 373 } 374 375 376 381 public Kind getKind() { 382 return kind; 383 } 384 385 386 391 public void setKind( Kind kind ) { 392 if( kind == null ) { 393 this.kind = Kind.DEAD; 394 } else { 395 this.kind = kind; 396 } 397 } 398 399 400 405 public void addPermission(NodePermission permission) { 406 if (this.permissions == null) { 407 this.permissions = new Vector (); 408 } 409 permissions.addElement(permission); 410 } 411 412 413 418 public void removePermission(NodePermission permission) { 419 if (this.permissions != null) { 420 permissions.removeElement(permission); 421 } 422 } 423 424 425 430 public Enumeration enumeratePermissions() { 431 if (this.permissions != null) { 432 return permissions.elements(); 433 } else { 434 return EmptyEnumeration.INSTANCE; 435 } 436 } 437 438 439 441 442 447 NodeProperty cloneObject() { 448 NodeProperty result = null; 449 try { 450 result = (NodeProperty) super.clone(); 451 } catch(CloneNotSupportedException e) { 452 } 453 return result; 454 } 455 456 457 462 public int hashCode() { 463 return getName().hashCode(); 464 } 465 466 467 474 public String toString() { 475 return getNamespace()+getName()+"="+getValue(); 476 } 477 478 479 487 public boolean equals(Object obj) { 488 if (!(obj instanceof NodeProperty)) { 489 return false; 490 } 491 NodeProperty other = (NodeProperty) obj; 492 return this.getName().equals(other.getName()) 493 && this.getNamespace().equals(other.getNamespace()); 494 } 495 496 497 500 public void validate() { 501 502 if (name == null) 503 throw new ObjectValidationFailedException 504 (Messages.message(NodeProperty.class.getName() + ".nullName")); 505 506 if (namespace == null) 507 throw new ObjectValidationFailedException 508 (Messages.message 509 (NodeProperty.class.getName() + ".nullNamespace")); 510 511 if (value == null) 512 throw new ObjectValidationFailedException 513 (Messages.message 514 (NodeProperty.class.getName() + ".nullValue")); 515 516 } 517 518 521 public static class Kind implements Serializable { 522 523 private static int 524 DEAD_ID = 0, 525 LIVE_ID = 1, 526 PROTECTED_ID = 2, 527 COMPUTED_ID = 3; 528 529 530 public static Kind 531 DEAD = new Kind( DEAD_ID ), 532 LIVE = new Kind( LIVE_ID ), 533 PROTECTED = new Kind( PROTECTED_ID ), 534 COMPUTED = new Kind( COMPUTED_ID ); 535 536 private int id = 0; 537 538 541 private Kind( int id ) { 542 this.id = id; 543 } 544 545 public boolean equals(Object object) { 546 if (this == object) { 547 return true; 548 } 549 if (object instanceof Kind) { 550 return ((Kind) object).id == id; 551 } 552 return false; 553 } 554 } 555 556 564 public static class NamespaceCache { 565 566 569 public final static String SLIDE_PREFIX = "S"; 570 571 574 public final static String SLIDE_URI = NodeProperty.SLIDE_NAMESPACE; 575 576 580 public final static Namespace SLIDE_NAMESPACE = getNamespace(SLIDE_PREFIX, SLIDE_URI);; 581 582 585 public final static String DEFAULT_PREFIX = "D"; 586 587 590 public final static String DEFAULT_URI = NodeProperty.DEFAULT_NAMESPACE; 591 592 596 public final static Namespace DEFAULT_NAMESPACE = getNamespace(DEFAULT_PREFIX, DEFAULT_URI);; 597 598 601 protected static Map namespaceMap; 602 603 604 615 public static Namespace getNamespace(String uri) { 616 return getNamespace("", uri); 617 } 618 619 632 public static Namespace getNamespace(String prefix, String uri) { 633 634 Namespace namespace = (Namespace)getNamespaceMap().get(uri); 635 if (namespace == null) { 636 namespace = Namespace.getNamespace(prefix, uri); 637 getNamespaceMap().put(namespace.getURI(), namespace); 638 } 639 return namespace; 640 } 641 642 647 protected static Map getNamespaceMap() { 648 if (namespaceMap == null) { 649 namespaceMap = new HashMap (); 650 } 651 return namespaceMap; 652 } 653 } 654 655 } 656 | Popular Tags |