KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > LayeredFileNameParser


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;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileType;
21
22 /**
23  * Implementation for layered filesystems.
24  * <p/>
25  * Additionally encodes the '!' character.
26  */

27 public class LayeredFileNameParser extends AbstractFileNameParser
28 {
29     private final static LayeredFileNameParser INSTANCE = new LayeredFileNameParser();
30
31     public static LayeredFileNameParser getInstance()
32     {
33         return INSTANCE;
34     }
35
36     public boolean encodeCharacter(char ch)
37     {
38         return super.encodeCharacter(ch) || ch == '!';
39     }
40
41     public FileName parseUri(final VfsComponentContext context, FileName base, final String JavaDoc filename) throws FileSystemException
42     {
43         final StringBuffer JavaDoc name = new StringBuffer JavaDoc();
44
45         // Extract the scheme
46
final String JavaDoc scheme = UriParser.extractScheme(filename, name);
47
48         // Extract the Layered file URI
49
final String JavaDoc rootUriName = extractRootName(name);
50         FileName rootUri = null;
51         if (rootUriName != null)
52         {
53             rootUri = context.parseURI(rootUriName);
54         }
55
56         // Decode and normalise the path
57
UriParser.canonicalizePath(name, 0, name.length(), this);
58         UriParser.fixSeparators(name);
59         FileType fileType = UriParser.normalisePath(name);
60         final String JavaDoc path = name.toString();
61
62         return new LayeredFileName(scheme, rootUri, path, fileType);
63     }
64
65     /**
66      * Pops the root prefix off a URI, which has had the scheme removed.
67      */

68     protected String JavaDoc extractRootName(final StringBuffer JavaDoc uri)
69         throws FileSystemException
70     {
71         // Looking for <name>!<abspath> (staring at the end)
72
int maxlen = uri.length();
73         int pos = maxlen - 1;
74         for (; pos > 0 && uri.charAt(pos) != '!'; pos--)
75         {
76         }
77
78         if (pos == 0 && uri.charAt(pos) != '!')
79         {
80             // not ! found, so take the whole path a root
81
// e.g. zip:/my/zip/file.zip
82
pos = maxlen;
83         }
84
85         // Extract the name
86
String JavaDoc prefix = uri.substring(0, pos);
87         if (pos < maxlen)
88         {
89             uri.delete(0, pos + 1);
90         }
91         else
92         {
93             uri.setLength(0);
94         }
95
96         return prefix;
97     }
98
99 }
100
Popular Tags