KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > XMLBeanInfo


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc beanClass;
41     /** <code>ID</code> attribute name */
42     private String JavaDoc idAttributeName = "id";
43     /** <code>IDREF</code> attribute name */
44     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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
// XXX for some reason caching isn't working at the moment
102
// it could be that this method is called too early
103
// and not reset when attributes change
104
// on the other hand, the speed gain is likely to be too
105
// small to bother about
106
//
107
//if ( cachedIDAttribute = false ) {
108
idAttributeDescriptor = findIDAttribute();
109           // cachedIDAttribute = true;
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         // we'll check to see if the bean already has an id
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                     // support a match either on local or qualified name
125
if ( getIDAttributeName().equals( attributes[i].getQualifiedName() )
126                         || getIDAttributeName().equals( attributes[i].getLocalName() )) {
127                         // we've got a match so use this attribute
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc toString() {
193         return
194                 "XMLBeanInfo [class=" + getBeanClass()
195                 + ", descriptor=" + getElementDescriptor() + "]";
196     }
197 }
198
Popular Tags