21 |
|
|
22 |
package nongnu.cashews.xml; |
package nongnu.cashews.xml; |
23 |
|
|
24 |
import java.io.ObjectStreamClass; |
import java.io.ObjectStreamField; |
25 |
import java.io.Serializable; |
import java.io.Serializable; |
26 |
|
|
27 |
import java.lang.reflect.Field; |
import java.lang.reflect.Field; |
40 |
|
|
41 |
import javax.xml.namespace.QName; |
import javax.xml.namespace.QName; |
42 |
|
|
43 |
|
import nongnu.cashews.commons.Pair; |
44 |
|
import nongnu.cashews.commons.PairList; |
45 |
|
|
46 |
import nongnu.cashews.language.grounding.MessagePart; |
import nongnu.cashews.language.grounding.MessagePart; |
47 |
import nongnu.cashews.language.grounding.SoapMessage; |
import nongnu.cashews.language.grounding.SoapMessage; |
48 |
import nongnu.cashews.language.grounding.SoapOperation; |
import nongnu.cashews.language.grounding.SoapOperation; |
110 |
|
|
111 |
/** |
/** |
112 |
* Serializes the specified object into an XML tree. If the supplied |
* Serializes the specified object into an XML tree. If the supplied |
113 |
* document argument is not <code>null</code>, the resulting tree |
* root argument is not <code>null</code>, the resulting tree |
114 |
* is appended it. Otherwise, a new document is created. |
* is appended to it. Otherwise, the top-level element of the |
115 |
|
* class is the root. |
116 |
* |
* |
117 |
* @param object the object to serialize. |
* @param object the object to serialize. |
118 |
* @param root the root node to append to, or <code>null</code> |
* @param root the root node to append to, or <code>null</code> |
125 |
Document document) |
Document document) |
126 |
throws IllegalAccessException |
throws IllegalAccessException |
127 |
{ |
{ |
128 |
List<Field> fields = new LinkedList<Field>(); |
return serialize(object, root, document, true); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* Serializes the specified object into an XML tree. If the supplied |
133 |
|
* root argument is not <code>null</code>, the resulting tree |
134 |
|
* is appended to it. Otherwise, the class name node is used. |
135 |
|
* |
136 |
|
* @param object the object to serialize. |
137 |
|
* @param root the root node to append to, or <code>null</code> |
138 |
|
* if a new root node should be created. |
139 |
|
* @param document the document to use for creation. |
140 |
|
* @param includeClassNameElement a flag which, when <code>false</code>, |
141 |
|
* causes the root node with the name of the class to be |
142 |
|
* suppressed. |
143 |
|
* @return the serialized object in XML form. |
144 |
|
* @throws IllegalAccessException if a field can't be accessed. |
145 |
|
* @throws IllegalStateException if the root node is null and the |
146 |
|
* class name node is suppressed. |
147 |
|
*/ |
148 |
|
public static Element serialize(Serializable object, Element root, |
149 |
|
Document document, |
150 |
|
boolean includeClassNameElement) |
151 |
|
throws IllegalAccessException |
152 |
|
{ |
153 |
|
PairList<XmlField,Field> fields = new PairList<XmlField,Field>(); |
154 |
Class clazz = object.getClass(); |
Class clazz = object.getClass(); |
|
String elementName = null; |
|
155 |
Xmlizable customObject = null; |
Xmlizable customObject = null; |
156 |
if (object instanceof Xmlizable) |
Element objRoot; |
157 |
|
if (!includeClassNameElement) |
158 |
{ |
{ |
159 |
customObject = (Xmlizable) object; |
if (root == null) |
160 |
elementName = customObject.getElementName(); |
throw new IllegalStateException("Impossible to serialize a class "+ |
161 |
|
"with no supplied root node or "+ |
162 |
|
"class name root node."); |
163 |
|
objRoot = root; |
164 |
|
root = null; |
165 |
|
} |
166 |
|
else |
167 |
|
{ |
168 |
|
String elementName = null; |
169 |
|
if (object instanceof Xmlizable) |
170 |
|
{ |
171 |
|
customObject = (Xmlizable) object; |
172 |
|
elementName = customObject.getElementName(); |
173 |
|
} |
174 |
|
if (elementName == null) |
175 |
|
elementName = clazz.getSimpleName(); |
176 |
|
objRoot = createElement(document, elementName); |
177 |
} |
} |
|
if (elementName == null) |
|
|
elementName = clazz.getSimpleName(); |
|
|
Element objRoot = createElement(document, elementName); |
|
178 |
if (customObject != null) |
if (customObject != null) |
179 |
addNamespaceDeclarations(customObject.getDeclaredNamespaces(), objRoot); |
addNamespaceDeclarations(customObject.getDeclaredNamespaces(), objRoot); |
180 |
while (clazz != null) |
while (clazz != null) |
181 |
{ |
{ |
182 |
fields.addAll(0, Arrays.asList(clazz.getDeclaredFields())); |
PairList<XmlField,Field> newFields = new PairList<XmlField,Field>(); |
183 |
|
try |
184 |
|
{ |
185 |
|
Field serialField = |
186 |
|
clazz.getDeclaredField("serialPersistentFields"); |
187 |
|
ObjectStreamField[] osFields = (ObjectStreamField[]) |
188 |
|
serialField.get(null); |
189 |
|
for (ObjectStreamField osField : osFields) |
190 |
|
{ |
191 |
|
XmlField xField; |
192 |
|
if (osField instanceof XmlField) |
193 |
|
xField = (XmlField) osField; |
194 |
|
else |
195 |
|
xField = new XmlField(osField); |
196 |
|
try |
197 |
|
{ |
198 |
|
newFields.add(xField, |
199 |
|
clazz.getDeclaredField(xField.getName())); |
200 |
|
} |
201 |
|
catch (NoSuchFieldException e) |
202 |
|
{ |
203 |
|
throw new |
204 |
|
IllegalStateException("Inconsistency between "+ |
205 |
|
"listed and actual serializable " + |
206 |
|
"fields in " + osField.getName() |
207 |
|
+ ".",e); |
208 |
|
} |
209 |
|
} |
210 |
|
} |
211 |
|
catch (NoSuchFieldException e) |
212 |
|
{ |
213 |
|
Field[] clFields = clazz.getDeclaredFields(); |
214 |
|
for (Field field : clFields) |
215 |
|
newFields.add(new XmlField(field.getName(), |
216 |
|
field.getType()),field); |
217 |
|
} |
218 |
|
fields.addAll(0,newFields); |
219 |
clazz = clazz.getSuperclass(); |
clazz = clazz.getSuperclass(); |
220 |
} |
} |
221 |
TypeMapper mapper = new TypeMapper(); |
TypeMapper mapper = new TypeMapper(); |
222 |
for (Field field: fields) |
for (Pair<XmlField,Field> pair: fields) |
223 |
{ |
{ |
224 |
|
XmlField xField = pair.getLeft(); |
225 |
|
Field field = pair.getRight(); |
226 |
if (Modifier.isTransient(field.getModifiers())) |
if (Modifier.isTransient(field.getModifiers())) |
227 |
continue; |
continue; |
228 |
System.out.println("field: " + field); |
System.out.println("field: " + field); |
246 |
serialize((Serializable) obj, objRoot, document); |
serialize((Serializable) obj, objRoot, document); |
247 |
} |
} |
248 |
else if (value instanceof Serializable) |
else if (value instanceof Serializable) |
249 |
serialize((Serializable) value, objRoot, document); |
{ |
250 |
|
Element element; |
251 |
|
if (xField.isFieldNameSerialized()) |
252 |
|
{ |
253 |
|
element = createElement(document, field.getName()); |
254 |
|
objRoot.appendChild(element); |
255 |
|
} |
256 |
|
else |
257 |
|
element = objRoot; |
258 |
|
serialize((Serializable) value, element, document, |
259 |
|
xField.isClassNameSerialized()); |
260 |
|
} |
261 |
else |
else |
262 |
{ |
{ |
263 |
Element element = createElement(document, field.getName()); |
Element element = createElement(document, field.getName()); |