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;
018    
019    import java.util.Arrays;
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.Iterator;
023    import java.util.List;
024    import org.w3c.dom.Node;
025    import org.w3c.dom.NodeList;
026    
027    import org.apache.camel.Converter;
028    
029    
030    
031    /**
032     * Some core java.lang based <a
033     * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
034     *
035     * @version $Revision: 36321 $
036     */
037    @Converter
038    public final class ObjectConverter {
039    
040        /**
041         * Utility classes should not have a public constructor.
042         */
043        private ObjectConverter() {
044        }
045    
046        public static boolean isCollection(Object value) {
047            // TODO we should handle primitive array types?
048            return value instanceof Collection || (value != null && value.getClass().isArray());
049        }
050    
051        /**
052         * Creates an iterator over the value if the value is a collection, an
053         * Object[] or a primitive type array; otherwise to simplify the caller's
054         * code, we just create a singleton collection iterator over a single value
055         */
056        @Converter
057        public static Iterator iterator(Object value) {
058            if (value == null) {
059                return Collections.EMPTY_LIST.iterator();
060            } else if (value instanceof Collection) {
061                Collection collection = (Collection)value;
062                return collection.iterator();
063            } else if (value.getClass().isArray()) {
064                // TODO we should handle primitive array types?
065                List<Object> list = Arrays.asList((Object[]) value);
066                return list.iterator();
067            } else if (value instanceof NodeList) {
068                // lets iterate through DOM results after performing XPaths
069                final NodeList nodeList = (NodeList) value;
070                return new Iterator<Node>() {
071                    int idx = -1;
072    
073                    public boolean hasNext() {
074                        return ++idx < nodeList.getLength();
075                    }
076    
077                    public Node next() {
078                        return nodeList.item(idx);
079                    }
080    
081                    public void remove() {
082                        throw new UnsupportedOperationException();
083                    }
084                };
085            } else {
086                return Collections.singletonList(value).iterator();
087            }
088        }
089    
090        /**
091         * Converts the given value to a boolean, handling strings or Boolean
092         * objects; otherwise returning false if the value could not be converted to
093         * a boolean
094         */
095        @Converter
096        public static boolean toBool(Object value) {
097            Boolean answer = toBoolean(value);
098            if (answer != null) {
099                return answer.booleanValue();
100            }
101            return false;
102        }
103    
104        /**
105         * Converts the given value to a Boolean, handling strings or Boolean
106         * objects; otherwise returning null if the value cannot be converted to a
107         * boolean
108         */
109        @Converter
110        public static Boolean toBoolean(Object value) {
111            if (value instanceof Boolean) {
112                return (Boolean)value;
113            }
114            if (value instanceof String) {
115                return "true".equalsIgnoreCase(value.toString()) ? Boolean.TRUE : Boolean.FALSE;
116            }
117            return null;
118        }
119    
120        /**
121         * Returns the boolean value, or null if the value is null
122         */
123        @Converter
124        public static Boolean toBoolean(Boolean value) {
125            if (value != null) {
126                return value.booleanValue();
127            }
128            return false;
129        }
130    
131    
132        /**
133         * Returns the converted value, or null if the value is null
134         */
135        @Converter
136        public static Byte toByte(Object value) {
137            if (value instanceof Byte) {
138                return (Byte) value;
139            } else if (value instanceof Number) {
140                Number number = (Number) value;
141                return number.byteValue();
142            } else if (value instanceof String) {
143                return Byte.parseByte((String) value);
144            } else {
145                return null;
146            }
147        }
148    
149        @Converter
150        public static byte[] toByteArray(String value) {
151            return value.getBytes();
152        }
153    
154        @Converter
155        public static char[] toCharArray(String value) {
156            return value.toCharArray();
157        }
158    
159        @Converter
160        public static String fromCharArray(char[] value) {
161            return new String(value);
162        }
163    
164        /**
165         * Returns the converted value, or null if the value is null
166         */
167        @Converter
168        public static Short toShort(Object value) {
169            if (value instanceof Short) {
170                return (Short) value;
171            } else if (value instanceof Number) {
172                Number number = (Number) value;
173                return number.shortValue();
174            } else if (value instanceof String) {
175                return Short.parseShort((String) value);
176            } else {
177                return null;
178            }
179        }
180    
181        /**
182         * Returns the converted value, or null if the value is null
183         */
184        @Converter
185        public static Integer toInteger(Object value) {
186            if (value instanceof Integer) {
187                return (Integer) value;
188            } else if (value instanceof Number) {
189                Number number = (Number) value;
190                return number.intValue();
191            } else if (value instanceof String) {
192                return Integer.parseInt((String) value);
193            } else {
194                return null;
195            }
196        }
197    
198        /**
199         * Returns the converted value, or null if the value is null
200         */
201        @Converter
202        public static Long toLong(Object value) {
203            if (value instanceof Long) {
204                return (Long) value;
205            } else if (value instanceof Number) {
206                Number number = (Number) value;
207                return number.longValue();
208            } else if (value instanceof String) {
209                return Long.parseLong((String) value);
210            } else {
211                return null;
212            }
213        }
214    
215        /**
216         * Returns the converted value, or null if the value is null
217         */
218        @Converter
219        public static Float toFloat(Object value) {
220            if (value instanceof Float) {
221                return (Float) value;
222            } else if (value instanceof Number) {
223                Number number = (Number) value;
224                return number.floatValue();
225            } else if (value instanceof String) {
226                return Float.parseFloat((String) value);
227            } else {
228                return null;
229            }
230        }
231    
232        /**
233         * Returns the converted value, or null if the value is null
234         */
235        @Converter
236        public static Double toDouble(Object value) {
237            if (value instanceof Double) {
238                return (Double) value;
239            } else if (value instanceof Number) {
240                Number number = (Number) value;
241                return number.doubleValue();
242            } else if (value instanceof String) {
243                return Double.parseDouble((String) value);
244            } else {
245                return null;
246            }
247        }
248    
249    
250    
251    }