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.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24
25 import com.teamkonzept.dom4jb.DOM4JBException;
26 import com.teamkonzept.dom4jb.beans.Introspector;
27 import com.teamkonzept.dom4jb.beans.Property;
28 import com.teamkonzept.dom4jb.beans.XMLBeanInfo;
29 import com.teamkonzept.dom4jb.dom.Document;
30 import com.teamkonzept.dom4jb.dom.Node;
31
32
33 public class CollectionAdapter extends GroupDescriptor {
34
35 private static final ContentAdapter contentAdapter = new ContentAdapter();
36 private static final HashMap descriptorMap = new HashMap();
37 private static final HashMap adapterMap = new HashMap();
38 static {
39 final ElementDescriptor desc =
40 new ElementDescriptor(XMLBeanInfo.DEFAULT_ITEM_NAME, Property.IDENTITY);
41
42 descriptorMap.put( null, desc );
43 descriptorMap.put( "", desc );
44
45 final ElementAdapter adap = new ElementAdapter( null );
46 adapterMap.put( null, adap );
47 adapterMap.put( "", adap );
48 }
49
50 protected final Groupable adapter;
51
52 /*** Creates new CollectionAdapter */
53 public CollectionAdapter() {
54 this.adapter = contentAdapter;
55 }
56
57 /*** Creates new CollectionAdapter */
58 public CollectionAdapter( final String itemName ) {
59 this.adapter = getAdapter( itemName );
60 }
61
62 /*** Creates new CollectionAdapter */
63 public CollectionAdapter( final Groupable itemAdapter ) {
64 this.adapter = itemAdapter;
65 }
66
67 protected static Groupable getAdapter( final String name ) {
68 Object group = adapterMap.get( name );
69 if ( group == null ) {
70 group = new ElementAdapter(
71 new ElementDescriptor( name, Property.IDENTITY ) );
72 synchronized( adapterMap ) {
73 adapterMap.put( name, group );
74 }
75 }
76 return (Groupable)group;
77 }
78
79 protected static ElementDescriptor getDescriptor( final String name ) {
80 Object desc = descriptorMap.get( name );
81 if ( desc == null ) {
82 desc = new ElementDescriptor( name, Property.IDENTITY );
83 synchronized( descriptorMap ) {
84 descriptorMap.put( name, desc );
85 }
86 }
87 return (ElementDescriptor)desc;
88 }
89
90 public ContentIterator getContentIterator( final Document doc,
91 final Object obj ) {
92
93 return new CollectionAdapterIterator( (Collection)obj, doc, adapter );
94 }
95
96 public boolean isEmpty() {
97 return false;
98 }
99
100 public static final class Item {
101 private final Object obj;
102 public Item( final Object obj ) {
103 this.obj = obj;
104 }
105 public Object get() { return this.obj; }
106 }
107
108 public void addTo( final List list ) {
109 list.add( this );
110 }
111
112 public static final class ContentAdapter implements Groupable {
113
114 public ContentIterator getContentIterator( final Document doc,
115 final Object obj ) {
116 try {
117 final XMLBeanInfo info = Introspector.getBeanInfo(obj.getClass());
118 final GroupDescriptor desc = info.getContentDescriptors();
119 if ( desc != null ) {
120 return desc.getContentIterator( doc, obj );
121 }
122 return null;
123 } catch ( IntrospectionException e ) {
124 throw new DOM4JBException( e );
125 }
126 }
127
128 public final Node getContent( final Document doc, final Object bean ) {
129 return null;
130 }
131
132 public final void addTo( final List list ) { /* NOP */ }
133 }
134
135 public static final class ElementAdapter implements Groupable {
136 private final ElementDescriptor descriptor;
137
138 public ElementAdapter( final ElementDescriptor descriptor ) {
139 this.descriptor = descriptor;
140 }
141
142 public ContentIterator getContentIterator( final Document doc,
143 final Object obj ) {
144 return null;
145 }
146
147 public Node getContent( final Document doc, final Object obj ) {
148 if ( obj == null ) {
149 return null;
150 }
151 try {
152 final XMLBeanInfo info = Introspector.getBeanInfo(obj.getClass());
153 final ElementDescriptor desc;
154 if (descriptor!=null) {
155 desc = descriptor;
156 } else {
157 desc = getDescriptor(info.getItemName());
158 }
159 return desc.getContent( doc, obj );
160 } catch ( IntrospectionException e ) {
161 throw new DOM4JBException( e );
162 }
163 }
164
165 public final void addTo( final List list ) { /* NOP */ }
166 }
167
168 /* %TODO%, wann wird dieser Adapter verwendet??
169 * es muß irgendwie möglich sein, diesen Adapter über den
170 * CollectionAdapater-Konstruktor auszuwählen
171 */
172 public static final class AttributeAdapter {
173
174 public ContentIterator getContentIterator( final Document doc,
175 final Object obj ) {
176 try {
177 final XMLBeanInfo info = Introspector.getBeanInfo( obj.getClass() );
178 final GroupDescriptor desc = info.getAttributeDescriptors();
179 if ( desc != null ) {
180 return desc.getContentIterator( doc, obj );
181 }
182 return null;
183 } catch ( IntrospectionException e ) {
184 throw new DOM4JBException( e );
185 }
186 }
187
188 public Node getContent( final Document doc, final Object bean ) {
189 return null;
190 }
191
192 public final void addTo( final List list ) { /* NOP */ }
193 }
194 }
This page was automatically generated by Maven