View Javadoc

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