1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 25 170 public final class FormFields extends AbstractCollection { 171 private final LinkedHashMap map=new LinkedHashMap(); 172 private final ArrayList formControls=new ArrayList(); 173 174 179 public FormFields(final Collection formControls) { 180 for (final Iterator i=formControls.iterator(); i.hasNext();) { 184 final FormControl formControl=(FormControl)i.next(); 185 if (formControl.getName()!=null && formControl.getName().length()!=0) { 186 formControl.addToFormFields(this); 187 this.formControls.add(formControl); 188 } 189 } 190 } 191 192 196 public int getCount() { 197 return map.size(); 198 } 199 200 208 public int size() { 209 return getCount(); 210 } 211 212 221 public FormField get(String fieldName) { 222 if (Config.CurrentCompatibilityMode.isFormFieldNameCaseInsensitive()) fieldName=fieldName.toLowerCase(); 223 return (FormField)map.get(fieldName); 224 } 225 226 237 public Iterator iterator() { 238 return map.values().iterator(); 239 } 240 241 253 public Collection getValues(final String fieldName) { 254 final FormField formField=get(fieldName); 255 return formField==null ? null : formField.getValues(); 256 } 257 258 271 public Map getDataSet() { 272 final HashMap map=new HashMap((int)(getCount()/0.7)); 273 for (final Iterator i=iterator(); i.hasNext();) { 274 final FormField formField=(FormField)i.next(); 275 final Collection values=formField.getValues(); 276 if (values.isEmpty()) continue; 277 final String [] valuesArray=new String [values.size()]; 278 final Iterator valuesIterator=values.iterator(); 279 for (int x=0; x<values.size(); x++) valuesArray[x]=valuesIterator.next().toString(); 280 map.put(formField.getName(),valuesArray); 281 } 282 return map; 283 } 284 285 289 public void clearValues() { 290 for (final Iterator i=formControls.iterator(); i.hasNext();) 291 ((FormControl)i.next()).clearValues(); 292 } 293 294 313 public void setDataSet(final Map dataSet) { 314 clearValues(); 315 if (map==null) return; 316 for (final Iterator i=dataSet.entrySet().iterator(); i.hasNext();) { 317 final Map.Entry entry=(Map.Entry)i.next(); 318 final String fieldName=entry.getKey().toString(); 319 final FormField formField=get(fieldName); 320 if (formField!=null) { 321 if (entry.getValue() instanceof Collection) 322 formField.addValues((Collection)entry.getValue()); 323 else 324 formField.addValues((CharSequence [])entry.getValue()); 325 } 326 } 327 } 328 329 344 public boolean setValue(final String fieldName, final CharSequence value) { 345 final FormField formField=get(fieldName); 346 return formField==null ? false : formField.setValue(value); 347 } 348 349 364 public boolean addValue(final String fieldName, final CharSequence value) { 365 final FormField formField=get(fieldName); 366 return formField==null ? false : formField.addValue(value); 367 } 368 369 382 public String [] getColumnLabels() { 383 initColumns(); 384 final String [] columnLabels=new String [columns.length]; 385 for (int i=0; i<columns.length; i++) { 386 final Column column=columns[i]; 387 final String fieldName=column.formField.getFirstFormControl().getName(); columnLabels[i]=column.predefinedValue!=null 389 ? fieldName+'.'+column.predefinedValue 390 : fieldName; 391 } 392 return columnLabels; 393 } 394 395 496 public String [] getColumnValues(final Map dataSet) { 497 initColumns(); 498 final String [] columnValues=new String [columns.length]; 499 if (Config.ColumnValueFalse!=null) { 500 for (int i=0; i<columns.length; i++) 502 if (columns[i].isBoolean) columnValues[i]=Config.ColumnValueFalse; 503 } 504 for (final Iterator i=dataSet.entrySet().iterator(); i.hasNext();) { 505 final Map.Entry entry=(Map.Entry)i.next(); 506 final String fieldName=entry.getKey().toString(); 507 final FormField formField=get(fieldName); 508 if (formField!=null) { 509 final Collection values=(entry.getValue() instanceof Collection) 510 ? (Collection)entry.getValue() 511 : Arrays.asList((CharSequence [])entry.getValue()); 512 final int columnIndex=formField.columnIndex; 513 for (final Iterator valueIterator=values.iterator(); valueIterator.hasNext();) { 514 final String value=valueIterator.next().toString(); 515 for (int ci=columnIndex; ci<columns.length; ci++) { 516 final Column column=columns[ci]; 517 if (column.formField!=formField) break; 518 if (column.predefinedValue!=null) { 519 if (!column.predefinedValue.equals(value)) continue; 520 columnValues[ci]=Config.ColumnValueTrue; 521 } else { 522 if (column.isBoolean) { 523 if (value!=null) columnValues[ci]=Config.ColumnValueTrue; 524 } else if (columnValues[ci]==null) { 525 columnValues[ci]=value; 526 } else { 527 columnValues[ci]=columnValues[ci]+Config.ColumnMultipleValueSeparator+value; 528 } 529 } 530 break; 531 } 532 } 533 } 534 } 535 return columnValues; 536 } 537 538 546 public String [] getColumnValues() { 547 return getColumnValues(getDataSet()); 548 } 549 550 private void initColumns() { 551 if (columns!=null) return; 552 final ArrayList columnList=new ArrayList(); 553 for (final Iterator i=iterator(); i.hasNext();) { 554 final FormField formField=(FormField)i.next(); 555 formField.columnIndex=columnList.size(); 556 if (!formField.allowsMultipleValues() || formField.getPredefinedValues().isEmpty()) { 557 columnList.add(new Column(formField,formField.getPredefinedValues().size()==1,null)); 558 } else { 559 for (final Iterator pvi=formField.getPredefinedValues().iterator(); pvi.hasNext();) 561 columnList.add(new Column(formField,true,(String )pvi.next())); 562 if (formField.getUserValueCount()>0) columnList.add(new Column(formField,false,null)); } 564 } 565 columns=(Column[])columnList.toArray(new Column[columnList.size()]); 566 } 567 private Column[] columns=null; 568 569 private static class Column { 570 public FormField formField; 571 public boolean isBoolean; 572 public String predefinedValue; 573 public Column(final FormField formField, final boolean isBoolean, final String predefinedValue) { 574 this.formField=formField; 575 this.isBoolean=isBoolean; 576 this.predefinedValue=predefinedValue; 577 } 578 } 579 580 584 public List getFormControls() { 585 return formControls; 586 } 587 588 603 public void merge(final FormFields formFields) { 604 for (final Iterator i=formFields.iterator(); i.hasNext();) { 605 final FormField formField=(FormField)i.next(); 606 final String fieldName=formField.getName(); 607 final FormField existingFormField=get(fieldName); 608 if (existingFormField==null) 609 add(formField); 610 else 611 existingFormField.merge(formField); 612 } 613 } 614 615 619 public String getDebugInfo() { 620 final StringBuffer sb=new StringBuffer (); 621 for (final Iterator i=iterator(); i.hasNext();) { 622 sb.append(i.next()); 623 } 624 return sb.toString(); 625 } 626 627 634 public String toString() { 635 return getDebugInfo(); 636 } 637 638 void add(final FormControl formControl) { 639 add(formControl,formControl.getPredefinedValue()); 640 } 641 642 void add(final FormControl formControl, final String predefinedValue) { 643 add(formControl,predefinedValue,formControl.name); 644 } 645 646 void addName(final FormControl formControl, final String fieldName) { 647 add(formControl,null,fieldName); 648 } 649 650 void add(final FormControl formControl, final String predefinedValue, String fieldName) { 651 if (Config.CurrentCompatibilityMode.isFormFieldNameCaseInsensitive()) fieldName=fieldName.toLowerCase(); 652 FormField formField=(FormField)map.get(fieldName); 653 if (formField==null) { 654 formField=new FormField(fieldName); 655 add(formField); 656 } 657 formField.addFormControl(formControl,predefinedValue); 658 } 659 660 void replaceInOutputDocument(final OutputDocument outputDocument) { 661 for (final Iterator i=formControls.iterator(); i.hasNext();) 662 outputDocument.replace((FormControl)i.next()); 663 } 664 665 private void add(final FormField formField) { 666 map.put(formField.getName(),formField); 667 } 668 } 669
| Popular Tags
|