converterutil.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,045 行 · 第 1/3 页
JAVA
1,045 行
}
returnArray = array;
} else if (float.class.equals(baseArrayClass)) {
float[] array = new float[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = Float.parseFloat(o.toString());
} else {
array[i] = Float.NaN;
}
}
returnArray = array;
} else if (short.class.equals(baseArrayClass)) {
short[] array = new short[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = Short.parseShort(o.toString());
} else {
array[i] = Short.MIN_VALUE;
}
}
returnArray = array;
} else if (byte.class.equals(baseArrayClass)) {
byte[] array = new byte[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = Byte.parseByte(o.toString());
} else {
array[i] = Byte.MIN_VALUE;
}
}
returnArray = array;
} else if (long.class.equals(baseArrayClass)) {
long[] array = new long[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = Long.parseLong(o.toString());
} else {
array[i] = Long.MIN_VALUE;
}
}
returnArray = array;
} else if (boolean.class.equals(baseArrayClass)) {
boolean[] array = new boolean[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = Boolean.getBoolean(o.toString());
}
}
returnArray = array;
} else if (char.class.equals(baseArrayClass)) {
char[] array = new char[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = o.toString().toCharArray()[0];
}
}
returnArray = array;
} else if (double.class.equals(baseArrayClass)) {
double[] array = new double[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
array[i] = Double.parseDouble(o.toString());
} else {
array[i] = Double.NaN;
}
}
returnArray = array;
} else if (Calendar.class.equals(baseArrayClass)) {
Calendar[] array = new Calendar[listSize];
for (int i = 0; i < listSize; i++) {
Object o = objectList.get(i);
if (o != null) {
if (o instanceof String){
array[i] = ConverterUtil.convertToDateTime(o.toString());
} else if (o instanceof Calendar) {
array[i] = (Calendar) o;
}
}
}
returnArray = array;
} else {
returnArray = Array.newInstance(baseArrayClass, listSize);
ConvertToArbitraryObjectArray(returnArray, baseArrayClass, objectList);
}
return returnArray;
}
/**
* @param returnArray
* @param baseArrayClass
* @param objectList
*/
private static void ConvertToArbitraryObjectArray(Object returnArray,
Class baseArrayClass,
List objectList) {
if (!(ADBBean.class.isAssignableFrom(baseArrayClass))) {
try {
for (int i = 0; i < objectList.size(); i++) {
Object o = objectList.get(i);
if (o == null) {
// if the string is null the object value must be null
Array.set(returnArray, i, null);
} else {
Array.set(returnArray, i, getObjectForClass(
baseArrayClass,
o.toString()));
}
}
return;
} catch (Exception e) {
//oops! - this cannot be converted fall through and
//try the other alternative
}
}
try {
objectList.toArray((Object[])returnArray);
} catch (Exception e) {
//we are over with alternatives - throw the
//converison exception
throw new ObjectConversionException(e);
}
}
/**
* We could have used the Arraya.asList() method but that returns an *immutable* list !!!!!
*
* @param array
* @return list
*/
public static List toList(Object[] array) {
if (array == null) {
return new ArrayList();
} else {
ArrayList list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
return list;
}
}
/**
* @param intValue
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static int compare(int intValue, String value) {
return intValue - Integer.parseInt(value);
}
/**
* @param doubleValue
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static double compare(double doubleValue, String value) {
return doubleValue - Double.parseDouble(value);
}
/**
* @param floatValue
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static float compare(float floatValue, String value) {
return floatValue - Float.parseFloat(value);
}
/**
* @param longValue
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static long compare(long longValue, String value) {
return longValue - Long.parseLong(value);
}
/**
* @param shortValue
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static int compare(short shortValue, String value) {
return shortValue - Short.parseShort(value);
}
/**
* @param byteVlaue
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static int compare(byte byteVlaue, String value) {
return byteVlaue - Byte.parseByte(value);
}
/**
* @param binBigInteger
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static int compare(BigInteger binBigInteger, String value) {
return binBigInteger.intValue() - Integer.parseInt(value);
}
/**
* @param binBigDecimal
* @param value
* @return 0 if equal , + value if greater than , - value if less than
*/
public static double compare(BigDecimal binBigDecimal, String value) {
return binBigDecimal.doubleValue() - Double.parseDouble(value);
}
public static long compare(Duration duration, String value) {
Duration compareValue = new Duration(value);
return duration.compare(compareValue);
}
public static long compare(Date date, String value) {
Date newDate = convertToDate(value);
return date.getTime() - newDate.getTime();
}
public static long compare(Time time, String value) {
Time newTime = new Time(value);
return time.getAsCalendar().getTimeInMillis() - newTime.getAsCalendar().getTimeInMillis();
}
public static long compare(Calendar calendar, String value) {
Calendar newCalendar = convertToDateTime(value);
return calendar.getTimeInMillis() - newCalendar.getTimeInMillis();
}
/**
* Converts the given .datahandler to a string
*
* @return string
*/
public static String getStringFromDatahandler(DataHandler dataHandler) {
try {
InputStream inStream;
if (dataHandler == null) {
return "";
}
inStream = dataHandler.getDataSource().getInputStream();
byte[] data = IOUtils.getStreamAsByteArray(inStream);
return Base64.encode(data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* A reflection based method to generate an instance of a given class and populate it with a
* given value
*
* @param clazz
* @param value
* @return object
*/
public static Object getObjectForClass(Class clazz, String value) {
//first see whether this class has a constructor that can
//take the string as an argument.
boolean continueFlag = false;
try {
Constructor stringConstructor = clazz.getConstructor(new Class[] { String.class });
return stringConstructor.newInstance(new Object[] { value });
} catch (NoSuchMethodException e) {
//oops - no such constructors - continue with the
//parse method
continueFlag = true;
} catch (Exception e) {
throw new ObjectConversionException(
ADBMessages.getMessage("converter.cannotGenerate",
clazz.getName()),
e);
}
if (!continueFlag) {
throw new ObjectConversionException(
ADBMessages.getMessage("converter.cannotConvert",
clazz.getName()));
}
try {
Method parseMethod = clazz.getMethod("parse", new Class[] { String.class });
Object instance = clazz.newInstance();
return parseMethod.invoke(instance, new Object[] { value });
} catch (NoSuchMethodException e) {
throw new ObjectConversionException(e);
} catch (Exception e) {
throw new ObjectConversionException(
ADBMessages.getMessage("converter.cannotGenerate",
clazz.getName()),
e);
}
}
/** A simple exception that is thrown when the conversion fails */
public static class ObjectConversionException extends RuntimeException {
public ObjectConversionException() {
}
public ObjectConversionException(String message) {
super(message);
}
public ObjectConversionException(Throwable cause) {
super(cause);
}
public ObjectConversionException(String message, Throwable cause) {
super(message, cause);
}
}
// serialization methods for xsd any type
/**
* serialize the any type string object
* @param string
* @param xmlStreamWriter
*/
public static void serializeAnyType(String string, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
if (xmlStreamWriter.getPrefix(Constants.XSI_NAMESPACE) == null){
String prefix = BeanUtil.getUniquePrefix();
xmlStreamWriter.writeNamespace(prefix,Constants.XSI_NAMESPACE);
xmlStreamWriter.setPrefix(prefix,Constants.XSI_NAMESPACE);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?