1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLBeanInfo.java,v 1.6 2003/01/09 22:34:07 rdonkin Exp $
3 * $Revision: 1.6 $
4 * $Date: 2003/01/09 22:34:07 $
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: XMLBeanInfo.java,v 1.6 2003/01/09 22:34:07 rdonkin Exp $
61 */
62 package org.apache.commons.betwixt;
63
64 /*** <p><code>XMLBeanInfo</code> represents the XML metadata information
65 * used to map a Java Bean cleanly to XML. This provides a default
66 * introspection mechansim, rather like {@link java.beans.BeanInfo}
67 * which can be customized through some mechansim, either via Java code
68 * or XSLT for example.</p>
69 *
70 * <h4><code>ID</code> and <code>IDREF</code> Attribute Names</h4>
71 * <p>These special attributes are defined in the xml specification.
72 * They are used by Betwixt to write bean graphs with cyclic references.
73 * In most cases, these will take the values 'id' and 'idref' respectively
74 * but these names can be varied in the DTD.
75 * Therefore, these names are specified by this class but default to the
76 * usual values.</p>
77 *
78 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
79 * @version $Revision: 1.6 $
80 */
81 public class XMLBeanInfo {
82 /*** Descriptor for main element */
83 private ElementDescriptor elementDescriptor;
84
85 /*** the beans class that this XML info refers to */
86 private Class beanClass;
87 /*** <code>ID</code> attribute name */
88 private String idAttributeName = "id";
89 /*** <code>IDREF</code> attribute name */
90 private String idrefAttributeName = "idref";
91 /*** Have we already cached the <code>idAttributeDescriptor</code>? */
92 private boolean cachedIDAttribute = false;
93 /*** Cached <code>ID</code> attribute descriptor */
94 private AttributeDescriptor idAttributeDescriptor;
95
96 /***
97 * Base constructor
98 * @param beanClass for this Class
99 */
100 public XMLBeanInfo( Class beanClass ) {
101 this.beanClass = beanClass;
102 }
103
104 /***
105 * Gets descriptor for bean represention
106 *
107 * @return ElementDescriptor describing root element
108 */
109 public ElementDescriptor getElementDescriptor() {
110 return elementDescriptor;
111 }
112
113 /***
114 * Sets descriptor for bean represention
115 *
116 * @param elementDescriptor descriptor for bean
117 */
118 public void setElementDescriptor(ElementDescriptor elementDescriptor) {
119 this.elementDescriptor = elementDescriptor;
120 }
121
122 /***
123 * Gets the beans class that this XML info refers to
124 *
125 * @return the beans class that this XML info refers to
126 */
127 public Class getBeanClass() {
128 return beanClass;
129 }
130
131 /***
132 * Sets the beans class that this XML info refers to
133 *
134 * @param beanClass the class that this refers to
135 */
136 public void setBeanClass(Class beanClass) {
137 this.beanClass = beanClass;
138 }
139
140 /***
141 * Search attributes for one matching <code>ID</code> attribute name
142 *
143 * @return the xml ID attribute
144 */
145 public AttributeDescriptor getIDAttribute() {
146 //
147 // XXX for some reason caching isn't working at the moment
148 // it could be that this method is called too early
149 // and not reset when attributes change
150 // on the other hand, the speed gain is likely to be too
151 // small to bother about
152 //
153 //if ( cachedIDAttribute = false ) {
154 idAttributeDescriptor = findIDAttribute();
155 // cachedIDAttribute = true;
156 //}
157 return idAttributeDescriptor;
158 }
159
160 /***
161 * ID attribute search implementation
162 * @return the AttributeDescriptor for the <code>ID</code> attribute
163 */
164 private AttributeDescriptor findIDAttribute() {
165 // we'll check to see if the bean already has an id
166 if ( getElementDescriptor().hasAttributes() ) {
167 AttributeDescriptor[] attributes = getElementDescriptor().getAttributeDescriptors();
168 if ( attributes != null ) {
169 for ( int i = 0, size = attributes.length; i < size; i++ ) {
170 // support a match either on local or qualified name
171 if ( getIDAttributeName().equals( attributes[i].getQualifiedName() )
172 || getIDAttributeName().equals( attributes[i].getLocalName() )) {
173 // we've got a match so use this attribute
174 return attributes[i];
175
176 }
177 }
178 }
179 }
180 return null;
181 }
182
183 /***
184 * <p>Get name of <code>ID</code> attribute.
185 * This is used to write (for example) automatic <code>ID</code>
186 * attribute values.</p>
187 *
188 * <p>The default name is 'id'.</p>
189 *
190 * @return name for the special <code>ID</code> attribute
191 */
192 public String getIDAttributeName() {
193 return idAttributeName;
194 }
195
196 /***
197 * Set name of <code>ID</code> attribute
198 * This is used to write (for example) automatic <code>ID</code>
199 * attribute values.</p>
200 *
201 * <p>The default name is 'id'.</p>
202 *
203 * @param idAttributeName the attribute name for the special <code>ID</code> attribute
204 */
205 public void setIDAttributeName(String idAttributeName) {
206 this.idAttributeName = idAttributeName;
207 }
208
209 /***
210 * <p>Get <code>IDREF</code> attribute name
211 * This is used (for example) to deal with cyclic references.
212 *
213 * <p>The default name is 'idref'.</p>
214 *
215 * @return name for the special <code>IDREF</code> attribute
216 */
217 public String getIDREFAttributeName() {
218 return idrefAttributeName;
219 }
220
221 /***
222 * Set <code>IDREF</code> attribute name
223 * This is used (for example) to deal with cyclic references.
224 *
225 * <p>The default name is 'idref'.</p>
226 *
227 * @param idrefAttributeName the attribute name for the special <code>IDREF</code> attribute
228 */
229 public void setIDREFAttributeName(String idrefAttributeName) {
230 this.idrefAttributeName = idrefAttributeName;
231 }
232
233 /***
234 * Gets log-friendly string representation.
235 *
236 * @return something useful for logging
237 */
238 public String toString() {
239 return
240 "XMLBeanInfo [class=" + getBeanClass()
241 + ", descriptor=" + getElementDescriptor() + "]";
242 }
243 }
This page was automatically generated by Maven