SessionUrn: modified to scope with windows (space ... in file name. Use URI instead...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SessionUrn.java
1 package uk.ac.vamsas.client.simpleclient;
2
3 import java.io.File;
4 import java.net.MalformedURLException;
5 import java.net.URI;
6
7 import uk.ac.vamsas.client.InvalidSessionUrnException;
8
9 /**
10  * SessionUrn for simpleclient sessions:
11  * simpleclient://{Absolute path to session directory}
12  * @author jimp
13  *
14  */
15 public class SessionUrn extends uk.ac.vamsas.client.SessionUrn {
16   /**
17    * a simple client session urn prefix
18    */
19   public static final String SIMPLECLIENT="simpleclient";
20   public static String VAMSASDOCUMENT="vdoc";
21   static {
22     TYPES.put(SIMPLECLIENT, SessionUrn.class);
23     TYPES.put(SessionUrn.VAMSASDOCUMENT, SessionUrn.class);
24   }
25   
26   /**
27    * Creates a SessionUrn object from a String.
28    * The string must be a string representation of a URI
29    * @param urnString
30    * @throws InvalidSessionUrnException
31    */
32   public SessionUrn(String urnString) throws InvalidSessionUrnException
33     {
34       super();
35       this.setURN(urnString);
36     }
37   
38   
39   
40   public SessionUrn(File sessionLocation) throws MalformedURLException {
41   // TODO: LATER: implement switch to have vamsas document or simpleclient sessions for same constructor
42   //  super(SIMPLECLIENT, sessionLocation.getAbsoluteFile().toURL());
43    // super(SIMPLECLIENT, sessionLocation.getAbsoluteFile().toURL());
44     super(SIMPLECLIENT, sessionLocation.getAbsoluteFile().toURI());
45     //else
46       // super(VAMSASDOCUMENT, sessionLocation);
47   }
48   public SessionUrn(VamsasSession session) throws MalformedURLException {
49     super(SIMPLECLIENT, session.sessionDir.getAbsoluteFile().toURL());
50   }
51   
52   
53   
54   /**
55    * @see uk.ac.vamsas.client.SessionUrn#setURN(java.lang.String)
56    */
57   protected void setURN(String urnString) throws NullPointerException, IllegalArgumentException {
58     super.setURN(urnString);
59   }
60
61   /**
62    * TODO: LATER: think about this again.
63    * @return File(urn.getPath())
64    */
65   public File asFile() {
66     String path = this.urn.getRawPath();
67    
68     if ("\\".equals(File.separator))
69       {
70       //remove last separator at last position if found
71         /* not needed anymore if (path.charAt(path.length() - 1) == '/')path = path.substring(0, path.length() - 1);
72         path = path.replaceAll("/", "\\\\");
73      */
74         int index = path.indexOf(File.separator);
75         
76         if (index >-1)
77         {//separator found, keep last part of the urn - filename
78           path = path.substring(index+1, path.length());
79         }
80       }
81     /*shall we use URI or String to create the object*/
82     /* from URI : URI.create("file://path") , but first / */
83     return new File(path);
84   }
85   // TODO: add abstract 'handler' methods for resolving the URN to a particular class
86 }