1 28 29 package com.caucho.servlets.webdav; 30 31 import com.caucho.server.webapp.Application; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.vfs.Path; 34 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.SAXException ; 37 38 import javax.servlet.ServletContext ; 39 import javax.servlet.http.HttpServletRequest ; 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 45 48 public class XmlApplicationPath extends ApplicationPath { 49 private Path _root; 50 private HashMap _map = new HashMap (); 51 52 public XmlApplicationPath() 53 { 54 } 55 56 59 public void setRoot(Path path) 60 { 61 _root = path; 62 } 63 64 67 public Path getRoot() 68 { 69 return _root; 70 } 71 72 79 public boolean remove(String path, 80 HttpServletRequest request, 81 ServletContext app) 82 throws IOException 83 { 84 removeAttributes(path); 85 86 return super.remove(path, request, app); 87 } 88 89 97 public Iterator getAttributeNames(String path, 98 HttpServletRequest request, 99 ServletContext app) 100 throws IOException 101 { 102 FileAttributes attrs = getAttributes(path); 103 104 if (attrs != null) 105 return attrs.getAttributeNames(); 106 else 107 return null; 108 } 109 110 118 public String getAttribute(AttributeName name, 119 String path, 120 HttpServletRequest request, 121 ServletContext app) 122 throws IOException 123 { 124 FileAttributes attrs = getAttributes(path); 125 126 if (attrs != null) 127 return attrs.getAttribute(name); 128 else 129 return null; 130 } 131 132 143 public boolean setAttribute(AttributeName name, String value, 144 String path, 145 HttpServletRequest request, 146 ServletContext app) 147 throws IOException 148 { 149 FileAttributes attrs = getAttributes(path); 150 151 if (attrs != null) 152 return attrs.setAttribute(name, value); 153 else 154 return false; 155 } 156 157 165 public boolean removeAttribute(AttributeName name, 166 String path, 167 HttpServletRequest request, 168 ServletContext app) 169 throws IOException 170 { 171 FileAttributes attrs = getAttributes(path); 172 173 if (attrs != null) 174 attrs.removeAttribute(name); 175 176 return true; 177 } 178 179 protected FileAttributes getAttributes(String path) 180 { 181 FileAttributes attrs = (FileAttributes) _map.get(path); 182 183 if (attrs == null) { 184 attrs = new FileAttributes(); 185 _map.put(path, attrs); 186 } 187 188 return attrs; 189 } 190 191 protected void removeAttributes(String path) 192 { 193 _map.remove(path); 194 } 195 196 201 public String []list(String path, 202 HttpServletRequest request, 203 ServletContext app) 204 throws IOException 205 { 206 ArrayList filteredList = new ArrayList (); 207 208 String []names = getPath(path, request, app).list(); 209 210 for (int i = 0; i < names.length; i++) { 211 if (! names[i].startsWith(".")) 212 filteredList.add(names[i]); 213 } 214 215 return (String []) filteredList.toArray(new String [filteredList.size()]); 216 } 217 218 221 protected Path getPath(String path, 222 HttpServletRequest request, 223 ServletContext app) 224 throws IOException 225 { 226 Path appDir = ((Application) app).getAppDir(); 227 228 if (_root != null) 229 appDir = _root; 230 231 Path filePath = appDir.lookup("./" + path); 232 String tail = filePath.getTail(); 233 234 if (tail.startsWith(".")) 235 return filePath.getParent().lookup(".bogus"); 236 else 237 return filePath; 238 } 239 240 public static class FileAttributes { 241 HashMap attributes = new HashMap (); 242 243 247 public Iterator getAttributeNames() 248 throws IOException 249 { 250 return attributes.keySet().iterator(); 251 } 252 253 256 public String getAttribute(AttributeName name) 257 throws IOException 258 { 259 return (String ) attributes.get(name); 260 } 261 262 270 public boolean setAttribute(AttributeName name, String value) 271 { 272 attributes.put(name, value); 273 274 return true; 275 } 276 277 282 public void removeAttribute(AttributeName name) 283 throws IOException 284 { 285 attributes.remove(name); 286 } 287 } 288 289 static class AttributeHandler extends org.xml.sax.helpers.DefaultHandler { 290 HashMap fileMap = new HashMap (); 291 AttributeName attributeName; 292 String fileName; 293 CharBuffer value; 294 295 boolean inHref; 296 FileAttributes fileAttributes; 297 298 HashMap getFileMap() 299 { 300 return fileMap; 301 } 302 303 public void startElement(String uri, String localName, 304 String qName, Attributes attributes) 305 { 306 if (localName.equals("file")) { 307 fileName = null; 308 fileAttributes = new FileAttributes(); 309 } 310 else if (localName.equals("href")) { 311 inHref = true; 312 value = CharBuffer.allocate(); 313 } 314 else if (attributeName == null) { 315 attributeName = new AttributeName(uri, localName, qName); 316 value = CharBuffer.allocate(); 317 } 318 } 319 320 public void characters(char []buffer, int offset, int length) 321 { 322 if (value != null) 323 value.append(buffer, offset, length); 324 } 325 326 public void endElement (String uri, String localName, String qName) 327 throws SAXException 328 { 329 if (localName.equals("file")) { 330 if (fileName != null) 331 fileMap.put(fileName, fileAttributes); 332 fileName = null; 333 fileAttributes = null; 334 } 335 else if (localName.equals("href")) { 336 fileName = value.close(); 337 value = null; 338 } 339 else if (attributeName == null) { 340 } 341 else if (localName.equals(attributeName.getLocal()) && 342 uri.equals(attributeName.getNamespace())) { 343 fileAttributes.setAttribute(attributeName, value.close()); 344 345 value = null; 346 attributeName = null; 347 } 348 } 349 } 350 } 351 | Popular Tags |