/** * Examines a property's type to see which method should be used to parse the property's value. * * @param desc The description of the property * @param value The value of the XML attribute containing the prop value * @return The value stored in the element * @throws IOException If there is an error reading the document */ public Object getObjectValue(PropertyDescriptor desc, String value) throws IOException { // Find out what kind of property it is Class type = desc.getPropertyType(); // If it's an array, get the base type if (type.isArray()) { type = type.getComponentType(); } // For native types, object wrappers for natives, and strings, use the // basic parse routine if (type.equals(Integer.TYPE) || type.equals(Long.TYPE) || type.equals(Short.TYPE) || type.equals(Byte.TYPE) || type.equals(Boolean.TYPE) || type.equals(Float.TYPE) || type.equals(Double.TYPE) || Integer.class.isAssignableFrom(type) || Long.class.isAssignableFrom(type) || Short.class.isAssignableFrom(type) || Byte.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type) || Float.class.isAssignableFrom(type) || Double.class.isAssignableFrom(type)) { return parseBasicType(type, value); } else if (String.class.isAssignableFrom(type)) { return value; } else if (java.util.Date.class.isAssignableFrom(type)) { // If it's a date, use the date parser return parseDate(value, JOXDateHandler.determineDateFormat()); } else { return null; } }
/** * Examines a property's type to see which method should be used to parse the property's value. * * @param desc The description of the property * @param element The XML element containing the property value * @return The value stored in the element * @throws IOException If there is an error reading the document */ public Object getObjectValue(PropertyDescriptor desc, Element element) throws IOException { // Find out what kind of property it is Class type = desc.getPropertyType(); // If it's an array, get the base type if (type.isArray()) { type = type.getComponentType(); } // For native types, object wrappers for natives, and strings, use the // basic parse routine if (type.equals(Integer.TYPE) || type.equals(Long.TYPE) || type.equals(Short.TYPE) || type.equals(Byte.TYPE) || type.equals(Boolean.TYPE) || type.equals(Float.TYPE) || type.equals(Double.TYPE) || Integer.class.isAssignableFrom(type) || Long.class.isAssignableFrom(type) || Short.class.isAssignableFrom(type) || Byte.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type) || Float.class.isAssignableFrom(type) || Double.class.isAssignableFrom(type) || String.class.isAssignableFrom(type)) { return readBasicType(type, element); } else if (java.util.Date.class.isAssignableFrom(type)) { // If it's a date, use the date parser return readDate(element); } else { try { // If it's an object, create a new instance of the object (it should // be a bean, or there will be trouble) Object newOb = type.newInstance(); // Copy the XML element into the bean readObject(newOb, element); return newOb; } catch (InstantiationException exc) { throw new IOException( "Error creating object for " + desc.getName() + ": " + exc.toString()); } catch (IllegalAccessException exc) { throw new IOException( "Error creating object for " + desc.getName() + ": " + exc.toString()); } } }
public static Object element(Object arg, ExecutionContext context) throws FunctionDomainException, TypeMismatchException { if (arg == null || arg == QueryService.UNDEFINED) return QueryService.UNDEFINED; if (arg instanceof Collection) { Collection c = (Collection) arg; // for remote distinct queries, the result of sub query could contain a // mix of String and PdxString which could be duplicates, so convert all // PdxStrings to String if (context.isDistinct() && ((DefaultQuery) context.getQuery()).isRemoteQuery()) { Set tempResults = new HashSet(); for (Object o : c) { if (o instanceof PdxString) { o = ((PdxString) o).toString(); } tempResults.add(o); } c.clear(); c.addAll(tempResults); tempResults = null; } checkSingleton(c.size()); return c.iterator().next(); } // not a Collection, must be an array Class clazz = arg.getClass(); if (!clazz.isArray()) throw new TypeMismatchException( LocalizedStrings.Functions_THE_ELEMENT_FUNCTION_CANNOT_BE_APPLIED_TO_AN_OBJECT_OF_TYPE_0 .toLocalizedString(clazz.getName())); // handle arrays if (arg instanceof Object[]) { Object[] a = (Object[]) arg; if (((DefaultQuery) context.getQuery()).isRemoteQuery() && context.isDistinct()) { for (int i = 0; i < a.length; i++) { if (a[i] instanceof PdxString) { a[i] = ((PdxString) a[i]).toString(); } } } checkSingleton(a.length); return a[0]; } if (arg instanceof int[]) { int[] a = (int[]) arg; checkSingleton(a.length); return Integer.valueOf(a[0]); } if (arg instanceof long[]) { long[] a = (long[]) arg; checkSingleton(a.length); return Long.valueOf(a[0]); } if (arg instanceof boolean[]) { boolean[] a = (boolean[]) arg; checkSingleton(a.length); return Boolean.valueOf(a[0]); } if (arg instanceof byte[]) { byte[] a = (byte[]) arg; checkSingleton(a.length); return Byte.valueOf(a[0]); } if (arg instanceof char[]) { char[] a = (char[]) arg; checkSingleton(a.length); return new Character(a[0]); } if (arg instanceof double[]) { double[] a = (double[]) arg; checkSingleton(a.length); return Double.valueOf(a[0]); } if (arg instanceof float[]) { float[] a = (float[]) arg; checkSingleton(a.length); return new Float(a[0]); } if (arg instanceof short[]) { short[] a = (short[]) arg; checkSingleton(a.length); return new Short(a[0]); } // did I miss something? throw new TypeMismatchException( LocalizedStrings.Functions_THE_ELEMENT_FUNCTION_CANNOT_BE_APPLIED_TO_AN_OBJECT_OF_TYPE_0 .toLocalizedString(clazz.getName())); }