KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > smb > SmbFileNameParser


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.smb;
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.FileNameParser;
22 import org.apache.commons.vfs.provider.URLFileNameParser;
23 import org.apache.commons.vfs.provider.UriParser;
24 import org.apache.commons.vfs.provider.VfsComponentContext;
25
26 /**
27  * Implementation for sftp. set default port to 139
28  */

29 public class SmbFileNameParser extends URLFileNameParser
30 {
31     private final static SmbFileNameParser INSTANCE = new SmbFileNameParser();
32
33     public SmbFileNameParser()
34     {
35         super(139);
36     }
37
38     public static FileNameParser getInstance()
39     {
40         return INSTANCE;
41     }
42
43     public FileName parseUri(final VfsComponentContext context, FileName base, final String JavaDoc filename) throws FileSystemException
44     {
45         final StringBuffer JavaDoc name = new StringBuffer JavaDoc();
46
47         String JavaDoc actualFilename = filename;
48
49         if (actualFilename.equals("smb://") || actualFilename.equals("smb:///")) {
50           //return new SmbMasterBrowserFileName();
51
actualFilename = "smb://_master_/_browser_/";
52         }
53         // Extract the scheme and authority parts
54
final Authority auth = extractToPath(actualFilename, name);
55
56         // extract domain
57
String JavaDoc username = auth.userName;
58         String JavaDoc domain = extractDomain(username);
59         if (domain != null) {
60           username = username.substring(domain.length() + 1);
61         }
62
63         // Decode and adjust separators
64
UriParser.canonicalizePath(name, 0, name.length(), this);
65         UriParser.fixSeparators(name);
66
67         // Extract the share
68
String JavaDoc share = UriParser.extractFirstElement(name);
69         if (share == null || share.length() == 0) {
70           share = "";
71           //throw new FileSystemException("vfs.provider.smb/missing-share-name.error",
72
// actualFilename);
73
}
74
75         // Normalise the path. Do this after extracting the share name,
76
// to deal with things like smb://hostname/share/..
77
FileType fileType = UriParser.normalisePath(name);
78         final String JavaDoc path = name.toString();
79
80         return new SmbFileName(
81             auth.scheme,
82             auth.hostName,
83             auth.port,
84             username,
85             auth.password,
86             domain,
87             share,
88             path,
89             fileType);
90
91     }
92
93     private String JavaDoc extractDomain(String JavaDoc username)
94     {
95         if (username == null)
96         {
97             return null;
98         }
99
100         for (int i = 0; i < username.length(); i++)
101         {
102             if (username.charAt(i) == '\\')
103             {
104                 return username.substring(0, i);
105             }
106         }
107
108         return null;
109     }
110 }
111
Popular Tags