66b73c33d85e299bb0032e05d59788c062249cb9
[vamsas.git] / src / uk / ac / vamsas / client / SessionUrn.java
1 /**
2  * 
3  */
4 package uk.ac.vamsas.client;
5
6 import java.net.URI;
7 import java.util.Hashtable;
8
9 /**
10  * @author jimp
11  * base class for vamsas session/document types
12  * uses java.net.URI internally for construction of URN
13  */
14 public abstract class SessionUrn {
15   protected URI urn;
16   /**
17    * The types of URI protocols we understand
18    */
19   protected static final Hashtable TYPES=new Hashtable();
20   
21   
22   protected SessionUrn()
23   {
24     //
25   }
26   /**
27    * construct urn for a locally stored session file
28    * @param type
29    * @param url
30    */
31   protected SessionUrn(String type, java.net.URL url) {
32     if (!TYPES.containsKey(type.toLowerCase()))
33       throw new Error("Unknown "+this.getClass().getName()+" type '"+type+"' for URL '"+url+"'");
34     try {
35       
36         this.setURN(type+"://"+url.getPath());
37     //  urn = URI.create(type+"://"+url.getPath());
38     } catch (Exception e) {
39       // TODO: something better than throwing an error should be done here.
40       e.printStackTrace(System.err);
41       throw new Error(e);
42     }
43   }
44   
45   protected SessionUrn(String type, URI uri) {
46     if (!TYPES.containsKey(type.toLowerCase()))
47       throw new Error("Unknown "+this.getClass().getName()+" type '"+type+"' for URI '"+uri+"'");
48     try {
49       //  this.setURN(type+"://"+uri.getPath());
50       //bad hack but should do the trick
51       this.setURN(type+"://"+uri.getRawPath());
52    } catch (Exception e) {
53       // TODO: something better than throwing an error should be done here.
54       e.printStackTrace(System.err);
55       throw new Error(e);
56     }
57   }
58   
59   public String getSessionUrn() {
60     return this.urn.toString();
61   }
62   
63   /**
64    * Set the urn attribute
65    * create a URI from the provided String
66    * 
67    * @param urnString urn to convert to a URN
68    */
69   protected void setURN(String urnString) throws    InvalidSessionUrnException// NullPointerException, IllegalArgumentException 
70     {
71       try {
72         this.urn = URI.create(urnString);
73       } catch (Exception e) {
74         throw new InvalidSessionUrnException(e);
75       }
76     }
77   
78 }