applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / SessionUrn.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client;\r
23 \r
24 import java.net.URI;\r
25 import java.util.Hashtable;\r
26 \r
27 /**\r
28  * @author jimp base class for vamsas session/document types uses java.net.URI\r
29  *         internally for construction of URN\r
30  */\r
31 public abstract class SessionUrn {\r
32   protected URI urn;\r
33 \r
34   /**\r
35    * The types of URI protocols we understand\r
36    */\r
37   protected static final Hashtable TYPES = new Hashtable();\r
38 \r
39   protected SessionUrn() {\r
40     //\r
41   }\r
42 \r
43   /**\r
44    * construct urn for a locally stored session file\r
45    * \r
46    * @param type\r
47    * @param url\r
48    */\r
49   protected SessionUrn(String type, java.net.URL url) {\r
50     if (!TYPES.containsKey(type.toLowerCase()))\r
51       throw new Error("Unknown " + this.getClass().getName() + " type '" + type\r
52           + "' for URL '" + url + "'");\r
53     try {\r
54 \r
55       this.setURN(type + "://" + url.getPath());\r
56       // urn = URI.create(type+"://"+url.getPath());\r
57     } catch (Exception e) {\r
58       // TODO: something better than throwing an error should be done here.\r
59       e.printStackTrace(System.err);\r
60       throw new Error(e);\r
61     }\r
62   }\r
63 \r
64   protected SessionUrn(String type, URI uri) {\r
65     if (!TYPES.containsKey(type.toLowerCase()))\r
66       throw new Error("Unknown " + this.getClass().getName() + " type '" + type\r
67           + "' for URI '" + uri + "'");\r
68     try {\r
69       // this.setURN(type+"://"+uri.getPath());\r
70       // bad hack but should do the trick\r
71       this.setURN(type + "://" + uri.getRawPath());\r
72     } catch (Exception e) {\r
73       // TODO: something better than throwing an error should be done here.\r
74       e.printStackTrace(System.err);\r
75       throw new Error(e);\r
76     }\r
77   }\r
78 \r
79   public String getSessionUrn() {\r
80     return this.urn.toString();\r
81   }\r
82 \r
83   /**\r
84    * Set the urn attribute create a URI from the provided String\r
85    * \r
86    * @param urnString\r
87    *          urn to convert to a URN\r
88    */\r
89   protected void setURN(String urnString) throws InvalidSessionUrnException// NullPointerException,\r
90                                                                            // IllegalArgumentException\r
91   {\r
92     try {\r
93       this.urn = URI.create(urnString);\r
94     } catch (Exception e) {\r
95       throw new InvalidSessionUrnException(e);\r
96     }\r
97   }\r
98 \r
99 }\r