KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > xstream > DomConverter


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import javax.xml.parsers.DocumentBuilder JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.apache.geronimo.kernel.util.XmlUtil;
34
35 /**
36  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class DomConverter implements Converter {
39     public boolean canConvert(Class JavaDoc clazz) {
40         return Document JavaDoc.class.isAssignableFrom(clazz) || Element JavaDoc.class.isAssignableFrom(clazz);
41     }
42
43     public void marshal(Object JavaDoc object, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
44         DomReader reader;
45         if (object instanceof Document JavaDoc) {
46             Document JavaDoc doc = (Document JavaDoc) object;
47             reader = new DomReader(doc);
48         } else {
49             Element JavaDoc element = (Element JavaDoc) object;
50             reader = new DomReader(element);
51         }
52
53         copy(reader, writer);
54     }
55
56     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
57         DocumentBuilderFactory JavaDoc documentBuilderFactory = XmlUtil.newDocumentBuilderFactory();
58         DocumentBuilder JavaDoc documentBuilder = null;
59         try {
60             documentBuilder = documentBuilderFactory.newDocumentBuilder();
61         } catch (ParserConfigurationException JavaDoc e) {
62             throw new ConversionException("Cannot instantiate " + Document JavaDoc.class.getName(), e);
63         }
64         Document JavaDoc document = documentBuilder.newDocument();
65         DomWriter writer = new DomWriter(document);
66
67         copy(reader, writer);
68
69         if (Document JavaDoc.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         // write the attributes
80
int attributeCount = reader.getAttributeCount();
81         for (int i = 0; i < attributeCount; i++) {
82             String JavaDoc attributeName = reader.getAttributeName(i);
83             String JavaDoc attributeValue = reader.getAttribute(i);
84             writer.addAttribute(attributeName, attributeValue);
85         }
86
87         // write the child nodes recursively
88
while (reader.hasMoreChildren()) {
89             reader.moveDown();
90             copy(reader, writer);
91             reader.moveUp();
92         }
93
94         // write the context if any
95
String JavaDoc value = reader.getValue();
96         if (value != null && value.trim().length() > 0) {
97             writer.setValue(value);
98         }
99
100         writer.endNode();
101     }
102 }
103
Popular Tags