|
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.io.Serializable; |
|
35 import java.util.Arrays; |
|
36 import java.util.Iterator; |
|
37 import java.util.List; |
|
38 import java.util.regex.Matcher; |
|
39 import java.util.regex.Pattern; |
|
40 |
|
41 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList; |
|
42 |
|
43 import net.fortuna.ical4j.util.Strings; |
|
44 |
|
45 /** |
|
46 * $Id$ [23-Apr-2004] |
|
47 * |
|
48 * Defines a list of iCalendar text elements. |
|
49 * @author Ben Fortuna |
|
50 */ |
|
51 public class TextList implements Serializable { |
|
52 |
|
53 private static final long serialVersionUID = -417427815871330636L; |
|
54 |
|
55 private List texts; |
|
56 |
|
57 /** |
|
58 * Default constructor. |
|
59 */ |
|
60 public TextList() { |
|
61 texts = new CopyOnWriteArrayList(); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Parses the specified string representation to create a list of categories. |
|
66 * @param aValue a string representation of a list of categories |
|
67 */ |
|
68 public TextList(final String aValue) { |
|
69 texts = new CopyOnWriteArrayList(); |
|
70 |
|
71 // match commas preceded by even number of backslashes.. |
|
72 final Pattern pattern = Pattern.compile("([^\\\\](?:\\\\{2})),|([^\\\\]),"); |
|
73 |
|
74 final Matcher matcher = pattern.matcher(aValue); |
|
75 String[] textValues = null; |
|
76 |
|
77 if (matcher.find()) { |
|
78 // HACK: add a marker (") for easy string splitting.. |
|
79 textValues = matcher.replaceAll("$1$2"").split("""); |
|
80 } |
|
81 else { |
|
82 // no special cases, split on commas not preceded by backslash.. |
|
83 textValues = aValue.split("(?<!\\\\),"); |
|
84 } |
|
85 |
|
86 for (int i = 0; i < textValues.length; i++) { |
|
87 texts.add(Strings.unescape(textValues[i])); |
|
88 } |
|
89 } |
|
90 |
|
91 /** |
|
92 * @param textValues an array of text values |
|
93 */ |
|
94 public TextList(String[] textValues) { |
|
95 texts = Arrays.asList(textValues); |
|
96 } |
|
97 |
|
98 /** |
|
99 * {@inheritDoc} |
|
100 */ |
|
101 public final String toString() { |
|
102 final StringBuffer b = new StringBuffer(); |
|
103 for (final Iterator i = texts.iterator(); i.hasNext();) { |
|
104 b.append(Strings.escape((String) i.next())); |
|
105 if (i.hasNext()) { |
|
106 b.append(','); |
|
107 } |
|
108 } |
|
109 return b.toString(); |
|
110 } |
|
111 |
|
112 /** |
|
113 * Add an address to the list. |
|
114 * @param text the category to add |
|
115 * @return true |
|
116 * @see List#add(java.lang.Object) |
|
117 */ |
|
118 public final boolean add(final String text) { |
|
119 return texts.add(text); |
|
120 } |
|
121 |
|
122 /** |
|
123 * @return boolean indicates if the list is empty |
|
124 * @see List#isEmpty() |
|
125 */ |
|
126 public final boolean isEmpty() { |
|
127 return texts.isEmpty(); |
|
128 } |
|
129 |
|
130 /** |
|
131 * @return an iterator |
|
132 * @see List#iterator() |
|
133 */ |
|
134 public final Iterator iterator() { |
|
135 return texts.iterator(); |
|
136 } |
|
137 |
|
138 /** |
|
139 * Remove a text from the list. |
|
140 * @param text the text element to remove |
|
141 * @return true if the list contained the specified text element |
|
142 * @see List#remove(java.lang.Object) |
|
143 */ |
|
144 public final boolean remove(final String text) { |
|
145 return texts.remove(text); |
|
146 } |
|
147 |
|
148 /** |
|
149 * @return the number of text elements in the list |
|
150 * @see List#size() |
|
151 */ |
|
152 public final int size() { |
|
153 return texts.size(); |
|
154 } |
|
155 } |