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 java.io.ByteArrayInputStream;
020    import java.io.InputStream;
021    import java.util.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import javax.xml.transform.TransformerException;
026    
027    import org.w3c.dom.Attr;
028    import org.w3c.dom.Element;
029    import org.w3c.dom.Node;
030    import org.w3c.dom.NodeList;
031    import org.w3c.dom.Text;
032    
033    import org.apache.camel.Converter;
034    import org.apache.camel.Exchange;
035    import org.apache.camel.util.ObjectHelper;
036    
037    /**
038     * Converts from some DOM types to Java types
039     *
040     * @version 
041     */
042    @Converter
043    public final class DomConverter {
044        private final XmlConverter xml;
045    
046        public DomConverter() {
047            xml = new XmlConverter();
048        }
049    
050        @Converter
051        public String toString(NodeList nodeList, Exchange exchange) throws TransformerException {
052            // converting NodeList to String is more tricky
053            // sometimes the NodeList is a Node which we can then leverage
054            // the XML converter to turn into XML incl. tags
055    
056            StringBuilder buffer = new StringBuilder();
057    
058            // use XML converter at first since it preserves tag names
059            boolean found = false;
060            if (nodeList instanceof Node) {
061                Node node = (Node) nodeList;
062                String s = xml.toString(node, exchange);
063                if (ObjectHelper.isNotEmpty(s)) {
064                    found = true;
065                    buffer.append(s);
066                }
067            } else {
068                // use XML converter at first since it preserves tag names
069                int size = nodeList.getLength();
070                for (int i = 0; i < size; i++) {
071                    Node node = nodeList.item(i);
072                    String s = xml.toString(node, exchange);
073                    if (ObjectHelper.isNotEmpty(s)) {
074                        found = true;
075                        buffer.append(s);
076                    }
077                }
078            }
079    
080            // and eventually we must fallback to append without tags, such as when you have
081            // used an xpath to select an attribute or text() or something
082            if (!found) {
083                append(buffer, nodeList);
084            }
085    
086            return buffer.toString();
087        }
088    
089        @Converter
090        public static Integer toInteger(NodeList nodeList) {
091            StringBuilder buffer = new StringBuilder();
092            append(buffer, nodeList);
093            String s = buffer.toString();
094            return Integer.valueOf(s);
095        }
096    
097        @Converter
098        public static Long toLong(NodeList nodeList) {
099            StringBuilder buffer = new StringBuilder();
100            append(buffer, nodeList);
101            String s = buffer.toString();
102            return Long.valueOf(s);
103        }
104    
105        @Converter
106        public static List<?> toList(NodeList nodeList) {
107            List<Object> answer = new ArrayList<Object>();
108            Iterator<Object> it = ObjectHelper.createIterator(nodeList);
109            while (it.hasNext()) {
110                answer.add(it.next());
111            }
112            return answer;
113        }
114    
115        @Converter
116        public InputStream toInputStream(NodeList nodeList, Exchange exchange) throws TransformerException {
117            return new ByteArrayInputStream(toByteArray(nodeList, exchange));
118        }
119    
120        @Converter
121        public byte[] toByteArray(NodeList nodeList, Exchange exchange) throws TransformerException {
122            String data = toString(nodeList, exchange);
123            return data.getBytes();
124        }
125    
126        private static void append(StringBuilder buffer, NodeList nodeList) {
127            int size = nodeList.getLength();
128            for (int i = 0; i < size; i++) {
129                append(buffer, nodeList.item(i));
130            }
131        }
132    
133        private static void append(StringBuilder buffer, Node node) {
134            if (node instanceof Text) {
135                Text text = (Text) node;
136                buffer.append(text.getTextContent());
137            } else if (node instanceof Attr) {
138                Attr attribute = (Attr) node;
139                buffer.append(attribute.getTextContent());
140            } else if (node instanceof Element) {
141                Element element = (Element) node;
142                append(buffer, element.getChildNodes());
143            }
144        }
145    }