View Javadoc

1   /*
2    * $Id: Ivorn.java,v 1.11 2004/10/06 17:37:47 mch Exp $
3    *
4    * Copyright 2003 AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid Software License,
7    * a copy of which has been included with this distribution in the LICENSE.txt file.
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        //separate authority, key & fragment
56        URI uri = new URI(ivorn); //make use of URI parsing
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          //this should never happen as it shouldn't be possible to create an agsl that isn't
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 $Log: Ivorn.java,v $
142 Revision 1.11  2004/10/06 17:37:47  mch
143 Added toURI
144 
145 Revision 1.10  2004/07/07 10:55:24  mch
146 Replaced two-string constructor
147 
148 Revision 1.9  2004/07/06 19:45:53  mch
149 Added isIvorn
150 
151 Revision 1.8  2004/07/06 19:24:25  mch
152 Minor fix :-)
153 
154 Revision 1.7  2004/07/06 19:20:28  mch
155 Doc updates and split between authority & key
156 
157 Revision 1.6  2004/06/17 17:34:08  jdt
158 Miscellaneous coding standards issues.
159 
160 Revision 1.5  2004/04/01 15:14:45  mch
161 Removed User/Agsl constructor as it makes a dependency on Agsl
162 
163 Revision 1.4  2004/04/01 14:50:07  mch
164 added constructor from user & agsl
165 
166 Revision 1.3  2004/03/25 12:21:30  mch
167 Tidied doc
168 
169 Revision 1.2  2004/03/12 15:11:33  dave
170 Removed extra import in IvornTest.
171 Fixed redundant '#' in Ivorn with no fragment.
172 Fixed missing new-line at end of file.
173 
174 Revision 1.1  2004/03/12 13:13:09  mch
175 Moved & Fixed null fragment error
176 
177 Revision 1.3  2004/03/08 14:24:36  mch
178 Added toRegistryString()
179 
180 Revision 1.2  2004/03/02 16:31:30  mch
181 Fixed case change
182 
183 Revision 1.1  2004/03/01 16:38:58  mch
184 Merged in from datacenter 4.1 and odd cvs/case problems
185 
186 Revision 1.1.2.1  2004/02/23 12:54:42  mch
187 It04 vospace identifiers
188 
189 Revision 1.1  2004/02/16 23:31:47  mch
190 IVO Resource Name representation
191 
192  */
193 
194