1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.url;
18
19 import org.apache.commons.vfs2.FileName;
20 import org.apache.commons.vfs2.FileSystemException;
21 import org.apache.commons.vfs2.provider.AbstractFileNameParser;
22 import org.apache.commons.vfs2.provider.URLFileName;
23 import org.apache.commons.vfs2.provider.URLFileNameParser;
24 import org.apache.commons.vfs2.provider.VfsComponentContext;
25 import org.apache.commons.vfs2.provider.local.GenericFileNameParser;
26
27
28
29
30
31
32 public class UrlFileNameParser extends AbstractFileNameParser {
33
34 private final URLFileNameParserParser.html#URLFileNameParser">URLFileNameParser urlFileNameParser = new URLFileNameParser(80);
35 private final GenericFileNameParsermeParser.html#GenericFileNameParser">GenericFileNameParser genericFileNameParser = new GenericFileNameParser();
36
37 public UrlFileNameParser() {
38 }
39
40 @Override
41 public boolean encodeCharacter(final char ch) {
42 return super.encodeCharacter(ch) || ch == '?';
43 }
44
45
46
47
48
49
50
51
52
53
54 @Override
55 public FileName parseUri(FileNamefinal VfsComponentContext context, final FileName base, final String uri)
56 throws FileSystemException {
57 if (isUrlBased(base, uri)) {
58 return urlFileNameParser.parseUri(context, base, uri);
59 }
60 return genericFileNameParser.parseUri(context, base, uri);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 protected boolean isUrlBased(final FileName base, final String fileName) {
78 if (base instanceof URLFileName) {
79 return true;
80 }
81
82 return countSlashes(fileName) == 2;
83 }
84
85
86
87
88
89
90
91 protected int countSlashes(final String fileName) {
92 int state = 0;
93 int nuofSlash = 0;
94 for (int pos = 0; pos < fileName.length(); pos++) {
95 final char c = fileName.charAt(pos);
96 if (state == 0) {
97 if (c >= 'a' && c <= 'z') {
98 continue;
99 }
100 if (c == ':') {
101 state++;
102 continue;
103 }
104 } else if (state == 1) {
105 if (c != '/') {
106 return nuofSlash;
107 }
108 nuofSlash++;
109 }
110 }
111 return nuofSlash;
112 }
113 }