1 package com.ibm.webdav; 2 3 15 import java.util.Vector ; 16 import java.util.Enumeration ; 17 import java.io.StringWriter ; 18 import java.io.StreamTokenizer ; 19 import java.io.IOException ; 20 21 33 public class ConditionTerm { 34 35 private Vector conditionFactors = new Vector (); 36 38 public ConditionTerm() { 39 } 40 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 54 public boolean contains(ConditionFactor factor) { 55 return conditionFactors.contains(factor); 56 } 57 64 public static ConditionTerm create(StreamTokenizer 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 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 95 public Enumeration getConditionFactors() { 96 return conditionFactors.elements(); 97 } 98 103 public boolean matches(ConditionTerm conditionTerm) { 104 int numberOfItemsToMatch = 0; 105 boolean match = true; 106 Enumeration 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 122 public int numberOfFactors() { 123 return conditionFactors.size(); 124 } 125 129 public String toString() { 130 StringWriter os = new StringWriter (); 131 os.write('('); 132 Enumeration 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 exc) { 144 } 145 return os.toString(); 146 } 147 } 148 | Popular Tags |