SessionUrn: added a constructor to build a SessionURN from a String
[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
6 import uk.ac.vamsas.client.InvalidSessionUrnException;
7
8 /**
9  * SessionUrn for simpleclient sessions:
10  * simpleclient://{Absolute path to session directory}
11  * @author jimp
12  *
13  */
14 public class SessionUrn extends uk.ac.vamsas.client.SessionUrn {
15   /**
16    * a simple client session urn prefix
17    */
18   public static final String SIMPLECLIENT="simpleclient";
19   public static String VAMSASDOCUMENT="vdoc";
20   static {
21     TYPES.put(SIMPLECLIENT, SessionUrn.class);
22     TYPES.put(SessionUrn.VAMSASDOCUMENT, SessionUrn.class);
23   }
24   
25   /**
26    * Creates a SessionUrn object from a String.
27    * The string must be a string representation of a URI
28    * @param urnString
29    * @throws InvalidSessionUrnException
30    */
31   public SessionUrn(String urnString) throws InvalidSessionUrnException
32     {
33       super();
34       this.setURN(urnString);
35     }
36   
37   public SessionUrn(File sessionLocation) throws MalformedURLException {
38   // TODO: LATER: implement switch to have vamsas document or simpleclient sessions for same constructor
39     super(SIMPLECLIENT, sessionLocation.getAbsoluteFile().toURL());
40     //else
41       // super(VAMSASDOCUMENT, sessionLocation);
42   }
43   public SessionUrn(VamsasSession session) throws MalformedURLException {
44     super(SIMPLECLIENT, session.sessionDir.getAbsoluteFile().toURL());
45   }
46   
47   
48   
49   /**
50    * @see uk.ac.vamsas.client.SessionUrn#setURN(java.lang.String)
51    */
52   protected void setURN(String urnString) throws NullPointerException, IllegalArgumentException {
53     super.setURN(urnString);
54   }
55
56   /**
57    * TODO: LATER: think about this again.
58    * @return File(urn.getPath())
59    */
60   public File asFile() {
61     String path = urn.getPath();
62    
63     if ("\\".equals(File.separator))
64       {
65       //remove last separator at last position if found
66         if (path.charAt(path.length() - 1) == '/')path = path.substring(0, path.length() - 1);
67         path = path.replaceAll("/", "\\\\");
68       }
69     int index = path.lastIndexOf(File.separator);
70   /*don t remember why
71     if (index >-1)
72       {//separator found, keep last part of the urn - filename
73       path = path.substring(index+1, path.length());
74       }
75     *yop
76     */
77     return new File(urn.getPath());
78   }
79   // TODO: add abstract 'handler' methods for resolving the URN to a particular class
80 }