You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2007/03/30 22:04:29 UTC
svn commit: r524258 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/impl/converter/
test/java/org/apache/camel/converter/
Author: chirino
Date: Fri Mar 30 13:04:28 2007
New Revision: 524258
URL: http://svn.apache.org/viewvc?view=rev&rev=524258
Log:
Update to the DefaultTypeConverter do that it also delegates to the new PropertyEditorTypeConverter so that
PropertyEditor conversions are available to the system.
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?view=diff&rev=524258&r1=524257&r2=524258
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java Fri Mar 30 13:04:28 2007
@@ -17,17 +17,17 @@
*/
package org.apache.camel.impl.converter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConverter;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
/**
* @version $Revision$
*/
@@ -36,10 +36,13 @@
private Map<TypeMapping, TypeConverter> typeMappings = new HashMap<TypeMapping, TypeConverter>();
private Injector injector;
private List<TypeConverterLoader> typeConverterLoaders = new ArrayList<TypeConverterLoader>();
+ private List<TypeConverter> fallbackConverters = new ArrayList<TypeConverter>();
private boolean loaded;
public DefaultTypeConverter() {
typeConverterLoaders.add(new AnnotationTypeConverterLoader());
+ fallbackConverters.add(new PropertyEditorTypeConverter());
+ fallbackConverters.add(new ToStringTypeConverter());
}
public <T> T convertTo(Class<T> toType, Object value) {
@@ -51,11 +54,13 @@
if (converter != null) {
return converter.convertTo(toType, value);
}
- if (value != null) {
- if (toType.equals(String.class)) {
- return (T) value.toString();
- }
- }
+
+ for (TypeConverter fallback : fallbackConverters) {
+ T rc = fallback.convertTo(toType, value);
+ if( rc!=null )
+ return rc;
+ }
+
return null;
}
@@ -207,6 +212,11 @@
answer *= 37 + fromType.hashCode();
}
return answer;
+ }
+
+ @Override
+ public String toString() {
+ return "["+fromType+"=>"+toType+"]";
}
}
}
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java?view=auto&rev=524258
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/PropertyEditorTypeConverter.java Fri Mar 30 13:04:28 2007
@@ -0,0 +1,64 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl.converter;
+
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
+
+import org.apache.camel.TypeConverter;
+
+/**
+ * Uses the java.beans.PropertyEditor conversion system to convert Objects to and from String values.
+ *
+ * @version $Revision: 523731 $
+ */
+public class PropertyEditorTypeConverter implements TypeConverter {
+
+
+ public <T> T convertTo(Class<T> toType, Object value) {
+
+ // We can't convert null values since we can't figure out a property editor for it.
+ if( value == null )
+ return null;
+
+ if( value.getClass() == String.class ) {
+
+ // No conversion needed.
+ if( toType == String.class ) {
+ return toType.cast(value);
+ }
+
+ PropertyEditor editor = PropertyEditorManager.findEditor(toType);
+ if( editor != null ) {
+ editor.setAsText(value.toString());
+ return toType.cast(editor.getValue());
+ }
+
+ } else if( toType == String.class ) {
+
+ PropertyEditor editor = PropertyEditorManager.findEditor(value.getClass());
+ if( editor != null ) {
+ editor.setValue(value);
+ return toType.cast(editor.getAsText());
+ }
+
+ }
+ return null;
+ }
+
+}
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java?view=auto&rev=524258
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java Fri Mar 30 13:04:28 2007
@@ -0,0 +1,39 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl.converter;
+
+import org.apache.camel.TypeConverter;
+
+/**
+ * A simple converter that can convert any object to a String type by using the toString() method of the
+ * object.
+ *
+ * @version $Revision: 523731 $
+ */
+public class ToStringTypeConverter implements TypeConverter {
+
+ public <T> T convertTo(Class<T> toType, Object value) {
+ if (value != null) {
+ if (toType.equals(String.class)) {
+ return (T) value.toString();
+ }
+ }
+ return null;
+ }
+
+}
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java?view=diff&rev=524258&r1=524257&r2=524258
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java Fri Mar 30 13:04:28 2007
@@ -17,14 +17,17 @@
*/
package org.apache.camel.converter;
+import java.beans.PropertyEditorManager;
+import java.beans.PropertyEditorSupport;
+import java.io.InputStream;
+
import junit.framework.TestCase;
+
import org.apache.camel.TypeConverter;
import org.apache.camel.impl.converter.DefaultTypeConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import java.io.InputStream;
-
/**
* @version $Revision$
*/
@@ -33,6 +36,32 @@
protected TypeConverter converter = new DefaultTypeConverter();
+ public static class IntegerPropertyEditor extends PropertyEditorSupport {
+ public void setAsText(String text) throws IllegalArgumentException {
+ setValue(new Integer(text));
+ }
+
+ public String getAsText() {
+ Integer value = (Integer) getValue();
+ return (value != null ? value.toString() : "");
+ }
+ }
+
+
+ @Override
+ protected void setUp() throws Exception {
+ PropertyEditorManager.registerEditor(Integer.class, IntegerPropertyEditor.class);
+ }
+
+ public void testIntegerPropertyEditorConversion() throws Exception {
+ Integer value = converter.convertTo(Integer.class, "1000");
+ assertNotNull(value);
+ assertEquals("Converted to Integer", new Integer(1000), value);
+
+ String text = converter.convertTo(String.class, value);
+ assertEquals("Converted to String", "1000", text);
+ }
+
public void testConvertStringAndBytes() throws Exception {
byte[] array = converter.convertTo(byte[].class, "foo");
assertNotNull(array);
@@ -50,4 +79,5 @@
String text = converter.convertTo(String.class, inputStream);
assertEquals("Converted to String", "bar", text);
}
+
}
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java?view=diff&rev=524258&r1=524257&r2=524258
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/JaxpTest.java Fri Mar 30 13:04:28 2007
@@ -41,6 +41,7 @@
// lets now convert back again
String text = converter.convertTo(String.class, document);
- assertEquals("Converted to String", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>", text);
+ // The preamble changes a little under Java 1.6 it adds a standalone="no" attribute.
+ assertTrue("Converted to String", text.endsWith("?><hello>world!</hello>"));
}
}