1 package net.sourceforge.formview; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.Serializable ; 6 import java.net.URL ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.StringTokenizer ; 14 15 import net.sourceforge.formview.displayer.DisplayerConfig; 16 import net.sourceforge.formview.displayer.DisplayersConfigResources; 17 import net.sourceforge.formview.displayer.IHTMLDisplayer; 18 import net.sourceforge.formview.permission.IPermissionsAdapter; 19 import net.sourceforge.formview.validator.ExtendedValidatorResources; 20 21 import org.apache.commons.collections.FastHashMap; 22 import org.apache.commons.digester.Digester; 23 import org.apache.commons.digester.xmlrules.DigesterLoader; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.commons.validator.Field; 27 import org.apache.commons.validator.Form; 28 import org.apache.commons.validator.FormSet; 29 import org.apache.commons.validator.Validator; 30 import org.xml.sax.SAXException ; 31 32 import au.id.jericho.lib.html.Attribute; 33 import au.id.jericho.lib.html.Attributes; 34 import au.id.jericho.lib.html.Element; 35 import au.id.jericho.lib.html.OutputDocument; 36 import au.id.jericho.lib.html.Source; 37 import au.id.jericho.lib.html.Tag; 38 39 40 47 public class FormViewResources implements Serializable { 48 49 private static final long serialVersionUID = 1L; 50 51 protected static Log log = LogFactory.getLog(FormViewResources.class); 52 53 57 protected FastHashMap hStates = new FastHashMap(); 58 59 63 protected FastHashMap hRolesDefinition = new FastHashMap(); 64 65 private RoleDefinition defaultRoleDefinition = null; 66 67 71 protected FastHashMap hDisplayers = new FastHashMap(); 72 73 protected FastHashMap hHTMLDisplayers = new FastHashMap(); 74 75 protected List lHTMLDisplayers = new ArrayList (); 76 77 87 protected Map contextValuesMap = new HashMap (); 88 89 93 private FormSetView formSet = new FormSetView(); 95 96 99 private String errorBehaviour; 100 101 102 111 public FormViewResources(InputStream in) throws IOException , SAXException { 112 this(new InputStream []{in}); 113 } 114 115 125 public FormViewResources(InputStream [] streams) 126 throws IOException , SAXException { 127 128 super(); 129 130 URL rulesUrl = this.getClass().getResource("digester-rules.xml"); 131 Digester digester = DigesterLoader.createDigester(rulesUrl); 132 digester.setNamespaceAware(true); 133 digester.setUseContextClassLoader(true); 135 136 143 144 for (int i = 0; i < streams.length; i++) { 145 digester.push(this); 146 digester.parse(streams[i]); 147 } 148 149 this.process(); 150 } 151 152 private void process() { 153 hStates.setFast(true); 154 hRolesDefinition.setFast(true); 155 156 if (this.defaultRoleDefinition == null) 158 this.defaultRoleDefinition = RoleDefinition.getDefaultRoleDefinition(); 159 160 161 } 162 163 public void mergeFormSetWithValidator(ExtendedValidatorResources validatorResources) { 164 165 171 FormSet validatorFormSet = validatorResources.getFormSet(); 173 Collection forms = validatorFormSet.getForms().values(); 174 for (Iterator iter = forms.iterator(); iter.hasNext();) { 175 Form validatorForm = (Form) iter.next(); 176 String formName = validatorForm.getName(); 177 FormView formView = formSet.getForm(formName); 179 boolean isFormViewExist = true; 180 if (formView == null) { 181 isFormViewExist = false; 182 formView = new FormView(); 184 formView.setName(formName); 185 formSet.addForm(formView, Validator.class); 186 } 187 188 List validatorFields = validatorForm.getFields(); 191 for (Iterator iterator = validatorFields.iterator(); iterator 192 .hasNext();) { 193 Field validatorField = (Field) iterator.next(); 194 String property = validatorField.getProperty(); 195 FieldView viewField = null; 197 if (isFormViewExist) 198 viewField = formView.getField(property); 199 if (viewField == null) { 200 viewField = new FieldView(); 202 viewField.setProperty(validatorField.getProperty()); 203 formView.addField(viewField, Validator.class); 205 } 206 mergeFieldWithValidator(viewField, validatorField); 207 } 208 } 209 } 210 211 212 private void mergeFieldWithValidator(FieldView viewField, Field validatorField) { 213 boolean isRequired = validatorField.isDependency("required"); 215 if (viewField.getRequired() == null) { 216 viewField.setRequired(String.valueOf(isRequired)); 217 } 218 String maxlength = validatorField.getVarValue("maxlength"); 220 if (viewField.getMaxlength() == null) { 221 viewField.setMaxlength(maxlength); 222 } 223 boolean isDate = validatorField.isDependency("date"); 225 if (viewField.getDate() == null) { 226 viewField.setDate(String.valueOf(isDate)); 227 } 228 } 229 231 public void addContextValue(String key, String value) { 232 if(!key.startsWith("${")) { 234 key = "${" + key; 235 } 236 if(!key.startsWith("}")) { 237 key = key + "}"; 238 } 239 contextValuesMap.put(key, value); 240 } 241 242 243 public void addState(State state) { 245 addState(state.getName(), state.getBehaviour()); 246 } 247 248 251 public void addState(String name, String behaviour) { 252 if (log.isDebugEnabled()) { 253 log.debug("Adding Global States => Name : " + name + ", Behaviour : " + behaviour); 254 } 255 this.hStates.put(name, behaviour); 256 } 257 258 public void addRoleDefinition(RoleDefinition roleDefinition) { 260 addRoleDefinition(roleDefinition.getName(), roleDefinition); 261 } 262 263 266 public void addRoleDefinition(String name, RoleDefinition roleDefinition) { 267 if (log.isDebugEnabled()) { 268 log.debug("Adding Role Definition => Name : " + name); 269 } 270 this.hRolesDefinition.put(name, roleDefinition); 271 if (this.defaultRoleDefinition == null && roleDefinition.isDefaultRoleDefintion()) { 273 this.defaultRoleDefinition = roleDefinition; 274 } 275 } 276 277 278 public void addDisplayer(Displayer displayer) { 280 addDisplayer(displayer.getName(), displayer.getClassName()); 281 } 282 283 286 public void addDisplayer(String name, String className) { 287 if (log.isDebugEnabled()) { 288 log.debug("Adding Displayer => Name : " + name + ", Class name : " + className); 289 } 290 this.hDisplayers.put(name, className); 291 } 292 293 public void mergeDisplayersWithDisplayerConfig(DisplayersConfigResources displayersResources) { 294 List displayersConfig = displayersResources.getDisplayerConfigs(); 296 for (Iterator iter = displayersConfig.iterator(); iter.hasNext();) { 297 DisplayerConfig displayerConfig = (DisplayerConfig) iter.next(); 298 addDisplayerClass(displayerConfig); 299 } 300 301 } 302 303 public void addDisplayerClass(IHTMLDisplayer htmlDisplayer) { 304 this.lHTMLDisplayers.add(htmlDisplayer); 305 this.hHTMLDisplayers.put(htmlDisplayer.getName(), htmlDisplayer); 306 } 307 309 310 314 public void addForm(FormView formView) { 315 formSet.addForm(formView); 316 330 } 331 332 333 334 public FormSetView getFormSet() { 335 return formSet; 336 346 } 347 348 public FormView getForm(String formKey, String state) { 349 FormSetView set = getFormSet(); 350 if ((set != null)) { 351 return set.getForm(formKey, state); 352 } 353 return null; 354 } 355 356 public String getDefaultBehaviour(String defaultState) { 357 return (String )hStates.get(defaultState); 358 } 359 360 362 public FormView getFormView(String formName, String state) { 363 Map roles = null; 364 return getFormView(formName, state, roles); 365 } 366 367 public FormView getFormView(String formName, String state, String [] roles) { 368 Map rolesMap = new HashMap (); 369 for(int i=0; i< roles.length; i++) { 370 rolesMap.put(roles[i], roles[i]); 371 } 372 return getFormView(formName, state, roles); 373 } 374 378 public FormView getFormView(String formName, String state, Map roles) { 379 FormView form = this.getForm(formName, state); 381 if (form != null) { 382 FormView formCloned = (FormView)form.clone(); 385 386 if (roles != null) { 388 } 390 else { 391 return formCloned; 392 } 393 } 394 return null; 395 } 396 public String processHtmlContent(FormView form, String state, IPermissionsAdapter permissionsAdapter, String htmlContent) { 398 String defaultBehaviour = this.getDefaultBehaviour(state); 399 OutputDocument outputDocument = new OutputDocument(htmlContent); 400 Source source = new Source(htmlContent); 401 402 Map indexedFieldMap = new HashMap (); for (Iterator iter = lHTMLDisplayers.iterator(); iter.hasNext();) { 405 boolean isHTMLSelect = false; 406 407 IHTMLDisplayer htmlDisplayer = (IHTMLDisplayer) iter.next(); 408 String elementType = htmlDisplayer.getName(); 409 String type1 = elementType; 410 String type2 = ""; 411 int index = elementType.indexOf("_"); 412 if (index != -1) { 413 type1= elementType.substring(0, index); 415 type2 = elementType.substring(index + 1, elementType.length()); 416 } 417 isHTMLSelect = Tag.SELECT.equalsIgnoreCase(type1); 419 List elementsOfType1 = source.findAllElements(type1); 421 for (Iterator iterElements = elementsOfType1.iterator(); iterElements.hasNext();) { 422 Element currentElement = (Element) iterElements.next(); 424 Attributes currentAttributes = currentElement.getAttributes(); 426 if (type2.length() > 0) { 427 String attributeType = currentAttributes.getValue("type"); 429 if (!attributeType.equalsIgnoreCase(type2)) 430 continue; 431 } 432 433 String attributesName = htmlDisplayer.getAttributesName(); 434 if (attributesName == null || attributesName.length() <1) 435 attributesName = "name"; 436 StringTokenizer attributesNameToken = new StringTokenizer (attributesName, ","); 437 while (attributesNameToken.hasMoreElements()) 439 { 440 String attributeName = (String )attributesNameToken.nextElement(); 441 String elementName = currentAttributes.getValue(attributeName); 443 FieldView field = null; 445 if (form != null && elementName != null) { 446 field = form.getField(elementName, state); 447 if (field != null && field.isIndexed()) { 448 Integer fieldIndex = (Integer )indexedFieldMap.get(elementName); 450 if (fieldIndex == null) { 451 fieldIndex = new Integer (0); 452 } 453 FieldView fieldWithIndex = field.getFieldWithIndex(fieldIndex); 455 if (fieldWithIndex != null) 456 field = fieldWithIndex; 457 indexedFieldMap.put(elementName, new Integer (fieldIndex.intValue() + 1)); 458 459 } 460 } 461 Map allContextValuesMap = getContextValuesMap(field); 463 if (isHTMLSelect) { 466 updateContextValuesMapWithHTMLSelect(currentElement, allContextValuesMap); 467 } 468 else { 469 String elementValue = currentAttributes.getValue("value"); 471 if (elementValue != null) { 472 allContextValuesMap.put("${elementValue}", elementValue); 473 } 474 } 475 updateContextValuesMapWithAttributesHTMLElement(currentElement, allContextValuesMap); 477 478 479 allContextValuesMap.put("${elementName}", ""); 481 if (elementName != null) 482 allContextValuesMap.put("${elementName}", elementName); 483 484 if (permissionsAdapter != null && field != null) { 486 if (field.getRoles() != null && field.getRoles().length() > 0) { 487 RoleDefinition fieldRoleDefinition = null; 488 String roleDefinitionName = field.getRoleDefinitionName(); 491 if (roleDefinitionName != null && roleDefinitionName.length() > 0 ) { 492 fieldRoleDefinition = (RoleDefinition)hRolesDefinition.get(roleDefinitionName); 493 } 494 if (fieldRoleDefinition == null) { 495 fieldRoleDefinition = defaultRoleDefinition; 496 } 497 String behaviour = permissionsAdapter.getBehaviour(field, defaultBehaviour, fieldRoleDefinition); 498 if (behaviour != null) { 499 field.setBehaviour(behaviour); 500 } 501 } 502 } 503 504 htmlDisplayer.processHTML(field, defaultBehaviour, allContextValuesMap, currentElement, outputDocument); 506 } 507 } 508 } 509 return outputDocument.toString(); 510 } 511 512 513 public Map getContextValuesMap(FieldView field) { 514 if (field == null) { 515 return contextValuesMap; 516 } 517 Map allContextValuesMap = new HashMap (contextValuesMap); 519 allContextValuesMap.put("${maxlength}", field.getMaxlength()); 520 return allContextValuesMap; 521 } 522 523 524 private void updateContextValuesMapWithHTMLSelect(Element selectElement, Map contextValuesMap) { 525 Source selectSource = new Source(selectElement); 526 int i=0; 528 List optionsSelected = null; 529 Element firstOption = null; 530 boolean isMultiple = false; 532 Attributes selectAttributes = selectElement.getAttributes(); 533 if (selectAttributes != null && selectAttributes.getValue("multiple") != null) { 534 isMultiple = true; 535 } 536 537 List options = selectSource.findAllElements(Tag.OPTION); 538 if (options != null) 539 { 540 for (Iterator iter = options.iterator(); iter.hasNext(); i++) { 541 Element option = (Element) iter.next(); 542 if (i ==0) { 543 firstOption = option; 544 } 545 Attributes optionAttributes = option.getAttributes(); 546 if (optionAttributes != null) { 547 boolean isOptionSelected = (optionAttributes.getValue("selected") != null); 548 if (isOptionSelected) { 549 String optionValue = optionAttributes.getValue("value"); 550 String optionLabel = option.getContent().toString(); 551 if (optionsSelected == null) 552 optionsSelected = new ArrayList (); 553 optionsSelected.add(new LabelValue(optionLabel, optionValue)); 554 } 555 556 569 } 570 } 571 if (optionsSelected == null && firstOption != null) { 572 Attributes optionAttributes = firstOption.getAttributes(); 573 if (optionAttributes != null) { 574 String optionValue = optionAttributes.getValue("value"); 575 String optionLabel = firstOption.getContent().toString(); 576 if (optionsSelected == null) 577 optionsSelected = new ArrayList (); 578 optionsSelected.add(new LabelValue(optionLabel, optionValue)); 579 } 580 } 581 582 if (!isMultiple && optionsSelected != null && optionsSelected.size() > 0) { 583 LabelValue firstLabelvalue = (LabelValue)optionsSelected.get(0); 584 putOptionToMap(contextValuesMap, firstLabelvalue.getValue(), firstLabelvalue.getLabel()); 585 } 586 else { 587 contextValuesMap.put("${optionsSelected}", optionsSelected); 588 } 589 } 590 } 591 592 593 594 private void putOptionToMap(Map contextValuesMap, String optionValue, String optionLabel) { 595 contextValuesMap.put("${optionValue}", optionValue); 596 contextValuesMap.put("${optionLabel}", optionLabel); 597 } 598 599 private void updateContextValuesMapWithAttributesHTMLElement(Element htmlElement, Map contextValuesMap) { 600 Attributes attributes = htmlElement.getAttributes(); 601 for (Iterator iter = attributes.iterator(); iter.hasNext();) { 602 Attribute attribute = (Attribute) iter.next(); 603 contextValuesMap.put("${" + attribute.getName() + "}", attribute.getValue()); 604 } 605 } 606 607 public String getErrorBehaviour() { 608 if (errorBehaviour == null) 609 return FormViewConstants.BEHAVIOUR_ERROR; 610 return errorBehaviour; 611 } 612 613 public void setErrorBehaviour(String errorBehaviour) { 614 this.errorBehaviour = errorBehaviour; 615 } 616 617 618 619 620 } 621 | Popular Tags |