1 16 package org.apache.cocoon.transformation; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.parameters.Parameters; 21 import org.apache.avalon.framework.service.ServiceSelector; 22 import org.apache.avalon.framework.thread.ThreadSafe; 23 24 import org.apache.avalon.excalibur.pool.Recyclable; 25 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.acting.ValidatorActionResult; 28 import org.apache.cocoon.transformation.helpers.FormValidatorHelper; 29 import org.apache.cocoon.components.modules.input.InputModule; 30 import org.apache.cocoon.environment.SourceResolver; 31 import org.apache.cocoon.util.HashMap; 32 import org.apache.cocoon.xml.dom.DOMStreamer; 33 import org.apache.commons.lang.BooleanUtils; 34 35 import org.w3c.dom.DocumentFragment ; 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.helpers.AttributesImpl ; 39 40 import java.io.IOException ; 41 import java.util.Iterator ; 42 import java.util.LinkedList ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 150 public class SimpleFormTransformer extends AbstractSAXTransformer implements Recyclable { 151 152 153 private boolean stripNumber = true; 154 155 156 157 private static final int ELEMENT_DEFAULT = 0; 158 159 private static final int ELEMENT_INPUT = 1; 160 161 private static final int ELEMENT_SELECT = 2; 162 163 private static final int ELEMENT_OPTION = 3; 164 165 private static final int ELEMENT_TXTAREA = 4; 166 167 private static final int ELEMENT_ERROR = 5; 168 169 private static final int ELEMENT_FORM = 6; 170 171 private static final int ELEMENT_REPEAT = 7; 172 173 private static final Integer defaultElement = new Integer (ELEMENT_DEFAULT); 174 175 176 private static final int TYPE_DEFAULT = 0; 177 178 private static final int TYPE_CHECKBOX = 1; 179 180 private static final int TYPE_RADIO = 2; 181 182 private static final Integer defaultType = new Integer (TYPE_DEFAULT); 183 184 protected static final String INPUT_MODULE_ROLE = InputModule.ROLE; 185 protected static final String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE + "Selector"; 186 187 188 private static final HashMap elementNames; 189 190 private static final HashMap inputTypes; 191 192 private static final HashMap validatorResults; 193 194 private static final HashMap validatorResultLabel; 195 196 197 static { 198 HashMap names = new HashMap(); 199 names.put("input", new Integer (ELEMENT_INPUT)); 200 names.put("select", new Integer (ELEMENT_SELECT)); 201 names.put("option", new Integer (ELEMENT_OPTION)); 202 names.put("textarea", new Integer (ELEMENT_TXTAREA)); 203 names.put("error", new Integer (ELEMENT_ERROR)); 204 names.put("form", new Integer (ELEMENT_FORM)); 205 names.put("repeat", new Integer (ELEMENT_REPEAT)); 206 elementNames = names; 207 names = null; 208 209 names = new HashMap(); 210 names.put("checkbox", new Integer (TYPE_CHECKBOX)); 211 names.put("radio", new Integer (TYPE_RADIO)); 212 inputTypes = names; 213 names = null; 214 215 names = new HashMap(); 216 names.put("ok", ValidatorActionResult.OK); 217 names.put("not-present", ValidatorActionResult.NOTPRESENT); 218 names.put("error", ValidatorActionResult.ERROR); 219 names.put("is-null", ValidatorActionResult.ISNULL); 220 names.put("too-small", ValidatorActionResult.TOOSMALL); 221 names.put("too-large", ValidatorActionResult.TOOLARGE); 222 names.put("no-match", ValidatorActionResult.NOMATCH); 223 validatorResultLabel = names; 224 225 names = new HashMap(); 226 names.put(ValidatorActionResult.OK, "ok"); 227 names.put(ValidatorActionResult.NOTPRESENT, "not-present"); 228 names.put(ValidatorActionResult.ERROR, "error"); 229 names.put(ValidatorActionResult.ISNULL, "is-null"); 230 names.put(ValidatorActionResult.TOOSMALL, "too-small"); 231 names.put(ValidatorActionResult.TOOLARGE, "too-large"); 232 names.put(ValidatorActionResult.NOMATCH, "no-match"); 233 validatorResults = names; 234 names = null; 235 } 236 237 238 protected Object [] values; 239 240 241 protected Map validationResults; 242 243 244 private boolean fixed; 245 246 private boolean documentFixed; 247 248 private String fixedName = "fixed"; 249 private String prefix; 250 private String suffix; 251 private String defaultPrefix; 252 private String defaultSuffix; 253 private String separator; 254 private String formName; 255 private boolean useFormName; 256 private boolean useFormNameTwice; 257 private boolean ignoreValidation; 258 private int decorationSize = 1; 259 260 private String defaultInput = "request-param"; 261 private Configuration defaultInputConf; 262 private Configuration inputConf; 263 private InputModule input; 264 private ServiceSelector inputSelector; 265 private String inputName; 266 267 268 protected boolean skipChildrenOnly; 269 270 271 protected int recordingCount; 272 273 274 protected List repeater; 275 276 277 protected Map formValues; 278 279 282 protected static class RepeaterStatus { 283 public String var = null; 284 public String expr = null; 285 public int count = 0; 286 287 public RepeaterStatus(String var, int count, String expr) { 288 this.var = var; 289 this.count = count; 290 this.expr = expr; 291 } 292 293 public String toString() { 294 return "[" + this.var + "," + this.expr + "," + this.count + "]"; 295 } 296 } 297 298 301 protected static class ValueList { 302 private int current = -1; 303 private Object [] values = null; 304 305 public ValueList(Object [] values) { 306 this.values = values; 307 this.current = (values != null && values.length > 0 ? 0 : -1); 308 } 309 310 public Object getNext() { 311 Object result = null; 312 if (this.values != null) { 313 if (this.current < this.values.length) { 314 result = this.values[this.current++]; 315 } 316 } 317 return result; 318 } 319 } 320 321 public SimpleFormTransformer() { 322 this.defaultNamespaceURI = ""; 323 } 324 325 326 private void reset() { 327 this.skipChildrenOnly = false; 328 this.values = null; 329 this.validationResults = null; 330 this.documentFixed = false; 331 this.fixed = false; 332 this.formName = null; 333 this.recordingCount = 0; 334 this.repeater = new LinkedList (); 335 this.formValues = new HashMap(); 336 337 if (this.inputSelector != null) { 338 if (this.input != null) 339 this.inputSelector.release(this.input); 340 this.manager.release(this.inputSelector); 341 } 342 } 343 344 347 public void configure(Configuration config) throws ConfigurationException { 348 super.configure(config); 349 350 this.defaultInputConf = config.getChild("input-module"); 351 this.defaultInput = this.defaultInputConf.getAttribute("name", this.defaultInput); 352 this.separator = config.getChild("separator").getValue(this.separator); 353 this.defaultPrefix = config.getChild("prefix").getValue(this.defaultPrefix); 354 this.defaultSuffix = config.getChild("suffix").getValue(this.defaultSuffix); 355 this.fixedName = config.getChild("fixed-attribute").getValue(this.fixedName); 356 this.useFormName = config.getChild("use-form-name").getValueAsBoolean(this.useFormName); 357 this.useFormNameTwice = 358 config.getChild("use-form-name-twice").getValueAsBoolean(this.useFormNameTwice); 359 this.useFormName = this.useFormName || this.useFormNameTwice; 360 if (this.useFormName) { 361 this.separator = 362 (this.separator == null || this.separator.equals("") ? "/" : this.separator); 363 this.defaultPrefix = this.separator; 364 } 365 this.ignoreValidation = 366 config.getChild("ignore-validation").getValueAsBoolean(this.ignoreValidation); 367 this.decorationSize = config.getChild("decoration").getValueAsInteger(this.decorationSize); 368 this.stripNumber = config.getChild("strip-number").getValueAsBoolean(this.stripNumber); 369 } 370 371 374 private void evaluateParameters() { 375 this.documentFixed = this.parameters.getParameterAsBoolean("fixed", false); 376 this.fixed = this.documentFixed; 377 this.prefix = this.parameters.getParameter("prefix", this.defaultPrefix); 378 this.suffix = this.parameters.getParameter("suffix", this.defaultSuffix); 379 this.inputName = this.parameters.getParameter("input", null); 380 this.decorationSize = 381 this.parameters.getParameterAsInteger("decoration", this.decorationSize); 382 this.stripNumber = this.parameters.getParameterAsBoolean("strip-number", this.stripNumber); 383 } 384 385 393 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 394 throws ProcessingException, SAXException , IOException { 395 396 this.reset(); 397 398 super.setup(resolver, objectModel, src, par); 399 400 if (request == null) { 401 getLogger().debug("no request object"); 402 throw new ProcessingException("no request object"); 403 } 404 this.evaluateParameters(); 405 this.setupInputModule(); 406 407 } 408 409 412 private void setupInputModule() { 413 this.inputConf = null; 414 if (this.ignoreValidation) { 415 this.validationResults = null; 416 } else { 417 this.validationResults = FormValidatorHelper.getResults(this.objectModel); 418 } 419 420 if (this.inputName == null) { 421 this.inputName = this.defaultInput; 422 this.inputConf = this.defaultInputConf; 423 } 424 425 try { 426 this.inputSelector = (ServiceSelector) this.manager.lookup(INPUT_MODULE_SELECTOR); 428 if (this.inputName != null 429 && this.inputSelector != null 430 && this.inputSelector.isSelectable(this.inputName)) { 431 this.input = (InputModule) this.inputSelector.select(this.inputName); 432 if (!(this.input instanceof ThreadSafe 433 && this.inputSelector instanceof ThreadSafe)) { 434 this.inputSelector.release(this.input); 435 this.manager.release(this.inputSelector); 436 this.input = null; 437 this.inputSelector = null; 438 } 439 } else { 440 if (this.inputName != null) 441 if (getLogger().isErrorEnabled()) 442 getLogger().error( 443 "A problem occurred setting up '" 444 + this.inputName 445 + "': Selector is " 446 + (this.inputSelector != null ? "not " : "") 447 + "null, Component is " 448 + (this.inputSelector != null 449 && this.inputSelector.isSelectable(this.inputName) 450 ? "known" 451 : "unknown")); 452 } 453 } catch (Exception e) { 454 if (getLogger().isWarnEnabled()) 455 getLogger().warn( 456 "A problem occurred setting up '" + this.inputName + "': " + e.getMessage()); 457 } 458 } 459 460 463 public void recycle() { 464 reset(); 465 super.recycle(); 466 } 467 468 471 protected String printAttributes(Attributes attr) { 472 StringBuffer sb = new StringBuffer (); 473 sb.append('['); 474 for (int i = 0; i < attr.getLength(); i++) { 475 sb.append('@').append(attr.getLocalName(i)).append("='").append( 476 attr.getValue(i)).append( 477 "' "); 478 } 479 sb.append(']'); 480 return sb.toString(); 481 } 482 483 487 protected void startCheckableElement( 488 String aName, 489 String uri, 490 String name, 491 String raw, 492 AttributesImpl attributes) 493 throws SAXException { 494 495 this.values = this.getValues(aName); 497 String checked = attributes.getValue("checked"); 498 String value = attributes.getValue("value"); 499 boolean found = false; 500 501 if (getLogger().isDebugEnabled()) 502 getLogger().debug( 503 "startCheckableElement " 504 + name 505 + " attributes " 506 + this.printAttributes(attributes)); 507 if (this.values != null) { 508 if (getLogger().isDebugEnabled()) 509 getLogger().debug("replacing"); 510 for (int i = 0; i < this.values.length; i++) { 511 if (this.values[i].equals(value)) { 512 found = true; 513 if (checked == null) { 514 attributes.addAttribute("", "checked", "checked", "CDATA", ""); 515 } 516 break; 517 } 518 } 519 if (!found && checked != null) { 520 attributes.removeAttribute(attributes.getIndex("checked")); 521 } 522 } 523 this.relayStartElement(uri, name, raw, attributes); 524 } 525 526 530 protected void startNonCheckableElement( 531 String aName, 532 String uri, 533 String name, 534 String raw, 535 AttributesImpl attributes) 536 throws SAXException { 537 538 Object fValue = this.getNextValue(aName); 540 String value = attributes.getValue("value"); 541 if (getLogger().isDebugEnabled()) 542 getLogger().debug( 543 "startNonCheckableElement " 544 + name 545 + " attributes " 546 + this.printAttributes(attributes)); 547 if (fValue != null) { 548 if (getLogger().isDebugEnabled()) 549 getLogger().debug("replacing"); 550 if (value != null) { 551 attributes.setValue(attributes.getIndex("value"), String.valueOf(fValue)); 552 } else { 553 attributes.addAttribute("", "value", "value", "CDATA", String.valueOf(fValue)); 554 } 555 } 556 this.relayStartElement(uri, name, raw, attributes); 557 } 558 559 563 protected void startInputElement(String uri, String name, String raw, Attributes attr) 564 throws SAXException { 565 566 String aName = getName(attr.getValue("name")); 568 String fixed = attr.getValue(this.fixedName); 569 570 if (getLogger().isDebugEnabled()) 571 getLogger().debug( 572 "startInputElement " + name + " attributes " + this.printAttributes(attr)); 573 if (aName == null || this.fixed || (fixed != null && BooleanUtils.toBoolean(fixed))) { 574 this.relayStartElement(uri, name, raw, attr); 575 576 } else { 577 if (getLogger().isDebugEnabled()) 578 getLogger().debug("replacing"); 579 580 attr = this.normalizeAttributes(attr); 581 582 AttributesImpl attributes = null; 583 if (attr instanceof AttributesImpl ) { 584 attributes = (AttributesImpl ) attr; 585 } else { 586 attributes = new AttributesImpl (attr); 587 } 588 String type = attributes.getValue("type"); 589 switch (((Integer ) inputTypes.get(type, defaultType)).intValue()) { 590 case TYPE_CHECKBOX : 591 case TYPE_RADIO : 592 this.startCheckableElement(aName, uri, name, raw, attributes); 593 break; 594 595 case TYPE_DEFAULT : 596 this.startNonCheckableElement(aName, uri, name, raw, attributes); 597 break; 598 } 599 this.values = null; 600 } 601 } 602 603 607 protected void startSelectElement(String uri, String name, String raw, Attributes attr) 608 throws SAXException { 609 610 String aName = getName(attr.getValue("name")); 612 String fixed = attr.getValue(this.fixedName); 613 this.values = null; 614 if (getLogger().isDebugEnabled()) 615 getLogger().debug( 616 "startSelectElement " + name + " attributes " + this.printAttributes(attr)); 617 if (aName != null && !(this.fixed || (fixed != null && BooleanUtils.toBoolean(fixed)))) { 618 if (attr.getIndex("multiple") > -1) { 619 this.values = this.getValues(aName); 620 } else { 621 Object val = this.getNextValue(aName); 622 if (val != null) { 623 this.values = new Object [1]; 624 this.values[0] = val; 625 } else { 626 this.values = null; 627 } 628 } 629 attr = this.normalizeAttributes(attr); 630 } 631 this.relayStartElement(uri, name, raw, attr); 632 } 633 634 640 protected void startOptionElement(String uri, String name, String raw, Attributes attr) 641 throws SAXException { 642 643 if (getLogger().isDebugEnabled()) 645 getLogger().debug( 646 "startOptionElement " + name + " attributes " + this.printAttributes(attr)); 647 if (this.values == null || this.fixed) { 648 this.relayStartElement(uri, name, raw, attr); 649 } else { 650 if (getLogger().isDebugEnabled()) 651 getLogger().debug("replacing"); 652 AttributesImpl attributes = null; 653 if (attr instanceof AttributesImpl ) { 654 attributes = (AttributesImpl ) attr; 655 } else { 656 attributes = new AttributesImpl (attr); 657 } 658 String selected = attributes.getValue("selected"); 659 String value = attributes.getValue("value"); 660 boolean found = false; 661 662 for (int i = 0; i < this.values.length; i++) { 663 if (this.values[i].equals(value)) { 664 found = true; 665 if (selected == null) { 666 attributes.addAttribute("", "selected", "selected", "CDATA", ""); 667 } 668 break; 669 } 670 } 671 if (!found && selected != null) { 672 attributes.removeAttribute(attributes.getIndex("selected")); 673 } 674 675 this.relayStartElement(uri, name, raw, attributes); 676 } 677 } 678 679 683 protected void startTextareaElement(String uri, String name, String raw, Attributes attributes) 684 throws SAXException { 685 686 String aName = getName(attributes.getValue("name")); 687 String fixed = attributes.getValue(this.fixedName); 688 Object value = null; 689 if (getLogger().isDebugEnabled()) 690 getLogger().debug( 691 "startTextareaElement " + name + " attributes " + this.printAttributes(attributes)); 692 if (aName != null) { 693 value = this.getNextValue(aName); 694 } 695 if (value == null || this.fixed || (fixed != null && BooleanUtils.toBoolean(fixed))) { 696 this.relayStartElement(uri, name, raw, attributes); 697 } else { 698 if (getLogger().isDebugEnabled()) 699 getLogger().debug("replacing"); 700 this.relayStartElement(uri, name, raw, this.normalizeAttributes(attributes)); 701 String valString = String.valueOf(value); 702 this.characters(valString.toCharArray(), 0, valString.length()); 703 if (this.ignoreEventsCount == 0) 705 this.skipChildrenOnly = true; 706 this.ignoreEventsCount++; 707 } 708 } 709 710 717 protected void startErrorElement(String uri, String name, String raw, Attributes attr) 718 throws SAXException { 719 720 if (getLogger().isDebugEnabled()) 721 getLogger().debug( 722 "startErrorElement " + name + " attributes " + this.printAttributes(attr)); 723 if (this.ignoreValidation) { 724 this.relayStartElement(uri, name, raw, attr); 725 } else if (this.validationResults == null || this.fixed) { 726 this.relayStartElement(true, false, uri, name, raw, attr); 727 } else { 728 String aName = attr.getValue("name"); 729 if (aName == null) { 730 this.relayStartElement(uri, name, raw, attr); 731 } else { 732 ValidatorActionResult validation = 733 FormValidatorHelper.getParamResult(this.objectModel, aName); 734 String when = attr.getValue("when"); 735 String when_ge = attr.getValue("when-ge"); 736 737 if ((when != null && when.equals(validatorResults.get(validation))) 738 || (when_ge != null 739 && validation.ge( 740 (ValidatorActionResult) validatorResultLabel.get( 741 when_ge, 742 ValidatorActionResult.MAXERROR)))) { 743 AttributesImpl attributes = null; 744 if (attr instanceof AttributesImpl ) { 745 attributes = (AttributesImpl ) attr; 746 } else { 747 attributes = new AttributesImpl (attr); 748 } 749 attributes.removeAttribute(attributes.getIndex("name")); 751 if (when != null) 752 attributes.removeAttribute(attributes.getIndex("when")); 753 if (when_ge != null) 754 attributes.removeAttribute(attributes.getIndex("when-ge")); 755 this.relayStartElement(uri, name, raw, this.normalizeAttributes(attributes)); 756 } else { 757 this.relayStartElement(true, true, uri, name, raw, attr); 758 } 759 } 760 } 761 } 762 763 772 protected void startFormElement(String uri, String name, String raw, Attributes attr) 773 throws SAXException { 774 775 String fixed = attr.getValue(this.fixedName); 776 if (this.useFormName) { 777 this.formName = attr.getValue("name"); 778 } 779 if (fixed == null) { 780 this.relayStartElement(uri, name, raw, attr); 781 } else { 782 if (!this.fixed && BooleanUtils.toBoolean(fixed)) { 783 this.fixed = true; 784 } 785 AttributesImpl attributes = null; 787 if (attr instanceof AttributesImpl ) { 788 attributes = (AttributesImpl ) attr; 789 } else { 790 attributes = new AttributesImpl (attr); 791 } 792 attributes.removeAttribute(attributes.getIndex(this.fixedName)); 793 this.relayStartElement(uri, name, raw, this.normalizeAttributes(attributes)); 794 } 795 } 796 797 808 protected void startRepeatElement(String uri, String name, String raw, Attributes attr) 809 throws SAXException { 810 811 if (this.recordingCount == 0) { 812 if (!(this.fixed || BooleanUtils.toBoolean(attr.getValue(this.fixedName)))) { 813 RepeaterStatus status = 814 new RepeaterStatus("${" + attr.getValue("using") + "}", 0, attr.getValue("on")); 815 this.repeater.add(status); 816 this.startRecording(); 817 this.recordingCount++; 818 } else { 819 this.relayStartElement(uri, name, raw, attr); 820 } 821 } else { 822 this.relayStartElement(uri, name, raw, attr); 823 this.recordingCount++; 824 } 825 } 826 827 836 protected void endRepeatElement(String uri, String name, String raw) throws SAXException { 837 this.recordingCount--; 838 if (this.recordingCount == 0) { 839 DocumentFragment fragment = this.endRecording(); 840 RepeaterStatus status = (RepeaterStatus) this.repeater.get(this.repeater.size() - 1); 841 Object [] vals = this.getValues(this.getName(status.expr)); 842 int count = (vals != null ? vals.length : 0); 843 for (status.count = 1; status.count <= count; status.count++) { 844 DOMStreamer streamer = new DOMStreamer(this, this); 845 streamer.stream(fragment); 846 } 847 this.repeater.remove(this.repeater.size() - 1); 848 } else { 849 this.relayEndElement(uri, name, raw); 850 if (this.recordingCount < 0) { 851 this.recordingCount = 0; 852 } 853 } 854 } 855 856 864 public void startTransformingElement(String uri, String name, String raw, Attributes attr) 865 throws SAXException { 866 867 if (this.ignoreEventsCount == 0 && this.recordingCount == 0) { 868 switch (((Integer ) elementNames.get(name, defaultElement)).intValue()) { 869 case ELEMENT_INPUT : 870 this.startInputElement(uri, name, raw, attr); 871 break; 872 873 case ELEMENT_SELECT : 874 this.startSelectElement(uri, name, raw, attr); 875 break; 876 877 case ELEMENT_OPTION : 878 this.startOptionElement(uri, name, raw, attr); 879 break; 880 881 case ELEMENT_TXTAREA : 882 this.startTextareaElement(uri, name, raw, attr); 883 break; 884 885 case ELEMENT_ERROR : 886 this.startErrorElement(uri, name, raw, attr); 887 break; 888 889 case ELEMENT_FORM : 890 this.startFormElement(uri, name, raw, attr); 891 break; 892 893 case ELEMENT_REPEAT : 894 this.startRepeatElement(uri, name, raw, attr); 895 break; 896 897 default : 898 this.relayStartElement(uri, name, raw, attr); 899 } 900 901 } else if (this.recordingCount > 0) { 902 switch (((Integer ) elementNames.get(name, defaultElement)).intValue()) { 903 case ELEMENT_REPEAT : 904 this.startRepeatElement(uri, name, raw, attr); 905 break; 906 907 default : 908 this.relayStartElement(uri, name, raw, attr); 909 } 910 } else { 911 this.relayStartElement(uri, name, raw, attr); 912 } 913 } 914 915 922 public void endTransformingElement(String uri, String name, String raw) throws SAXException { 923 924 if (this.ignoreEventsCount > 0) { 925 this.relayEndElement(uri, name, raw); 926 } else if (this.recordingCount > 0) { 927 switch (((Integer ) elementNames.get(name, defaultElement)).intValue()) { 928 case ELEMENT_REPEAT : 929 this.endRepeatElement(uri, name, raw); 930 break; 931 932 default : 933 this.relayEndElement(uri, name, raw); 934 } 935 } else { 936 switch (((Integer ) elementNames.get(name, defaultElement)).intValue()) { 937 case ELEMENT_SELECT : 938 this.values = null; 939 this.relayEndElement(uri, name, raw); 940 break; 941 case ELEMENT_INPUT : 942 case ELEMENT_OPTION : 943 case ELEMENT_TXTAREA : 944 case ELEMENT_ERROR : 945 this.relayEndElement(uri, name, raw); 946 break; 947 case ELEMENT_FORM : 948 this.fixed = this.documentFixed; 949 this.formName = null; 950 this.relayEndElement(uri, name, raw); 951 break; 952 953 case ELEMENT_REPEAT : 954 this.endRepeatElement(uri, name, raw); 955 break; 956 957 default : 958 this.relayEndElement(uri, name, raw); 959 } 960 } 961 } 962 963 970 private Attributes normalizeAttributes(Attributes attr) { 971 Attributes result = attr; 972 if (this.stripNumber && this.repeater.size() > 0) { 973 String name = attr.getValue("name"); 974 if (name != null) { 975 for (Iterator i = this.repeater.iterator(); i.hasNext();) { 976 RepeaterStatus status = (RepeaterStatus) i.next(); 977 int pos = name.indexOf(status.var); 978 if (pos >= 0) { 979 AttributesImpl attributes; 980 if (result instanceof AttributesImpl ) { 981 attributes = (AttributesImpl ) result; 982 } else { 983 attributes = new AttributesImpl (result); 984 } 985 name = 986 name.substring(0, pos - this.decorationSize) 987 + name.substring(pos + status.var.length() + this.decorationSize); 988 attributes.setValue(attributes.getIndex("name"), name); 989 result = attributes; 990 } 991 } 992 } 993 } 994 return result; 995 } 996 997 1002 private String getName(String name) { 1003 String result = name; 1004 if (this.useFormName && this.formName != null) { 1005 if (this.separator != null) { 1006 if (this.useFormNameTwice) { 1007 result = 1008 this.formName + this.separator + this.formName + this.separator + result; 1009 } else { 1010 result = this.formName + this.separator + result; 1011 } 1012 } else { 1013 if (this.useFormNameTwice) { 1014 result = this.formName + result; 1015 } else { 1016 result = this.formName + this.formName + result; 1018 } 1019 } 1020 } 1021 if (this.prefix != null) { 1022 result = this.prefix + result; 1023 } 1024 if (this.suffix != null) { 1025 result = result + this.prefix; 1026 } 1027 if (this.repeater.size() > 0) { 1028 for (Iterator i = this.repeater.iterator(); i.hasNext();) { 1029 RepeaterStatus status = (RepeaterStatus) i.next(); 1030 int pos = result.indexOf(status.var); 1031 if (pos != -1) { 1032 result = 1033 result.substring(0, pos) 1034 + status.count 1035 + result.substring(pos + status.var.length()); 1036 } 1037 } 1038 } 1039 return result; 1040 } 1041 1042 1049 private Object getNextValue(String name) { 1050 Object result = null; 1051 if (this.formValues.containsKey(name)) { 1052 ValueList vList = (ValueList) this.formValues.get(name); 1053 result = vList.getNext(); 1054 } else { 1055 ValueList vList = new ValueList(this.getValues(name)); 1056 result = vList.getNext(); 1057 this.formValues.put(name, vList); 1058 } 1059 return result; 1060 } 1061 1062 1065 private Object [] getValues(String name) { 1066 Object [] values = null; 1067 ServiceSelector iputSelector = null; 1068 InputModule iput = null; 1069 try { 1070 if (this.input != null) { 1071 values = input.getAttributeValues(name, this.inputConf, objectModel); 1074 if (getLogger().isDebugEnabled()) 1075 getLogger().debug("cached module " + this.input 1076 + " attribute " + name 1077 + " returns " + values); 1078 } else { 1079 iputSelector = (ServiceSelector)this.manager.lookup(INPUT_MODULE_SELECTOR); 1082 if (this.inputName != null 1083 && iputSelector != null 1084 && iputSelector.isSelectable(this.inputName)) { 1085 1086 iput = (InputModule) iputSelector.select(this.inputName); 1087 } 1088 if (iput != null) { 1089 values = iput.getAttributeValues(name, this.inputConf, objectModel); 1090 } 1091 if (getLogger().isDebugEnabled()) 1092 getLogger().debug( 1093 "fresh module " + iput + " attribute " + name + " returns " + values); 1094 } 1095 } catch (Exception e) { 1096 if (getLogger().isWarnEnabled()) 1097 getLogger().warn( 1098 "A problem occurred acquiring a value from '" 1099 + this.inputName 1100 + "' for '" 1101 + name 1102 + "': " 1103 + e.getMessage()); 1104 } finally { 1105 if (iputSelector != null) { 1107 if (iput != null) 1108 iputSelector.release(iput); 1109 this.manager.release(iputSelector); 1110 } 1111 } 1112 1113 return values; 1114 } 1115 1116 1125 protected void relayStartElement(String uri, String name, String raw, Attributes attr) 1126 throws SAXException { 1127 this.relayStartElement(false, false, uri, name, raw, attr); 1128 } 1129 1130 1144 protected void relayStartElement( 1145 boolean skip, 1146 boolean skipChildrenOnly, 1147 String uri, 1148 String name, 1149 String raw, 1150 Attributes attr) 1151 throws SAXException { 1152 1153 try { 1154 if (this.ignoreEventsCount > 0) { 1155 this.ignoreEventsCount++; 1156 super.startTransformingElement(uri, name, raw, attr); 1157 } else { 1158 if (skip) 1159 this.skipChildrenOnly = skipChildrenOnly; 1160 if (skip && !skipChildrenOnly) 1161 this.ignoreEventsCount++; 1162 super.startTransformingElement(uri, name, raw, attr); 1163 if (skip && skipChildrenOnly) 1164 this.ignoreEventsCount++; 1165 } 1166 } catch (ProcessingException e) { 1167 throw new SAXException (e); 1168 } catch (IOException e) { 1169 throw new SAXException (e); 1170 } 1171 } 1172 1173 1182 protected void relayEndElement(String uri, String name, String raw) throws SAXException { 1183 1184 if (this.ignoreEventsCount == 1 && this.skipChildrenOnly) 1185 this.ignoreEventsCount--; 1186 try { 1187 super.endTransformingElement(uri, name, raw); 1188 } catch (ProcessingException e) { 1189 throw new SAXException (e); 1190 } catch (IOException e) { 1191 throw new SAXException (e); 1192 } catch (Exception e) { 1193 getLogger().error("exception", e); 1194 } 1195 1196 if (this.ignoreEventsCount > 0) 1197 this.ignoreEventsCount--; 1198 } 1199 1200} 1201 | Popular Tags |