1
2
3
4
5
6
7
8
9
10 package org.astrogrid.store;
11
12 import java.net.URI;
13 import java.net.URISyntaxException;
14
15 /***
16 * International Virtual Observatory Resource Name. Used to name specific IVO resources.
17 * <p>
18 * They act as keys to VO Registries; give the registry an IVORN and it will
19 * return the associated VOResource document
20 * <p>
21 * Ivorns are of the form:
22 * <pre>
23 * ivo://something/anything/athing/etc#somethingwithmeaningtothiscontext
24 * </pre>
25 * For example:
26 * <pre>
27 * ivo://roe.ac.uk/storee#path/to/file.ext
28 * </pre>
29 * <i>might</i> resolve to an FTP server, on which the path 'path/to/file.ext'
30 * would locate a document.
31 * <p>
32 * Ivorns are immutable - ie once created, they cannot be changed. You can
33 * make new ones out of old ones.
34 * <p>
35 * This is a badly named package. IVORNs can be used for things other than
36 * stores...
37 *
38 * @author MCH, KMB, KTN, DM, ACD
39 */
40
41
42 public class Ivorn
43 {
44 public final static String SCHEME = "ivo";
45
46 private String key = null;
47 private String authority = null;
48 private String fragment = null;
49
50 /*** Construct from given string */
51 public Ivorn(String ivorn) throws URISyntaxException
52 {
53 assert ivorn.startsWith(SCHEME+":") : "Scheme should be "+SCHEME+":";
54
55
56 URI uri = new URI(ivorn);
57
58 authority = uri.getAuthority();
59 key = uri.getPath();
60 fragment = uri.getFragment();
61 }
62
63 /*** Construct from given authority, key and fragment */
64 public Ivorn(String anAuthority, String aKey, String aFragment)
65 {
66 this.key = "/"+aKey;
67 this.authority = anAuthority;
68 this.fragment = aFragment;
69 }
70
71 /*** Construct from 'path' and fragment; path consists of anAuthority
72 * and a key, eg 'roe.ac.uk/myspace'.
73 * @deprecated? use three part constructor
74 */
75 public Ivorn(String aPath, String aFragment) {
76
77 int slash = aPath.indexOf('/');
78 if (slash == -1) {
79 throw new IllegalArgumentException("path should consist of <authority>/<key>");
80 }
81 authority = aPath.substring(0,slash);
82 key = aPath.substring(slash);
83 fragment = aFragment;
84 }
85
86 /*** Returns identifier scheme */
87 public String getScheme() { return SCHEME; }
88
89 /*** Returns ivo id without the scheme.
90 * eg 'ivo://roe.ac.uk/mch/myspace' returns 'roe.ac.uk/mch/myspace'
91 */
92 public String getPath() {
93 if (key == null) {
94 return authority;
95 } else {
96 return authority+key;
97 }
98 }
99
100 /*** Returns specific-to-service - ie fragment */
101 public String getFragment() {
102 return fragment;
103 }
104
105 /*** String representation */
106 public String toString() {
107 if (fragment == null) {
108 return SCHEME+"://"+getPath();
109 } else {
110 return SCHEME+"://"+getPath()+"#"+fragment;
111 }
112 }
113
114 /*** Returns the IVORN in URI form */
115 public URI toUri() {
116 try {
117 return new URI(toString());
118 }
119 catch (URISyntaxException e) {
120
121 throw new RuntimeException("Application error: "+e+" for IVORN "+toString());
122 }
123 }
124
125 /*** Representation to be used when submitting the IVORN to a registry
126 * to be resolved */
127 public String toRegistryString() {
128 return SCHEME+"://"+getPath();
129 }
130
131 /*** Returns true if the given string is likely to be an ivorn - ie if it
132 * starts with ivo://
133 */
134 public static boolean isIvorn(String aString) {
135 return aString.toLowerCase().startsWith(SCHEME+"://");
136 }
137
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194