1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider.local;
18
19 import org.apache.commons.vfs.FileName;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.FileType;
22
23 /***
24 * A parser for Windows file names.
25 *
26 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
28 */
29 public class WindowsFileNameParser
30 extends LocalFileNameParser
31 {
32 /***
33 * Pops the root prefix off a URI, which has had the scheme removed.
34 */
35 protected String extractRootPrefix(final String uri,
36 final StringBuffer name)
37 throws FileSystemException
38 {
39 return extractWindowsRootPrefix(uri, name);
40 }
41
42 protected FileName createFileName(String scheme, final String rootFile, final String path, final FileType type)
43 {
44 return new WindowsFileName(scheme, rootFile, path, type);
45 }
46
47 /***
48 * Extracts a Windows root prefix from a name.
49 */
50 private String extractWindowsRootPrefix(final String uri,
51 final StringBuffer name)
52 throws FileSystemException
53 {
54
55
56
57
58
59 int startPos = 0;
60 int maxlen = Math.min(4, name.length());
61 for (; startPos < maxlen && name.charAt(startPos) == '/'; startPos++)
62 {
63 }
64 if (startPos == maxlen && name.length() > startPos && name.charAt(startPos + 1) == '/')
65 {
66
67 throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
68 }
69 name.delete(0, startPos);
70
71
72 String driveName = extractDrivePrefix(name);
73 if (driveName != null)
74 {
75 return driveName;
76 }
77
78
79 if (startPos < 2)
80 {
81 throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
82 }
83
84 return "//" + extractUNCPrefix(uri, name);
85 }
86
87 /***
88 * Extracts a drive prefix from a path. Leading '/' chars have been removed.
89 */
90 private String extractDrivePrefix(final StringBuffer name)
91 {
92
93 if (name.length() < 3)
94 {
95
96 return null;
97 }
98 char ch = name.charAt(0);
99 if (ch == '/' || ch == ':')
100 {
101
102 return null;
103 }
104 if (name.charAt(1) != ':')
105 {
106
107 return null;
108 }
109 if (name.charAt(2) != '/')
110 {
111
112 return null;
113 }
114
115 String prefix = name.substring(0, 2);
116 name.delete(0, 2);
117
118 return prefix.intern();
119 }
120
121 /***
122 * Extracts a UNC name from a path. Leading '/' chars have been removed.
123 */
124 private String extractUNCPrefix(final String uri,
125 final StringBuffer name)
126 throws FileSystemException
127 {
128
129
130
131 int maxpos = name.length();
132 int pos = 0;
133 for (; pos < maxpos && name.charAt(pos) != '/'; pos++)
134 {
135 }
136 pos++;
137 if (pos >= maxpos)
138 {
139 throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
140 }
141
142
143 int startShareName = pos;
144 for (; pos < maxpos && name.charAt(pos) != '/'; pos++)
145 {
146 }
147 if (pos == startShareName)
148 {
149 throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
150 }
151
152
153 String prefix = name.substring(0, pos);
154 name.delete(0, pos);
155 return prefix;
156 }
157 }