01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE 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
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19: package de.schlund.pfixcore.oxm.impl;
20:
21: import java.util.Stack;
22:
23: /**
24: * @author mleidig@schlund.de
25: */
26: public class SerializationContext {
27:
28: private SerializerRegistry registry;
29: private Stack<Object> objectStack = new Stack<Object>();
30: private Stack<XPathPosition> positionStack = new Stack<XPathPosition>();
31:
32: public SerializationContext(SerializerRegistry registry) {
33: this .registry = registry;
34: }
35:
36: public void serialize(Object obj, XMLWriter writer)
37: throws SerializationException {
38: SimpleTypeSerializer simpleSerializer = registry
39: .getSimpleTypeSerializer(obj.getClass());
40: if (simpleSerializer != null) {
41: String value = serialize(obj, simpleSerializer);
42: writer.writeCharacters(value);
43: } else {
44: ComplexTypeSerializer complexSerializer = registry
45: .getSerializer(obj.getClass());
46: if (complexSerializer != null) {
47: serialize(obj, writer, complexSerializer);
48: }
49: }
50: }
51:
52: public void serialize(Object obj, XMLWriter writer,
53: ComplexTypeSerializer serializer)
54: throws SerializationException {
55: if (!objectStack.contains(obj)) {
56: objectStack.push(obj);
57: positionStack.push(writer.getCurrentPosition());
58: serializer.serialize(obj, this , writer);
59: objectStack.pop();
60: positionStack.pop();
61: } else {
62: int ind = objectStack.indexOf(obj);
63: XPathPosition p = positionStack.get(ind);
64: writer.writeAttribute("xpathref", p.getXPath());
65: }
66: }
67:
68: public String serialize(Object obj) throws SerializationException {
69: SimpleTypeSerializer serializer = registry
70: .getSimpleTypeSerializer(obj.getClass());
71: return serialize(obj, serializer);
72: }
73:
74: public String serialize(Object obj, SimpleTypeSerializer serializer)
75: throws SerializationException {
76: return serializer.serialize(obj, this );
77: }
78:
79: public boolean hasSimpleTypeSerializer(Class<?> clazz) {
80: SimpleTypeSerializer serializer = registry
81: .getSimpleTypeSerializer(clazz);
82: return serializer != null;
83: }
84:
85: public String mapClassName(Object obj) {
86: return registry.mapClassName(obj);
87: }
88:
89: }
|