1 package com.ibm.webdav; 2 3 17 import java.util.Vector ; 18 import java.util.Enumeration ; 19 import java.io.StringWriter ; 20 import java.io.StringReader ; 21 import java.io.IOException ; 22 import java.io.StreamTokenizer ; 23 24 36 public class Precondition 37 { 38 private Vector conditions = new Vector (); 39 41 public Precondition() 42 { 43 } 44 48 public Precondition(String ifHeader) throws WebDAVException 49 { 50 StreamTokenizer tokenizer = new StreamTokenizer (new StringReader (ifHeader)); 51 tokenizer.wordChars('!', '/'); 53 tokenizer.wordChars(':', '@'); 54 tokenizer.wordChars('[', '_'); 55 tokenizer.ordinaryChar('('); 56 tokenizer.ordinaryChar(')'); 57 tokenizer.ordinaryChar('<'); 58 tokenizer.ordinaryChar('>'); 59 tokenizer.ordinaryChar('['); 60 tokenizer.ordinaryChar(']'); 61 62 tokenizer.quoteChar('"'); 63 64 int token = 0; 65 try { 66 token = tokenizer.nextToken(); 67 switch (token) { 68 case '<': { 69 while (token == '<') { 70 addCondition(Condition.create(tokenizer)); 71 token = tokenizer.ttype; 72 } 73 break; 74 } 75 case '(': { 76 while (token == '(') { 77 addCondition(Condition.create(tokenizer)); 78 token = tokenizer.ttype; 79 } 80 break; 81 } 82 } 83 if (token != StreamTokenizer.TT_EOF) { 84 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: "+(char)token+" expected: EOF"); 85 } 86 if (!getConditions().hasMoreElements()) { 87 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in If header: list is empty: "+ifHeader); 88 } 89 } catch (IOException exc) { 90 } 91 } 92 97 public void addCondition(Condition condition) throws WebDAVException { 98 Enumeration conditions = getConditions(); 100 if (condition.getResourceURI() != null) { 101 while (conditions.hasMoreElements()) { 102 Condition existingCondition = (Condition) conditions.nextElement(); 103 if (existingCondition.getResourceURI() != null && existingCondition.getResourceURI().equals(condition.getResourceURI())) { 104 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, condition.getResourceURI() + " cannot be specified more than once in an If header"); 105 } 106 } 107 } 108 this.conditions.addElement(condition); 109 } 110 119 public void addStateTokenCondition(String resourceURI, String stateToken) throws WebDAVException { 120 Condition condition = new Condition(resourceURI); 121 ConditionTerm term = new ConditionTerm(); 122 term.addConditionFactor(new StateToken(stateToken)); 123 condition.addConditionTerm(term); 124 addCondition(condition); 125 } 126 132 public static Precondition create(String ifHeader) throws WebDAVException { 133 return new Precondition(ifHeader); 134 } 135 139 public Enumeration getConditions() { 140 return conditions.elements(); 141 } 142 146 public boolean matches(Condition condition) { 147 boolean match = false; 148 Enumeration conditions = getConditions(); 149 while (!match && conditions.hasMoreElements()) { 150 Condition existingCondition = (Condition) conditions.nextElement(); 151 match = existingCondition.matches(condition); 152 } 153 return match; 154 } 155 159 public String toString() { 160 StringWriter os = new StringWriter (); 161 Enumeration conditions = getConditions(); 162 while (conditions.hasMoreElements()) { 163 Condition condition = (Condition) conditions.nextElement(); 164 os.write(condition.toString()); 165 if (conditions.hasMoreElements()) { 166 os.write(' '); 167 } 168 } 169 try { 170 os.close(); 171 } catch (Exception exc) { 172 } 173 return os.toString(); 174 } 175 } 176 | Popular Tags |