Java Code Examples for org.apache.commons.jxpath.JXPathContext#setLenient()

The following examples show how to use org.apache.commons.jxpath.JXPathContext#setLenient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EPLUtilities.java    From jetstream-esper with GNU General Public License v2.0 6 votes vote down vote up
public static Object getAttribute(Object object, String key) {
  try {
    Object event = object;
    if (object instanceof String) {
      ObjectMapper mapper = new ObjectMapper();
      event = mapper.readValue(object.toString(), HashMap.class);
    }
    if (event != null) {
      JXPathContext context = JXPathContext.newContext(event);
      context.setLenient(true);
      return context.getValue(key);
    }
  }
  catch (Exception e) { // NOPMD
    return null;
  }
  return null;
}
 
Example 2
Source File: ObjectGraphNavigatorJXPathImpl.java    From oval with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ObjectGraphNavigationResult navigateTo(final Object root, final String xpath) throws InvalidConfigurationException {
   Assert.argumentNotNull("root", root);
   Assert.argumentNotNull("xpath", xpath);

   try {
      final JXPathContext ctx = JXPathContext.newContext(root);
      ctx.setLenient(true); // do not throw an exception if object graph is incomplete, e.g. contains null-values

      Pointer pointer = ctx.getPointer(xpath);

      // no match found or invalid xpath
      if (pointer instanceof NullPropertyPointer || pointer instanceof NullPointer)
         return null;

      if (pointer instanceof BeanPointer) {
         final Pointer parent = ((BeanPointer) pointer).getImmediateParentPointer();
         if (parent instanceof PropertyPointer) {
            pointer = parent;
         }
      }

      if (pointer instanceof PropertyPointer) {
         final PropertyPointer pp = (PropertyPointer) pointer;
         final Class<?> beanClass = pp.getBean().getClass();
         AccessibleObject accessor = ReflectionUtils.getField(beanClass, pp.getPropertyName());
         if (accessor == null) {
            accessor = ReflectionUtils.getGetter(beanClass, pp.getPropertyName());
         }
         return new ObjectGraphNavigationResult(root, xpath, pp.getBean(), accessor, pointer.getValue());
      }

      throw new InvalidConfigurationException("Don't know how to handle pointer [" + pointer + "] of type [" + pointer.getClass().getName() + "] for xpath ["
         + xpath + "]");
   } catch (final JXPathNotFoundException ex) {
      // thrown if the xpath is invalid
      throw new InvalidConfigurationException(ex);
   }
}
 
Example 3
Source File: EPLUtilities.java    From jetstream-esper with GNU General Public License v2.0 5 votes vote down vote up
/**
 * transformToObject - This method converts incoming JsonString to HashMap. Need/use of this method is, in EPL we need
 * to use pass Objects through window. But EPL allows primitives type alone. For that we 'll convert Obejct to
 * JsonString and pass it to the stream. After that we need to convert back the Json String to original Object type.
 * 
 */
@SuppressWarnings("unchecked")
public static HashMap<String, Object> transformToObjectAndRemoveKey(String jsonStr, String key) throws Exception {
  if (jsonStr != null) {
    ObjectMapper mapper = new ObjectMapper();
    HashMap<String, Object> event = mapper.readValue(jsonStr, HashMap.class);
    if (key != null && !key.equals("")) {
      JXPathContext context = JXPathContext.newContext(event);
      context.setLenient(true);
      context.removePath(key);
    }
    return event;
  }
  return null;
}
 
Example 4
Source File: PathValueMessageFilter.java    From suro with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean apply(Object input) {
    JXPathContext jxpath = JXPathContext.newContext(input);
    // We should allow non-existing path, and let predicate handle it. 
    jxpath.setLenient(true);
    
    Object value = jxpath.getValue(xpath);
   
    return predicate.apply(value);
}
 
Example 5
Source File: PathExistsMessageFilter.java    From suro with Apache License 2.0 5 votes vote down vote up
@Override
public boolean apply(Object input) {
    JXPathContext jxpath = JXPathContext.newContext(input);
    // We should allow non-existing path, and let predicate handle it.
    jxpath.setLenient(true);

    Pointer pointer = jxpath.getPointer(xpath);
   
    return pointer != null && !(pointer instanceof NullPointer) && pointer.getValue() != null;
}
 
Example 6
Source File: XPathContextFactory.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@code JXPathContext} based on the passed in arguments.
 *
 * @param root the root node
 * @param handler the node handler
 * @param <T> the type of the nodes to be handled
 * @return the newly created context
 */
public <T> JXPathContext createContext(final T root, final NodeHandler<T> handler)
{
    final JXPathContext context =
            JXPathContext.newContext(ConfigurationNodePointerFactory
                    .wrapNode(root, handler));
    context.setLenient(true);
    return context;
}
 
Example 7
Source File: JXPath172DynamicTest.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Helper, returns a {@link JXPathContext} filled with a Map whose "value"
 * key is associated to the passed <code>val</code> value.
 * 
 * @param val
 * @return A {@link JXPathContext}, never <code>null</code>.
 */
private JXPathContext getContext(final String val, boolean lenient)
{
    final HashMap map = new HashMap();
    // if (val!=null) // no diffs
    map.put("value", val);
    final Object target = map;
    final JXPathContext context = JXPathContext.newContext(null, target);
    context.setLenient(lenient);
    return context;
}
 
Example 8
Source File: JXPath172Test.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Helper, returns a {@link JXPathContext} filled with {@link TestBean172}
 * whose {@link TestBean172#getValue()} method returns the passed
 * <code>val</code> value.
 * 
 * @param val
 * @return A {@link JXPathContext}, never <code>null</code>.
 */
private JXPathContext getContext(final String val, boolean lenient)
{
    final TestBean172 b = new TestBean172();
    b.setValue(val);
    final Object target = b;
    final JXPathContext context = JXPathContext.newContext(null, target);
    context.setLenient(lenient);
    return context;
}
 
Example 9
Source File: PathValuePredicate.java    From suro with Apache License 2.0 4 votes vote down vote up
@Override
  public boolean apply(@Nullable String input) {
JXPathContext context = JXPathContext.newContext(input);
context.setLenient(true);
   return Objects.equal(context.getValue(valueXpath), context.getValue(inputXpath));
  }
 
Example 10
Source File: JXPathServletContextTest.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
public void testPageContext() {
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("app", "app");

    MockServletConfig servletConfig = new MockServletConfig();
    servletConfig.setServletContext(servletContext);

    MockHttpSession session = new MockHttpSession();
    session.setupServletContext(servletContext);
    session.setAttribute("session", "session");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute("request", "request");
    request.setSession(session);

    MockPageContext pageContext = new MockPageContext();
    pageContext.setServletConfig(servletConfig);
    pageContext.setServletRequest(request);
    pageContext.setAttribute("page", "page");

    assertSame("Request session", session, request.getSession());


    JXPathContext context = JXPathServletContexts.getPageContext(pageContext);
    context.setLenient(true);
    
    checkPointerIterator(context);

    assertEquals("Page Scope", "page", context.getValue("page"));
    assertEquals("Request Scope", "request", context.getValue("request"));
    assertEquals("Session Scope", "session", context.getValue("session"));
    assertEquals("Application Scope", "app", context.getValue("app"));

    assertEquals("Explicit Page Scope", "page", context.getValue("$page/page"));
    assertEquals("Explicit Request Scope", "request", context.getValue("$request/request"));
    assertEquals("Explicit Session Scope", "session", context.getValue("$session/session"));
    assertEquals("Explicit Application Scope", "app", context.getValue("$application/app"));

    // iterate through the elements of page context only (two elements expected, 'page' and the context)
    Iterator it = context.iteratePointers("$page/*");
    assertTrue("element not found", it.hasNext());
    it.next();
    it.next();
    assertFalse("too many elements", it.hasNext());

    // test setting a value in the context
    context.setValue("/foo1", "bar1");
    assertEquals("Context property", "bar1", context.getValue("/foo1"));

    context.setValue("$page/foo2", "bar2");
    assertEquals("Context property", "bar2", context.getValue("$page/foo2"));
}