1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java,v 1.6 2003/01/07 22:32:57 rdonkin Exp $
3 * $Revision: 1.6 $
4 * $Date: 2003/01/07 22:32:57 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: AddDefaultsRule.java,v 1.6 2003/01/07 22:32:57 rdonkin Exp $
61 */
62 package org.apache.commons.betwixt.digester;
63
64 import java.beans.BeanInfo;
65 import java.beans.Introspector;
66 import java.beans.PropertyDescriptor;
67 import java.util.Set;
68
69 import org.apache.commons.betwixt.AttributeDescriptor;
70 import org.apache.commons.betwixt.ElementDescriptor;
71 import org.apache.commons.betwixt.NodeDescriptor;
72 import org.apache.commons.betwixt.XMLBeanInfo;
73 import org.apache.commons.logging.Log;
74 import org.apache.commons.logging.LogFactory;
75 import org.xml.sax.Attributes;
76 import org.xml.sax.SAXException;
77
78 /*** <p><code>AddDefaultsRule</code> appends all the default properties
79 * to the current element.</p>
80 *
81 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
82 * @version $Revision: 1.6 $
83 */
84 public class AddDefaultsRule extends RuleSupport {
85
86 /*** Logger */
87 private static final Log log = LogFactory.getLog( AddDefaultsRule.class );
88
89 /*** Base constructor */
90 public AddDefaultsRule() {
91 }
92
93 // Rule interface
94 //-------------------------------------------------------------------------
95
96 /***
97 * Process the beginning of this element.
98 *
99 * @param attributes The attribute list of this element
100 * @throws Exception generally this will indicate an unrecoverable error
101 */
102 public void begin(Attributes attributes) throws Exception {
103 Class beanClass = getBeanClass();
104 Set procesedProperties = getProcessedPropertyNameSet();
105 if ( beanClass != null ) {
106 try {
107 boolean attributesForPrimitives = getXMLInfoDigester().isAttributesForPrimitives();
108 BeanInfo beanInfo = Introspector.getBeanInfo( beanClass );
109 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
110 if ( descriptors != null ) {
111 for ( int i = 0, size = descriptors.length; i < size; i++ ) {
112 PropertyDescriptor descriptor = descriptors[i];
113 // have we already created a property for this
114 String name = descriptor.getName();
115 if ( procesedProperties.contains( name ) ) {
116 continue;
117 }
118 NodeDescriptor nodeDescriptor = XMLIntrospectorHelper.createDescriptor(
119 descriptor, attributesForPrimitives, getXMLIntrospector()
120 );
121 if ( nodeDescriptor != null ) {
122 addDescriptor( nodeDescriptor );
123 }
124 }
125 }
126 } catch (Exception e) {
127 log.info( "Caught introspection exception", e );
128 }
129 }
130
131 // default any addProperty() methods
132 XMLIntrospectorHelper.defaultAddMethods(
133 getXMLIntrospector(),
134 getRootElementDescriptor(),
135 beanClass );
136 }
137
138
139 // Implementation methods
140 //-------------------------------------------------------------------------
141
142 /***
143 * Add a desciptor to the top object on the Digester stack.
144 *
145 * @param nodeDescriptor add this <code>NodeDescriptor</code>. Must not be null.
146 * @throws SAXException if the parent for the addDefaults element is not a <element>
147 * or if the top object on the stack is not a <code>XMLBeanInfo</code> or a
148 * <code>ElementDescriptor</code>
149 */
150 protected void addDescriptor( NodeDescriptor nodeDescriptor ) throws SAXException {
151 Object top = digester.peek();
152 if ( top instanceof XMLBeanInfo ) {
153 log.warn( "It is advisable to put an <addDefaults/> element inside an <element> tag" );
154
155 XMLBeanInfo beanInfo = (XMLBeanInfo) top;
156 // if there is already a root element descriptor then use it
157 // otherwise use this descriptor
158 if ( nodeDescriptor instanceof ElementDescriptor ) {
159 ElementDescriptor elementDescriptor = (ElementDescriptor) nodeDescriptor;
160 ElementDescriptor root = beanInfo.getElementDescriptor() ;
161 if ( root == null ) {
162 beanInfo.setElementDescriptor( elementDescriptor );
163 } else {
164 root.addElementDescriptor( elementDescriptor );
165 }
166 } else {
167 throw new SAXException(
168 "the <addDefaults> element should be within an <element> tag" );
169 }
170 } else if ( top instanceof ElementDescriptor ) {
171 ElementDescriptor parent = (ElementDescriptor) top;
172 if ( nodeDescriptor instanceof ElementDescriptor ) {
173 parent.addElementDescriptor( (ElementDescriptor) nodeDescriptor );
174 } else {
175 parent.addAttributeDescriptor( (AttributeDescriptor) nodeDescriptor );
176 }
177 } else {
178 throw new SAXException(
179 "Invalid use of <addDefaults>. It should be nested inside <element> element" );
180 }
181 }
182
183 /***
184 * Gets an <code>ElementDescriptor</code> for the top on digester's stack.
185 *
186 * @return the top object or the element description if the top object
187 * is an <code>ElementDescriptor</code> or a <code>XMLBeanInfo</code> class (respectively)
188 * Otherwise null.
189 */
190 protected ElementDescriptor getRootElementDescriptor() {
191 Object top = digester.peek();
192 if ( top instanceof XMLBeanInfo ) {
193 XMLBeanInfo beanInfo = (XMLBeanInfo) top;
194 return beanInfo.getElementDescriptor();
195
196 } else if ( top instanceof ElementDescriptor ) {
197 ElementDescriptor parent = (ElementDescriptor) top;
198 // XXX: could maybe walk up the parent hierarchy?
199 return parent;
200 }
201 return null;
202 }
203 }
This page was automatically generated by Maven