1 16 19 20 package org.apache.pluto.portalImpl.util; 21 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.apache.pluto.util.StringUtils; 27 28 35 36 public abstract class NameValuePairs 37 { 38 39 45 46 public int size () 47 { 48 return (iEntries.size ()); 49 } 50 51 60 61 public String getString (String aName) 62 { 63 return (findString (aName)); 64 } 65 66 77 78 public String getString (String aName, String aDefault) 79 { 80 String result = findString (aName); 81 82 if (result == null) 83 result = aDefault; 84 85 return (result); 86 } 87 88 97 98 public String [] getStrings (String aName) 99 { 100 String [] result = null; 101 102 Entry entry = findEntry (aName); 103 104 if (entry != null) 105 result = entry.iValues; 106 107 return (result); 108 109 } 110 111 public Integer getInteger (String aName) 112 { 113 return (getInteger (aName, null)); 114 } 115 116 public Integer getInteger (String aName, Integer aDefault) 117 { 118 Integer result = aDefault; 119 120 String value = findString (aName); 121 122 if (value != null) 123 result = Integer.valueOf (value); 124 125 return (result); 126 } 127 128 public int getInteger (String aName, int aDefault) 129 { 130 int result = aDefault; 131 132 String value = findString (aName); 133 134 if (value != null) 135 result = Integer.parseInt (value); 136 137 return (result); 138 } 139 140 public Boolean getBoolean (String aName) 141 { 142 return (getBoolean (aName, null)); 143 } 144 145 public Boolean getBoolean (String aName, Boolean aDefault) 146 { 147 Boolean result = aDefault; 148 149 String value = findString (aName); 150 151 if (value != null) 152 { 153 result = StringUtils.booleanOf (value); 154 } 155 156 return (result); 157 } 158 159 public boolean getBoolean (String aName, boolean aDefault) 160 { 161 return (getBoolean (aName, aDefault ? Boolean.TRUE : Boolean.FALSE).booleanValue ()); 162 } 163 164 public Iterator names () 165 { 166 return (new EntryIterator (this)); 167 } 168 169 public final Iterator keys () 170 { 171 return (names ()); 172 } 173 174 public void setParent (NameValuePairs aParent) 175 { 176 iParent = aParent; 177 } 178 179 public String toString () 180 { 181 return (iEntries.toString ()); 182 } 183 184 186 protected NameValuePairs () 187 { 188 } 189 190 protected void add (String aName, String aValue) 191 { 192 add (aName, new String [] { aValue }); 193 } 194 195 protected void add (String aName, String [] aValues) 196 { 197 if (aName == null) 198 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aName\" cannot be null.")); 199 if (aValues == null) 200 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aValues\" cannot be null.")); 201 202 for (int i = 0; i < aValues.length; i++) 203 { 204 if (aValues [i] == null) 205 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aValues\" cannot contain null.")); 206 } 207 208 Entry entry = findEntry (aName); 209 210 if (entry == null) 211 { 212 entry = new Entry (aName, aValues); 213 214 iEntries.add (entry); 215 } 216 else 217 { 218 String [] values = new String [entry.iValues.length + aValues.length]; 219 220 System.arraycopy (entry.iValues, 0, values, 0, entry.iValues.length); 221 System.arraycopy (aValues, 0, values, entry.iValues.length, aValues.length); 222 223 entry.iValues = values; 224 } 225 } 226 227 protected Entry findEntry (String aName) 228 { 229 if (aName == null) 230 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aName\" cannot be null!")); 231 232 Entry result = null; 233 234 for (Iterator iter = iEntries.iterator (); iter.hasNext (); ) 235 { 236 Entry entry = (Entry) iter.next (); 237 238 if (entry.iName.equals (aName)) 239 { 240 result = entry; 241 break; 242 } 243 } 244 245 if (result == null && iParent != null) 246 { 247 result = iParent.findEntry (aName); 248 } 249 250 return (result); 251 } 252 253 protected void removeEntry (String aName) 254 { 255 if (aName == null) 256 throw (new IllegalArgumentException ("NameValuePairs: Argument \"aName\" cannot be null!")); 257 258 boolean found = false; 259 260 for (Iterator iter = iEntries.iterator (); iter.hasNext (); ) 261 { 262 Entry entry = (Entry) iter.next (); 263 264 if (entry.iName.equals (aName)) 265 { 266 iter.remove (); 267 268 found = true; 269 break; 270 } 271 } 272 273 if (! found && iParent != null) 274 { 275 iParent.removeEntry (aName); 276 } 277 } 278 279 private NameValuePairs iParent; 280 281 private List iEntries = new LinkedList (); 282 283 private String findString (String aName) 284 { 285 String result = null; 286 287 Entry entry = findEntry (aName); 288 289 if (entry != null) 290 result = entry.iValues [0]; 291 292 return (result); 293 } 294 295 NameValuePairs getParent() 296 { 297 return iParent; 298 } 299 300 List getEntries() 301 { 302 return iEntries; 303 } 304 305 public static class Entry 306 { 307 308 String iName; 309 String [] iValues; 310 311 protected Entry (String aName, String [] aValues) 312 { 313 iName = aName; 314 iValues = aValues; 315 } 316 317 public String toString () 318 { 319 StringBuffer result = new StringBuffer (); 320 321 result.append (iName); 322 result.append (" = "); 323 324 for (int i = 0; i < iValues.length; i++) 325 { 326 if (i > 0) 327 result.append (", "); 328 329 result.append (iValues [i]); 330 } 331 332 return (result.toString ()); 333 } 334 } 335 336 private static class EntryIterator implements Iterator 337 { 338 339 private NameValuePairs iPairs; 340 private Iterator iIterator; 341 342 private EntryIterator (NameValuePairs aPairs) 343 { 344 iPairs = aPairs; 345 iIterator = iPairs.getEntries().iterator(); 346 } 347 348 350 public boolean hasNext () 351 { 352 if (!nextParent()) return false; 353 return (iIterator.hasNext ()); 354 } 355 356 public Object next () 357 { 358 if (!nextParent()) return null; 359 return (((Entry) iIterator.next ()).iName); 360 } 361 362 public void remove() 363 { 364 iIterator.remove (); 365 } 366 367 369 private boolean nextParent() 370 { 371 while (!iIterator.hasNext ()) 372 { 373 iPairs = iPairs.getParent(); 374 if (iPairs==null) return false; 375 iIterator = iPairs.getEntries().iterator(); 376 } 377 return true; 378 } 379 380 } 381 } 382 | Popular Tags |