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
019 import org.w3c.dom.Attr;
020 import org.w3c.dom.Document;
021 import org.w3c.dom.Element;
022 import org.w3c.dom.Node;
023 import org.w3c.dom.NodeList;
024 import org.w3c.dom.Text;
025 import org.apache.camel.Converter;
026
027 /**
028 * Converts from some DOM types to Java types
029 *
030 * @version $Revision: 738457 $
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 private static void append(StringBuffer buffer, NodeList nodeList) {
047 int size = nodeList.getLength();
048 for (int i = 0; i < size; i++) {
049 append(buffer, nodeList.item(i));
050 }
051 }
052
053 private static void append(StringBuffer buffer, Node node) {
054 if (node instanceof Text) {
055 Text text = (Text) node;
056 buffer.append(text.getTextContent());
057 } else if (node instanceof Attr) {
058 Attr attribute = (Attr) node;
059 buffer.append(attribute.getTextContent());
060 } else if (node instanceof Element) {
061 Element element = (Element) node;
062 append(buffer, element.getChildNodes());
063 } else if (node instanceof Document) {
064 Document doc = (Document) node;
065 append(buffer, doc.getChildNodes());
066 }
067 }
068 }