KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileType;
20
21 /**
22  * A file name that represents a 'generic' URI, as per RFC 2396. Consists of
23  * a scheme, userinfo (typically username and password), hostname, port, and
24  * path.
25  *
26  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27  * @version $Revision: 1.5 $ $Date: 2007/02/12 14:08:04 $
28  */

29 public class GenericFileName extends AbstractFileName
30 {
31     private final String JavaDoc userName;
32     private final String JavaDoc hostName;
33     private final int defaultPort;
34     private final String JavaDoc password;
35     private final int port;
36     private static final char[] RESERVED = {':', '@', '/', '?', '[', ']', '#'};
37     //private static final char[] PASSWORD_RESERVED = {'@', '/'};
38

39     protected GenericFileName(final String JavaDoc scheme,
40                               final String JavaDoc hostName,
41                               final int port,
42                               final int defaultPort,
43                               final String JavaDoc userName,
44                               final String JavaDoc password,
45                               final String JavaDoc path,
46                               final FileType type
47     )
48     {
49         super(scheme, path, type);
50         this.hostName = hostName;
51         this.defaultPort = defaultPort;
52         this.password = password;
53         this.userName = userName;
54         if (port > 0)
55         {
56             this.port = port;
57         }
58         else
59         {
60             this.port = getDefaultPort();
61         }
62     }
63
64     /**
65      * Returns the user name part of this name.
66      */

67     public String JavaDoc getUserName()
68     {
69         return userName;
70     }
71
72     /**
73      * Returns the password part of this name.
74      */

75     public String JavaDoc getPassword()
76     {
77         return password;
78     }
79
80     /**
81      * Returns the host name part of this name.
82      */

83     public String JavaDoc getHostName()
84     {
85         return hostName;
86     }
87
88     /**
89      * Returns the port part of this name.
90      */

91     public int getPort()
92     {
93         return port;
94     }
95
96     /**
97      * Returns the default port for this file name.
98      */

99     public int getDefaultPort()
100     {
101         return defaultPort;
102     }
103
104     public FileName createName(String JavaDoc absPath, FileType type)
105     {
106         return new GenericFileName(
107             getScheme(),
108             hostName,
109             port,
110             defaultPort,
111             userName,
112             password,
113             absPath,
114             type);
115     }
116
117     /**
118      * Builds the root URI for this file name.
119      */

120     protected void appendRootUri(final StringBuffer JavaDoc buffer)
121     {
122         buffer.append(getScheme());
123         buffer.append("://");
124         appendCredentials(buffer);
125         buffer.append(hostName);
126         if (port != getDefaultPort())
127         {
128             buffer.append(':');
129             buffer.append(port);
130         }
131     }
132
133     /**
134      * append the user credentials
135      */

136     protected void appendCredentials(StringBuffer JavaDoc buffer)
137     {
138         if (userName != null && userName.length() != 0)
139         {
140             UriParser.appendEncoded(buffer, userName, RESERVED);
141             if (password != null && password.length() != 0)
142             {
143                 buffer.append(':');
144                 UriParser.appendEncoded(buffer, password, RESERVED);
145             }
146             buffer.append('@');
147         }
148     }
149 }
150
Popular Tags