1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.local;
18
19 import org.apache.commons.vfs2.FileName;
20 import org.apache.commons.vfs2.FileSystemException;
21 import org.apache.commons.vfs2.FileType;
22
23
24
25
26 public class WindowsFileNameParser extends LocalFileNameParser {
27
28 @Override
29 protected FileName createFileName(final String scheme, final String rootFile, final String path,
30 final FileType type) {
31 return new WindowsFileName(scheme, rootFile, path, type);
32 }
33
34
35
36
37 private String extractDrivePrefix(final StringBuilder name) {
38
39 if (name.length() < 3) {
40
41 return null;
42 }
43 final char ch = name.charAt(0);
44 if (ch == '/' || ch == ':') {
45
46 return null;
47 }
48 if (name.charAt(1) != ':') {
49
50 return null;
51 }
52 if (name.charAt(2) != '/') {
53
54 return null;
55 }
56
57 final String prefix = name.substring(0, 2);
58 name.delete(0, 2);
59
60 return prefix.intern();
61 }
62
63
64
65
66 @Override
67 protected String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException {
68 return extractWindowsRootPrefix(uri, name);
69 }
70
71
72
73
74 private String extractUNCPrefix(final String uri, final StringBuilder name) throws FileSystemException {
75
76
77
78 final int maxpos = name.length();
79 int pos = 0;
80 for (; pos < maxpos && name.charAt(pos) != '/'; pos++) {
81
82 }
83 pos++;
84 if (pos >= maxpos) {
85 throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
86 }
87
88
89 final int startShareName = pos;
90 for (; pos < maxpos && name.charAt(pos) != '/'; pos++) {
91
92 }
93 if (pos == startShareName) {
94 throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
95 }
96
97
98 final String prefix = name.substring(0, pos);
99 name.delete(0, pos);
100 return prefix;
101 }
102
103
104
105
106 private String extractWindowsRootPrefix(final String uri, final StringBuilder name) throws FileSystemException {
107
108
109
110
111
112 int startPos = 0;
113 final int maxlen = Math.min(4, name.length());
114 for (; startPos < maxlen && name.charAt(startPos) == '/'; startPos++) {
115
116 }
117 if (startPos == maxlen && name.length() > (startPos + 1) && name.charAt(startPos + 1) == '/') {
118
119 throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
120 }
121 name.delete(0, startPos);
122
123
124 final String driveName = extractDrivePrefix(name);
125 if (driveName != null) {
126 return driveName;
127 }
128
129
130 if (startPos < 2) {
131 throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
132 }
133
134 return "//" + extractUNCPrefix(uri, name);
135 }
136 }