01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: import org.apache.commons.vfs.provider.AbstractFileNameParser;
23: import org.apache.commons.vfs.provider.UriParser;
24: import org.apache.commons.vfs.provider.VfsComponentContext;
25:
26: /**
27: * A name parser.
28: *
29: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
30: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
31: */
32: public abstract class LocalFileNameParser extends
33: AbstractFileNameParser {
34: /**
35: * Determines if a name is an absolute file name.
36: */
37: public boolean isAbsoluteName(final String name) {
38: // TODO - this is yucky
39: StringBuffer b = new StringBuffer(name);
40: try {
41: UriParser.fixSeparators(b);
42: extractRootPrefix(name, b);
43: return true;
44: } catch (FileSystemException e) {
45: return false;
46: }
47: }
48:
49: /**
50: * Pops the root prefix off a URI, which has had the scheme removed.
51: */
52: protected abstract String extractRootPrefix(final String uri,
53: final StringBuffer name) throws FileSystemException;
54:
55: public FileName parseUri(final VfsComponentContext context,
56: FileName base, final String filename)
57: throws FileSystemException {
58: final StringBuffer name = new StringBuffer();
59:
60: // Extract the scheme
61: String scheme = UriParser.extractScheme(filename, name);
62: if (scheme == null) {
63: scheme = "file";
64: }
65:
66: // Remove encoding, and adjust the separators
67: UriParser.canonicalizePath(name, 0, name.length(), this );
68:
69: UriParser.fixSeparators(name);
70:
71: // Extract the root prefix
72: final String rootFile = extractRootPrefix(filename, name);
73:
74: // Normalise the path
75: FileType fileType = UriParser.normalisePath(name);
76:
77: final String path = name.toString();
78:
79: return createFileName(scheme, rootFile, path, fileType);
80: }
81:
82: protected abstract FileName createFileName(String scheme,
83: final String rootFile, final String path,
84: final FileType type);
85: }
|