View Javadoc

1   /*
2    * $Id: TargetMaker.java,v 1.1 2004/11/09 17:42:22 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.slinger.targets;
8   
9   
10  
11  
12  
13  import java.io.OutputStream;
14  import java.io.Writer;
15  import java.net.MalformedURLException;
16  import java.net.URI;
17  import java.net.URISyntaxException;
18  import java.net.URL;
19  import org.astrogrid.store.Agsl;
20  import org.astrogrid.store.Ivorn;
21  import org.astrogrid.store.Msrl;
22  
23  /***
24   * Factory to create targets
25   *
26   */
27  
28  public abstract class TargetMaker  {
29  
30     /***
31      * Tests the string & creates the right kind of TargetIndicator
32      */
33     public static TargetIndicator makeIndicator(String id) throws MalformedURLException, URISyntaxException {
34        if ((id == null) || (id.trim().length() == 0) || (id.toLowerCase().equals("null")) || (id.toLowerCase().equals(NullTarget.NULL_TARGET_URI.toString().toLowerCase()))) {
35           return new NullTarget();
36        }
37        else if (id.startsWith("mailto:")) {
38           return new EmailTarget(id);
39        }
40        else if (Msrl.isMsrl(id)) {
41           return new MySpaceTarget(new Msrl(id));
42        }
43        else if (isUrl(id)) {
44           return new UrlTarget(new URL(id));
45        }
46        else if (Ivorn.isIvorn(id)) {
47           return new IvornTarget(new Ivorn(id));
48        }
49  //replaced by Uri and MySpace targets      else if (Agsl.isAgsl(id)) {
50  //         return new AgslTarget(new Agsl(id));
51  //      }
52        else {
53           throw new IllegalArgumentException("'"+id+" should start 'mailto:' or '"+Ivorn.SCHEME+"' or '"+Agsl.SCHEME+"'");
54        }
55     }
56  
57     /*** Returns true if the given string is a valid URL, false if not */
58     public static boolean isUrl(String urlCandidate) {
59        try {
60           new URL(urlCandidate);
61           return true;
62        }
63        catch (MalformedURLException mue) {
64           return false;
65        }
66     }
67     
68     /***
69      * Creates the right kind of TargetIndicator for the given URI
70      */
71     public static TargetIndicator makeIndicator(URI target) throws MalformedURLException, URISyntaxException {
72        return makeIndicator(target.toString());
73     }
74     
75     /***
76      * Makes a target that will write to the given writer
77      */
78     public static TargetIndicator makeIndicator(Writer target)  {
79        return new WriterTarget(target);
80     }
81  
82     /***
83      * Makes a target that will write to the given writer.  Set closeIt to false
84      * to inform the targetindicator user not to close the writer when it is
85      * finished
86      */
87     public static TargetIndicator makeIndicator(Writer target, boolean closeIt)  {
88        return new WriterTarget(target, closeIt);
89     }
90     
91     /***
92      * Makes a target that will write to the given URL
93      */
94     public static TargetIndicator makeIndicator(URL target)  throws URISyntaxException {
95        return new UrlTarget(target);
96     }
97     
98     /***
99      * Makes a target that will write to the given msypace reference
100     */
101    public static TargetIndicator makeIndicator(Msrl target)  throws URISyntaxException {
102       return new MySpaceTarget(target);
103    }
104    
105    /***
106     * Makes a target that will write to the given AGSL.  Returns the UrlTarget
107     * or MySpaceTarget as appropriate
108     * @ deprecated - use URLs or Msrls directly
109     */
110    public static TargetIndicator makeIndicator(Agsl target) throws MalformedURLException, URISyntaxException  {
111       //remove 'astrogrid:store:' and makeIndicator from remainder
112       String uri = target.toUri().toString().substring(Agsl.SCHEME.length()+1);
113       return makeIndicator(uri);
114    }
115    
116    /***
117     * Makes a target that will write to the given IVORN
118     */
119    public static TargetIndicator makeIndicator(Ivorn target)  {
120       return new IvornTarget(target);
121    }
122    
123 
124    /***
125     * Makes a target that will write to the given stream
126     */
127    public static TargetIndicator makeIndicator(OutputStream target)  {
128       return new StreamTarget(target);
129    }
130 
131 }
132 /*
133  $Log: TargetMaker.java,v $
134  Revision 1.1  2004/11/09 17:42:22  mch
135  Fixes to tests after fixes for demos, incl adding closable to targetIndicators
136 
137  Revision 1.2  2004/11/03 00:17:56  mch
138  PAL_MCH Candidate 2 merge
139 
140  Revision 1.1.2.2  2004/11/02 21:51:54  mch
141  Replaced AgslTarget with UrlTarget and MySpaceTarget
142 
143  Revision 1.1.2.1  2004/11/02 19:41:26  mch
144  Split TargetIndicator to indicator and maker
145 
146  Revision 1.3.6.1  2004/11/01 20:47:23  mch
147  Added a little bit of doc and introduced MsrlTarget/UrlTargets
148 
149  Revision 1.3  2004/10/18 13:11:30  mch
150  Lumpy Merge
151 
152  Revision 1.2.2.2  2004/10/16 14:29:07  mch
153  Forwardable null targets
154 
155  Revision 1.2.2.1  2004/10/15 19:59:05  mch
156  Lots of changes during trip to CDS to improve int test pass rate
157 
158  Revision 1.2  2004/10/12 17:41:41  mch
159  added isForwardable
160 
161  Revision 1.1  2004/10/06 21:12:17  mch
162  Big Lump of changes to pass Query OM around instead of Query subclasses, and TargetIndicator mixed into Slinger
163 
164  Revision 1.5  2004/10/05 14:55:00  mch
165  Added factory methods
166 
167  Revision 1.4  2004/09/07 01:39:27  mch
168  Moved email keys from TargetIndicator to Slinger
169 
170  Revision 1.3  2004/09/07 01:01:29  mch
171  Moved testConnection to server slinger
172 
173  Revision 1.2  2004/09/07 00:54:20  mch
174  Tidied up Querier/Plugin/Results, and removed deprecated SPI-visitor-SQL-translator
175 
176  Revision 1.1  2004/08/25 23:38:33  mch
177  (Days changes) moved many query- and results- related classes, renamed packages, added tests, added CIRCLE to sql/adql parsers
178 
179  Revision 1.3  2004/08/19 08:35:54  mch
180  Fix to email constructor
181 
182  Revision 1.2  2004/08/18 22:27:57  mch
183  Better error checking
184 
185  Revision 1.1  2004/08/17 20:19:36  mch
186  Moved TargetIndicator to client
187 
188  Revision 1.5  2004/07/15 17:07:23  mch
189  Added factory method to make from a string
190 
191  Revision 1.4  2004/03/15 19:16:12  mch
192  Lots of fixes to status updates
193 
194  Revision 1.3  2004/03/15 17:08:11  mch
195  Added compression adn format placeholders
196 
197  Revision 1.2  2004/03/14 16:55:48  mch
198  Added XSLT ADQL->SQL support
199 
200  Revision 1.1  2004/03/14 04:13:04  mch
201  Wrapped output target in TargetIndicator
202 
203  */
204 
205 
206