1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.astrogrid.common.ivorn ;
26
27 import org.apache.commons.logging.Log ;
28 import org.apache.commons.logging.LogFactory ;
29
30 import java.net.URI ;
31 import java.net.URISyntaxException ;
32
33 import org.astrogrid.store.Ivorn ;
34
35 /***
36 * A factory for mock Ivorn identifiers.
37 * Extends the basic Ivorn, but makes sure that the authority starts with a fixed prefix, 'org.astrogrid.mock'.
38 * Our delegate factories can use this to create mock delegates.
39 * @todo Refactor this as a factory rather than extends Ivorn.
40 * @todo Refactor this to be consistent with the other Ivorn factories.
41 *
42 */
43 public class MockIvorn
44 extends Ivorn
45 {
46
47 /***
48 * The default mock identifier.
49 * If an Ivorn matches this ident, delegate factories should generate a mock delegate.
50 *
51 */
52 public static final String MOCK_IDENT = "org.astrogrid.mock" ;
53
54 /***
55 * Create a mock Ivorn.
56 * @param ident The ivo ident, with no extra fields.
57 *
58 */
59 public MockIvorn(String ident)
60 throws URISyntaxException
61 {
62 super(
63 createIdent(ident)
64 ) ;
65 }
66
67 /***
68 * Create a mock Ivorn.
69 * @param ident The ivo ident, with no extra fields.
70 * @param path The path, added after the ident.
71 *
72 */
73 public MockIvorn(String ident, String path)
74 throws URISyntaxException
75 {
76 super(
77 createIdent(ident, path)
78 ) ;
79 }
80
81 /***
82 * Create a mock Ivorn.
83 * @param ident The ivo ident, with no extra fields.
84 * @param path The path, added after the ident.
85 * @param fragment The URI fragment string.
86 *
87 */
88 protected MockIvorn(String ident, String path, String fragment)
89 throws URISyntaxException
90 {
91 super(
92 createIdent(ident, path, fragment)
93 ) ;
94 }
95
96 /***
97 * Create a mock ident.
98 * @param ident The ivo ident, with no extra fields.
99 *
100 */
101 protected static String createIdent(String ident)
102 {
103 return createIdent(ident, null, null, null) ;
104 }
105
106 /***
107 * Create a mock ident.
108 * @param ident The ivo ident, with no extra fields.
109 * @param path The path, added after the ident.
110 *
111 */
112 protected static String createIdent(String ident, String path)
113 {
114 return createIdent(ident, path, null, null) ;
115 }
116
117 /***
118 * Create a mock ident.
119 * @param ident The ivo ident, with no extra fields.
120 * @param path The path, added after the ident.
121 * @param fragment The URI fragment string.
122 *
123 */
124 protected static String createIdent(String ident, String path, String fragment)
125 {
126 return createIdent(ident, path, null, fragment) ;
127 }
128
129 /***
130 * Create a mock ident.
131 * This allows you to set almost all of the URI fields.
132 * @param ident The ivo ident, with no extra fields.
133 * @param path The path, added after the ident.
134 * @param query The URI query string.
135 * @param fragment The URI fragment string.
136 *
137 */
138 protected static String createIdent(String ident, String path, String query, String fragment)
139 {
140
141
142 StringBuffer buffer = new StringBuffer() ;
143
144
145 buffer.append(Ivorn.SCHEME) ;
146 buffer.append("://") ;
147
148
149 if (null != ident)
150 {
151
152
153 if (ident.length() > 0)
154 {
155
156
157 if (false == ident.startsWith(MOCK_IDENT))
158 {
159
160
161 buffer.append(MOCK_IDENT) ;
162 buffer.append(".") ;
163 }
164 buffer.append(ident) ;
165 }
166
167
168 else {
169
170
171 buffer.append(MOCK_IDENT) ;
172 }
173 }
174
175
176 else {
177
178
179 buffer.append(MOCK_IDENT) ;
180 }
181
182
183 if (null != path)
184 {
185 buffer.append("/") ;
186 buffer.append(path) ;
187 }
188 if (null != query)
189 {
190 buffer.append("?") ;
191 buffer.append(query) ;
192 }
193 if (null != fragment)
194 {
195 buffer.append("#") ;
196 buffer.append(fragment) ;
197 }
198
199
200 return buffer.toString() ;
201 }
202
203 /***
204 * Returns true if the Ivorn is a mock identifier.
205 * If this works, then it should probably be move to org.astrogrid.store.Ivorn.
206 *
207 */
208 public static boolean isMock(Ivorn ivorn)
209 {
210
211
212 return isMock(
213 ivorn.toString()
214 ) ;
215 }
216
217 /***
218 * Returns true if the String is a mock identifier.
219 * If this works, then it should probably be move to org.astrogrid.store.Ivorn.
220 *
221 */
222 public static boolean isMock(String string)
223 {
224
225
226 try {
227 return isMock(
228 new URI(string)
229 ) ;
230 }
231
232
233 catch (URISyntaxException ouch)
234 {
235 return false ;
236 }
237 }
238
239 /***
240 * Returns true if the URI is a mock identifier.
241 * If this works, then it should probably be move to org.astrogrid.store.Ivorn.
242 *
243 */
244 public static boolean isMock(URI uri)
245 {
246
247
248 return (null != uri.getHost()) ? uri.getHost().startsWith(MOCK_IDENT) : false ;
249 }
250 }