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.StreamTokenizer;
016: import java.io.IOException;
017:
018: /** A ConditionFactor represents some state of a resource that must be
019: * satisfied in order for the associated request to be valid. The ConditionFactors in
020: * a ConditionTerm must all match with states of the resource, i.e., they are AND'ed
021: * together. Conditions are contained in a Precondition which is used in a
022: * WebDAV If header. ConditionFactors are either constructed by the client, or may
023: * have been given to the client in a previous method request. A ConditionFactor can
024: * be either a StateToken or an EntityTag as defined by section 9.4 of the WebDAV
025: * spec.
026: * @author Jim Amsden <jamsden@us.ibm.com>
027: * @see com.ibm.webdav.Precondition
028: * @see com.ibm.webdav.ConditionFactor
029: * @see com.ibm.webdav.ConditionTerm
030: * @see com.ibm.webdav.EntityTag
031: * @see com.ibm.webdav.StateToken
032: */
033: public abstract class ConditionFactor {
034: // ------------------------------------------------------------------------
035:
036: private boolean not_ = false;
037:
038: /** Create a ConditionFactor (either a StateToken or EntityTag) by parsing
039: * the tokenizer contining an If header value.
040: * @param tokenizer a StreamTokenizer containing the contents of a state token or entity tag
041: * from a WebDAV If header
042: * @return the parsed ConditionFactor
043: * @exception com.ibm.webdav.WebDAVException thrown if there is a syntax error in the If header
044: */
045: public static ConditionFactor create(StreamTokenizer tokenizer)
046: throws WebDAVException {
047: boolean not = false;
048: ConditionFactor factor = null;
049: try {
050: int token = tokenizer.ttype;
051: if (token == StreamTokenizer.TT_WORD) {
052: if (tokenizer.sval.equalsIgnoreCase("Not")) {
053: not = true;
054: } else {
055: throw new WebDAVException(
056: WebDAVStatus.SC_BAD_REQUEST,
057: "Error parsing If header: expected Not");
058: }
059: token = tokenizer.nextToken();
060: }
061: switch (token) {
062: case '<': {
063: factor = StateToken.create(tokenizer);
064: break;
065: }
066: case '[': {
067: factor = EntityTag.create(tokenizer);
068: break;
069: }
070: default: {
071: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
072: "Error parsing If header: saw: " + (char) token
073: + " expected: < or [");
074: }
075: }
076: } catch (IOException exc) {
077: }
078: factor.setNot(not);
079: return factor;
080: }
081:
082: /** Negate the comparison on this ConditionFactor?
083: @return true if the condition factor was negated in the If header
084: */
085: public boolean not() {
086: return not_;
087: }
088:
089: /** Set how to compare to this ConditionFactor. Value is true implies match for
090: * a valid request, false implies the request is valid only if the ConditionFactor
091: * doesn't match.
092: * @param value true means negate the condition
093: */
094: public void setNot(boolean value) {
095: not_ = value;
096: }
097:
098: /** Return a String representation of this ConditionFactor as defined by the If
099: * header in section 9.4 of the WebDAV spec.
100: * @return a string representation of a state token or entity tag
101: */
102: public abstract String toString();
103: }
|