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     */
017    package org.apache.camel.converter.jaxp;
018    import org.w3c.dom.Attr;
019    import org.w3c.dom.Document;
020    import org.w3c.dom.Element;
021    import org.w3c.dom.Node;
022    import org.w3c.dom.NodeList;
023    import org.w3c.dom.Text;
024    import org.apache.camel.Converter;
025    
026    
027    /**
028     * Converts from some DOM types to Java types
029     *
030     * @version $Revision: 41910 $
031     */
032    @Converter
033    public final class DomConverter {
034    
035        private DomConverter() {
036            // Utility Class
037        }
038    
039        @Converter
040        public static String toString(NodeList nodeList) {
041            StringBuffer buffer = new StringBuffer();
042            append(buffer, nodeList);
043            return buffer.toString();
044        }
045    
046    /*
047        @Converter
048        public static String toString(Node node) {
049            StringBuffer buffer = new StringBuffer();
050            append(buffer, node);
051            return buffer.toString();
052        }
053    */
054    
055        protected static void append(StringBuffer buffer, NodeList nodeList) {
056            int size = nodeList.getLength();
057            for (int i = 0; i < size; i++) {
058                append(buffer, nodeList.item(i));
059            }
060        }
061    
062        protected static void append(StringBuffer buffer, Node node) {
063            if (node instanceof Text) {
064                Text text = (Text) node;
065                buffer.append(text.getTextContent());
066            } else if (node instanceof Attr) {
067                Attr attribute = (Attr) node;
068                buffer.append(attribute.getTextContent());
069    
070            } else if (node instanceof Element) {
071                Element element = (Element) node;
072                append(buffer, element.getChildNodes());
073            } else if (node instanceof Document) {
074                Document doc = (Document) node;
075                append(buffer, doc.getChildNodes());
076            }
077        }
078    }