KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > PropertyName


1 package com.ibm.webdav;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17
18  /** This class represents the universal name for a
19  * property.
20  * @author Jason Crawford <ccjason@us.ibm.com>
21  */

22 public class PropertyName {
23
24     protected String JavaDoc ns = null;
25     protected String JavaDoc local = null;
26
27     public static final PropertyName pnResourcetype = createPropertyNameQuietly( "DAV:resourcetype" );
28     public static final PropertyName pnCreationdate = createPropertyNameQuietly( "DAV:creationdate" );
29     public static final PropertyName pnSupportedlock = createPropertyNameQuietly( "DAV:supportedlock" );
30     public static final PropertyName pnContenttype = createPropertyNameQuietly( "DAV:getcontenttype" );
31     public static final PropertyName pnGetlastmodified = createPropertyNameQuietly( "DAV:getlastmodified" );
32     public static final PropertyName pnGetcontentlength = createPropertyNameQuietly( "DAV:getcontentlength" );
33     public static final PropertyName pnLockdiscovery = createPropertyNameQuietly( "DAV:lockdiscovery" );
34
35 /**
36  * PropertyName constructor.
37  * @param tag a tag whose name will be used as the property name of
38  * constructed PropertyName.
39  */

40 public PropertyName( org.w3c.dom.Element JavaDoc tag ) {
41     // (jlc 4/4/99) we dont' check for valid local names
42
// because if
43
// it was already in a tag, it must be okay. Perhaps
44
// we should declare that we throw a
45
// InvalidPropertyNameException as a place holder
46
// for possibly having stricter checks down the
47
// road? It's tempting.
48

49     local = tag.getLocalName();
50     ns = tag.getNamespaceURI();
51
52 }
53 /**
54  * PropertyName constructor. This constructor will only
55  * work for property names in the "DAV:" namespace and
56  * the tag parameter must begin with "DAV:".
57  * @param tag the property name. ie. DAV:resourcetype
58  */

59 public PropertyName( String JavaDoc tag ) throws InvalidPropertyNameException {
60     if (!tag.startsWith("DAV:")) {
61         throw new InvalidPropertyNameException( "Invalid tag parameter: "+tag );
62     } else {
63         tag = tag.substring( 4 );
64     }
65     local = tag;
66     ns = "DAV:";
67 }
68 /**
69  * PropertyName constructor.
70  * @param tag is the local property name. ie "resourcetype"
71  * @param namespace is the namespace string. ie "DAV:"
72  */

73 public PropertyName( String JavaDoc namespace, String JavaDoc tag ) throws InvalidPropertyNameException {
74     ns = namespace;
75     local = tag;
76     if (local.indexOf(':') >= 0) {
77         throw new InvalidPropertyNameException( "local part is "+tag );
78     }
79 }
80 /**
81  * Returns a combined namespace+local string.
82  * @return java.lang.String
83  */

84 public String JavaDoc asExpandedString() {
85     return ns+":"+local;
86 }
87 /**
88  * This factory takes a propertyname string. Unlike
89  * the constructor with the same argument signature,
90  * this method will throw a runtime exception in
91  * situations where the contructor would throw
92  * an InvalidPropertyNameException. This is
93  * important because runtime exceptions don't have
94  * to be declared. This can be convenient if a
95  * caller knows that their provided property name
96  * is correct (by the definition of the PropertyName(String)
97  * constructor) and don't want to sprinkle exception
98  * declarations all over their code... or
99  * add try/catch blocks... when they
100  * know an InvalidPropertyNameException will never be thrown.
101  * @return com.ibm.webdav.PropertyName
102  * @param propname java.lang.String - See the documentation
103  * for the PropertyName( String) constructor for info.
104  */

105 public static PropertyName createPropertyNameQuietly( String JavaDoc propname ) {
106     PropertyName res;
107     try {
108         res = new PropertyName( propname );
109     } catch (InvalidPropertyNameException excc) {
110         // we catch this so that we don't have to
111
// declare it in the callers. If it
112
// occurs we have an internal problem.
113
throw new RuntimeException JavaDoc( "internal error: bad property: "+propname );
114     }
115     return res;
116 }
117 /**
118  * Compares two objects for equality. Returns a boolean that indicates
119  * whether this object is equivalent to the specified object. This method
120  * is used when an object is stored in a hashtable.
121  * <p>
122  * In the case of this class, if the contatenated ns+local string names
123  * are equal, then the PropertyNames are equal.
124  * <p>
125  * It should be noted that this method can take a String as a
126  * parameter and match on it. That equality relationship is
127  * not communative though since the String.equals(Object) method
128  * will never return true if passed a PropertyName.
129  *
130  * @param obj the Object to compare with
131  * @return true if these Objects are equal; false otherwise.
132  * @see java.util.Hashtable
133  */

134 public boolean equals( Object JavaDoc obj) {
135     String JavaDoc s1 = ns+local;
136     if (obj instanceof PropertyName) {
137         PropertyName param1 = (PropertyName)obj;
138         String JavaDoc s2 = param1.ns + param1.local;
139         return s1.equals(s2);
140     } else if (obj instanceof String JavaDoc) {
141         String JavaDoc param1 = (String JavaDoc)obj;
142         return s1.equals(param1);
143     }
144     return false;
145 }
146 /**
147  * Returns a the local portion of a property name. For
148  * example it would return "resourcetype" if the PropertyName
149  * was representing a property name for DAV:resourcetype.
150  * @return a string representation of the local property name
151  * without any prefix or namespace.
152  */

153 public String JavaDoc getLocal() {
154     return local;
155 }
156 /**
157  * Returns a the namespace of the PropertyName.
158  * @return a string representation of the namespace of a property name.
159  * For example "DAV:".
160  */

161 public String JavaDoc getNamespace() {
162     return ns;
163 }
164 /**
165  * Generates a hash code for the receiver.
166  * This method is supported primarily for
167  * hash tables, such as those provided in java.util.
168  * @return an integer hash code for the receiver
169  * @see java.util.Hashtable
170  */

171 public int hashCode() {
172     // Insert code to generate a hash code for the receiver here.
173
// This implementation forwards the message to super. You may replace or supplement this.
174
// NOTE: if two objects are equal (equals(Object) returns true) they must have the same hash code
175
String JavaDoc join = ns + local;
176     return join.hashCode();
177 }
178 /**
179  * Returns a consise human readable string describing the property name.
180  * @return java.lang.String
181  */

182 public String JavaDoc toString() {
183     return asExpandedString();
184 }
185 }
186
Popular Tags