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

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

Introduction

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

Prototype

public abstract Object getValue(String xpath);

Source Link

Document

Evaluates the xpath and returns the resulting object.

Usage

From source file:jp.go.nict.langrid.commons.jxpath.WSDLUtil.java

/**
 * //from   w w  w .  j  av  a  2 s . com
 * 
 */
public static URL getServiceAddress(InputStream wsdl) throws IOException, MalformedURLException, SAXException {
    JXPathContext context = newWSDLContext(wsdl, "_");
    context.registerNamespace("wsdlsoap", "http://schemas.xmlsoap.org/wsdl/soap/");
    String url = (String) context.getValue("_:definitions/_:service/_:port/wsdlsoap:address/@location");
    if (url == null)
        return null;
    else
        return new URL(url);
}

From source file:blueprint.sdk.util.JXPathHelper.java

/**
 * @param xpath XPath to evaluate//  www .j av  a  2 s  . c o  m
 * @param context target context, JXPathContext.newContext(Node)
 * @return String or null(not found)
 */
public static String evaluate(String xpath, JXPathContext context) {
    String result = null;

    try {
        result = (String) context.getValue(xpath);
    } catch (JXPathNotFoundException ignored) {
    }

    return result;
}

From source file:jp.go.nict.langrid.bpel.deploy.BPRDeployer.java

/**
 * /*from   w  ww. j  a v a  2  s . com*/
 * 
 */
static DeploymentResult parseResult(String aResult) throws SAXException, IOException {
    DeploymentResult result = new DeploymentResult();
    result.setSourceString(aResult);

    JXPathContext context = JXPathUtil.newXMLContext(aResult);
    context.setLenient(true);

    JXPathContext summary = JXPathContext.newContext(context,
            context.getPointer("/deploymentSummary").getNode());
    result.setSummaryErrorCount(Integer.parseInt(summary.getValue("/@numErrors").toString()));
    result.setSummaryWarningCount(Integer.parseInt(summary.getValue("/@numWarnings").toString()));
    result.setSummaryMessages(summary.getValue("/globalMessages").toString());

    summary.setLenient(true);
    Node deploymentInfoNode = (Node) summary.getPointer("/deploymentInfo").getNode();
    if (deploymentInfoNode != null) {
        JXPathContext info = JXPathContext.newContext(summary, deploymentInfoNode);
        result.setDeploymentErrorCount(Integer.parseInt(info.getValue("/@numErrors").toString()));
        result.setDeploymentWarningCount(Integer.parseInt(info.getValue("/@numWarnings").toString()));
        result.setDeployed(Boolean.parseBoolean(info.getValue("/@deployed").toString()));
        result.setPddFilename(info.getValue("/@pddName").toString());
        result.setDeploymentLog(info.getValue("/log").toString());
    }
    return result;
}

From source file:jp.go.nict.langrid.bpel.ProcessAnalyzer.java

/**
 * /* w  w w .  jav  a 2 s  .c om*/
 * 
 */
public static WSDL analyzeWsdl(byte[] body) throws MalformedURLException, SAXException, URISyntaxException {
    JXPathContext context = JXPathContext.newContext(new DOMParser().parseXML(new ByteArrayInputStream(body)));
    context.registerNamespace("_", Constants.WSDL_URI);
    context.registerNamespace("wsdlsoap", Constants.WSDLSOAP_URI);
    context.registerNamespace("plnk1", Constants.PLNK_URI);
    context.registerNamespace("plnk2", Constants.PLNK_URI_2);

    WSDL wsdl = new WSDL();
    wsdl.setBody(body);
    wsdl.setTargetNamespace(new URI(context.getValue("/_:definitions/@targetNamespace").toString()));

    // 
    // 
    HashMap<String, PartnerLinkType> links = new HashMap<String, PartnerLinkType>();
    {
        Iterator<?> pli = context.iteratePointers("/_:definitions/plnk1:partnerLinkType");
        while (pli.hasNext()) {
            JXPathContext pltc = JXPathContext.newContext(context, ((Pointer) pli.next()).getNode());
            pltc.registerNamespace("plnk1", Constants.PLNK_URI);
            PartnerLinkType plt = new PartnerLinkType();
            plt.setName(pltc.getValue("/@name").toString());

            HashMap<String, Role> roles = new HashMap<String, Role>();
            Iterator<?> ri = pltc.iteratePointers("/plnk1:role");
            while (ri.hasNext()) {
                JXPathContext rc = JXPathContext.newContext(pltc, ((Pointer) ri.next()).getNode());
                rc.registerNamespace("plnk1", Constants.PLNK_URI);
                Role role = new Role();
                role.setName(rc.getValue("/@name").toString());
                role.setPortType(toQName(rc, rc.getValue("/plnk1:portType/@name").toString()));
                roles.put(role.getName(), role);
            }
            plt.setRoles(roles);
            links.put(plt.getName(), plt);
        }
    }
    {
        Iterator<?> pli = context.iteratePointers("/_:definitions/plnk2:partnerLinkType");
        while (pli.hasNext()) {
            JXPathContext pltc = JXPathContext.newContext(context, ((Pointer) pli.next()).getNode());
            pltc.registerNamespace("plnk2", Constants.PLNK_URI_2);
            PartnerLinkType plt = new PartnerLinkType();
            plt.setName(pltc.getValue("/@name").toString());

            HashMap<String, Role> roles = new HashMap<String, Role>();
            Iterator<?> ri = pltc.iteratePointers("/plnk2:role");
            while (ri.hasNext()) {
                JXPathContext rc = JXPathContext.newContext(pltc, ((Pointer) ri.next()).getNode());
                Role role = new Role();
                role.setName(rc.getValue("/@name").toString());
                role.setPortType(toQName(rc, rc.getValue("/@portType").toString()));
                roles.put(role.getName(), role);
            }
            plt.setRoles(roles);
            links.put(plt.getName(), plt);
        }
    }
    wsdl.setPlinks(links);

    { // definitions/portType??
        HashMap<String, EndpointReference> portTypeToEndpointReference = new HashMap<String, EndpointReference>();
        Iterator<?> pti = context.iteratePointers("/_:definitions/_:portType");
        while (pti.hasNext()) {
            JXPathContext ptc = JXPathContext.newContext(context, ((Pointer) pti.next()).getNode());
            portTypeToEndpointReference.put(ptc.getValue("/@name").toString(), null);
        }
        wsdl.setPortTypeToEndpointReference(portTypeToEndpointReference);
    }

    { // definitions/binding??
        HashMap<String, QName> bindingTypes = new HashMap<String, QName>();
        Iterator<?> bi = context.iteratePointers("/_:definitions/_:binding");
        while (bi.hasNext()) {
            JXPathContext bc = JXPathContext.newContext(context, ((Pointer) bi.next()).getNode());
            bindingTypes.put(bc.getValue("/@name").toString(), toQName(bc, bc.getValue("/@type").toString()));
        }
        wsdl.setBindingTypes(bindingTypes);
    }

    String firstServiceName = null;
    { // definitions/service??
        HashMap<String, Service> services = new HashMap<String, Service>();
        Iterator<?> si = context.iteratePointers("/_:definitions/_:service");
        while (si.hasNext()) {
            JXPathContext sc = JXPathContext.newContext(context, ((Pointer) si.next()).getNode());
            sc.registerNamespace("_", Constants.WSDL_URI);
            String serviceName = sc.getValue("/@name").toString();

            Service s = new Service();
            s.setName(serviceName);
            if (firstServiceName == null) {
                firstServiceName = serviceName;
            }

            // port??
            HashMap<String, Port> ports = new HashMap<String, Port>();
            Iterator<?> pi = sc.iteratePointers("/_:port");
            while (pi.hasNext()) {
                JXPathContext pc = JXPathContext.newContext(sc, ((Pointer) pi.next()).getNode());
                pc.registerNamespace("wsdlsoap", Constants.WSDLSOAP_URI);
                Port p = new Port();
                p.setName(pc.getValue("/@name").toString());
                p.setBinding(toQName(pc, pc.getValue("/@binding").toString()));
                p.setAddress(new URL(pc.getValue("/wsdlsoap:address/@location").toString()));
                ports.put(p.getName(), p);
            }
            s.setPorts(ports);

            services.put(s.getName(), s);
        }
        wsdl.setServices(services);
    }

    if (firstServiceName == null) {
        wsdl.setFilename("unknown" + WSDL_EXTENSION);
    } else {
        wsdl.setFilename(firstServiceName + WSDL_EXTENSION);
    }
    return wsdl;
}

From source file:com.ebay.jetstream.epl.EPLUtilities.java

public static Object getAttribute(Object object, String key) {
    try {//from w  ww.j ava 2s . c om
        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;
}

From source file:com.yahoo.xpathproto.handler.RfcTimestampHandler.java

@Override
public Object getProtoValue(final JXPathContext context, final Context vars, final Config.Entry entry) {
    final Object object = context.getValue(entry.getPath());
    if (null == object) {
        return null;
    }/*from  w w w.  j a va2 s  .co  m*/

    return parseDate(object.toString());
}

From source file:com.ixcode.framework.model.binding.ModelXPathResolver.java

/**
 * @todo maybe there is already a way to deal witht he root domain being a collection ?
 * @todo this is a nasteee way of doing it - we need to work out how to sort it...
 * /[10]/someCollection[1]/*www  .ja v  a  2 s  .  co m*/
 * @param xpath
 * @return
 */
public Object getModel(String xpath) {
    Object model;
    if (xpath.length() == 0) {
        model = _pathContext.getValue("/");
    } else if (xpath.startsWith("/[")) { // must be a collection in the root ...
        int index = Integer.parseInt(xpath.substring(2, xpath.indexOf(']')));
        Collection rootCollection = (Collection) _pathContext.getValue("/");
        model = findModel(rootCollection, index);
        String childPath = xpath.substring(xpath.indexOf(']') + 1);
        if (childPath.startsWith("/")) {
            JXPathContext childContext = JXPathContext.newContext(model);
            model = childContext.getValue(childPath);
        }
    } else {
        model = _pathContext.getValue(xpath);
    }
    return model;
}

From source file:com.discursive.jccook.xml.jxpath.TeamExample.java

public void start() throws Exception {
    League league = new League();

    Team team = new Team();
    league.getTeams().add(team);/*from   ww w  . j a v  a 2  s .  co  m*/

    Person coach = new Person();
    coach.setFirstName("Coach Bob");
    team.setCoach(coach);

    Person player1 = new Person();
    player1.setFirstName("Player Charlie");
    team.getPlayers().add(player1);

    Person player2 = new Person();
    player2.setFirstName("Player Ted");
    team.getPlayers().add(player2);

    Person player3 = new Person();
    player3.setFirstName("Player Bart");
    team.getPlayers().add(player3);

    Team team2 = new Team();
    league.getTeams().add(team2);

    Person coach2 = new Person();
    coach2.setFirstName("Coach Susan");
    team2.setCoach(coach2);

    Person player4 = new Person();
    player4.setFirstName("Player Jim");
    team2.getPlayers().add(player4);

    JXPathContext context = JXPathContext.newContext(league);
    System.out.println("** Retrieve the first name of Ted's coach");
    Object value = context.getValue("teams/players[firstName = 'Player Ted']/../coach/firstName");
    System.out.println(value);

    context = JXPathContext.newContext(league);
    System.out.println("** Retrieve the players on Coach Susan's team");
    value = context.getValue("teams/coach[firstName = 'Coach Susan']/../players");
    System.out.println(value);
}

From source file:eu.tripledframework.eventstore.infrastructure.ReflectionObjectConstructor.java

private Object getValue(String expr, DomainEvent event) {
    JXPathContext context = JXPathContext.newContext(event);
    return context.getValue(expr);
}

From source file:com.ignorelist.kassandra.steam.scraper.SharedConfig.java

public synchronized VdfNode getAppsNode() {
    if (null != appsNode) {
        return appsNode;
    }//  w ww. j  a  v  a 2s .  c  o m
    try {
        JXPathContext pathContext = JXPathContext.newContext(getRootNode());
        appsNode = (VdfNode) pathContext.getValue("//children[name='apps']");
        return appsNode;
    } catch (IOException | RecognitionException ex) {
        System.err.println(ex);
    }
    throw new IllegalStateException();
}