KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > xml > SerializableEntity


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.xml;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.reflect.PropertyUtils;
27 import org.w3c.dom.Element JavaDoc;
28
29 /**
30  * A flyweight wrapper for serializing with XML mapping. This object is NOT thread-safe.
31  *
32  * @since 1.2
33  * @author Andrus Adamchik
34  */

35 class SerializableEntity implements XMLSerializable {
36
37     Element JavaDoc descriptor;
38     XMLMappingDescriptor descriptorMap;
39
40     // same node can store more than one object during encoding
41
transient Object JavaDoc object;
42
43     public SerializableEntity(XMLMappingDescriptor descriptorMap, Element JavaDoc descriptor) {
44         this.descriptor = descriptor;
45         this.descriptorMap = descriptorMap;
46     }
47
48     String JavaDoc getName() {
49         return descriptor.getAttribute("name");
50     }
51
52     Element JavaDoc getDescriptor() {
53         return descriptor;
54     }
55
56     void setObject(Object JavaDoc object) {
57         this.object = object;
58     }
59
60     public void encodeAsXML(XMLEncoder encoder) {
61         if (object instanceof Collection JavaDoc) {
62             Collection JavaDoc c = (Collection JavaDoc) object;
63             if (!c.isEmpty()) {
64
65                 // push the first node, and create the rest as peers.
66
Iterator JavaDoc it = c.iterator();
67                 encodeObject(encoder, it.next(), true);
68                 while (it.hasNext()) {
69                     encodeObject(encoder, it.next(), false);
70                 }
71                 
72                 // Make sure we pop the node we just pushed -- needed for fix to CAY-597.
73
encoder.pop();
74             }
75         }
76         else {
77             encodeObject(encoder, this.object, true);
78             
79             // Needed for fix to CAY-597. This makes sure we get back to the appropriate level in the DOM, rather than constantly re-rooting the tree.
80
encoder.pop();
81         }
82     }
83
84     public void decodeFromXML(XMLDecoder decoder) {
85         throw new CayenneRuntimeException("Decoding is not supported by this object");
86     }
87
88     void encodeObject(XMLEncoder encoder, Object JavaDoc object, boolean push) {
89         encoder.setRoot(descriptor.getAttribute("xmlTag"), null, push);
90
91         Iterator JavaDoc it = XMLUtil.getChildren(descriptor).iterator();
92         while (it.hasNext()) {
93
94             Element JavaDoc property = (Element JavaDoc) it.next();
95             String JavaDoc xmlTag = property.getAttribute("xmlTag");
96             String JavaDoc name = property.getAttribute("name");
97             Object JavaDoc value = PropertyUtils.getProperty(object, name);
98
99             if (value == null) {
100                 continue;
101             }
102
103             SerializableEntity relatedEntity = descriptorMap.getEntity(xmlTag);
104             if (relatedEntity != null) {
105                 relatedEntity.setObject(value);
106                 relatedEntity.encodeAsXML(encoder);
107             }
108             else {
109                 encoder.encodeProperty(xmlTag, value, false);
110             }
111         }
112     }
113 }
114
Popular Tags