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