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 URLFileNameParser urlFileNameParser = new URLFileNameParser(80);
35 private final GenericFileNameParser genericFileNameParser = new GenericFileNameParser();
36
37
38
39
40 public UrlFileNameParser() {
41 }
42
43
44
45
46
47
48
49 protected int countSlashes(final String fileName) {
50 int state = 0;
51 int nuofSlash = 0;
52 for (int pos = 0; pos < fileName.length(); pos++) {
53 final char c = fileName.charAt(pos);
54 if (state == 0) {
55 if (c >= 'a' && c <= 'z') {
56 continue;
57 }
58 if (c == ':') {
59 state++;
60 continue;
61 }
62 } else if (state == 1) {
63 if (c != '/') {
64 return nuofSlash;
65 }
66 nuofSlash++;
67 }
68 }
69 return nuofSlash;
70 }
71
72 @Override
73 public boolean encodeCharacter(final char ch) {
74 return super.encodeCharacter(ch) || ch == '?';
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 protected boolean isUrlBased(final FileName base, final String fileName) {
92 if (base instanceof URLFileName) {
93 return true;
94 }
95
96 return countSlashes(fileName) == 2;
97 }
98
99
100
101
102
103
104
105
106
107
108 @Override
109 public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri)
110 throws FileSystemException {
111 if (isUrlBased(base, uri)) {
112 return urlFileNameParser.parseUri(context, base, uri);
113 }
114 return genericFileNameParser.parseUri(context, base, uri);
115 }
116 }