KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.commons.vfs.provider;
2
3 import org.apache.commons.httpclient.URIException;
4 import org.apache.commons.httpclient.util.URIUtil;
5 import org.apache.commons.vfs.FileName;
6 import org.apache.commons.vfs.FileSystemException;
7 import org.apache.commons.vfs.FileType;
8
9 public class URLFileName extends GenericFileName
10 {
11     private final String JavaDoc queryString;
12
13     public URLFileName(final String JavaDoc scheme,
14                        final String JavaDoc hostName,
15                        final int port,
16                        final int defaultPort,
17                        final String JavaDoc userName,
18                        final String JavaDoc password,
19                        final String JavaDoc path,
20                        final FileType type,
21                        final String JavaDoc queryString)
22     {
23         super(scheme, hostName, port, defaultPort, userName, password, path, type);
24         this.queryString = queryString;
25     }
26
27     /**
28      * get the query string
29      *
30      * @return the query string part of the filename
31      */

32     public String JavaDoc getQueryString()
33     {
34         return queryString;
35     }
36
37     /**
38      * get the path and query string e.g. /path/servlet?param1=true
39      *
40      * @return the path and its query string
41      */

42     public String JavaDoc getPathQuery()
43     {
44         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(250);
45         sb.append(getPath());
46         sb.append("?");
47         sb.append(getQueryString());
48
49         return sb.toString();
50     }
51
52     /**
53      * get the path encoded suitable for url like filesystem e.g. (http, webdav)
54      *
55      * @param charset the charset used for the path encoding
56      */

57     public String JavaDoc getPathQueryEncoded(String JavaDoc charset) throws URIException, FileSystemException
58     {
59         if (getQueryString() == null)
60         {
61             if (charset != null)
62             {
63                 return URIUtil.encodePath(getPathDecoded(), charset);
64             }
65             else
66             {
67                 return URIUtil.encodePath(getPathDecoded());
68             }
69         }
70
71         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(250);
72         if (charset != null)
73         {
74             sb.append(URIUtil.encodePath(getPathDecoded(), charset));
75         }
76         else
77         {
78             sb.append(URIUtil.encodePath(getPathDecoded()));
79         }
80         sb.append("?");
81         sb.append(getQueryString());
82         return sb.toString();
83     }
84
85     public FileName createName(final String JavaDoc absPath, FileType type)
86     {
87         return new URLFileName(getScheme(),
88             getHostName(),
89             getPort(),
90             getDefaultPort(),
91             getUserName(),
92             getPassword(),
93             absPath,
94             type,
95             getQueryString());
96     }
97
98     /**
99      * append query string to the uri
100      *
101      * @return the uri
102      */

103     protected String JavaDoc createURI()
104     {
105         if (getQueryString() != null)
106         {
107             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(250);
108             sb.append(super.createURI());
109             sb.append("?");
110             sb.append(getQueryString());
111
112             return sb.toString();
113         }
114
115         return super.createURI();
116     }
117
118     public String JavaDoc getURIEncoded(String JavaDoc charset) throws FileSystemException, URIException
119     {
120         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
121         appendRootUri(sb);
122         sb.append(getPathQueryEncoded(charset));
123         return sb.toString();
124     }
125 }
126
Popular Tags