1. /*
  2. * Copyright 2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.betwixt.digester;
  17. import org.apache.commons.betwixt.Descriptor;
  18. import org.apache.commons.digester.Rule;
  19. import org.xml.sax.Attributes;
  20. /**
  21. * Maps option tree to an option in the
  22. * {@link org.apache.commons.betwixt.Options}
  23. * on the current description.
  24. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  25. * @since 0.5
  26. */
  27. public class OptionRule extends Rule {
  28. private String currentValue;
  29. private String currentName;
  30. /**
  31. * @see org.apache.commons.digester.Rule#begin(java.lang.String, java.lang.String, Attributes)
  32. */
  33. public void begin(String namespace, String name, Attributes attributes)
  34. throws Exception {
  35. currentValue = null;
  36. currentName = null;
  37. }
  38. /**
  39. * @see org.apache.commons.digester.Rule#end(java.lang.String, java.lang.String)
  40. */
  41. public void end(String namespace, String name) {
  42. if (currentName != null && currentValue != null) {
  43. Object top = getDigester().peek();
  44. if (top instanceof Descriptor) {
  45. Descriptor descriptor = (Descriptor) top;
  46. descriptor.getOptions().addOption(currentName, currentValue);
  47. }
  48. }
  49. }
  50. /**
  51. * Gets the rule that maps the <code>name</code> element
  52. * associated with the option
  53. * @return <code>Rule</code>, not null
  54. */
  55. public Rule getNameRule() {
  56. return new Rule() {
  57. public void body(String namespace, String name, String text) {
  58. currentName = text;
  59. }
  60. };
  61. }
  62. /**
  63. * Gets the rule that maps the <code>value</code> element
  64. * associated with the option
  65. * @return <code>Rule</code>, not null
  66. */
  67. public Rule getValueRule() {
  68. return new Rule() {
  69. public void body(String namespace, String name, String text) {
  70. currentValue = text;
  71. }
  72. };
  73. }
  74. }