KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > vfs > VfsURI


1 package fr.jayasoft.ivy.repository.vfs;
2
3 import fr.jayasoft.ivy.Ivy;
4
5 public class VfsURI {
6     private String JavaDoc host;
7     private String JavaDoc passwd;
8     private String JavaDoc path;
9     private String JavaDoc scheme;
10     private String JavaDoc user;
11
12     // VFS Schemes
13
static final public String JavaDoc SCHEME_CIFS = "smb";
14     static final public String JavaDoc SCHEME_FILE = "file";
15     static final public String JavaDoc SCHEME_FTP = "ftp";
16     static final public String JavaDoc SCHEME_HTTP = "http";
17     static final public String JavaDoc SCHEME_HTTPS = "https";
18     static final public String JavaDoc SCHEME_SFTP = "sftp";
19     static final public String JavaDoc SCHEME_WEBDAV = "webdav";
20     static final public String JavaDoc[] SUPPORTED_SCHEMES = new String JavaDoc[] {
21         // add other schemes here if other can be tested on your machine
22
SCHEME_FILE,
23     };
24
25     /**
26      * Create a set of valid VFS URIs for the file access protocol
27      *
28      * @param resourcePath relative path (from the base repo) to the resource to be accessed
29      * @return
30      */

31     static public VfsURI vfsURIFactory(String JavaDoc scheme, String JavaDoc resource, Ivy ivy) {
32         VfsURI vfsURI = null;
33         if (scheme.equals(SCHEME_CIFS)) {
34             vfsURI = new VfsURI(SCHEME_CIFS,
35                             ivy.getVariable(VfsTestHelper.PROP_VFS_USER_ID),
36                             ivy.getVariable(VfsTestHelper.PROP_VFS_USER_PASSWD),
37                             ivy.getVariable(VfsTestHelper.PROP_VFS_HOST),
38                             ivy.getVariable(VfsTestHelper.PROP_VFS_SAMBA_REPO) + "/" + resource);
39         } else if (scheme.equals(SCHEME_FILE)) {
40             vfsURI = new VfsURI(SCHEME_FILE,
41                              null,
42                              null,
43                              null,
44                              VfsTestHelper.CWD + "/" + VfsTestHelper.TEST_REPO_DIR + "/" + resource);
45         } else if (scheme.equals(SCHEME_FTP)) {
46             vfsURI = new VfsURI(SCHEME_FTP,
47                              ivy.getVariable(VfsTestHelper.PROP_VFS_USER_ID),
48                              ivy.getVariable(VfsTestHelper.PROP_VFS_USER_PASSWD),
49                              ivy.getVariable(VfsTestHelper.PROP_VFS_HOST),
50                              VfsTestHelper.CWD + "/" + VfsTestHelper.TEST_REPO_DIR + "/" + resource);
51         } else if (scheme.equals(SCHEME_SFTP)) {
52             vfsURI = new VfsURI(SCHEME_SFTP,
53                           ivy.getVariable(VfsTestHelper.PROP_VFS_USER_ID),
54                           ivy.getVariable(VfsTestHelper.PROP_VFS_USER_PASSWD),
55                           ivy.getVariable(VfsTestHelper.PROP_VFS_HOST),
56                           VfsTestHelper.CWD + "/" + VfsTestHelper.TEST_REPO_DIR + "/" + resource) ;
57         }
58         return vfsURI;
59     }
60     
61     /**
62      * Create a wellformed VFS resource identifier
63      *
64      * @param scheme the name of the scheme used to acces the resource
65      * @param user a user name. May be <code>null</code>
66      * @param passwd a passwd. May be <code>null</code>
67      * @param host a host identifier. May be <code>null</code>
68      * @param path a scheme spacific path to a resource
69      */

70     public VfsURI(String JavaDoc scheme, String JavaDoc user, String JavaDoc passwd, String JavaDoc host, String JavaDoc path) {
71         this.scheme = scheme.trim();
72         
73         if (user != null) {
74             this.user = user.trim();
75         } else {
76             this.user = null;
77         }
78         
79         if (passwd != null) {
80             this.passwd = passwd.trim();
81         } else {
82             this.passwd = null;
83         }
84         
85         if (host != null) {
86             this.host = host.trim();
87         } else {
88             this.host = null;
89         }
90         
91         this.path = normalizePath(path);
92     }
93     
94     /**
95      * Return a well-formed VFS Resource identifier
96      *
97      * @return <code>String<code> representing a well formed VFS resource identifier
98      */

99     public String JavaDoc getVfsURI() {
100         StringBuffer JavaDoc uri = new StringBuffer JavaDoc();
101         uri.append(this.scheme + "://");
102         
103         // not all resource identifiers include user/passwd specifiers
104
if (user != null && user.trim().length() > 0) {
105             uri.append(this.user + ":");
106             
107             if (passwd != null && passwd.trim().length() > 0) {
108                 this.passwd = passwd.trim();
109             } else {
110                 this.passwd = "";
111             }
112             uri.append(this.passwd + "@");
113         }
114         
115         // not all resource identifiers include a host specifier
116
if (host != null && host.trim().length() > 0) {
117             this.host = host.trim();
118             uri.append(this.host);
119         }
120
121         uri.append(this.path);
122         return uri.toString();
123     }
124
125     /**
126      * Convert a resource path to the format required for a VFS resource identifier
127      *
128      * @param path <code>String</code> path to the resource
129      * @return <code>String</code> representing a normalized resource path
130      */

131     private String JavaDoc normalizePath(String JavaDoc path) {
132         // all backslashes replaced with forward slashes
133
String JavaDoc normalizedPath = path.replaceAll("\\\\", "/");
134         
135         // collapse multiple instance of forward slashes to single slashes
136
normalizedPath = normalizedPath.replaceAll("//+", "/");
137         
138         // ensure that our path starts with a forward slash
139
if(! normalizedPath.startsWith("/")) {
140             normalizedPath = "/" + normalizedPath;
141         }
142         
143         return normalizedPath.trim();
144     }
145
146     public String JavaDoc toString() {
147         return getVfsURI();
148     }
149     
150     public String JavaDoc getScheme() {
151         return scheme;
152     }
153 }
154
Popular Tags