1 23 24 package org.apache.slide.search.basic; 25 26 import java.net.URL ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 import org.apache.slide.content.NodeProperty; 32 import org.apache.slide.search.BadGatewayException; 33 import org.apache.slide.search.BadQueryException; 34 import org.apache.slide.search.QueryScope; 35 import org.jdom.Element; 36 import org.jdom.Namespace; 37 38 39 44 public class BasicQueryScope implements QueryScope { 45 46 private static Namespace slideNamespace = 47 NodeProperty.NamespaceCache.getNamespace (NodeProperty.SLIDE_NAMESPACE); 48 49 50 private String href; 51 52 53 private int depth; 54 55 56 private boolean isCollection = true; 57 58 59 private Set excludedScopes = new HashSet (); 60 61 62 private Set includeSet = new HashSet (); 63 private Set excludeSet = new HashSet (); 64 65 70 74 79 public BasicQueryScope (String href, int depth, Set includeSet, Set excludeSet) { 80 this.href = href; 81 this.depth = depth; 82 this.includeSet = includeSet; 83 this.excludeSet = excludeSet; 84 } 85 86 91 public BasicQueryScope (Element fromElement) 92 throws BadQueryException 93 { 94 this.depth = QueryScope.DEPTH_INFINITY; 95 96 Namespace namespace = fromElement.getNamespace(); 97 String name = fromElement.getName (); 98 99 if (! (namespace.getURI().equals (NodeProperty.DEFAULT_NAMESPACE) 100 && name.equals (Literals.FROM))) 101 throw new BadQueryException ("expected DAV:from"); 102 103 Element scope = fromElement.getChild (Literals.SCOPE, namespace); 104 105 if (scope.getChildren (Literals.HREF, namespace).size() != 1) { 106 throw new BadQueryException ("exactly one href element must be defined"); 107 } 108 109 href = scope.getChildTextTrim (Literals.HREF, namespace); 110 111 try { 112 URL url = new URL (href); 113 throw new BadGatewayException (href + ": Bad Gateway (no server redirection allowed)"); 114 } 115 catch (java.net.MalformedURLException e) {} 117 118 121 Element depth = scope.getChild (Literals.DEPTH, namespace); 122 if (depth != null) { 123 String d = depth.getTextTrim(); 124 if (d.equals ("0")) 125 this.depth = 0; 126 else if (d.equals ("1")) 127 this.depth = 1; 128 } 129 130 Iterator it = scope.getChildren (Literals.EXCLUDE, slideNamespace).iterator(); 131 while (it.hasNext()) { 132 Element excluded = (Element)it.next(); 133 String href = excluded.getText(); 134 excludedScopes.add (href); 135 } 136 137 it = scope.getChildren (Literals.INCLUDE_LASTPATHSEGEMENT, namespace).iterator(); 138 while (it.hasNext()) { 139 Element incl = (Element)it.next(); 140 String pattern = incl.getText(); 141 includeSet.add (pattern); 142 } 143 144 it = scope.getChildren (Literals.EXCLUDE_LASTPATHSEGEMENT, namespace).iterator(); 145 while (it.hasNext()) { 146 Element incl = (Element)it.next(); 147 String pattern = incl.getText(); 148 excludeSet.add (pattern); 149 } 150 151 } 152 153 154 160 public String getHref() { 161 return href; 162 } 163 164 170 public int getDepth() { 171 return depth; 172 } 173 174 175 182 public boolean equals (Object o) { 183 184 if (! (o instanceof QueryScope)) 185 return false; 186 187 QueryScope other = (QueryScope)o; 188 if (!href.equals (other.getHref())) 189 return false; 190 191 if (! (depth == other.getDepth())) 192 return false; 193 194 return true; 195 } 196 197 202 public String toString () { 203 return href + ", depth = " + depth; 204 } 205 206 public void setIsCollection(boolean isCollection) { 207 this.isCollection = isCollection; 208 if (isCollection == true && !href.endsWith("/")) 209 href = href + "/"; 210 } 211 212 public boolean isCollection() { 213 return isCollection; 214 } 215 216 217 223 public Set getExcludedScopes() { 224 return excludedScopes; 225 } 226 227 232 public Set getIncludeSet() { 233 return includeSet; 234 } 235 236 241 public Set getExcludeSet() { 242 return excludeSet; 243 } 244 245 246 } 247 | Popular Tags |