1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt;
17
18 /*** <p><code>XMLBeanInfo</code> represents the XML metadata information
19 * used to map a Java Bean cleanly to XML. This provides a default
20 * introspection mechansim, rather like {@link java.beans.BeanInfo}
21 * which can be customized through some mechansim, either via Java code
22 * or XSLT for example.</p>
23 *
24 * <h4><code>ID</code> and <code>IDREF</code> Attribute Names</h4>
25 * <p>These special attributes are defined in the xml specification.
26 * They are used by Betwixt to write bean graphs with cyclic references.
27 * In most cases, these will take the values 'id' and 'idref' respectively
28 * but these names can be varied in the DTD.
29 * Therefore, these names are specified by this class but default to the
30 * usual values.</p>
31 *
32 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33 * @version $Revision: 1.9 $
34 */
35 public class XMLBeanInfo {
36 /*** Descriptor for main element */
37 private ElementDescriptor elementDescriptor;
38
39 /*** the beans class that this XML info refers to */
40 private Class beanClass;
41 /*** <code>ID</code> attribute name */
42 private String idAttributeName = "id";
43 /*** <code>IDREF</code> attribute name */
44 private String idrefAttributeName = "idref";
45 /*** Have we already cached the <code>idAttributeDescriptor</code>? */
46 private boolean cachedIDAttribute = false;
47 /*** Cached <code>ID</code> attribute descriptor */
48 private AttributeDescriptor idAttributeDescriptor;
49
50 /***
51 * Base constructor
52 * @param beanClass for this Class
53 */
54 public XMLBeanInfo( Class beanClass ) {
55 this.beanClass = beanClass;
56 }
57
58 /***
59 * Gets descriptor for bean represention
60 *
61 * @return ElementDescriptor describing root element
62 */
63 public ElementDescriptor getElementDescriptor() {
64 return elementDescriptor;
65 }
66
67 /***
68 * Sets descriptor for bean represention
69 *
70 * @param elementDescriptor descriptor for bean
71 */
72 public void setElementDescriptor(ElementDescriptor elementDescriptor) {
73 this.elementDescriptor = elementDescriptor;
74 }
75
76 /***
77 * Gets the beans class that this XML info refers to
78 *
79 * @return the beans class that this XML info refers to
80 */
81 public Class getBeanClass() {
82 return beanClass;
83 }
84
85 /***
86 * Sets the beans class that this XML info refers to
87 *
88 * @param beanClass the class that this refers to
89 */
90 public void setBeanClass(Class beanClass) {
91 this.beanClass = beanClass;
92 }
93
94 /***
95 * Search attributes for one matching <code>ID</code> attribute name
96 *
97 * @return the xml ID attribute
98 */
99 public AttributeDescriptor getIDAttribute() {
100
101
102
103
104
105
106
107
108 idAttributeDescriptor = findIDAttribute();
109
110
111 return idAttributeDescriptor;
112 }
113
114 /***
115 * ID attribute search implementation
116 * @return the AttributeDescriptor for the <code>ID</code> attribute
117 */
118 private AttributeDescriptor findIDAttribute() {
119
120 if ( getElementDescriptor().hasAttributes() ) {
121 AttributeDescriptor[] attributes = getElementDescriptor().getAttributeDescriptors();
122 if ( attributes != null ) {
123 for ( int i = 0, size = attributes.length; i < size; i++ ) {
124
125 if ( getIDAttributeName().equals( attributes[i].getQualifiedName() )
126 || getIDAttributeName().equals( attributes[i].getLocalName() )) {
127
128 return attributes[i];
129
130 }
131 }
132 }
133 }
134 return null;
135 }
136
137 /***
138 * <p>Get name of <code>ID</code> attribute.
139 * This is used to write (for example) automatic <code>ID</code>
140 * attribute values.</p>
141 *
142 * <p>The default name is 'id'.</p>
143 *
144 * @return name for the special <code>ID</code> attribute
145 */
146 public String getIDAttributeName() {
147 return idAttributeName;
148 }
149
150 /***
151 * Set name of <code>ID</code> attribute
152 * This is used to write (for example) automatic <code>ID</code>
153 * attribute values.</p>
154 *
155 * <p>The default name is 'id'.</p>
156 *
157 * @param idAttributeName the attribute name for the special <code>ID</code> attribute
158 */
159 public void setIDAttributeName(String idAttributeName) {
160 this.idAttributeName = idAttributeName;
161 }
162
163 /***
164 * <p>Get <code>IDREF</code> attribute name
165 * This is used (for example) to deal with cyclic references.
166 *
167 * <p>The default name is 'idref'.</p>
168 *
169 * @return name for the special <code>IDREF</code> attribute
170 */
171 public String getIDREFAttributeName() {
172 return idrefAttributeName;
173 }
174
175 /***
176 * Set <code>IDREF</code> attribute name
177 * This is used (for example) to deal with cyclic references.
178 *
179 * <p>The default name is 'idref'.</p>
180 *
181 * @param idrefAttributeName the attribute name for the special <code>IDREF</code> attribute
182 */
183 public void setIDREFAttributeName(String idrefAttributeName) {
184 this.idrefAttributeName = idrefAttributeName;
185 }
186
187 /***
188 * Gets log-friendly string representation.
189 *
190 * @return something useful for logging
191 */
192 public String toString() {
193 return
194 "XMLBeanInfo [class=" + getBeanClass()
195 + ", descriptor=" + getElementDescriptor() + "]";
196 }
197 }