|
1 /** |
|
2 * Copyright (c) 2012, Ben Fortuna |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * o Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * o Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * o Neither the name of Ben Fortuna nor the names of any other contributors |
|
17 * may be used to endorse or promote products derived from this software |
|
18 * without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package net.fortuna.ical4j.model; |
|
33 |
|
34 import java.net.URISyntaxException; |
|
35 |
|
36 import net.fortuna.ical4j.model.parameter.Abbrev; |
|
37 import net.fortuna.ical4j.model.parameter.AltRep; |
|
38 import net.fortuna.ical4j.model.parameter.Cn; |
|
39 import net.fortuna.ical4j.model.parameter.CuType; |
|
40 import net.fortuna.ical4j.model.parameter.DelegatedFrom; |
|
41 import net.fortuna.ical4j.model.parameter.DelegatedTo; |
|
42 import net.fortuna.ical4j.model.parameter.Dir; |
|
43 import net.fortuna.ical4j.model.parameter.Encoding; |
|
44 import net.fortuna.ical4j.model.parameter.FbType; |
|
45 import net.fortuna.ical4j.model.parameter.FmtType; |
|
46 import net.fortuna.ical4j.model.parameter.Language; |
|
47 import net.fortuna.ical4j.model.parameter.Member; |
|
48 import net.fortuna.ical4j.model.parameter.PartStat; |
|
49 import net.fortuna.ical4j.model.parameter.Range; |
|
50 import net.fortuna.ical4j.model.parameter.RelType; |
|
51 import net.fortuna.ical4j.model.parameter.Related; |
|
52 import net.fortuna.ical4j.model.parameter.Role; |
|
53 import net.fortuna.ical4j.model.parameter.Rsvp; |
|
54 import net.fortuna.ical4j.model.parameter.ScheduleAgent; |
|
55 import net.fortuna.ical4j.model.parameter.ScheduleStatus; |
|
56 import net.fortuna.ical4j.model.parameter.SentBy; |
|
57 import net.fortuna.ical4j.model.parameter.Type; |
|
58 import net.fortuna.ical4j.model.parameter.TzId; |
|
59 import net.fortuna.ical4j.model.parameter.Value; |
|
60 import net.fortuna.ical4j.model.parameter.Vvenue; |
|
61 import net.fortuna.ical4j.model.parameter.XParameter; |
|
62 import net.fortuna.ical4j.util.Strings; |
|
63 |
|
64 /** |
|
65 * A factory for creating iCalendar parameters. |
|
66 * |
|
67 * $Id $ |
|
68 * |
|
69 * [05-Apr-2004] |
|
70 * |
|
71 * @author Ben Fortuna |
|
72 */ |
|
73 public class ParameterFactoryImpl extends AbstractContentFactory implements ParameterFactory { |
|
74 |
|
75 private static final long serialVersionUID = -4034423507432249165L; |
|
76 |
|
77 private static ParameterFactoryImpl instance = new ParameterFactoryImpl(); |
|
78 |
|
79 protected ParameterFactoryImpl() { |
|
80 registerDefaultFactory(Parameter.ABBREV, new AbbrevFactory()); |
|
81 registerDefaultFactory(Parameter.ALTREP, new AltRepFactory()); |
|
82 registerDefaultFactory(Parameter.CN, new CnFactory()); |
|
83 registerDefaultFactory(Parameter.CUTYPE, new CuTypeFactory()); |
|
84 registerDefaultFactory(Parameter.DELEGATED_FROM, new DelegatedFromFactory()); |
|
85 registerDefaultFactory(Parameter.DELEGATED_TO, new DelegatedToFactory()); |
|
86 registerDefaultFactory(Parameter.DIR, new DirFactory()); |
|
87 registerDefaultFactory(Parameter.ENCODING, new EncodingFactory()); |
|
88 registerDefaultFactory(Parameter.FMTTYPE, new FmtTypeFactory()); |
|
89 registerDefaultFactory(Parameter.FBTYPE, new FbTypeFactory()); |
|
90 registerDefaultFactory(Parameter.LANGUAGE, new LanguageFactory()); |
|
91 registerDefaultFactory(Parameter.MEMBER, new MemberFactory()); |
|
92 registerDefaultFactory(Parameter.PARTSTAT, new PartStatFactory()); |
|
93 registerDefaultFactory(Parameter.RANGE, new RangeFactory()); |
|
94 registerDefaultFactory(Parameter.RELATED, new RelatedFactory()); |
|
95 registerDefaultFactory(Parameter.RELTYPE, new RelTypeFactory()); |
|
96 registerDefaultFactory(Parameter.ROLE, new RoleFactory()); |
|
97 registerDefaultFactory(Parameter.RSVP, new RsvpFactory()); |
|
98 registerDefaultFactory(Parameter.SCHEDULE_AGENT, new ScheduleAgentFactory()); |
|
99 registerDefaultFactory(Parameter.SCHEDULE_STATUS, new ScheduleStatusFactory()); |
|
100 registerDefaultFactory(Parameter.SENT_BY, new SentByFactory()); |
|
101 registerDefaultFactory(Parameter.TYPE, new TypeFactory()); |
|
102 registerDefaultFactory(Parameter.TZID, new TzIdFactory()); |
|
103 registerDefaultFactory(Parameter.VALUE, new ValueFactory()); |
|
104 registerDefaultFactory(Parameter.VVENUE, new VvenueFactory()); |
|
105 } |
|
106 |
|
107 /** |
|
108 * @return Returns the instance. |
|
109 */ |
|
110 public static ParameterFactoryImpl getInstance() { |
|
111 return instance; |
|
112 } |
|
113 |
|
114 /** |
|
115 * Creates a parameter. |
|
116 * @param name name of the parameter |
|
117 * @param value a parameter value |
|
118 * @return a component |
|
119 * @throws URISyntaxException thrown when the specified string is not a valid representation of a URI for selected |
|
120 * parameters |
|
121 */ |
|
122 public Parameter createParameter(final String name, final String value) |
|
123 throws URISyntaxException { |
|
124 final ParameterFactory factory = (ParameterFactory) getFactory(name); |
|
125 Parameter parameter = null; |
|
126 if (factory != null) { |
|
127 parameter = factory.createParameter(name, value); |
|
128 } |
|
129 else if (isExperimentalName(name)) { |
|
130 parameter = new XParameter(name, value); |
|
131 } |
|
132 else if (allowIllegalNames()) { |
|
133 parameter = new XParameter(name, value); |
|
134 } |
|
135 else { |
|
136 throw new IllegalArgumentException("Invalid parameter name: " |
|
137 + name); |
|
138 } |
|
139 return parameter; |
|
140 } |
|
141 |
|
142 /** |
|
143 * @param name |
|
144 * @return |
|
145 */ |
|
146 private boolean isExperimentalName(final String name) { |
|
147 return name.startsWith(Parameter.EXPERIMENTAL_PREFIX) |
|
148 && name.length() > Parameter.EXPERIMENTAL_PREFIX.length(); |
|
149 } |
|
150 |
|
151 private static class AbbrevFactory implements ParameterFactory { |
|
152 private static final long serialVersionUID = 1L; |
|
153 |
|
154 public Parameter createParameter(final String name, final String value) throws URISyntaxException { |
|
155 return new Abbrev(value); |
|
156 } |
|
157 } |
|
158 |
|
159 private static class AltRepFactory implements ParameterFactory { |
|
160 private static final long serialVersionUID = 1L; |
|
161 |
|
162 public Parameter createParameter(final String name, final String value) throws URISyntaxException { |
|
163 return new AltRep(value); |
|
164 } |
|
165 } |
|
166 |
|
167 private static class CnFactory implements ParameterFactory { |
|
168 private static final long serialVersionUID = 1L; |
|
169 |
|
170 public Parameter createParameter(final String name, |
|
171 final String value) throws URISyntaxException { |
|
172 return new Cn(value); |
|
173 } |
|
174 } |
|
175 |
|
176 private static class CuTypeFactory implements ParameterFactory { |
|
177 private static final long serialVersionUID = 1L; |
|
178 |
|
179 public Parameter createParameter(final String name, final String value) throws URISyntaxException { |
|
180 CuType parameter = new CuType(value); |
|
181 if (CuType.INDIVIDUAL.equals(parameter)) { |
|
182 parameter = CuType.INDIVIDUAL; |
|
183 } |
|
184 else if (CuType.GROUP.equals(parameter)) { |
|
185 parameter = CuType.GROUP; |
|
186 } |
|
187 else if (CuType.RESOURCE.equals(parameter)) { |
|
188 parameter = CuType.RESOURCE; |
|
189 } |
|
190 else if (CuType.ROOM.equals(parameter)) { |
|
191 parameter = CuType.ROOM; |
|
192 } |
|
193 else if (CuType.UNKNOWN.equals(parameter)) { |
|
194 parameter = CuType.UNKNOWN; |
|
195 } |
|
196 return parameter; |
|
197 } |
|
198 } |
|
199 |
|
200 private static class DelegatedFromFactory implements ParameterFactory { |
|
201 private static final long serialVersionUID = 1L; |
|
202 |
|
203 public Parameter createParameter(final String name, |
|
204 final String value) throws URISyntaxException { |
|
205 return new DelegatedFrom(value); |
|
206 } |
|
207 } |
|
208 |
|
209 private static class DelegatedToFactory implements ParameterFactory { |
|
210 private static final long serialVersionUID = 1L; |
|
211 |
|
212 public Parameter createParameter(final String name, |
|
213 final String value) throws URISyntaxException { |
|
214 return new DelegatedTo(value); |
|
215 } |
|
216 } |
|
217 |
|
218 private static class DirFactory implements ParameterFactory { |
|
219 private static final long serialVersionUID = 1L; |
|
220 |
|
221 public Parameter createParameter(final String name, |
|
222 final String value) throws URISyntaxException { |
|
223 return new Dir(value); |
|
224 } |
|
225 } |
|
226 |
|
227 private static class EncodingFactory implements ParameterFactory { |
|
228 private static final long serialVersionUID = 1L; |
|
229 |
|
230 public Parameter createParameter(final String name, |
|
231 final String value) throws URISyntaxException { |
|
232 Encoding parameter = new Encoding(value); |
|
233 if (Encoding.EIGHT_BIT.equals(parameter)) { |
|
234 parameter = Encoding.EIGHT_BIT; |
|
235 } |
|
236 else if (Encoding.BASE64.equals(parameter)) { |
|
237 parameter = Encoding.BASE64; |
|
238 } |
|
239 return parameter; |
|
240 } |
|
241 } |
|
242 |
|
243 private static class FmtTypeFactory implements ParameterFactory { |
|
244 private static final long serialVersionUID = 1L; |
|
245 |
|
246 public Parameter createParameter(final String name, |
|
247 final String value) throws URISyntaxException { |
|
248 return new FmtType(value); |
|
249 } |
|
250 } |
|
251 |
|
252 private static class FbTypeFactory implements ParameterFactory { |
|
253 private static final long serialVersionUID = 1L; |
|
254 |
|
255 public Parameter createParameter(final String name, |
|
256 final String value) throws URISyntaxException { |
|
257 FbType parameter = new FbType(value); |
|
258 if (FbType.FREE.equals(parameter)) { |
|
259 parameter = FbType.FREE; |
|
260 } |
|
261 else if (FbType.BUSY.equals(parameter)) { |
|
262 parameter = FbType.BUSY; |
|
263 } |
|
264 else if (FbType.BUSY_TENTATIVE.equals(parameter)) { |
|
265 parameter = FbType.BUSY_TENTATIVE; |
|
266 } |
|
267 else if (FbType.BUSY_UNAVAILABLE.equals(parameter)) { |
|
268 parameter = FbType.BUSY_UNAVAILABLE; |
|
269 } |
|
270 return parameter; |
|
271 } |
|
272 } |
|
273 |
|
274 private static class LanguageFactory implements ParameterFactory { |
|
275 private static final long serialVersionUID = 1L; |
|
276 |
|
277 public Parameter createParameter(final String name, |
|
278 final String value) throws URISyntaxException { |
|
279 return new Language(value); |
|
280 } |
|
281 } |
|
282 |
|
283 private static class MemberFactory implements ParameterFactory { |
|
284 private static final long serialVersionUID = 1L; |
|
285 |
|
286 public Parameter createParameter(final String name, |
|
287 final String value) throws URISyntaxException { |
|
288 return new Member(value); |
|
289 } |
|
290 } |
|
291 |
|
292 private static class PartStatFactory implements ParameterFactory { |
|
293 private static final long serialVersionUID = 1L; |
|
294 |
|
295 public Parameter createParameter(final String name, |
|
296 final String value) throws URISyntaxException { |
|
297 PartStat parameter = new PartStat(value); |
|
298 if (PartStat.NEEDS_ACTION.equals(parameter)) { |
|
299 parameter = PartStat.NEEDS_ACTION; |
|
300 } |
|
301 else if (PartStat.ACCEPTED.equals(parameter)) { |
|
302 parameter = PartStat.ACCEPTED; |
|
303 } |
|
304 else if (PartStat.DECLINED.equals(parameter)) { |
|
305 parameter = PartStat.DECLINED; |
|
306 } |
|
307 else if (PartStat.TENTATIVE.equals(parameter)) { |
|
308 parameter = PartStat.TENTATIVE; |
|
309 } |
|
310 else if (PartStat.DELEGATED.equals(parameter)) { |
|
311 parameter = PartStat.DELEGATED; |
|
312 } |
|
313 else if (PartStat.COMPLETED.equals(parameter)) { |
|
314 parameter = PartStat.COMPLETED; |
|
315 } |
|
316 else if (PartStat.IN_PROCESS.equals(parameter)) { |
|
317 parameter = PartStat.IN_PROCESS; |
|
318 } |
|
319 return parameter; |
|
320 } |
|
321 } |
|
322 |
|
323 private static class RangeFactory implements ParameterFactory { |
|
324 private static final long serialVersionUID = 1L; |
|
325 |
|
326 public Parameter createParameter(final String name, |
|
327 final String value) throws URISyntaxException { |
|
328 Range parameter = new Range(value); |
|
329 if (Range.THISANDFUTURE.equals(parameter)) { |
|
330 parameter = Range.THISANDFUTURE; |
|
331 } |
|
332 else if (Range.THISANDPRIOR.equals(parameter)) { |
|
333 parameter = Range.THISANDPRIOR; |
|
334 } |
|
335 return parameter; |
|
336 } |
|
337 } |
|
338 |
|
339 private static class RelatedFactory implements ParameterFactory { |
|
340 private static final long serialVersionUID = 1L; |
|
341 |
|
342 public Parameter createParameter(final String name, |
|
343 final String value) throws URISyntaxException { |
|
344 Related parameter = new Related(value); |
|
345 if (Related.START.equals(parameter)) { |
|
346 parameter = Related.START; |
|
347 } |
|
348 else if (Related.END.equals(parameter)) { |
|
349 parameter = Related.END; |
|
350 } |
|
351 return parameter; |
|
352 } |
|
353 } |
|
354 |
|
355 private static class RelTypeFactory implements ParameterFactory { |
|
356 private static final long serialVersionUID = 1L; |
|
357 |
|
358 public Parameter createParameter(final String name, |
|
359 final String value) throws URISyntaxException { |
|
360 RelType parameter = new RelType(value); |
|
361 if (RelType.PARENT.equals(parameter)) { |
|
362 parameter = RelType.PARENT; |
|
363 } |
|
364 else if (RelType.CHILD.equals(parameter)) { |
|
365 parameter = RelType.CHILD; |
|
366 } |
|
367 if (RelType.SIBLING.equals(parameter)) { |
|
368 parameter = RelType.SIBLING; |
|
369 } |
|
370 return parameter; |
|
371 } |
|
372 } |
|
373 |
|
374 private static class RoleFactory implements ParameterFactory { |
|
375 private static final long serialVersionUID = 1L; |
|
376 |
|
377 public Parameter createParameter(final String name, |
|
378 final String value) throws URISyntaxException { |
|
379 Role parameter = new Role(value); |
|
380 if (Role.CHAIR.equals(parameter)) { |
|
381 parameter = Role.CHAIR; |
|
382 } |
|
383 else if (Role.REQ_PARTICIPANT.equals(parameter)) { |
|
384 parameter = Role.REQ_PARTICIPANT; |
|
385 } |
|
386 else if (Role.OPT_PARTICIPANT.equals(parameter)) { |
|
387 parameter = Role.OPT_PARTICIPANT; |
|
388 } |
|
389 else if (Role.NON_PARTICIPANT.equals(parameter)) { |
|
390 parameter = Role.NON_PARTICIPANT; |
|
391 } |
|
392 return parameter; |
|
393 } |
|
394 } |
|
395 |
|
396 private static class RsvpFactory implements ParameterFactory { |
|
397 private static final long serialVersionUID = 1L; |
|
398 |
|
399 public Parameter createParameter(final String name, |
|
400 final String value) throws URISyntaxException { |
|
401 Rsvp parameter = new Rsvp(value); |
|
402 if (Rsvp.TRUE.equals(parameter)) { |
|
403 parameter = Rsvp.TRUE; |
|
404 } |
|
405 else if (Rsvp.FALSE.equals(parameter)) { |
|
406 parameter = Rsvp.FALSE; |
|
407 } |
|
408 return parameter; |
|
409 } |
|
410 } |
|
411 |
|
412 private static class ScheduleAgentFactory implements ParameterFactory { |
|
413 public Parameter createParameter(final String name, |
|
414 final String value) throws URISyntaxException { |
|
415 final ScheduleAgent parameter = new ScheduleAgent(value); |
|
416 if (ScheduleAgent.SERVER.equals(parameter)) { |
|
417 return ScheduleAgent.SERVER; |
|
418 } |
|
419 else if (ScheduleAgent.CLIENT.equals(parameter)) { |
|
420 return ScheduleAgent.CLIENT; |
|
421 } |
|
422 else if (ScheduleAgent.NONE.equals(parameter)) { |
|
423 return ScheduleAgent.NONE; |
|
424 } |
|
425 return parameter; |
|
426 } |
|
427 } |
|
428 |
|
429 private static class ScheduleStatusFactory implements ParameterFactory { |
|
430 public Parameter createParameter(final String name, |
|
431 final String value) throws URISyntaxException { |
|
432 return new ScheduleStatus(value); |
|
433 } |
|
434 } |
|
435 |
|
436 private static class SentByFactory implements ParameterFactory { |
|
437 private static final long serialVersionUID = 1L; |
|
438 |
|
439 public Parameter createParameter(final String name, |
|
440 final String value) throws URISyntaxException { |
|
441 return new SentBy(value); |
|
442 } |
|
443 } |
|
444 |
|
445 private static class VvenueFactory implements ParameterFactory { |
|
446 private static final long serialVersionUID = 1L; |
|
447 |
|
448 public Parameter createParameter(final String name, final String value) |
|
449 throws URISyntaxException { |
|
450 return new Vvenue(value); |
|
451 } |
|
452 } |
|
453 |
|
454 private static class TypeFactory implements ParameterFactory { |
|
455 private static final long serialVersionUID = 1L; |
|
456 |
|
457 public Parameter createParameter(final String name, |
|
458 final String value) throws URISyntaxException { |
|
459 return new Type(value); |
|
460 } |
|
461 } |
|
462 |
|
463 private static class TzIdFactory implements ParameterFactory { |
|
464 private static final long serialVersionUID = 1L; |
|
465 |
|
466 public Parameter createParameter(final String name, |
|
467 final String value) throws URISyntaxException { |
|
468 return new TzId(Strings.unescape(value)); |
|
469 } |
|
470 } |
|
471 |
|
472 private static class ValueFactory implements ParameterFactory { |
|
473 private static final long serialVersionUID = 1L; |
|
474 |
|
475 public Parameter createParameter(final String name, |
|
476 final String value) throws URISyntaxException { |
|
477 Value parameter = new Value(value); |
|
478 if (Value.BINARY.equals(parameter)) { |
|
479 parameter = Value.BINARY; |
|
480 } |
|
481 else if (Value.BOOLEAN.equals(parameter)) { |
|
482 parameter = Value.BOOLEAN; |
|
483 } |
|
484 else if (Value.CAL_ADDRESS.equals(parameter)) { |
|
485 parameter = Value.CAL_ADDRESS; |
|
486 } |
|
487 else if (Value.DATE.equals(parameter)) { |
|
488 parameter = Value.DATE; |
|
489 } |
|
490 else if (Value.DATE_TIME.equals(parameter)) { |
|
491 parameter = Value.DATE_TIME; |
|
492 } |
|
493 else if (Value.DURATION.equals(parameter)) { |
|
494 parameter = Value.DURATION; |
|
495 } |
|
496 else if (Value.FLOAT.equals(parameter)) { |
|
497 parameter = Value.FLOAT; |
|
498 } |
|
499 else if (Value.INTEGER.equals(parameter)) { |
|
500 parameter = Value.INTEGER; |
|
501 } |
|
502 else if (Value.PERIOD.equals(parameter)) { |
|
503 parameter = Value.PERIOD; |
|
504 } |
|
505 else if (Value.RECUR.equals(parameter)) { |
|
506 parameter = Value.RECUR; |
|
507 } |
|
508 else if (Value.TEXT.equals(parameter)) { |
|
509 parameter = Value.TEXT; |
|
510 } |
|
511 else if (Value.TIME.equals(parameter)) { |
|
512 parameter = Value.TIME; |
|
513 } |
|
514 else if (Value.URI.equals(parameter)) { |
|
515 parameter = Value.URI; |
|
516 } |
|
517 else if (Value.UTC_OFFSET.equals(parameter)) { |
|
518 parameter = Value.UTC_OFFSET; |
|
519 } |
|
520 return parameter; |
|
521 } |
|
522 } |
|
523 |
|
524 } |