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 java.net.URI;
20
21 import org.apache.commons.vfs2.FileName;
22 import org.apache.commons.vfs2.FileSystemException;
23 import org.apache.commons.vfs2.FileType;
24 import org.apache.commons.vfs2.provider.AbstractFileNameParser;
25 import org.apache.commons.vfs2.provider.UriParser;
26 import org.apache.commons.vfs2.provider.VfsComponentContext;
27
28
29
30
31 public abstract class LocalFileNameParser extends AbstractFileNameParser {
32
33 protected abstract FileName createFileName(String scheme, final String rootFile, final String path,
34 final FileType type);
35
36
37
38
39
40
41
42
43
44 protected abstract String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException;
45
46 private String[] getSchemes(final VfsComponentContext context, final FileName base, final String uri) {
47 if (context == null) {
48 return new String[] { base != null ? base.getScheme() : URI.create(uri).getScheme() };
49 }
50 return context.getFileSystemManager().getSchemes();
51 }
52
53
54
55
56
57
58
59 public boolean isAbsoluteName(final String name) {
60
61 final StringBuilder b = new StringBuilder(name);
62 try {
63 UriParser.fixSeparators(b);
64 extractRootPrefix(name, b);
65 return true;
66 } catch (final FileSystemException e) {
67 return false;
68 }
69 }
70
71 @Override
72 public FileName parseUri(FileNamefinal VfsComponentContext context, final FileName base, final String uri)
73 throws FileSystemException {
74 final StringBuilder nameBuilder = new StringBuilder();
75
76
77 String scheme = UriParser.extractScheme(getSchemes(context, base, uri), uri, nameBuilder);
78 if (scheme == null && base != null) {
79 scheme = base.getScheme();
80 }
81 if (scheme == null) {
82 scheme = "file";
83 }
84
85
86 UriParser.canonicalizePath(nameBuilder, 0, nameBuilder.length(), this);
87
88 UriParser.fixSeparators(nameBuilder);
89
90
91 final String rootFile = extractRootPrefix(uri, nameBuilder);
92
93
94 final FileType fileType = UriParser.normalisePath(nameBuilder);
95
96 final String path = nameBuilder.toString();
97
98 return createFileName(scheme, rootFile, path, fileType);
99 }
100 }