KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.io.StreamTokenizer JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 /** A ConditionTerm represents some state configuration of a resource that must be
22  * satisfied in order for the associated request to be valid. The ConditionFactors in
23  * a ConditionTerm must all match with states of the resource, i.e., they are AND'ed
24  * together. ConditionTerms are contained in a Condition which is used in the Precondition
25  * of a WebDAV If header.
26  * @author Jim Amsden <jamsden@us.ibm.com>
27  * @see com.ibm.webdav.Precondition
28  * @see com.ibm.webdav.ConditionFactor
29  * @see com.ibm.webdav.ConditionTerm
30  * @see com.ibm.webdav.EntityTag
31  * @see com.ibm.webdav.StateToken
32  */

33 public class ConditionTerm {
34
35    private Vector JavaDoc conditionFactors = new Vector JavaDoc();
36 /** Construct a Condition with no associated Resource URI.
37 */

38 public ConditionTerm() {
39 }
40 /** Add a ConditionFactor to a ConditionTerm.
41  * @param the factor to add
42  * @exception com.ibm.webdav.WebDAVException thrown if the term already contains the factor
43  */

44 public void addConditionFactor(ConditionFactor factor) throws WebDAVException {
45     if (conditionFactors.contains(factor)) {
46         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: duplicate entry in list");
47     }
48     conditionFactors.addElement(factor);
49 }
50 /** Does this ConditionTerm contain the given ConditionFactor?
51  * @param factor the factor to check for
52  * @return true if the term contains the given factor
53  */

54 public boolean contains(ConditionFactor factor) {
55     return conditionFactors.contains(factor);
56 }
57 /** Create a ConditionTerm by parsing the given If header as defined by
58  * section 9.4 in the WebDAV spec.
59  *
60  * @param tokenizer a StreamTokenizer on the contents of a WebDAV If header
61  * @return the parsed ConditionTerm
62  * @exception com.ibm.webdav.WebDAVException thrown if there is a syntax error in the If header
63  */

64 public static ConditionTerm create(StreamTokenizer JavaDoc tokenizer) throws WebDAVException {
65     ConditionTerm term = new ConditionTerm();
66     try {
67         int token = tokenizer.ttype;
68         if (token == '(') {
69             token = tokenizer.nextToken();
70         } else {
71             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: (");
72         }
73         while (token == StreamTokenizer.TT_WORD || token == '<' || token == '[') {
74             term.addConditionFactor(ConditionFactor.create(tokenizer));
75             token = tokenizer.ttype;
76         }
77         if (token == ')') {
78             token = tokenizer.nextToken();
79         } else {
80             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: )");
81         }
82     } catch (IOException JavaDoc exc) {
83     }
84     if (!term.getConditionFactors().hasMoreElements()) {
85         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: missing State-Token or entity-tag");
86     }
87     return term;
88 }
89 /** Get all the ConditionFactors in this Condition. The ConditionFactors in
90  * a Condition must all match with states of the resource, i.e., they are AND'ed
91  * together. ConditionTerms are contained in a Condition which is used in the
92  * Precondition of a WebDAV If header.
93  * @return an Enumeration of ConditionFactors
94  */

95 public Enumeration JavaDoc getConditionFactors() {
96     return conditionFactors.elements();
97 }
98 /** See if this ConditionTerm matches the given ConditionTerm. This is an
99  * AND operation. All the factors in the ConditionTerm must match.
100  * @param conditionTerm the term to match
101  * @return true if all the factors in the term match those in this term
102  */

103 public boolean matches(ConditionTerm conditionTerm) {
104     int numberOfItemsToMatch = 0;
105     boolean match = true;
106     Enumeration JavaDoc factors = getConditionFactors();
107     while (match && factors.hasMoreElements()) {
108         ConditionFactor factor = (ConditionFactor) factors.nextElement();
109         if (factor.not()) {
110             match = !conditionTerm.contains(factor);
111         } else {
112             match = conditionTerm.contains(factor);
113             numberOfItemsToMatch++;
114         }
115     }
116     match = match && numberOfItemsToMatch == conditionTerm.numberOfFactors();
117     return match;
118 }
119 /** Get the number of ConditionFactors in this ConditionTerm.
120  * @return the number of factors in this term
121  */

122 public int numberOfFactors() {
123     return conditionFactors.size();
124 }
125 /** Return a String representation of this ConditionTerm as defined by section 9.4
126  * of the WebDAV Spec.
127  * @return a string representation of this term
128  */

129 public String JavaDoc toString() {
130     StringWriter JavaDoc os = new StringWriter JavaDoc();
131     os.write('(');
132     Enumeration JavaDoc factors = getConditionFactors();
133     while (factors.hasMoreElements()) {
134         ConditionFactor factor = (ConditionFactor) factors.nextElement();
135         os.write(factor.toString());
136         if (factors.hasMoreElements()) {
137             os.write(' ');
138         }
139     }
140     os.write(')');
141     try {
142         os.close();
143     } catch (Exception JavaDoc exc) {
144     }
145     return os.toString();
146 }
147 }
148
Popular Tags