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 javax.xml.transform.Result;
22: import javax.xml.transform.dom.DOMResult;
23:
24: import org.w3c.dom.Document;
25: import org.w3c.dom.Node;
26:
27: import de.schlund.pfixcore.oxm.Marshaller;
28: import de.schlund.pfixcore.oxm.MarshallingException;
29:
30: /**
31: *
32: * Marshaller implementation which serializes an object into a W3C DOM
33: *
34: * @author mleidig@schlund.de
35: */
36: public class MarshallerImpl implements Marshaller {
37:
38: SerializerRegistry registry;
39:
40: public MarshallerImpl(SerializerRegistry registry) {
41: this .registry = registry;
42: }
43:
44: public void marshal(Object obj, Result result)
45: throws MarshallingException {
46: if (!(result instanceof DOMResult))
47: throw new IllegalArgumentException(
48: "Result must be of type: "
49: + DOMResult.class.getName());
50: DOMResult domResult = (DOMResult) result;
51: Node node = domResult.getNode();
52: if (node instanceof Document)
53: node = ((Document) node).getDocumentElement();
54: XMLWriter xmlWriter = new DOMWriter(node);
55: SerializationContext context = new SerializationContext(
56: registry);
57: try {
58: context.serialize(obj, xmlWriter);
59: } catch (SerializationException x) {
60: throw new MarshallingException(
61: "Error while marshalling object.", x);
62: }
63: }
64:
65: }
|