001: package com.ibm.webdav;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017:
018: /** This class represents the universal name for a
019: * property.
020: * @author Jason Crawford <ccjason@us.ibm.com>
021: */
022: public class PropertyName {
023:
024: protected String ns = null;
025: protected String local = null;
026:
027: public static final PropertyName pnResourcetype = createPropertyNameQuietly("DAV:resourcetype");
028: public static final PropertyName pnCreationdate = createPropertyNameQuietly("DAV:creationdate");
029: public static final PropertyName pnSupportedlock = createPropertyNameQuietly("DAV:supportedlock");
030: public static final PropertyName pnContenttype = createPropertyNameQuietly("DAV:getcontenttype");
031: public static final PropertyName pnGetlastmodified = createPropertyNameQuietly("DAV:getlastmodified");
032: public static final PropertyName pnGetcontentlength = createPropertyNameQuietly("DAV:getcontentlength");
033: public static final PropertyName pnLockdiscovery = createPropertyNameQuietly("DAV:lockdiscovery");
034:
035: /**
036: * PropertyName constructor.
037: * @param tag a tag whose name will be used as the property name of
038: * constructed PropertyName.
039: */
040: public PropertyName(org.w3c.dom.Element tag) {
041: // (jlc 4/4/99) we dont' check for valid local names
042: // because if
043: // it was already in a tag, it must be okay. Perhaps
044: // we should declare that we throw a
045: // InvalidPropertyNameException as a place holder
046: // for possibly having stricter checks down the
047: // road? It's tempting.
048:
049: local = tag.getLocalName();
050: ns = tag.getNamespaceURI();
051:
052: }
053:
054: /**
055: * PropertyName constructor. This constructor will only
056: * work for property names in the "DAV:" namespace and
057: * the tag parameter must begin with "DAV:".
058: * @param tag the property name. ie. DAV:resourcetype
059: */
060: public PropertyName(String tag) throws InvalidPropertyNameException {
061: if (!tag.startsWith("DAV:")) {
062: throw new InvalidPropertyNameException(
063: "Invalid tag parameter: " + tag);
064: } else {
065: tag = tag.substring(4);
066: }
067: local = tag;
068: ns = "DAV:";
069: }
070:
071: /**
072: * PropertyName constructor.
073: * @param tag is the local property name. ie "resourcetype"
074: * @param namespace is the namespace string. ie "DAV:"
075: */
076: public PropertyName(String namespace, String tag)
077: throws InvalidPropertyNameException {
078: ns = namespace;
079: local = tag;
080: if (local.indexOf(':') >= 0) {
081: throw new InvalidPropertyNameException("local part is "
082: + tag);
083: }
084: }
085:
086: /**
087: * Returns a combined namespace+local string.
088: * @return java.lang.String
089: */
090: public String asExpandedString() {
091: return ns + ":" + local;
092: }
093:
094: /**
095: * This factory takes a propertyname string. Unlike
096: * the constructor with the same argument signature,
097: * this method will throw a runtime exception in
098: * situations where the contructor would throw
099: * an InvalidPropertyNameException. This is
100: * important because runtime exceptions don't have
101: * to be declared. This can be convenient if a
102: * caller knows that their provided property name
103: * is correct (by the definition of the PropertyName(String)
104: * constructor) and don't want to sprinkle exception
105: * declarations all over their code... or
106: * add try/catch blocks... when they
107: * know an InvalidPropertyNameException will never be thrown.
108: * @return com.ibm.webdav.PropertyName
109: * @param propname java.lang.String - See the documentation
110: * for the PropertyName( String) constructor for info.
111: */
112: public static PropertyName createPropertyNameQuietly(String propname) {
113: PropertyName res;
114: try {
115: res = new PropertyName(propname);
116: } catch (InvalidPropertyNameException excc) {
117: // we catch this so that we don't have to
118: // declare it in the callers. If it
119: // occurs we have an internal problem.
120: throw new RuntimeException("internal error: bad property: "
121: + propname);
122: }
123: return res;
124: }
125:
126: /**
127: * Compares two objects for equality. Returns a boolean that indicates
128: * whether this object is equivalent to the specified object. This method
129: * is used when an object is stored in a hashtable.
130: * <p>
131: * In the case of this class, if the contatenated ns+local string names
132: * are equal, then the PropertyNames are equal.
133: * <p>
134: * It should be noted that this method can take a String as a
135: * parameter and match on it. That equality relationship is
136: * not communative though since the String.equals(Object) method
137: * will never return true if passed a PropertyName.
138: *
139: * @param obj the Object to compare with
140: * @return true if these Objects are equal; false otherwise.
141: * @see java.util.Hashtable
142: */
143: public boolean equals(Object obj) {
144: String s1 = ns + local;
145: if (obj instanceof PropertyName) {
146: PropertyName param1 = (PropertyName) obj;
147: String s2 = param1.ns + param1.local;
148: return s1.equals(s2);
149: } else if (obj instanceof String) {
150: String param1 = (String) obj;
151: return s1.equals(param1);
152: }
153: return false;
154: }
155:
156: /**
157: * Returns a the local portion of a property name. For
158: * example it would return "resourcetype" if the PropertyName
159: * was representing a property name for DAV:resourcetype.
160: * @return a string representation of the local property name
161: * without any prefix or namespace.
162: */
163: public String getLocal() {
164: return local;
165: }
166:
167: /**
168: * Returns a the namespace of the PropertyName.
169: * @return a string representation of the namespace of a property name.
170: * For example "DAV:".
171: */
172: public String getNamespace() {
173: return ns;
174: }
175:
176: /**
177: * Generates a hash code for the receiver.
178: * This method is supported primarily for
179: * hash tables, such as those provided in java.util.
180: * @return an integer hash code for the receiver
181: * @see java.util.Hashtable
182: */
183: public int hashCode() {
184: // Insert code to generate a hash code for the receiver here.
185: // This implementation forwards the message to super. You may replace or supplement this.
186: // NOTE: if two objects are equal (equals(Object) returns true) they must have the same hash code
187: String join = ns + local;
188: return join.hashCode();
189: }
190:
191: /**
192: * Returns a consise human readable string describing the property name.
193: * @return java.lang.String
194: */
195: public String toString() {
196: return asExpandedString();
197: }
198: }
|