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: import java.io.*;
016:
017: /** A StateToken is a ConditionFactor describing some state of a resource represented
018: * as a URI. A typical example would be the WebDAV lock token.
019: * @author Jim Amsden <jamsden@us.ibm.com>
020: * @see com.ibm.webdav.Precondition
021: * @see com.ibm.webdav.ConditionTerm
022: * @see com.ibm.webdav.ConditionFactor
023: * @see com.ibm.webdav.EntityTag
024: * @see com.ibm.webdav.StateToken
025: */
026: public class StateToken extends ConditionFactor {
027: private String uri = null; // represents some state of a resource expressed as a URI
028:
029: /** Construct a StateToken. Should never be called.
030: */
031: private StateToken() {
032: }
033:
034: /** Construct a StateToken with the given URI.
035: * @param uri the URI of the state token
036: */
037: public StateToken(String uri) {
038: this .uri = uri;
039: }
040:
041: /** Create a StateToken by parsing the given If header as defined by
042: * section 9.4 in the WebDAV spec.
043: *
044: * @param tokenizer a StreamTokenizer on the contents of a WebDAV If header
045: * @return the parsed ConditionFactor (StateToken)
046: */
047: public static ConditionFactor create(StreamTokenizer tokenizer)
048: throws WebDAVException {
049: StateToken stateToken = new StateToken();
050: try {
051: int token = tokenizer.ttype;
052: if (token == '<') {
053: token = tokenizer.nextToken();
054: } else {
055: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
056: "Error parsing If header: saw: " + (char) token
057: + " expected: <");
058: }
059: if (token == StreamTokenizer.TT_WORD) {
060: stateToken.setURI(tokenizer.sval);
061: token = tokenizer.nextToken();
062: } else {
063: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
064: "Error parsing If header: saw: " + (char) token
065: + " expected a URI");
066: }
067: if (token == '>') {
068: token = tokenizer.nextToken();
069: } else {
070: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
071: "Error parsing If header: saw: " + (char) token
072: + " expected: >");
073: }
074: } catch (IOException exc) {
075: }
076: return stateToken;
077: }
078:
079: /** Compare with another StateToken.
080: * @param factor the state token to compare with
081: * @return true if this state token has the same URI as the factor
082: */
083: public boolean equals(Object factor) {
084: return factor != null && factor instanceof StateToken
085: && getURI().equals(((StateToken) factor).getURI());
086: }
087:
088: /** Get the URI of this StateToken. The URI represents some state of the
089: * resource in the containing Condition, for example, the lock token.
090: * @return the URI for this state token
091: */
092: public String getURI() {
093: return uri;
094: }
095:
096: /** Set the URI of this StateToken. The URI represents some state of the
097: * resource in the containing Condition, for example, the lock token.
098: * @param value the URI for this state token
099: */
100: public void setURI(String value) {
101: uri = value;
102: }
103:
104: /** Return a String representation of this StateToken as defined by the If
105: * header in section 9.4 of the WebDAV spec.
106: * @return a string representation of this state token
107: */
108: public String toString() {
109: StringWriter os = new StringWriter();
110: if (not()) {
111: os.write("Not ");
112: }
113: os.write('<');
114: os.write(getURI());
115: os.write('>');
116: try {
117: os.close();
118: } catch (Exception exc) {
119: }
120: return os.toString();
121: }
122: }
|