View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/common/src/java/org/astrogrid/common/ivorn/MockIvorn.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/09/16 23:18:08 $</cvs:date>
5    * <cvs:version>$Revision: 1.7 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: MockIvorn.java,v $
9    *   Revision 1.7  2004/09/16 23:18:08  dave
10   *   Replaced debug logging in Community.
11   *   Added stream close() to FileStore.
12   *
13   *   Revision 1.6.82.1  2004/09/16 09:58:48  dave
14   *   Replaced debug with commons logging ....
15   *
16   *   Revision 1.6  2004/06/18 13:45:20  dave
17   *   Merged development branch, dave-dev-200406081614, into HEAD
18   *
19   *   Revision 1.5.36.1  2004/06/17 13:38:58  dave
20   *   Tidied up old CVS log entries
21   *
22   * </cvs:log>
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         // Put it all back together.
142         StringBuffer buffer = new StringBuffer() ;
143         //
144         // Start with the ivo scheme.
145         buffer.append(Ivorn.SCHEME) ;
146         buffer.append("://") ;
147         //
148         // If we have an ident.
149         if (null != ident)
150             {
151             //
152             // If the ident is not empty.
153             if (ident.length() > 0)
154                 {
155                 //
156                 // If the ident is not a mock ident
157                 if (false == ident.startsWith(MOCK_IDENT))
158                     {
159                     //
160                     // Add the mock ident to the beginning.
161                     buffer.append(MOCK_IDENT) ;
162                     buffer.append(".") ;
163                     }
164                 buffer.append(ident) ;
165                 }
166             //
167             // If the ident is empty.
168             else {
169                 //
170                 // Use the mock ident instead.
171                 buffer.append(MOCK_IDENT) ;
172                 }
173             }
174         //
175         // If we don't have an ident.
176         else {
177             //
178             // Use the mock ident.
179             buffer.append(MOCK_IDENT) ;
180             }
181         //
182         // Add the rest of the elements.
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         // Return the new string.
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         // Convert the Ivorn into a String.
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         // Convert the String into a URI.
226         try {
227             return isMock(
228                 new URI(string)
229                 ) ;
230             }
231         //
232         // If it isn't valid, then it isn't a valid mock either.
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         // Check if the host part starts with the mock ident.
248         return (null != uri.getHost()) ? uri.getHost().startsWith(MOCK_IDENT) : false ;
249         }
250     }