KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > content > NodeProperty


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeProperty.java,v 1.25.2.1 2004/10/18 15:00:49 luetzkendorf Exp $
3  * $Revision: 1.25.2.1 $
4  * $Date: 2004/10/18 15:00:49 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.content;
25
26 import java.io.Serializable JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Vector JavaDoc;
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 /**
43  * Node property class
44  *
45  * @version $Revision: 1.25.2.1 $
46  */

47 public final class NodeProperty implements Serializable JavaDoc, Cloneable JavaDoc {
48     
49     
50     // -------------------------------------------------------------- Constants
51

52     
53     public static final String JavaDoc DEFAULT_NAMESPACE = "DAV:";
54     public static final String JavaDoc SLIDE_NAMESPACE =
55         "http://jakarta.apache.org/slide/";
56     
57     protected static final String JavaDoc I_STANDARDLIVEPROPERTIESCLASS = "standardLivePropertiesClass";
58     protected static final String JavaDoc I_STANDARDLIVEPROPERTIESCLASS_DEFAULT = "org.apache.slide.webdav.util.resourcekind.AbstractResourceKind";
59     
60     /**
61      * The standard live properties.
62      */

63     public static Set JavaDoc allLiveProperties;
64
65     /**
66      * The standard protected properties.
67      */

68     public static Set JavaDoc allProtectedProperties;
69
70     /**
71      * The standard computed properties.
72      */

73     public static Set JavaDoc allComputedProperties;
74     
75     static {
76         try {
77             if (Domain.isInitialized()) {
78                 Class JavaDoc slpc = Class.forName( Domain.getParameter(I_STANDARDLIVEPROPERTIESCLASS, I_STANDARDLIVEPROPERTIESCLASS_DEFAULT) );
79                 Method JavaDoc lp = slpc.getMethod( "getAllLiveProperties", new Class JavaDoc[]{} );
80                 allLiveProperties = (Set JavaDoc)lp.invoke( null, new Object JavaDoc[]{} ); // obj=null since method is static
81
Method JavaDoc pp = slpc.getMethod( "getAllProtectedProperties", new Class JavaDoc[]{} );
82                 allProtectedProperties = (Set JavaDoc)pp.invoke( null, new Object JavaDoc[]{} ); // obj=null since method is static
83
Method JavaDoc cp = slpc.getMethod( "getAllComputedProperties", new Class JavaDoc[]{} );
84                 allComputedProperties = (Set JavaDoc)cp.invoke( null, new Object JavaDoc[]{} ); // obj=null since method is static
85
}
86         }
87         catch( Exception JavaDoc 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     // ----------------------------------------------------------- Constructors
96

97     
98     /**
99      * Constructor.
100      *
101      * @param name Name
102      * @param value Value
103      */

104     public NodeProperty(String JavaDoc name, Object JavaDoc value) {
105         setName(name);
106         setValue(value);
107         this.namespace = DEFAULT_NAMESPACE;
108         this.type = new String JavaDoc();
109         this.kind = determineKind( namespace, name );
110     }
111     
112     
113     /**
114      * Constructor.
115      *
116      * @param name Name
117      * @param value Value
118      * @param namespace Namespace
119      */

120     public NodeProperty(String JavaDoc name, Object JavaDoc value, String JavaDoc namespace) {
121         this(name, value);
122         setNamespace(namespace);
123         this.kind = determineKind( namespace, name );
124     }
125     
126     
127     /**
128      * Constructor.
129      *
130      * @param name Name
131      * @param value Value
132      * @param namespace Namespace
133      * @param type the type
134      */

135     public NodeProperty(String JavaDoc name, Object JavaDoc value, String JavaDoc namespace, String JavaDoc type) {
136         this(name, value);
137         setNamespace(namespace);
138         this.type = type;
139         this.kind = determineKind( namespace, name );
140     }
141     
142     
143     /**
144      * Constructor.
145      *
146      * @param name Name
147      * @param value Value
148      * @param protectedProperty Is the property protected?
149      */

150     public NodeProperty(String JavaDoc name, Object JavaDoc 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     /**
160      * Constructor.
161      *
162      * @param name Name
163      * @param value Value
164      * @param namespace Namespace
165      * @param type Type info
166      * @param protectedProperty Protected property
167      */

168     public NodeProperty(String JavaDoc name, Object JavaDoc value, String JavaDoc namespace,
169                         String JavaDoc 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     // ----------------------------------------------------- Instance Variables
181

182     
183     /**
184      * Property name.
185      */

186     private String JavaDoc name;
187     
188     
189     /**
190      * Namespace of the property.
191      */

192     private String JavaDoc namespace;
193     
194     
195     /**
196      * Property value.
197      */

198     private Object JavaDoc value;
199     
200     
201     /**
202      * Value Type. If the value is stored as a String representation of the
203      * value, the type field is used.
204      * FIXME : Remove that
205      */

206     private String JavaDoc type;
207     
208     
209     /** The kind of property: dead, live, protected, computed */
210     private Kind kind = Kind.DEAD;
211     
212     
213     /**
214      * Permission list.
215      */

216     private Vector JavaDoc permissions = null;
217     
218     
219     // ------------------------------------------------------------- Properties
220

221     /**
222      * Determine the kind of the property given by the specified namespace and name.
223      */

224     private static Kind determineKind( String JavaDoc namespace, String JavaDoc 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     /**
239      * Kind accessor.
240      *
241      * @return true, if this is a dead property
242      */

243     public boolean isDeadProperty() {
244         return( (this.kind == Kind.DEAD) );
245     }
246     
247     
248     /**
249      * Kind accessor.
250      *
251      * @return true, if this is a computed (live) property
252      */

253     public boolean isComputed() {
254         return( (this.kind == Kind.COMPUTED) );
255     }
256     
257     
258     /**
259      * Kind accessor.
260      *
261      * @return true, if this is a protected (live) property
262      */

263     public boolean isProtected() {
264         return(
265                   (this.kind == Kind.PROTECTED) || (this.kind == Kind.COMPUTED) );
266     }
267     
268     
269     /**
270      * Kind accessor.
271      *
272      * @return true, if this is a live property
273      */

274     public boolean isLiveProperty() {
275         return(
276                   (this.kind == Kind.LIVE) || (this.kind == Kind.PROTECTED) || (this.kind == Kind.COMPUTED) );
277     }
278     
279     
280     /**
281      * Property name accessor.
282      *
283      * @return String property name
284      */

285     public String JavaDoc getName() {
286         return this.name;
287     }
288     
289     
290     /**
291      * Property name mutator.
292      *
293      * @param name Name
294      */

295     void setName(String JavaDoc name) {
296         if (name == null) {
297             this.name = new String JavaDoc();
298         } else {
299             this.name = name;
300         }
301     }
302     
303     
304     /**
305      * Namespace accessor.
306      *
307      * @return String definition
308      */

309     public String JavaDoc getNamespace() {
310         return this.namespace;
311     }
312     
313     
314     /**
315      * Namespace mutator.
316      *
317      * @param namespace New namespace
318      */

319     void setNamespace(String JavaDoc namespace) {
320         if (namespace == null) {
321             this.namespace = "";
322         } else {
323             this.namespace = namespace;
324         }
325     }
326     
327     
328     /**
329      * Value accessor.
330      *
331      * @return Object value
332      */

333     public Object JavaDoc getValue() {
334         return value;
335     }
336     
337     
338     /**
339      * Value mutator.
340      *
341      * @param value Value
342      */

343     void setValue(Object JavaDoc value) {
344         if (value == null) {
345             this.value = new String JavaDoc();
346         } else {
347             this.value = value;
348         }
349     }
350     
351     
352     /**
353      * Type accessor.
354      *
355      * @return String type
356      */

357     public String JavaDoc getType() {
358         return type;
359     }
360     
361     
362     /**
363      * Type mutator.
364      *
365      * @param type Type
366      */

367     void setType(String JavaDoc type) {
368         if (type == null) {
369             this.type = new String JavaDoc();
370         } else {
371             this.type = type;
372         }
373     }
374     
375     
376     /**
377      * Kind accessor.
378      *
379      * @return the property kind (dead, live, protected, computed)
380      */

381     public Kind getKind() {
382         return kind;
383     }
384     
385     
386     /**
387      * Kind mutator.
388      *
389      * @param kind the kind
390      */

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     /**
401      * Add permission.
402      *
403      * @param permission Permission
404      */

405     public void addPermission(NodePermission permission) {
406         if (this.permissions == null) {
407             this.permissions = new Vector JavaDoc();
408         }
409         permissions.addElement(permission);
410     }
411     
412     
413     /**
414      * Remove permission.
415      *
416      * @param permission Permission to remove
417      */

418     public void removePermission(NodePermission permission) {
419         if (this.permissions != null) {
420             permissions.removeElement(permission);
421         }
422     }
423     
424     
425     /**
426      * Enumerate permissions.
427      *
428      * @return Enumeration permissions
429      */

430     public Enumeration JavaDoc enumeratePermissions() {
431         if (this.permissions != null) {
432             return permissions.elements();
433         } else {
434             return EmptyEnumeration.INSTANCE;
435         }
436     }
437     
438     
439     // --------------------------------------------------------- Object Methods
440

441     
442     /**
443      * Clone.
444      *
445      * @return Object clone
446      */

447     NodeProperty cloneObject() {
448         NodeProperty result = null;
449         try {
450             result = (NodeProperty) super.clone();
451         } catch(CloneNotSupportedException JavaDoc e) {
452         }
453         return result;
454     }
455     
456     
457     /**
458      * Hash Code.
459      *
460      * @return int Hash code value
461      */

462     public int hashCode() {
463         return getName().hashCode();
464     }
465     
466     
467     /**
468      * String representation of the permission.
469      * <p/>
470      * Format : ObjectUri-SubjectUri-ActionUri-InheritanceFlag
471      *
472      * @return String String representation
473      */

474     public String JavaDoc toString() {
475         return getNamespace()+getName()+"="+getValue();
476     }
477     
478     
479     /**
480      * Equals.
481      *
482      * @param obj Object to test
483      * @return boolean True if the two object are equal :
484      * <li>obj is of type NodeProperty and is not null</li>
485      * <li>The property names are equal</li>
486      */

487     public boolean equals(Object JavaDoc 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     /**
498      * Validate.
499      */

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     /**
519      * The kind of a property: dead, live, protected, computed
520      */

521     public static class Kind implements Serializable JavaDoc {
522         
523         private static int
524             DEAD_ID = 0,
525             LIVE_ID = 1,
526             PROTECTED_ID = 2,
527             COMPUTED_ID = 3;
528         
529         /** The discrete values */
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         /**
539          * Private constructor
540          */

541         private Kind( int id ) {
542             this.id = id;
543         }
544
545         public boolean equals(Object JavaDoc 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     /**
557      * The usage of this class avoids the creation of mutliple Namespace objects
558      * with the same URI but different prefix. Just use it as a replacement
559      * for the <code>org.jdom.Namespace</code>.
560      * It also predefines Namespace objects for the <code>DAV:</code> and
561      * the <code>http://jakarta.apache.org/slide/</code> namespace
562      * (with an appropriate prefix).
563      */

564     public static class NamespaceCache {
565         
566         /**
567          * String constant for <code>S</code>.
568          */

569         public final static String JavaDoc SLIDE_PREFIX = "S";
570         
571         /**
572          * String constant for <code>http://jakarta.apache.org/slide/</code>.
573          */

574         public final static String JavaDoc SLIDE_URI = NodeProperty.SLIDE_NAMESPACE;
575         
576         /**
577          * Namespace with {@link #SLIDE_PREFIX SLIDE_PREFIX} and
578          * {@link #SLIDE_URI SLIDE_URI}.
579          */

580         public final static Namespace SLIDE_NAMESPACE = getNamespace(SLIDE_PREFIX, SLIDE_URI);;
581         
582         /**
583          * String constant for <code>D</code>.
584          */

585         public final static String JavaDoc DEFAULT_PREFIX = "D";
586         
587         /**
588          * String constant for <code>DAV:</code>.
589          */

590         public final static String JavaDoc DEFAULT_URI = NodeProperty.DEFAULT_NAMESPACE;
591         
592         /**
593          * Namespace with {@link #DEFAULT_PREFIX DEFAULT_PREFIX} and
594          * {@link #DEFAULT_URI DEFAULT_URI}.
595          */

596         public final static Namespace DEFAULT_NAMESPACE = getNamespace(DEFAULT_PREFIX, DEFAULT_URI);;
597         
598         /**
599          * Maps the namespace' URI to the Namespace object.
600          */

601         protected static Map JavaDoc namespaceMap;
602         
603         
604         /**
605          * Returns the Namespace for the given <code>uri</code>.
606          * If there is already an entry in the cache for this URI,
607          * this Namespace will be returned.
608          * Otherwise a new Namespace with the prefix <code>""</code>
609          * (default namespace) will be created and put into the cache.
610          *
611          * @param uri the URI for which to return the Namespace.
612          *
613          * @return the Namespace for the given URI.
614          */

615         public static Namespace getNamespace(String JavaDoc uri) {
616             return getNamespace("", uri);
617         }
618         
619         /**
620          * Returns the Namespace for the given <code>prefix</code> and
621          * <code>uri</code>.
622          * If there is already an entry in the cache for this URI,
623          * this Namespace will be returned.
624          * Otherwise a new Namespace with the given <code>prefix</code>
625          * and <code>uri</code> will be created and put into the cache.
626          *
627          * @param prefix the prefix for which to return the Namespace.
628          * @param uri the URI for which to return the Namespace.
629          *
630          * @return the Namespace for the given URI.
631          */

632         public static Namespace getNamespace(String JavaDoc prefix, String JavaDoc 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         /**
643          * Returns the {@link #namespaceMap namespaceMap}.
644          *
645          * @return the {@link #namespaceMap namespaceMap}.
646          */

647         protected static Map JavaDoc getNamespaceMap() {
648             if (namespaceMap == null) {
649                 namespaceMap = new HashMap JavaDoc();
650             }
651             return namespaceMap;
652         }
653     }
654     
655 }
656
Popular Tags