01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.geronimo.kernel.config.xstream;
17:
18: import java.io.IOException;
19:
20: import com.thoughtworks.xstream.converters.Converter;
21: import com.thoughtworks.xstream.converters.MarshallingContext;
22: import com.thoughtworks.xstream.converters.UnmarshallingContext;
23: import com.thoughtworks.xstream.converters.ConversionException;
24: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
25: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
26: import org.w3c.dom.Element;
27: import org.w3c.dom.NodeList;
28: import org.w3c.dom.Node;
29:
30: /**
31: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
32: */
33: public class XStreamGBeanStateConverter implements Converter {
34: public boolean canConvert(Class clazz) {
35: return XStreamGBeanState.class.isAssignableFrom(clazz);
36: }
37:
38: public void marshal(Object object, HierarchicalStreamWriter writer,
39: MarshallingContext marshallingContext) {
40: XStreamGBeanState gbeanState = (XStreamGBeanState) object;
41: Element element = null;
42: try {
43: element = gbeanState.getGBeanState();
44: } catch (IOException e) {
45: throw new ConversionException(
46: "Cannot get xml version of gbeans", e);
47: }
48: marshallingContext.convertAnother(element);
49: }
50:
51: public Object unmarshal(HierarchicalStreamReader reader,
52: UnmarshallingContext unmarshallingContext) {
53: Element element = (Element) unmarshallingContext
54: .convertAnother(reader, Element.class);
55: NodeList childNodes = element.getChildNodes();
56: for (int i = 0; i < childNodes.getLength(); i++) {
57: Node node = childNodes.item(i);
58: if (node instanceof Element) {
59: element = (Element) node;
60: return new XStreamGBeanState(element);
61: }
62: }
63: throw new ConversionException("No nested nodes found");
64: }
65: }
|