1 package org.apache.commons.betwixt.digester;
2
3 /*
4 * ====================================================================
5 *
6 * The Apache Software License, Version 1.1
7 *
8 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
9 * reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 *
23 * 3. The end-user documentation included with the redistribution, if
24 * any, must include the following acknowlegement:
25 * "This product includes software developed by the
26 * Apache Software Foundation (http://www.apache.org/)."
27 * Alternately, this acknowlegement may appear in the software itself,
28 * if and wherever such third-party acknowlegements normally appear.
29 *
30 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
31 * Foundation" must not be used to endorse or promote products derived
32 * from this software without prior written permission. For written
33 * permission, please contact apache@apache.org.
34 *
35 * 5. Products derived from this software may not be called "Apache"
36 * nor may "Apache" appear in their names without prior written
37 * permission of the Apache Group.
38 *
39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This software consists of voluntary contributions made by many
54 * individuals on behalf of the Apache Software Foundation. For more
55 * information on the Apache Software Foundation, please see
56 * <http://www.apache.org/>.
57 */
58 import java.beans.BeanInfo;
59 import java.beans.Introspector;
60 import java.beans.PropertyDescriptor;
61
62 import org.apache.commons.betwixt.AttributeDescriptor;
63 import org.apache.commons.betwixt.ElementDescriptor;
64 import org.apache.commons.betwixt.expression.ConstantExpression;
65 import org.apache.commons.logging.Log;
66 import org.apache.commons.logging.LogFactory;
67 import org.xml.sax.Attributes;
68 import org.xml.sax.SAXException;
69
70 /***
71 * <p><code>AttributeRule</code> the digester Rule for parsing the
72 * <attribute> elements.</p>
73 *
74 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
75 * @version $Id: AttributeRule.java,v 1.5 2003/01/07 22:32:57 rdonkin Exp $
76 */
77 public class AttributeRule extends RuleSupport {
78
79 /*** Logger */
80 private static final Log log = LogFactory.getLog( AttributeRule.class );
81 /*** This loads all classes created by name. Defaults to this class's classloader */
82 private ClassLoader classLoader;
83 /*** The <code>Class</code> whose .betwixt file is being digested */
84 private Class beanClass;
85
86 /*** Base constructor */
87 public AttributeRule() {
88 this.classLoader = getClass().getClassLoader();
89 }
90
91 // Rule interface
92 //-------------------------------------------------------------------------
93
94 /***
95 * Process the beginning of this element.
96 *
97 * @param attributes The attribute list of this element
98 * @throws SAXException if the attribute tag is not inside an element tag
99 */
100 public void begin(Attributes attributes) throws SAXException {
101
102 AttributeDescriptor descriptor = new AttributeDescriptor();
103 String name = attributes.getValue( "name" );
104 descriptor.setQualifiedName( name );
105 descriptor.setLocalName( name );
106 String uri = attributes.getValue( "uri" );
107 if ( uri != null ) {
108 descriptor.setURI( uri );
109 }
110 String propertyName = attributes.getValue( "property" );
111 descriptor.setPropertyName( propertyName );
112 descriptor.setPropertyType( loadClass( attributes.getValue( "type" ) ) );
113
114 if ( propertyName != null && propertyName.length() > 0 ) {
115 configureDescriptor(descriptor);
116 } else {
117 String value = attributes.getValue( "value" );
118 if ( value != null ) {
119 descriptor.setTextExpression( new ConstantExpression( value ) );
120 }
121 }
122
123 Object top = digester.peek();
124 if ( top instanceof ElementDescriptor ) {
125 ElementDescriptor parent = (ElementDescriptor) top;
126 parent.addAttributeDescriptor( descriptor );
127 } else {
128 throw new SAXException( "Invalid use of <attribute>. It should "
129 + "be nested inside an <element> element" );
130 }
131
132 digester.push(descriptor);
133 }
134
135
136 /***
137 * Process the end of this element.
138 */
139 public void end() {
140 Object top = digester.pop();
141 }
142
143
144 // Implementation methods
145 //-------------------------------------------------------------------------
146 /***
147 * Loads a class (using the appropriate classloader)
148 *
149 * @param name the name of the class to load
150 * @return the class instance loaded by the appropriate classloader
151 */
152 protected Class loadClass( String name ) {
153 // XXX: should use a ClassLoader to handle complex class loading situations
154 if ( name != null ) {
155 try {
156 return classLoader.loadClass(name);
157 } catch (Exception e) { // SWALLOW
158 }
159 }
160 return null;
161 }
162
163 /***
164 * Set the Expression and Updater from a bean property name
165 * @param attributeDescriptor configure this <code>AttributeDescriptor</code>
166 * from the property with a matching name in the bean class
167 */
168 protected void configureDescriptor(AttributeDescriptor attributeDescriptor) {
169 Class beanClass = getBeanClass();
170 if ( beanClass != null ) {
171 String name = attributeDescriptor.getPropertyName();
172 try {
173 BeanInfo beanInfo = Introspector.getBeanInfo( beanClass );
174 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
175 if ( descriptors != null ) {
176 for ( int i = 0, size = descriptors.length; i < size; i++ ) {
177 PropertyDescriptor descriptor = descriptors[i];
178 if ( name.equals( descriptor.getName() ) ) {
179 XMLIntrospectorHelper
180 .configureProperty( attributeDescriptor, descriptor );
181 getProcessedPropertyNameSet().add( name );
182 break;
183 }
184 }
185 }
186 } catch (Exception e) {
187 log.warn( "Caught introspection exception", e );
188 }
189 }
190 }
191 }
This page was automatically generated by Maven