1. package org.apache.commons.betwixt.digester;
  2. /*
  3. * Copyright 2001-2004 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.beans.PropertyDescriptor;
  18. import java.lang.reflect.Method;
  19. import org.apache.commons.betwixt.ElementDescriptor;
  20. import org.apache.commons.betwixt.TextDescriptor;
  21. import org.apache.commons.betwixt.XMLBeanInfo;
  22. import org.apache.commons.betwixt.expression.ConstantExpression;
  23. import org.apache.commons.betwixt.expression.MethodExpression;
  24. import org.apache.commons.betwixt.expression.MethodUpdater;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.xml.sax.Attributes;
  28. import org.xml.sax.SAXException;
  29. /**
  30. * <p>Rule for parsing <text> elements.
  31. * These allow mixed content text to be specified.
  32. * A mixed content element example:
  33. * <pre>
  34. * <foo>text<bar/></foo>
  35. * </pre>
  36. * </p>
  37. *
  38. * @author Robert Burrell Donkin
  39. * @version $Id: TextRule.java,v 1.9 2004/06/13 21:32:45 rdonkin Exp $
  40. */
  41. public class TextRule extends MappedPropertyRule {
  42. /** Logger */
  43. private static final Log log = LogFactory.getLog( TextRule.class );
  44. /** Base constructor */
  45. public TextRule() {}
  46. // Rule interface
  47. //-------------------------------------------------------------------------
  48. /**
  49. * Process the beginning of this element.
  50. *
  51. * @param attributes The attribute list of this element
  52. * @throws SAXException 1. If this tag's parent is not an element tag.
  53. * 2. If this tag has a value attribute together with either a property
  54. * or type attribute.
  55. */
  56. public void begin(String name, String namespace, Attributes attributes) throws SAXException {
  57. TextDescriptor descriptor = new TextDescriptor();
  58. String value = attributes.getValue( "value" );
  59. String propertyName = attributes.getValue( "property" );
  60. String propertyType = attributes.getValue( "type" );
  61. if ( value != null) {
  62. if ( propertyName != null || propertyType != null ) {
  63. // not allowed
  64. throw new SAXException(
  65. "You cannot specify attribute 'value' together with either "
  66. + " the 'property' or 'type' attributes");
  67. }
  68. // fixed value text
  69. descriptor.setTextExpression( new ConstantExpression( value ) );
  70. } else {
  71. // property based text
  72. descriptor.setPropertyName( propertyName );
  73. Class beanClass = getBeanClass();
  74. // set the property type using reflection
  75. descriptor.setPropertyType(
  76. getPropertyType( propertyType, beanClass, propertyName )
  77. );
  78. if ( beanClass != null ) {
  79. String descriptorPropertyName = descriptor.getPropertyName();
  80. PropertyDescriptor propertyDescriptor =
  81. getPropertyDescriptor( beanClass, descriptorPropertyName );
  82. if ( propertyDescriptor != null ) {
  83. Method readMethod = propertyDescriptor.getReadMethod();
  84. descriptor.setTextExpression( new MethodExpression( readMethod ) );
  85. Method writeMethod = propertyDescriptor.getWriteMethod();
  86. if (writeMethod != null) {
  87. descriptor.setUpdater( new MethodUpdater(writeMethod));
  88. }
  89. getProcessedPropertyNameSet().add( descriptorPropertyName );
  90. }
  91. }
  92. }
  93. Object top = digester.peek();
  94. if ( top instanceof XMLBeanInfo ) {
  95. XMLBeanInfo beanInfo = (XMLBeanInfo) top;
  96. ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
  97. if (elementDescriptor == null) {
  98. elementDescriptor.addContentDescriptor( descriptor );
  99. }
  100. } else if ( top instanceof ElementDescriptor ) {
  101. ElementDescriptor parent = (ElementDescriptor) top;
  102. parent.addContentDescriptor( descriptor );
  103. } else {
  104. throw new SAXException( "Invalid use of <text>. It should "
  105. + "be nested <text> nodes" );
  106. }
  107. }
  108. }