View Javadoc

1   package fr.in2p3.jsaga.impl.url;
2   
3   import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
4   import org.ogf.saga.error.*;
5   import org.ogf.saga.url.URL;
6   
7   /* ***************************************************
8   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
9   * ***             http://cc.in2p3.fr/             ***
10  * ***************************************************
11  * File:   URLHelper
12  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
13  * Date:   29 oct. 2007
14  * ***************************************************
15  * Description:                                      */
16  /**
17   *
18   */
19  public class URLHelper {
20  	
21  	private static String[] localSchemes={"file","zip"};
22  	
23      public static boolean isDirectory(URL url) throws NotImplementedException {
24          return isDirectory(url.getPath());
25      }
26      public static boolean isDirectory(String path) throws NotImplementedException {
27          return path.endsWith("/") || path.endsWith("/.") || path.endsWith("/..") || path.equals(".") || path.equals("..");
28      }
29  
30      public static URL toFileURL(URL url) throws NotImplementedException, BadParameterException {
31          if (isDirectory(url)) {
32              throw new BadParameterException("File URL must not end with slash: "+url);
33          }
34          return url;
35      }
36      public static URL toDirectoryURL(URL url) throws NotImplementedException, BadParameterException {
37          String path = url.getPath();
38          if (!path.endsWith("/")) {
39              url.setPath(path+"/");
40          }
41          return url;
42      }
43      public static String toFilePath(String path) throws NotImplementedException, BadParameterException {
44          if (isDirectory(path)) {
45              throw new BadParameterException("File path must not end with slash: "+path);
46          }
47          return path;
48      }
49  
50      public static String toDirectoryPath(String path) throws NotImplementedException, BadParameterException {
51          if (!path.endsWith("/")) {
52              return path+"/";
53          } else {
54              return path;
55          }
56      }
57  
58      public static URL createURL(URL base, URL relativePath) throws NotImplementedException, BadParameterException, NoSuccessException {
59          // check URL
60          boolean isDir = base.getPath().endsWith("/");
61          boolean isAbsolute = relativePath.getPath().startsWith("/");
62          if (!isDir && !isAbsolute) {
63              throw new BadParameterException("INTERNAL ERROR: path must be relative to a directory: "+base);
64          }
65          URL url;
66          url = base.resolve(relativePath);
67          if ( ((AbstractURLImpl)relativePath).hasCache() ) {
68              FileAttributes cache = ((AbstractURLImpl)relativePath).getCache();
69              ((AbstractURLImpl)url).setCache(cache);
70          }
71          return url;
72      }
73  
74      public static URL createURL(URL base, String name) throws NotImplementedException, BadParameterException, NoSuccessException {
75      	return createURL(base, new RelativeURLImpl(name));
76      }
77  
78      public static URL getParentURL(URL base) throws NotImplementedException, BadParameterException, NoSuccessException {
79          // get parent directory
80          String parent = (base.getPath().endsWith("/") ? ".." : ".");
81          // resolve
82          return base.resolve(new RelativeURLImpl(parent));
83      }
84  
85      public static String getName(URL url) throws NotImplementedException {
86          String[] names = url.getPath().split("/");
87          String name;
88          if (names.length > 0) {
89              name = names[names.length-1];
90              if (name.equals("")) {
91                  name = null;
92              }
93          } else {
94              name = "/";
95          }
96          return name;
97      }
98      
99      public static boolean startsWithLocalScheme(String url) {
100     	if (url == null || url == "") {
101     		return false;
102     	}
103     	for (String scheme : localSchemes) {
104     		if (url.startsWith(scheme+":/")) return true;
105     	}
106     	return false;
107     }
108 
109 }