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.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 * Converts from some DOM types to Java types
028 *
029 * @version $Revision: 830197 $
030 */
031 @Converter
032 public final class DomConverter {
033
034 private DomConverter() {
035 // Utility Class
036 }
037
038 @Converter
039 public static String toString(NodeList nodeList) {
040 StringBuffer buffer = new StringBuffer();
041 append(buffer, nodeList);
042 return buffer.toString();
043 }
044
045 @Converter
046 public static Integer toInteger(NodeList nodeList) {
047 StringBuffer buffer = new StringBuffer();
048 append(buffer, nodeList);
049 String s = buffer.toString();
050 return Integer.valueOf(s);
051 }
052
053 @Converter
054 public static Long toLong(NodeList nodeList) {
055 StringBuffer buffer = new StringBuffer();
056 append(buffer, nodeList);
057 String s = buffer.toString();
058 return Long.valueOf(s);
059 }
060
061 private static void append(StringBuffer buffer, NodeList nodeList) {
062 int size = nodeList.getLength();
063 for (int i = 0; i < size; i++) {
064 append(buffer, nodeList.item(i));
065 }
066 }
067
068 private static void append(StringBuffer buffer, Node node) {
069 if (node instanceof Text) {
070 Text text = (Text) node;
071 buffer.append(text.getTextContent());
072 } else if (node instanceof Attr) {
073 Attr attribute = (Attr) node;
074 buffer.append(attribute.getTextContent());
075 } else if (node instanceof Element) {
076 Element element = (Element) node;
077 append(buffer, element.getChildNodes());
078 }
079 }
080 }