1 package com.ibm.webdav; 2 3 15 import java.io.*; 16 import java.util.*; 17 18 30 public class Condition { 31 32 private String uri = null; 33 private Vector conditionTerms = new Vector(); 34 36 public Condition() { 37 } 38 41 public Condition(String uri) { 42 this.uri = uri; 43 } 44 47 public void addConditionTerm(ConditionTerm term) throws WebDAVException { 48 conditionTerms.addElement(term); 49 } 50 54 public boolean contains(ConditionTerm term) { 55 boolean match = false; 57 Enumeration terms = getConditionTerms(); 58 while (!match && terms.hasMoreElements()) { 59 ConditionTerm t = (ConditionTerm) terms.nextElement(); 60 match = term.matches(t); 61 } 62 return match; 63 } 64 70 public static Condition create(StreamTokenizer tokenizer) throws WebDAVException { 71 Condition condition = new Condition(); 72 try { 73 int token = tokenizer.ttype; 74 if (token == '<') { 75 token = tokenizer.nextToken(); 76 if (token == StreamTokenizer.TT_WORD) { 77 condition.setResourceURI(tokenizer.sval); 78 } else { 79 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: missing resource URI"); 80 } 81 token = tokenizer.nextToken(); 82 if (token == '>') { 83 token = tokenizer.nextToken(); 84 } else { 85 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: >"); 86 } 87 } 88 if (token == '(') { 89 while (token == '(') { 90 condition.addConditionTerm(ConditionTerm.create(tokenizer)); 91 token = tokenizer.ttype; 92 } 93 } else { 94 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: ( or <"); 95 } 96 } catch (IOException exc) { 97 } 98 return condition; 99 } 100 107 public static Condition create(String ifHeader) throws WebDAVException { 108 StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(ifHeader)); 109 tokenizer.wordChars('!', '/'); 111 tokenizer.wordChars(':', '@'); 112 tokenizer.ordinaryChar('('); 113 tokenizer.ordinaryChar(')'); 114 tokenizer.ordinaryChar('<'); 115 tokenizer.ordinaryChar('>'); 116 tokenizer.ordinaryChar('['); 117 tokenizer.ordinaryChar(']'); 118 tokenizer.quoteChar('"'); 119 Condition condition = null; 120 try { 121 int token = tokenizer.nextToken(); 122 condition = Condition.create(tokenizer); 123 token = tokenizer.ttype; 124 if (token != StreamTokenizer.TT_EOF) { 125 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: EOF"); 126 } 127 } catch (IOException exc) { 128 } 129 return condition; 130 } 131 136 public Enumeration getConditionTerms() { 137 return conditionTerms.elements(); 138 } 139 144 public String getResourceURI() { 145 return uri; 146 } 147 152 public boolean matches(Condition condition) { 153 boolean match = true; 155 if (uri != null) { 156 try { 157 163 179 match = uri.equals(condition.getResourceURI()); 180 } catch (Exception exc) { 181 match = false; 182 } 183 } 184 if (!match) { 185 return false; 186 } 187 match = false; 189 Enumeration terms = getConditionTerms(); 190 while (!match && terms.hasMoreElements()) { 191 ConditionTerm term = (ConditionTerm) terms.nextElement(); 192 match = condition.contains(term); 193 } 194 return match; 195 } 196 202 public void setResourceURI(String value) { 203 uri = value; 204 } 205 209 public String toString() { 210 StringWriter os = new StringWriter(); 211 if (getResourceURI() != null) { 212 os.write('<'); 213 os.write(getResourceURI()); 214 os.write("> "); 215 } 216 Enumeration terms = getConditionTerms(); 217 while (terms.hasMoreElements()) { 218 ConditionTerm term = (ConditionTerm) terms.nextElement(); 219 os.write(term.toString()); 220 if (terms.hasMoreElements()) { 221 os.write(' '); 222 } 223 } 224 try { 225 os.close(); 226 } catch (Exception exc) { 227 } 228 return os.toString(); 229 } 230 } 231 | Popular Tags |