1 3 package com.sslexplorer.boot; 4 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Properties ; 9 import java.util.StringTokenizer ; 10 11 23 public class PropertyList extends ArrayList <String > { 24 25 private static final long serialVersionUID = -6816117522796660457L; 26 public static final PropertyList EMPTY_LIST = new PropertyList(); 27 28 31 public PropertyList() { 32 super(); 33 } 34 35 41 public PropertyList(String propertyList) { 42 this(); 43 setAsPropertyText(propertyList); 44 } 45 46 53 public PropertyList(String propertyList, char delimiter) { 54 this(); 55 setAsDelimitedList(propertyList, delimiter); 56 } 57 58 64 public String getPropertyItem(int i) { 65 return (String ) get(i); 66 } 67 68 75 public void setAsPropertyText(String propertyText) { 76 setAsDelimitedList(propertyText, '!'); 77 } 78 79 86 public void setAsDelimitedList(String propertyText, char delimiter) { 87 clear(); 88 StringBuffer buf = new StringBuffer (); 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 120 public String getAsPropertyText() { 121 StringBuffer buf = new StringBuffer (); 122 for (Iterator i = iterator(); i.hasNext();) { 123 if (buf.length() > 0) { 124 buf.append("!"); 125 } 126 String toString = i.next().toString(); 127 String backslashEscaped = toString.replaceAll("\\\\", "\\\\\\\\"); 128 String bangEscaped = backslashEscaped.replaceAll("\\!", "!!"); 129 buf.append(bangEscaped); 130 } 131 return buf.toString(); 132 } 133 134 140 public void setAsTextFieldText(String textFieldText) { 141 clear(); 142 StringTokenizer t = new StringTokenizer (textFieldText, "\r\n"); 143 while (t.hasMoreTokens()) { 144 add(((String ) t.nextToken()).trim()); 145 } 146 } 147 148 157 public String getAsTextFieldText() { 158 StringBuffer buf = new StringBuffer (); 159 for (Iterator 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 176 public static PropertyList createFromTextFieldText(String textFieldText) { 177 PropertyList l = new PropertyList(); 178 l.setAsTextFieldText(textFieldText); 179 return l; 180 } 181 182 188 public static PropertyList createFromArray(String [] 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 203 public String [] asArray() { 204 String [] arr = new String [size()]; 205 toArray(arr); 206 return arr; 207 } 208 209 216 public int[] toIntArray() throws NumberFormatException { 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 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 245 public List getIntegerObjectList() { 246 List <Integer > l =new ArrayList <Integer >(); 247 for(Iterator i = iterator(); i.hasNext(); ) { 248 l.add(new Integer ((String )i.next())); 249 } 250 return l; 251 } 252 253 259 public Properties getAsNameValuePairs() { 260 Properties p = new Properties (); 261 for(String nameValuePair : this) { 262 new NameValuePair(nameValuePair).add(p); 263 } 264 return p; 265 } 266 267 } 268 | Popular Tags |