Example usage for org.apache.commons.jxpath JXPathContext newContext

List of usage examples for org.apache.commons.jxpath JXPathContext newContext

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext newContext.

Prototype

public static JXPathContext newContext(Object contextBean) 

Source Link

Document

Creates a new JXPathContext with the specified object as the root node.

Usage

From source file:org.openconcerto.xml.JXPathXPath.java

private JXPathContext getJXPath(final Object context) {
    final JXPathContext newContext = JXPathContext.newContext(context);
    for (final Namespace ns : this.namespaces)
        newContext.registerNamespace(ns.getPrefix(), ns.getURI());
    for (final Entry<String, Object> e : this.variables.entrySet())
        newContext.getVariables().declareVariable(e.getKey(), e.getValue());
    // otherwise /a/b on an empty document throws an exception
    newContext.setLenient(true);// ww w . jav  a 2 s  .c  o  m
    return newContext;
}

From source file:org.openinfinity.core.aspect.ArgumentBuilder.java

/**
 * Executes field callbacks on found and defined attribute names.
 * //  www .j  a  v  a2 s  .  co  m
 * @param argumentGatheringCallback Represents the actual callback method.
 * @param arguments Represents the arguments for method.
 * @param allowedFields Represents the allowed argument's field names.
 */
public void executeArgumentGatheringCallbackBasedOnDefinedFields(
        ArgumentGatheringFieldCallback<Field, Object> argumentGatheringCallback, Object[] arguments,
        String[] allowedFields) {
    for (String allowedField : allowedFields) {
        for (Object object : arguments) {
            try {
                JXPathContext context = JXPathContext.newContext(object);
                Object value = context.getValue(allowedField);
                String argument = object == null ? "null argument" : object.getClass().getName();
                generateKeyValueString(builder, object, value, argument);
                Field field = getField(allowedField, object, argument);
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                argumentGatheringCallback.onField(field, object);
            } catch (Throwable throwable) {
                LOGGER.warn(ExceptionUtil.getStackTraceString(throwable));
            }
        }
    }
}

From source file:org.openinfinity.core.aspect.ArgumentBuilder.java

/**
 * Returns builded argument information based on allowed field names presented by using XPATH.
 * /*from w  w  w  .  jav  a  2  s . c  om*/
 * @param name
 * @param argumentInfo
 * @return
 */
public void extractArgumentInfoByFilteringFields(JoinPoint joinPoint, String[] allowedFields) {
    Object[] objects = joinPoint.getArgs();
    if (objects.length > 0) {
        for (String allowedField : allowedFields) {
            for (Object object : objects) {
                try {
                    JXPathContext context = JXPathContext.newContext(object);
                    Object value = context.getValue(allowedField);
                    builder.append(
                            object == null ? "null argument" : object.getClass().getName() + "." + allowedField)
                            .append("=[").append((value == null ? "null value" : (value))).append("] ");
                } catch (Throwable throwable) {
                    LOGGER.warn(ExceptionUtil.getStackTraceString(throwable));
                }
            }
        }
    } else {
        builder.append("without any parameters");
    }
}

From source file:org.openinfinity.core.aspect.ArgumentGatheringJoinPointInterceptor.java

private void buildFieldBasedArgumentInformation(String[] allowedFields, StringBuilder builder,
        Object[] objects) {//from   ww  w  . j av a  2s .  c  om
    for (String allowedField : allowedFields) {
        for (Object object : objects) {
            try {
                JXPathContext context = JXPathContext.newContext(object);
                Object value = context.getValue(allowedField);
                builder.append(
                        object == null ? "null argument" : object.getClass().getName() + "." + allowedField)
                        .append("=[").append((value == null ? "null value" : (value))).append("] ");
            } catch (Throwable throwable) {
                LOGGER.warn(ExceptionUtil.getStackTraceString(throwable));
            }
        }
    }
}

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);
    }//w ww  .j  a v  a  2 s.c o m
    JXPathContext context = JXPathContext.newContext(originalArguments[updatedArgumentIndex]);
    Pointer pointer = compiledExpression.createPath(context);
    return pointer.getValue();
}

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

@Override
public Object[] applyModification(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);
    }// w ww.  j  ava  2s.com
    JXPathContext context = JXPathContext.newContext(originalArguments[updatedArgumentIndex]);
    Pointer pointer = compiledExpression.createPath(context);
    pointer.setValue(valueToSet);
    return originalArguments;
}

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

@Override
public void revertModifications(Object[] modifiedArguments, Object previousValue) {
    if (updatedArgumentIndex >= modifiedArguments.length) {
        throw new VariationRuntimeException(
                "Failed to apply variaion \"" + getVariationID() + "\". Number of argument to modify is ["
                        + updatedArgumentIndex + "] but arguments length is " + modifiedArguments.length);
    }/*from w  w  w  . j  a va2s.c  o m*/
    JXPathContext context = JXPathContext.newContext(modifiedArguments[updatedArgumentIndex]);
    compiledExpression.setValue(context, previousValue);
}

From source file:org.openlmis.fulfillment.service.OrderCsvHelper.java

private void writeCsvLineItem(Order order, OrderLineItem orderLineItem, List<OrderFileColumn> orderFileColumns,
        Writer writer, int counter) throws IOException {
    JXPathContext orderContext = JXPathContext.newContext(order);
    JXPathContext lineItemContext = JXPathContext.newContext(orderLineItem);
    for (OrderFileColumn orderFileColumn : orderFileColumns) {
        if (orderFileColumn.getNested() == null || orderFileColumn.getNested().isEmpty()) {
            if (orderFileColumns.indexOf(orderFileColumn) < orderFileColumns.size() - 1) {
                writer.write(",");
            }/*  w w w  .  ja  v  a2s.  c  om*/
            continue;
        }
        Object columnValue = getColumnValue(counter, orderContext, lineItemContext, orderFileColumn);

        if (columnValue instanceof ZonedDateTime) {
            columnValue = ((ZonedDateTime) columnValue).format(ofPattern(orderFileColumn.getFormat()));
        } else if (columnValue instanceof LocalDate) {
            columnValue = ((LocalDate) columnValue).format(ofPattern(orderFileColumn.getFormat()));
        }
        if (ENCLOSE_VALUES_WITH_QUOTES) {
            writer.write("\"" + (columnValue).toString() + "\"");
        } else {
            writer.write((columnValue).toString());
        }
        if (orderFileColumns.indexOf(orderFileColumn) < orderFileColumns.size() - 1) {
            writer.write(",");
        }
    }
}

From source file:org.openlmis.fulfillment.service.OrderCsvHelper.java

private Object getValue(Object object, String keyPath) {
    JXPathContext context = JXPathContext.newContext(object);

    return context.getValue(keyPath);
}

From source file:org.openlmis.order.helper.OrderCsvHelper.java

private void writeCsvLineItem(Order order, RnrLineItem rnrLineItem, List<OrderFileColumn> orderFileColumns,
        Writer writer, int counter) throws IOException {
    boolean encloseValuesWithQuotes = false;
    if (!configurationLoaded) {
        // allow the user to control what line separator to use from the administrative pages
        // this value was different between a windows and linux target systems.
        // this could have been written better.
        lineSeparator = StringEscapeUtils.unescapeJava(
                configSettingService.getConfigurationStringValue(ConfigurationSettingKey.CSV_LINE_SEPARATOR));
        // setting to enclose or not to enclose values in quotes.
        encloseValuesWithQuotes = configSettingService.getBoolValue(ConfigurationSettingKey.CSV_APPLY_QUOTES);
        configurationLoaded = true;//from w w w . java2s  .c o  m
    }
    JXPathContext orderContext = JXPathContext.newContext(order);
    JXPathContext lineItemContext = JXPathContext.newContext(rnrLineItem);
    for (OrderFileColumn orderFileColumn : orderFileColumns) {
        if (orderFileColumn.getNested() == null || orderFileColumn.getNested().isEmpty()) {
            if (orderFileColumns.indexOf(orderFileColumn) < orderFileColumns.size() - 1)
                writer.write(",");
            continue;
        }
        Object columnValue = getColumnValue(counter, orderContext, lineItemContext, orderFileColumn);

        if (columnValue instanceof Date) {
            columnValue = forPattern(orderFileColumn.getFormat()).print(((Date) columnValue).getTime());
        }
        if (encloseValuesWithQuotes) {
            writer.write("\"" + (columnValue).toString() + "\"");
        } else {
            writer.write((columnValue).toString());
        }
        if (orderFileColumns.indexOf(orderFileColumn) < orderFileColumns.size() - 1)
            writer.write(",");
    }
}