Example usage for org.apache.commons.jxpath Pointer getValue

List of usage examples for org.apache.commons.jxpath Pointer getValue

Introduction

In this page you can find the example usage for org.apache.commons.jxpath Pointer getValue.

Prototype

Object getValue();

Source Link

Document

Returns the value of the object, property or collection element this pointer represents.

Usage

From source file:org.onecmdb.core.utils.xpath.commands.UpdateCommand.java

protected Object getValues(String outputAttribute) {
    // Parse if not already done
    parseInputAttributes();/* ww w .j  av a 2  s .com*/

    // Fetch the values strings, un parsed if expression.
    String value = attributeMap.get(outputAttribute);
    if (value == null) {
        return (null);
    }

    Object parsedValue = null;

    // Check if it's an expression
    if (value.startsWith("[")) {
        if (!value.endsWith("]")) {
            throw new IllegalArgumentException("ERROR: Value expression must be ended with a ']' ");
        }
        String path = value.substring(1);
        path = path.substring(0, path.length() - 1);

        JXPathContext context = getXPathContext();
        Iterator<Pointer> pointers = (Iterator<Pointer>) context.iteratePointers(path);
        while (pointers.hasNext()) {
            Pointer pointer = pointers.next();
            parsedValue = pointer.getValue();
            // Migth warn here if there exists multiple.
        }
    } else {
        // No need to parse this, will be a String representaion of the value.
        parsedValue = value;
    }

    return (parsedValue);

}

From source file:org.onecmdb.core.utils.xpath.generator.CSVContentGenerator.java

public void transfer(OutputStream out) {
    log.debug("Debug Query path <" + cmd.getPath() + ">");
    PrintWriter text = new PrintWriter(new OutputStreamWriter(out), false);
    Iterator<Pointer> iter = cmd.getPathPointers();

    // Need to peek inside the iter.
    Pointer p = null;
    if (iter.hasNext()) {
        p = (Pointer) iter.next();//  w w  w  .j a v  a  2s.c  o m
    }

    boolean first = true;

    String[] outputAttributes = cmd.getOutputAttributeAsArray();

    if (outputAttributes.length == 1 && outputAttributes[0].equals("*")) {
        // Expand all.
        if (p != null) {
            Object v = p.getValue();
            if (v instanceof IDynamicHandler) {
                IDynamicHandler dynBean = (IDynamicHandler) v;
                outputAttributes = dynBean.getProperties();
            }
        }
    }

    if (outputAttributes.length == 0) {
        out(text, cmd.getPath());
    } else {
        for (int i = 0; i < outputAttributes.length; i++) {
            String outAttr = outputAttributes[i];
            text.print(cmd.getPath() + "/" + outAttr);
            if (i < (outputAttributes.length - 1)) {
                out(text, delimiter);
            }
        }
    }

    outln(text, "");
    if (p != null) {
        do {
            if (p == null) {
                p = iter.next();
            }
            if (outputAttributes.length == 0) {
                out(text, p.getValue().toString());
            } else {
                for (int i = 0; i < outputAttributes.length; i++) {
                    String outputAttribute = outputAttributes[i];
                    if (false) {
                        JXPathContext context = cmd.getRelativeContext(p);

                        Object o = context.getValue(outputAttribute);
                        if (o != null) {
                            if (o instanceof List) {
                                if (((List) o).size() == 1) {
                                    o = ((List) o).get(0);
                                }
                            }
                            out(text, o.toString());
                        }
                    }
                    if (true) {
                        Iterator<Pointer> outputAttrPointersIter = cmd.getRelativePointers(p, outputAttribute);
                        while (outputAttrPointersIter.hasNext()) {
                            Pointer outputPointer = outputAttrPointersIter.next();
                            String value = outputPointer.getValue().toString();
                            out(text, value);
                            if (outputAttrPointersIter.hasNext()) {
                                out(text, ",");
                            }
                        }
                    }
                    if (i < (outputAttributes.length - 1)) {
                        out(text, delimiter);
                    }

                }
            }
            outln(text, "");
            p = null;
        } while (iter.hasNext());
    }
    text.flush();
}

From source file:org.onecmdb.core.utils.xpath.generator.PropertyContentGenerator.java

public void transfer(OutputStream out) {
    log.debug("Debug Query path <" + cmd.getPath() + ">");
    PrintWriter text = new PrintWriter(new OutputStreamWriter(out), false);
    Iterator<Pointer> iter = cmd.getPathPointers();

    // Need to peek inside the iter.
    Pointer p = null;
    if (iter.hasNext()) {
        p = (Pointer) iter.next();//  w w w  . ja  v a 2s.  co  m
    }

    boolean first = true;

    String[] outputAttributes = cmd.getOutputAttributeAsArray();

    if (outputAttributes.length == 1 && outputAttributes[0].equals("*")) {
        // Expand all.
        if (p != null) {
            Object v = p.getValue();
            if (v instanceof IDynamicHandler) {
                IDynamicHandler dynBean = (IDynamicHandler) v;
                outputAttributes = dynBean.getProperties();
            }
        }
    }
    if (p != null) {
        do {
            if (p == null) {
                p = iter.next();
            }

            Object rootObject = p.getValue();

            if (!(rootObject instanceof InstanceContext || rootObject instanceof TemplateContext)) {
                throw new IllegalArgumentException(
                        "Illegal path '" + cmd.getPath() + "'. Must point to a ICi.");
            }

            if (outputAttributes.length == 0) {
                outln(text, getPropertyName(p, null) + "=" + p.getValue().toString());
            } else {
                for (int i = 0; i < outputAttributes.length; i++) {
                    String outputAttribute = outputAttributes[i];

                    JXPathContext context = cmd.getRelativeContext(p);
                    if (true) {
                        Iterator iterPointer = context.iteratePointers(outputAttribute);
                        int count = 0;
                        String uniqueStr = null;
                        while (iterPointer.hasNext()) {
                            Pointer valuePointer = (Pointer) iterPointer.next();
                            Object o = valuePointer.getValue();
                            if (iterPointer.hasNext() || uniqueStr != null) {
                                // More than one.
                                uniqueStr = "[" + count + "]";
                            }
                            count++;
                            String property = getPropertyName(rootObject, outputAttribute);
                            if (uniqueStr != null) {
                                property = property + uniqueStr;
                            }
                            if (o instanceof List) {
                                int index = 0;
                                for (Object v : (List) o) {
                                    outln(text, property + "[" + index + "]=" + v.toString());
                                    index++;
                                }
                            } else {
                                outln(text, property + "=" + (o == null ? "" : o.toString()));
                            }

                        }
                    }
                }
            }
            p = null;
        } while (iter.hasNext());
    }
    text.flush();
}

From source file:org.onecmdb.core.utils.xpath.generator.XMLContentGenerator.java

private void generateBeans() {
    Iterator<Pointer> iter = cmd.getPathPointers();

    boolean first = true;
    while (iter.hasNext()) {
        Pointer p = (Pointer) iter.next();

        Object value = p.getValue();
        if (value instanceof InstanceCollectionContext) {
            Iterator colIterator = cmd.getRelativePointers(p, "*");
            while (colIterator.hasNext()) {
                Pointer instancePointer = (Pointer) colIterator.next();
                Object instanceValue = instancePointer.getValue();

                if (instanceValue instanceof InstanceContext) {
                    generateInstanceXML(instancePointer, (InstanceContext) instanceValue);
                }//from  ww  w  .j  a  v a  2 s  .co  m
            }

        }

        if (value instanceof TemplateCollectionContext) {
            Iterator colIterator = cmd.getRelativePointers(p, "*");
            while (colIterator.hasNext()) {
                Pointer templatePointer = (Pointer) colIterator.next();
                Object templateValue = templatePointer.getValue();

                if (templateValue instanceof TemplateContext) {
                    generateTemplateXML(templatePointer, (TemplateContext) templateValue);
                }
            }
        }

        if (value instanceof TemplateContext) {
            generateTemplateXML(p, (TemplateContext) value);
        }

        if (value instanceof InstanceContext) {
            InstanceContext context = (InstanceContext) value;
            generateInstanceXML(p, context);
        }
    }
}

From source file:org.openl.rules.variation.JXPathVariation.java

@Override
public Object currentValue(Object[] originalArguments) {
    if (updatedArgumentIndex >= originalArguments.length) {
        throw new VariationRuntimeException(
                "Failed to apply variaion \"" + getVariationID() + "\". Number of argument to modify is ["
                        + updatedArgumentIndex + "] but arguments length is " + originalArguments.length);
    }//from w ww  .java 2s.c  om
    JXPathContext context = JXPathContext.newContext(originalArguments[updatedArgumentIndex]);
    Pointer pointer = compiledExpression.createPath(context);
    return pointer.getValue();
}

From source file:org.openvpms.archetype.function.list.ListFunctions.java

/**
 * Convenience function to sort and concatenate the object names of the specified node, separated by commas.
 *
 * @param context the expression context. Must refer to an {@link IMObject}.
 * @param node    the collection node name
 * @return the concatenated names, or {@code null} if the context doesn't refer to an {@link IMObject}
 *//*from   www  .ja va 2 s.c  o  m*/
public String sortNamesOf(ExpressionContext context, String node) {
    Pointer pointer = context.getContextNodePointer();
    Object value = pointer.getValue();
    if (value instanceof IMObject) {
        IMObjectBean bean = new IMObjectBean((IMObject) value, service);
        return sortNames(bean.getValues(node, IMObject.class));
    }
    return null;
}

From source file:org.openvpms.archetype.function.party.PartyFunctions.java

/**
 * Returns the full name for the passed party.
 *
 * @param context the expression context. Expected to reference a patient
 *                party or an act./*from   www  . j av a 2s .  co m*/
 * @return the parties full name.
 */
public String getPartyFullName(ExpressionContext context) {
    Pointer pointer = context.getContextNodePointer();
    Object value = pointer.getValue();
    if (value instanceof Party) {
        return getPartyFullName((Party) value);
    } else if (value instanceof Act) {
        return getPartyFullName((Act) value);
    }
    return null;
}

From source file:org.openvpms.archetype.function.party.PartyFunctions.java

/**
 * Returns the current owner party for the passed party.
 *
 * @param context the expression context. Expected to reference a patient party or act containing an
 *                <em>participation.patient</em>
 * @return the patients current owner Party. May be {@code null}
 *//*from  www . jav a  2 s.  c o  m*/
public Party getPatientOwner(ExpressionContext context) {
    Pointer pointer = context.getContextNodePointer();
    Object value = pointer.getValue();
    if (value instanceof Party) {
        return getPatientOwner((Party) value);
    } else if (value instanceof Act) {
        return getPatientOwner((Act) value);
    }
    return null;
}

From source file:org.openvpms.archetype.function.party.PartyFunctions.java

/**
 * Returns a formatted list of preferred contacts for a party.
 *
 * @param context the expression context. Expected to reference a party.
 * @return a formatted list of contacts. May be {@code null}
 *//*from ww  w . j  a  va 2  s .c om*/
public String getPreferredContacts(ExpressionContext context) {
    Pointer pointer = context.getContextNodePointer();
    if (pointer == null || !(pointer.getValue() instanceof Party)) {
        return null;
    }

    return getPreferredContacts((Party) pointer.getValue());
}

From source file:org.openvpms.archetype.function.party.PartyFunctions.java

/**
 * Returns a formatted billing address for a party.
 *
 * @param context the expression context. Expected to reference a party or
 *                act//ww  w .ja  v  a2  s.c o m
 * @return a formatted billing address, or {@code null}
 */

public String getBillingAddress(ExpressionContext context) {
    Pointer pointer = context.getContextNodePointer();
    Object value = pointer.getValue();
    if (value instanceof Party) {
        return getBillingAddress((Party) value);

    } else if (value instanceof Act) {
        return getBillingAddress((Act) value);
    }
    return null;
}