1
2
3
4
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
50
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
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
134
135
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
195
196
197
198
199
200
201
202
203
204
205
206