001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel.config.xstream;
017:
018: import javax.xml.parsers.DocumentBuilderFactory;
019: import javax.xml.parsers.DocumentBuilder;
020: import javax.xml.parsers.ParserConfigurationException;
021:
022: import com.thoughtworks.xstream.converters.Converter;
023: import com.thoughtworks.xstream.converters.MarshallingContext;
024: import com.thoughtworks.xstream.converters.UnmarshallingContext;
025: import com.thoughtworks.xstream.converters.ConversionException;
026: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
027: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
028: import com.thoughtworks.xstream.io.xml.DomReader;
029: import com.thoughtworks.xstream.io.xml.DomWriter;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032: import org.apache.geronimo.kernel.util.XmlUtil;
033:
034: /**
035: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
036: */
037: public class DomConverter implements Converter {
038: public boolean canConvert(Class clazz) {
039: return Document.class.isAssignableFrom(clazz)
040: || Element.class.isAssignableFrom(clazz);
041: }
042:
043: public void marshal(Object object, HierarchicalStreamWriter writer,
044: MarshallingContext marshallingContext) {
045: DomReader reader;
046: if (object instanceof Document) {
047: Document doc = (Document) object;
048: reader = new DomReader(doc);
049: } else {
050: Element element = (Element) object;
051: reader = new DomReader(element);
052: }
053:
054: copy(reader, writer);
055: }
056:
057: public Object unmarshal(HierarchicalStreamReader reader,
058: UnmarshallingContext unmarshallingContext) {
059: DocumentBuilderFactory documentBuilderFactory = XmlUtil
060: .newDocumentBuilderFactory();
061: DocumentBuilder documentBuilder = null;
062: try {
063: documentBuilder = documentBuilderFactory
064: .newDocumentBuilder();
065: } catch (ParserConfigurationException e) {
066: throw new ConversionException("Cannot instantiate "
067: + Document.class.getName(), e);
068: }
069: Document document = documentBuilder.newDocument();
070: DomWriter writer = new DomWriter(document);
071:
072: copy(reader, writer);
073:
074: if (Document.class.isAssignableFrom(unmarshallingContext
075: .getRequiredType())) {
076: return document;
077: } else {
078: return document.getDocumentElement();
079: }
080: }
081:
082: public static void copy(HierarchicalStreamReader reader,
083: HierarchicalStreamWriter writer) {
084: writer.startNode(reader.getNodeName());
085:
086: // write the attributes
087: int attributeCount = reader.getAttributeCount();
088: for (int i = 0; i < attributeCount; i++) {
089: String attributeName = reader.getAttributeName(i);
090: String attributeValue = reader.getAttribute(i);
091: writer.addAttribute(attributeName, attributeValue);
092: }
093:
094: // write the child nodes recursively
095: while (reader.hasMoreChildren()) {
096: reader.moveDown();
097: copy(reader, writer);
098: reader.moveUp();
099: }
100:
101: // write the context if any
102: String value = reader.getValue();
103: if (value != null && value.trim().length() > 0) {
104: writer.setValue(value);
105: }
106:
107: writer.endNode();
108: }
109: }
|