Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.opensymphony.xwork2.conversion;

/**
* Represents specialized converters which can convert value to given type T
*/
public interface InternalConverter<T> {

boolean canConvert(Object value);

T convert(Object value);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.opensymphony.xwork2.conversion;

import java.util.ArrayList;
import java.util.List;

/**
* Holds reference to all internal converters to simplify lookup and registration
*/
public class InternalConverters<T> {

private final InternalConverter<T> NO_OP_CONVERTER = new InternalConverter<T>() {

public boolean canConvert(Object value) {
return false;
}

public T convert(Object value) {
return null;
}
};

private List<InternalConverter<T>> converters = new ArrayList<InternalConverter<T>>();

public boolean register(InternalConverter<T> converter) {
return converters.add(converter);
}

public InternalConverter<T> lookup(Object value) {
for (InternalConverter<T> converter : converters) {
if (converter.canConvert(value)) {
return converter;
}
}
return NO_OP_CONVERTER;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.opensymphony.xwork2.conversion.impl;

import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.conversion.InternalConverter;
import com.opensymphony.xwork2.conversion.InternalConverters;
import com.opensymphony.xwork2.conversion.string.StringDoubleConverter;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Member;
Expand All @@ -12,9 +17,18 @@

public class StringConverter extends DefaultTypeConverter {


private InternalConverters<String> converters = new InternalConverters<String>();

@Override
public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {
String result = null;
String result;

InternalConverter<String> converter = converters.lookup(value);
result = converter.convert(value);
if (result != null) {
return result;
}

if (value instanceof int[]) {
int[] x = (int[]) value;
Expand Down Expand Up @@ -70,4 +84,13 @@ public Object convertValue(Map<String, Object> context, Object target, Member me
}
return result;
}

@Inject
public void setLocaleProvider(LocaleProvider provider) {
super.setLocaleProvider(provider);

converters = new InternalConverters<String>();
converters.register(new StringDoubleConverter(provider));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.opensymphony.xwork2.conversion.string;

import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.conversion.InternalConverter;

import java.text.NumberFormat;

public class StringDoubleConverter implements InternalConverter<String> {

private LocaleProvider provider;

public StringDoubleConverter(LocaleProvider provider) {
this.provider = provider;
}

public boolean canConvert(Object value) {
return value.getClass() == Double.class || value.getClass() == double.class;
}

public String convert(Object value) {
NumberFormat format = NumberFormat.getNumberInstance(provider.getLocale());
return format.format(value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.opensymphony.xwork2.conversion.impl;

import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.XWorkTestCase;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class StringConverterTest extends XWorkTestCase {

private Map<String, Object> context = new HashMap<String, Object>();

public void testDoubleToString() throws Exception {
// given
Double d = 10.01;

StringConverter converter = new StringConverter();
converter.setLocaleProvider(buildProvider("DE"));

// when
Object actual = converter.convertValue(context, null, null, null, d, String.class);

// then
assertEquals("10,01", actual);
}

private LocaleProvider buildProvider(final String locale) {
return new LocaleProvider() {
public Locale getLocale() {
return new Locale(locale);
}
};
}

}