1 /*
2 * Copyright (C) 2002 Carsten Krebs (Team-Konzept GmbH & Co.KG)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package com.teamkonzept.dom4jb.schema;
19
20 import java.beans.IntrospectionException;
21 import java.beans.PropertyDescriptor;
22 import java.lang.reflect.InvocationTargetException;
23
24 import com.teamkonzept.dom4jb.DOM4JBException;
25 import com.teamkonzept.dom4jb.beans.ComplexProperty;
26 import com.teamkonzept.dom4jb.beans.Introspector;
27 import com.teamkonzept.dom4jb.beans.XMLBeanInfo;
28 import com.teamkonzept.dom4jb.dom.Document;
29 import com.teamkonzept.dom4jb.dom.Node;
30 import com.teamkonzept.dom4jb.dom.NodeName;
31
32 public class ElementDescriptor extends ComplexProperty {
33
34 protected final NodeName name;
35 protected final XMLBeanInfo type;
36
37 /*** Creates new ElementDescriptor */
38 public ElementDescriptor(final NodeName name,
39 final PropertyDescriptor property,
40 final XMLBeanInfo type) {
41 super(property);
42 this.name = name;
43 this.type = type;
44 }
45
46 /*** Creates new ElementDescriptor */
47 public ElementDescriptor(final NodeName name,
48 final PropertyDescriptor property) {
49 this(name, property, null);
50 }
51
52 /*** Creates new ElementDescriptor */
53 public ElementDescriptor(final String name,
54 final PropertyDescriptor property) {
55 this(NodeName.getInstance(name), property);
56 }
57
58 /*** Creates new ElementDescriptor */
59 public ElementDescriptor(final String name,
60 final PropertyDescriptor property,
61 final XMLBeanInfo type) {
62 this(NodeName.getInstance(name), property, type);
63 }
64
65 /*** Creates new ElementDescriptor */
66 public ElementDescriptor(final String name, final Class beanClass)
67 throws IntrospectionException {
68
69 this(name, getProperty(name, beanClass));
70 }
71
72 /*** Creates new ElementDescriptor */
73 public ElementDescriptor(final String elementName,
74 final String propertyName,
75 final Class beanClass)
76 throws IntrospectionException {
77
78 this(elementName, getProperty(propertyName, beanClass));
79 }
80
81 /*** Creates new ElementDescriptor */
82 public ElementDescriptor(final PropertyDescriptor property) {
83 this(property.getName(), property);
84 }
85
86 /*** Creates new ElementDescriptor */
87 public ElementDescriptor( final String name,
88 final Class beanClass,
89 final XMLBeanInfo type)
90 throws IntrospectionException {
91
92 this(name, getProperty(name, beanClass), type);
93 }
94
95 protected final GroupDescriptor getContentDescriptors(final Object obj) {
96 if (obj != null) {
97 final GroupDescriptor descriptor =
98 getBeanInfo(obj).getContentDescriptors();
99 if (descriptor != null) {
100 return descriptor;
101 }
102 }
103 return GroupDescriptor.EMPTY_DESCRIPTOR;
104 }
105
106 protected final GroupDescriptor getAttributeDescriptors(final Object obj) {
107 if (obj != null) {
108 final GroupDescriptor descriptor =
109 getBeanInfo(obj).getAttributeDescriptors();
110
111 if (descriptor != null) {
112 return descriptor;
113 }
114 }
115 return GroupDescriptor.EMPTY_DESCRIPTOR;
116 }
117
118 private XMLBeanInfo getBeanInfo(final Object obj) {
119 if (type != null) {
120 return type;
121 }
122 try {
123 return Introspector.getBeanInfo(obj.getClass());
124 } catch (IntrospectionException e) {
125 throw new DOM4JBException(e);
126 }
127 }
128
129 public Node getContent(final Document document, final Object bean) {
130 return document.createElement(this, bean);
131 }
132
133 public NodeName getName() {
134 return this.name;
135 }
136
137 public ContentIterator getChildNodeIterator(final Document document,
138 final Object bean) {
139 try {
140 final Object obj = getObject(bean);
141 return getContentDescriptors(obj).getContentIterator(document, obj);
142 } catch (IllegalAccessException e) {
143 throw new DOM4JBException(e);
144 } catch (InvocationTargetException e) {
145 throw new DOM4JBException(e);
146 }
147 }
148
149 public ContentIterator getAttributeIterator(final Document document,
150 final Object bean) {
151 try {
152 final Object obj = getObject(bean);
153 return getAttributeDescriptors(obj).getContentIterator(
154 document,
155 obj);
156 } catch (IllegalAccessException e) {
157 throw new DOM4JBException(e);
158 } catch (InvocationTargetException e) {
159 throw new DOM4JBException(e);
160 }
161 }
162
163 public String toString() {
164 final StringBuffer buf = new StringBuffer("Element(");
165 buf.append(name.toString());
166 buf.append(':');
167 buf.append(super.toString());
168 buf.append(')');
169 return buf.toString();
170 }
171 }
This page was automatically generated by Maven