KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 /**
23  * A parser for Windows file names.
24  *
25  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
26  */

27 public class WindowsFileNameParser
28     extends LocalFileNameParser
29 {
30     /**
31      * Pops the root prefix off a URI, which has had the scheme removed.
32      */

33     protected String JavaDoc extractRootPrefix(final String JavaDoc uri,
34                                        final StringBuffer JavaDoc name)
35         throws FileSystemException
36     {
37         return extractWindowsRootPrefix(uri, name);
38     }
39
40     protected FileName createFileName(String JavaDoc scheme, final String JavaDoc rootFile, final String JavaDoc path, final FileType type)
41     {
42         return new WindowsFileName(scheme, rootFile, path, type);
43     }
44
45     /**
46      * Extracts a Windows root prefix from a name.
47      */

48     private String JavaDoc extractWindowsRootPrefix(final String JavaDoc uri,
49                                             final StringBuffer JavaDoc name)
50         throws FileSystemException
51     {
52         // Looking for:
53
// ('/'){0, 3} <letter> ':' '/'
54
// ['/'] '//' <name> '/' <name> ( '/' | <end> )
55

56         // Skip over first 4 (unc) leading '/' chars
57
int startPos = 0;
58         int maxlen = Math.min(4, name.length());
59         for (; startPos < maxlen && name.charAt(startPos) == '/'; startPos++)
60         {
61         }
62         if (startPos == maxlen && name.length() > startPos && name.charAt(startPos + 1) == '/')
63         {
64             // Too many '/'
65
throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
66         }
67         name.delete(0, startPos);
68
69         // Look for drive name
70
String JavaDoc driveName = extractDrivePrefix(name);
71         if (driveName != null)
72         {
73             return driveName;
74         }
75
76         // Look for UNC name
77
if (startPos < 2)
78         {
79             throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
80         }
81
82         return "//" + extractUNCPrefix(uri, name);
83     }
84
85     /**
86      * Extracts a drive prefix from a path. Leading '/' chars have been removed.
87      */

88     private String JavaDoc extractDrivePrefix(final StringBuffer JavaDoc name)
89     {
90         // Looking for <letter> ':' '/'
91
if (name.length() < 3)
92         {
93             // Too short
94
return null;
95         }
96         char ch = name.charAt(0);
97         if (ch == '/' || ch == ':')
98         {
99             // Missing drive letter
100
return null;
101         }
102         if (name.charAt(1) != ':')
103         {
104             // Missing ':'
105
return null;
106         }
107         if (name.charAt(2) != '/')
108         {
109             // Missing separator
110
return null;
111         }
112
113         String JavaDoc prefix = name.substring(0, 2);
114         name.delete(0, 2);
115         return prefix;
116     }
117
118     /**
119      * Extracts a UNC name from a path. Leading '/' chars have been removed.
120      */

121     private String JavaDoc extractUNCPrefix(final String JavaDoc uri,
122                                     final StringBuffer JavaDoc name)
123         throws FileSystemException
124     {
125         // Looking for <name> '/' <name> ( '/' | <end> )
126

127         // Look for first separator
128
int maxpos = name.length();
129         int pos = 0;
130         for (; pos < maxpos && name.charAt(pos) != '/'; pos++)
131         {
132         }
133         pos++;
134         if (pos >= maxpos)
135         {
136             throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
137         }
138
139         // Now have <name> '/'
140
int startShareName = pos;
141         for (; pos < maxpos && name.charAt(pos) != '/'; pos++)
142         {
143         }
144         if (pos == startShareName)
145         {
146             throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
147         }
148
149         // Now have <name> '/' <name> ( '/' | <end> )
150
String JavaDoc prefix = name.substring(0, pos);
151         name.delete(0, pos);
152         return prefix;
153     }
154 }
155
Popular Tags