Example usage for org.apache.commons.jxpath.util TypeUtils convert

List of usage examples for org.apache.commons.jxpath.util TypeUtils convert

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.util TypeUtils convert.

Prototype

public static Object convert(Object object, Class toType) 

Source Link

Document

Converts the supplied object to the specified type.

Usage

From source file:org.eclipse.e4.emf.internal.xpath.helper.ValueUtils.java

private static Object convert(Object value, Class<?> type) {
    try {/*from   www.ja v a2 s.  c o  m*/
        return TypeUtils.convert(value, type);
    } catch (Exception ex) {
        throw new JXPathException("Cannot convert value of class "
                + (value == null ? "null" : value.getClass().getName()) + " to type " + type, ex);
    }
}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

@Override
public void setValue(Object value) {
    if (value == null)
        value = "";//null??
    if ((node instanceof org.dom4j.CharacterData || node instanceof Attribute || node instanceof DocumentType
            || node instanceof Entity || node instanceof ProcessingInstruction)) {
        String string = (String) TypeUtils.convert(value, String.class);
        if (string != null && !string.equals("")) {
            ((Node) node).setText(string);
        } else {/* ww  w .  j a v a  2s.  com*/
            ((Node) node).getParent().remove((Node) node);
        }
    } else if (node instanceof Document) {
        Document theOriginalDoc = (Document) node;
        Element theOrigialRoot = theOriginalDoc.getRootElement();

        if (value instanceof Document) {//?document

            Document valueDoc = (Document) value;
            Element valueRoot = valueDoc.getRootElement();

            if (theOrigialRoot == null || valueRoot == null
                    || theOrigialRoot.getQName().equals(valueRoot.getQName())) {

                theOriginalDoc.clearContent();

                List content = valueDoc.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        theOriginalDoc.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueRoot.getQName() + " to " + theOrigialRoot.getQName());

            }

        } else if (value instanceof Element) {
            Element valueElem = (Element) value;
            if (valueElem.getQName().equals(theOrigialRoot.getQName())) {
                theOriginalDoc.clearContent();
                Element newValueElem = (Element) valueElem.clone();
                theOriginalDoc.setRootElement(newValueElem);
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElem.getQName() + " to " + theOrigialRoot.getQName());
            }
        } else {
            throw new RuntimeException("Can NOT assign " + value + " to " + theOrigialRoot.getQName());

        }
        //         else if (value instanceof Comment){
        //            Comment cmmt = (Comment)((Comment)value).clone();
        //            theOriginalDoc.add(cmmt);
        //            
        //         }else if (value instanceof ProcessingInstruction){
        //            ProcessingInstruction instru = (ProcessingInstruction)((ProcessingInstruction)value).clone();
        //            theOriginalDoc.add(instru);
        //         }

    } else if (node instanceof Element) {
        Element originalElem = ((Element) node);

        if (value != null && value instanceof Element) {
            Element valueElm = (Element) value;
            if (originalElem.getQName().equals(valueElm.getQName())) {
                originalElem.clearContent();
                List content = valueElm.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        originalElem.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElm.getQName() + " to " + originalElem.getQName());
            }

        } else if (value != null && value instanceof Text) {
            originalElem.clearContent();
            Text txt = (Text) ((Text) value).clone();
            originalElem.add(txt);
        } else if (value != null && value instanceof CDATA) {
            originalElem.clearContent();
            CDATA cdata = (CDATA) ((CDATA) value).clone();
            originalElem.add(cdata);
        } else if (value != null && value instanceof java.util.Date) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format((java.util.Date) value);
            originalElem.clearContent();
            originalElem.addText(dateStr);
        } else {
            String string = (String) TypeUtils.convert(value, String.class);
            originalElem.clearContent();
            originalElem.addText(string);
        }

    }

}

From source file:org.xchain.framework.jxpath.GenericsWisePackageFunctions.java

public Function getFunction(String namespace, String name, Object[] parameters) {
    if ((namespace == null && this.namespace != null) //NOPMD
            || (namespace != null && !namespace.equals(this.namespace))) {
        return null;
    }/*  w  w w . j a  va 2 s .c o  m*/

    if (parameters == null) {
        parameters = EMPTY_ARRAY;
    }

    if (parameters.length >= 1) {
        Object target = TypeUtils.convert(parameters[0], Object.class);
        if (target != null) {
            Method method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }

            if (target instanceof NodeSet) {
                target = ((NodeSet) target).getPointers();
            }

            method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }

            if (target instanceof Collection) {
                Iterator iter = ((Collection) target).iterator();
                if (iter.hasNext()) {
                    target = iter.next();
                    if (target instanceof Pointer) {
                        target = ((Pointer) target).getValue();
                    }
                } else {
                    target = null;
                }
            }
        }
        if (target != null) {
            Method method = MethodLookupUtils.lookupMethod(target.getClass(), name, parameters);
            if (method != null) {
                return new MethodFunction(method);
            }
        }
    }

    String fullName = classPrefix + name;
    int inx = fullName.lastIndexOf('.');
    if (inx == -1) {
        return null;
    }

    String className = fullName.substring(0, inx);
    String methodName = fullName.substring(inx + 1);

    Class functionClass;
    try {
        functionClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
    } catch (ClassNotFoundException ex) {
        throw new JXPathException(
                "Cannot invoke extension function " + (namespace != null ? namespace + ":" + name : name), ex);
    }

    if (methodName.equals("new")) {
        Constructor constructor = MethodLookupUtils.lookupConstructor(functionClass, parameters);
        if (constructor != null) {
            return new ConstructorFunction(constructor);
        }
    } else {
        Method method = MethodLookupUtils.lookupStaticMethod(functionClass, methodName, parameters);
        if (method != null) {
            return new MethodFunction(method);
        }
    }
    return null;
}

From source file:org.xchain.framework.jxpath.MethodLookupUtils.java

/**
 * Look up a method.//  w w w . j  a  v  a2s . c  om
 * @param targetClass owning class
 * @param name method name
 * @param parameters method parameters
 * @return Method found if any
 */
public static Method lookupMethod(Class targetClass, String name, Object[] parameters) {
    if (parameters == null || parameters.length < 1 || parameters[0] == null) {
        return null;
    }

    if (matchType(targetClass, parameters[0]) == NO_MATCH) {
        return null;
    }

    targetClass = TypeUtils.convert(parameters[0], targetClass).getClass();

    boolean tryExact = true;
    int count = parameters.length - 1;
    Class[] types = new Class[count];
    Object[] arguments = new Object[count];
    for (int i = 0; i < count; i++) {
        Object param = parameters[i + 1];
        arguments[i] = param;
        if (param != null) {
            types[i] = param.getClass();
        } else {
            types[i] = null;
            tryExact = false;
        }
    }

    Method method = null;

    if (tryExact) {
        // First - without type conversion
        try {
            method = targetClass.getMethod(name, types);
            if (method != null && !Modifier.isStatic(method.getModifiers())) {
                return method;
            }
        } catch (NoSuchMethodException ex) { //NOPMD
            // Ignore
        }
    }

    int currentMatch = 0;
    boolean ambiguous = false;

    // Then - with type conversion
    Method[] methods = targetClass.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isBridge())
            // This check prevents inheritance issues in type hierarchies containing generics 
            // from disrupting method lookups.
            continue;
        if (!Modifier.isStatic(methods[i].getModifiers()) && methods[i].getName().equals(name)) {
            int match = matchParameterTypes(methods[i].getParameterTypes(), arguments);
            if (match != NO_MATCH) {
                if (match > currentMatch) {
                    method = methods[i];
                    currentMatch = match;
                    ambiguous = false;
                } else if (match == currentMatch) {
                    ambiguous = true;
                }
            }
        }
    }
    if (ambiguous) {
        throw new JXPathException("Ambigous method call: " + name);
    }
    return method;
}