KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > PropertyList


1 /*
2  */

3 package com.sslexplorer.boot;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Properties JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11 /**
12  * Extension of an {@link java.util.ArrayList} that is used to store lists
13  * of strings. This class is used with SSL-Explorers custom input components
14  * such as the {@link com.sslexplorer.input.tags.MultiSelectListBoxTag} and
15  * {@link com.sslexplorer.input.tags.MultiEntryListBoxTag}.
16  * <p>
17  * This list of strings may be created from or converted to and from
18  * either <i>Text Field Text</i> (a newline separated list of strings) or
19  * <i>Property Text</i> (a ! delimited list).
20  *
21  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
22  */

23 public class PropertyList extends ArrayList JavaDoc<String JavaDoc> {
24
25     private static final long serialVersionUID = -6816117522796660457L;
26     public static final PropertyList EMPTY_LIST = new PropertyList();
27
28     /**
29      * Constructor. Create empty property list
30      */

31     public PropertyList() {
32         super();
33     }
34
35     /**
36      * Constructor.
37      *
38      * @param propertyList string in the <i>Property List</i> format.
39      * @see #setAsPropertyText(String)
40      */

41     public PropertyList(String JavaDoc propertyList) {
42         this();
43         setAsPropertyText(propertyList);
44     }
45
46     /**
47      * Constructor.
48      *
49      * @param propertyList string in the any character separated format. Occurences of the delimited may be escaed
50      * @param delimiter
51      * @see #setAsPropertyText(String)
52      */

53     public PropertyList(String JavaDoc propertyList, char delimiter) {
54         this();
55         setAsDelimitedList(propertyList, delimiter);
56     }
57
58     /**
59      * Get the string item at the specified index
60      *
61      * @param i index
62      * @return string item
63      */

64     public String JavaDoc getPropertyItem(int i) {
65         return (String JavaDoc) get(i);
66     }
67
68     /**
69      * Set list of strings from a string in the <i>Property Text</i> format.
70      * This is a bang (!) delimited list of string elements. Strings containing ! should escape this
71      * character with a backslash (\).
72      *
73      * @param propertyText property list string
74      */

75     public void setAsPropertyText(String JavaDoc propertyText) {
76         setAsDelimitedList(propertyText, '!');
77     }
78
79     /**
80      * Set list of strings from a string in the any character delimited format.
81      * Occurences of the delimiter should be escaped by a backslash (\) character.
82      *
83      * @param propertyText list string
84      * @param delimiter element delimiter
85      */

86     public void setAsDelimitedList(String JavaDoc propertyText, char delimiter) {
87         clear();
88         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
89         char ch = ' ';
90         boolean escaped = false;
91         if(propertyText!=null) {
92             for (int i = 0; i < propertyText.length(); i++) {
93                 ch = propertyText.charAt(i);
94                 if (ch == '\\' && !escaped) {
95                     escaped = true;
96                 } else {
97                     if (ch == delimiter && !escaped) {
98                         add(buf.toString());
99                         buf.setLength(0);
100                     } else {
101                         buf.append(ch);
102                         escaped = false;
103                     }
104                 }
105             }
106             if (buf.length() > 0) {
107                 add(buf.toString());
108             }
109         }
110     }
111
112     /**
113      * Get the list of strings in <i>Property Text</i> format. This is a
114      * strings in which each string element is delimited by a bang (!) character.
115      * <p>
116      * Strings containing the ! character escape it using a backslash (\).
117      *
118      * @return list of strings in property text format
119      */

120     public String JavaDoc getAsPropertyText() {
121         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
122         for (Iterator JavaDoc i = iterator(); i.hasNext();) {
123             if (buf.length() > 0) {
124                 buf.append("!");
125             }
126             String JavaDoc toString = i.next().toString();
127             String JavaDoc backslashEscaped = toString.replaceAll("\\\\", "\\\\\\\\");
128             String JavaDoc bangEscaped = backslashEscaped.replaceAll("\\!", "!!");
129             buf.append(bangEscaped);
130         }
131         return buf.toString();
132     }
133
134     /**
135      * Set list of strings from a string in the <i>Text Field Text</i> format.
136      * This is a newline (\n) delimited list of string elements.
137      *
138      * @param textFieldText string in text field text format
139      */

140     public void setAsTextFieldText(String JavaDoc textFieldText) {
141         clear();
142         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(textFieldText, "\r\n");
143         while (t.hasMoreTokens()) {
144             add(((String JavaDoc) t.nextToken()).trim());
145         }
146     }
147
148     /**
149      * Get the list of strings in <i>Property Text</i> format. This is a
150      * strings in which each string element is delimited by a newline (\n) character. It is often
151      * used as the request parameter used to pass a value into and out from
152      * one of SSL-Explorers custom input components such as {@link com.sslexplorer.input.tags.MultiEntryListBoxTag}
153      * or {@link com.sslexplorer.input.tags.MultiSelectListBoxTag}.
154      *
155      * @return list of strings in property text format
156      */

157     public String JavaDoc getAsTextFieldText() {
158         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
159         for (Iterator JavaDoc i = iterator(); i.hasNext();) {
160             if (buf.length() > 0) {
161                 buf.append("\n");
162             }
163             buf.append(i.next().toString());
164         }
165         return buf.toString();
166     }
167     
168     /**
169      * Utility method to create a list from a string in the <i>Text Field Text</i>
170      * format.
171      *
172      * @param textFieldText list of strings in text field text format.
173      * @return property list
174      * @see #setAsTextFieldText(String)
175      */

176     public static PropertyList createFromTextFieldText(String JavaDoc textFieldText) {
177         PropertyList l = new PropertyList();
178         l.setAsTextFieldText(textFieldText);
179         return l;
180     }
181     
182     /**
183      * Create a property list from an array of strings
184      *
185      * @param strings array of strings
186      * @return property list
187      */

188     public static PropertyList createFromArray(String JavaDoc[] strings) {
189         PropertyList l = new PropertyList();
190         if (strings != null) {
191             for (int i = 0; i < strings.length; i++) {
192                 l.add(strings[i]);
193             }
194         }
195         return l;
196     }
197
198     /**
199      * Get this property list as an array of strings
200      *
201      * @return property list as array of strings
202      */

203     public String JavaDoc[] asArray() {
204         String JavaDoc[] arr = new String JavaDoc[size()];
205         toArray(arr);
206         return arr;
207     }
208
209     /**
210      * Get this property list as an array of primitive integers. If any
211      * string is not an integer then an exception will be thrown
212      *
213      * @return property list as array of primitive integers
214      * @throws NumberFormatException
215      */

216     public int[] toIntArray() throws NumberFormatException JavaDoc {
217         int[] arr = new int[size()];
218         for(int i = size() - 1 ; i >=0 ; i--) {
219             arr[i] = Integer.parseInt(get(i).toString());
220         }
221         return arr;
222     }
223
224     /**
225      * Create a new property list from an array of primitive integers.
226      *
227      * @param integers array of primitive integers
228      * @return property list
229      */

230     public static PropertyList createFromArray(int[] integers) {
231         PropertyList l = new PropertyList();
232         if (integers != null) {
233             for (int i = 0; i < integers.length; i++) {
234                 l.add(String.valueOf(integers[i]));
235             }
236         }
237         return l;
238     }
239
240     /**
241      * Return a new list as a list of {@link Integer} objects
242      *
243      * @return property list as list of Integer objects
244      */

245     public List JavaDoc getIntegerObjectList() {
246         List JavaDoc<Integer JavaDoc> l =new ArrayList JavaDoc<Integer JavaDoc>();
247         for(Iterator JavaDoc i = iterator(); i.hasNext(); ) {
248             l.add(new Integer JavaDoc((String JavaDoc)i.next()));
249         }
250         return l;
251     }
252
253     /**
254      * Get this list as a {@link Properties} object assuming that each
255      * item in the list is a string in name / value format (separated by =).
256      *
257      * @return list as name / value pairs
258      */

259     public Properties JavaDoc getAsNameValuePairs() {
260         Properties JavaDoc p = new Properties JavaDoc();
261         for(String JavaDoc nameValuePair : this) {
262             new NameValuePair(nameValuePair).add(p);
263         }
264         return p;
265     }
266
267 }
268
Popular Tags