KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > local > LocalFileNameParser


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.local;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileType;
21 import org.apache.commons.vfs.provider.AbstractFileNameParser;
22 import org.apache.commons.vfs.provider.UriParser;
23 import org.apache.commons.vfs.provider.VfsComponentContext;
24
25 /**
26  * A name parser.
27  *
28  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
29  */

30 public abstract class LocalFileNameParser extends AbstractFileNameParser
31 {
32     /**
33      * Determines if a name is an absolute file name.
34      */

35     public boolean isAbsoluteName(final String JavaDoc name)
36     {
37         // TODO - this is yucky
38
StringBuffer JavaDoc b = new StringBuffer JavaDoc(name);
39         try
40         {
41             UriParser.fixSeparators(b);
42             extractRootPrefix(name, b);
43             return true;
44         }
45         catch (FileSystemException e)
46         {
47             return false;
48         }
49     }
50
51     /**
52      * Pops the root prefix off a URI, which has had the scheme removed.
53      */

54     protected abstract String JavaDoc extractRootPrefix(final String JavaDoc uri,
55                                                 final StringBuffer JavaDoc name)
56         throws FileSystemException;
57
58
59     public FileName parseUri(final VfsComponentContext context, FileName base, final String JavaDoc filename) throws FileSystemException
60     {
61         final StringBuffer JavaDoc name = new StringBuffer JavaDoc();
62
63         // Extract the scheme
64
String JavaDoc scheme = UriParser.extractScheme(filename, name);
65         if (scheme == null)
66         {
67             scheme = "file";
68         }
69
70         // Remove encoding, and adjust the separators
71
UriParser.canonicalizePath(name, 0, name.length(), this);
72
73         UriParser.fixSeparators(name);
74         
75         // Extract the root prefix
76
final String JavaDoc rootFile = extractRootPrefix(filename, name);
77
78         // Normalise the path
79
FileType fileType = UriParser.normalisePath(name);
80
81         final String JavaDoc path = name.toString();
82
83         return createFileName(
84             scheme,
85             rootFile,
86             path,
87             fileType);
88     }
89
90     protected abstract FileName createFileName(String JavaDoc scheme, final String JavaDoc rootFile, final String JavaDoc path, final FileType type);
91 }
92
Popular Tags