View Javadoc

1   package fr.in2p3.jsaga;
2   
3   import org.ogf.saga.url.URL;
4   import org.ogf.saga.url.URLFactory;
5   
6   /* ***************************************************
7   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
8   * ***             http://cc.in2p3.fr/             ***
9   * ***************************************************
10  * File:   uri
11  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
12  * Date:   1 juil. 2007
13  * ***************************************************
14  * Description:                                      */
15  /**
16   *
17   */
18  public class uri {
19      private static final String JSAGA_FACTORY = Base.getSagaFactory();
20  
21      public static String protocol(String uri) throws Exception {
22          return URLFactory.createURL(JSAGA_FACTORY, uri).getScheme();
23      }
24  
25      public static String host(String uri) throws Exception {
26          String host = URLFactory.createURL(JSAGA_FACTORY, uri).getHost();
27          if (host != null) {
28              return host;
29          } else {
30              return "localhost";
31          }
32      }
33  
34      public static String basedirURI(String uri) throws Exception {
35          URL u = URLFactory.createURL(JSAGA_FACTORY, uri);
36          return new java.net.URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), _basedirPath(u), null, null).toString();
37      }
38  
39      public static String basedirPath(String uri) throws Exception {
40          return _basedirPath(URLFactory.createURL(JSAGA_FACTORY, uri));
41      }
42  
43      public static String filename(String uri) throws Exception {
44          return _filename(URLFactory.createURL(JSAGA_FACTORY, uri));
45      }
46  
47      public static String context(String uri) throws Exception {
48          return URLFactory.createURL(JSAGA_FACTORY, uri).getFragment();
49      }
50  
51      public static boolean isDirectory(String uri) throws Exception {
52          return URLFactory.createURL(JSAGA_FACTORY, uri).getPath().endsWith("/");
53      }
54  
55      public static boolean isRelative(String uri) throws Exception {
56          return URLFactory.createURL(JSAGA_FACTORY, uri).getPath().startsWith("/");
57      }
58  
59      private static String _basedirPath(URL uri) throws Exception {
60          String[] array = uri.getPath().split("/");
61          if (array.length > 0) {
62              StringBuffer buffer = new StringBuffer();
63              // root
64              if (array[0].equals("")) {
65                  buffer.append('/');
66              }
67              // baseDir
68              for (int i=0; i<array.length-1; i++) {
69                  if (!array[i].equals("")) {
70                      buffer.append(array[i]);
71                      buffer.append('/');
72                  }
73              }
74              return buffer.toString();
75          } else {
76              return "/";
77          }
78      }
79  
80      private static String _filename(URL uri) throws Exception {
81          String[] array = uri.getPath().split("/");
82          if (array.length > 0) {
83              return array[array.length-1];
84          } else {
85              return "";
86          }
87      }
88  }