1 /**
2 *
3 * Copyright 2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.kernel.config.xstream;
18
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.ParserConfigurationException;
22
23 import com.thoughtworks.xstream.converters.Converter;
24 import com.thoughtworks.xstream.converters.MarshallingContext;
25 import com.thoughtworks.xstream.converters.UnmarshallingContext;
26 import com.thoughtworks.xstream.converters.ConversionException;
27 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
28 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
29 import com.thoughtworks.xstream.io.xml.DomReader;
30 import com.thoughtworks.xstream.io.xml.DomWriter;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.apache.geronimo.kernel.util.XmlUtil;
34
35 /**
36 * @version $Rev: 428843 $ $Date: 2006-08-04 11:43:59 -0700 (Fri, 04 Aug 2006) $
37 */
38 public class DomConverter implements Converter {
39 public boolean canConvert(Class clazz) {
40 return Document.class.isAssignableFrom(clazz) || Element.class.isAssignableFrom(clazz);
41 }
42
43 public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
44 DomReader reader;
45 if (object instanceof Document) {
46 Document doc = (Document) object;
47 reader = new DomReader(doc);
48 } else {
49 Element element = (Element) object;
50 reader = new DomReader(element);
51 }
52
53 copy(reader, writer);
54 }
55
56 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
57 DocumentBuilderFactory documentBuilderFactory = XmlUtil.newDocumentBuilderFactory();
58 DocumentBuilder documentBuilder = null;
59 try {
60 documentBuilder = documentBuilderFactory.newDocumentBuilder();
61 } catch (ParserConfigurationException e) {
62 throw new ConversionException("Cannot instantiate " + Document.class.getName(), e);
63 }
64 Document document = documentBuilder.newDocument();
65 DomWriter writer = new DomWriter(document);
66
67 copy(reader, writer);
68
69 if (Document.class.isAssignableFrom(unmarshallingContext.getRequiredType())) {
70 return document;
71 } else {
72 return document.getDocumentElement();
73 }
74 }
75
76 public static void copy(HierarchicalStreamReader reader, HierarchicalStreamWriter writer) {
77 writer.startNode(reader.getNodeName());
78
79
80 int attributeCount = reader.getAttributeCount();
81 for (int i = 0; i < attributeCount; i++) {
82 String attributeName = reader.getAttributeName(i);
83 String attributeValue = reader.getAttribute(i);
84 writer.addAttribute(attributeName, attributeValue);
85 }
86
87
88 while (reader.hasMoreChildren()) {
89 reader.moveDown();
90 copy(reader, writer);
91 reader.moveUp();
92 }
93
94
95 String value = reader.getValue();
96 if (value != null && value.trim().length() > 0) {
97 writer.setValue(value);
98 }
99
100 writer.endNode();
101 }
102 }