1 16 package org.apache.commons.vfs.provider.url; 17 18 import org.apache.commons.vfs.FileName; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.provider.AbstractFileNameParser; 21 import org.apache.commons.vfs.provider.URLFileName; 22 import org.apache.commons.vfs.provider.URLFileNameParser; 23 import org.apache.commons.vfs.provider.VfsComponentContext; 24 import org.apache.commons.vfs.provider.local.GenericFileNameParser; 25 26 32 public class UrlFileNameParser extends AbstractFileNameParser 33 { 34 private URLFileNameParser url = new URLFileNameParser(80); 35 private GenericFileNameParser generic = new GenericFileNameParser(); 36 37 public UrlFileNameParser() 38 { 39 super(); 40 } 41 42 public boolean encodeCharacter(char ch) 43 { 44 return super.encodeCharacter(ch) || ch == '?'; 45 } 46 47 public FileName parseUri(final VfsComponentContext context, final FileName base, final String filename) throws FileSystemException 48 { 49 if (isUrlBased(base, filename)) 50 { 51 return url.parseUri(context, base, filename); 52 } 53 54 return generic.parseUri(context, base, filename); 55 } 56 57 64 protected boolean isUrlBased(final FileName base, final String filename) 65 { 66 if (base instanceof URLFileName) 67 { 68 return true; 69 } 70 71 int nuofSlash = countSlashes(filename); 72 return nuofSlash == 2; 73 } 74 75 81 protected int countSlashes(final String filename) 82 { 83 int state = 0; 84 int nuofSlash = 0; 85 for (int pos = 0; pos<filename.length(); pos++) 86 { 87 char c = filename.charAt(pos); 88 if (state == 0) 89 { 90 if (c >= 'a' && c <= 'z') 91 { 92 continue; 93 } 94 if (c == ':') 95 { 96 state++; 97 continue; 98 } 99 } 100 else if (state == 1) 101 { 102 if (c == '/') 103 { 104 nuofSlash++; 105 } 106 else 107 { 108 return nuofSlash; 109 } 110 } 111 } 112 return nuofSlash; 113 } 114 } 115 | Popular Tags |