Example usage for org.apache.commons.jxpath.xml DocumentContainer DocumentContainer

List of usage examples for org.apache.commons.jxpath.xml DocumentContainer DocumentContainer

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.xml DocumentContainer DocumentContainer.

Prototype

public DocumentContainer(URL xmlURL, String model) 

Source Link

Document

Construct a new DocumentContainer.

Usage

From source file:net.sbbi.upnp.services.UPNPService.java

private void parseSCPD() {
    try {//  w w w  .  j  a  v a  2s . c  o  m
        DocumentContainer.registerXMLParser(DocumentContainer.MODEL_DOM, new JXPathParser());
        UPNPService = new DocumentContainer(SCPDURL, DocumentContainer.MODEL_DOM);
        JXPathContext context = JXPathContext.newContext(this);
        Pointer rootPtr = context.getPointer("UPNPService/scpd");
        JXPathContext rootCtx = context.getRelativeContext(rootPtr);

        specVersionMajor = Integer.parseInt((String) rootCtx.getValue("specVersion/major"));
        specVersionMinor = Integer.parseInt((String) rootCtx.getValue("specVersion/minor"));

        parseServiceStateVariables(rootCtx);

        Pointer actionsListPtr = rootCtx.getPointer("actionList");
        JXPathContext actionsListCtx = context.getRelativeContext(actionsListPtr);
        Double arraySize = (Double) actionsListCtx.getValue("count( action )");
        UPNPServiceActions = new HashMap();
        for (int i = 1; i <= arraySize.intValue(); i++) {
            ServiceAction action = new ServiceAction();
            action.name = (String) actionsListCtx.getValue("action[" + i + "]/name");
            action.parent = this;
            Pointer argumentListPtr = null;
            try {
                argumentListPtr = actionsListCtx.getPointer("action[" + i + "]/argumentList");
            } catch (JXPathException ex) {
                // there is no arguments list.
            }
            if (argumentListPtr != null) {
                JXPathContext argumentListCtx = actionsListCtx.getRelativeContext(argumentListPtr);
                Double arraySizeArgs = (Double) argumentListCtx.getValue("count( argument )");

                List orderedActionArguments = new ArrayList();
                for (int z = 1; z <= arraySizeArgs.intValue(); z++) {
                    ServiceActionArgument arg = new ServiceActionArgument();
                    arg.name = (String) argumentListCtx.getValue("argument[" + z + "]/name");
                    String direction = (String) argumentListCtx.getValue("argument[" + z + "]/direction");
                    arg.direction = direction.equals(ServiceActionArgument.DIRECTION_IN)
                            ? ServiceActionArgument.DIRECTION_IN
                            : ServiceActionArgument.DIRECTION_OUT;
                    String stateVarName = (String) argumentListCtx
                            .getValue("argument[" + z + "]/relatedStateVariable");
                    ServiceStateVariable stateVar = (ServiceStateVariable) UPNPServiceStateVariables
                            .get(stateVarName);
                    if (stateVar == null) {
                        throw new IllegalArgumentException(
                                "Unable to find any state variable named " + stateVarName + " for service "
                                        + getServiceId() + " action " + action.name + " argument " + arg.name);
                    }
                    arg.relatedStateVariable = stateVar;
                    orderedActionArguments.add(arg);
                }

                if (arraySizeArgs.intValue() > 0) {
                    action.setActionArguments(orderedActionArguments);
                }
            }

            UPNPServiceActions.put(action.getName(), action);
        }
        parsedSCPD = true;
    } catch (Throwable t) {
        throw new RuntimeException("Error during lazy SCDP file parsing at " + SCPDURL, t);
    }
}

From source file:net.sbbi.upnp.devices.UPNPRootDevice.java

/**
 * Constructor for the root device, constructs itself from
 * An xml device definition file provided by the UPNP device via http normally.
 * @param deviceDefLoc the location of the XML device definition file
 *                     using "the urn:schemas-upnp-org:device-1-0" namespace
 * @param maxAge the maximum age in secs of this UPNP device before considered to be outdated
 * @throws MalformedURLException if the location URL is invalid and cannot be used to populate this root object and its child devices
 *         IllegalStateException if the device has an unsupported version, currently only version 1.0 is supported
 *///from ww  w  .  j  ava 2s .  co m
public UPNPRootDevice(URL deviceDefLoc, String maxAge) throws MalformedURLException, IllegalStateException {
    this.deviceDefLoc = deviceDefLoc;
    DocumentContainer.registerXMLParser(DocumentContainer.MODEL_DOM, new JXPathParser());
    UPNPDevice = new DocumentContainer(deviceDefLoc, DocumentContainer.MODEL_DOM);
    validityTime = Integer.parseInt(maxAge) * 1000;
    creationTime = System.currentTimeMillis();

    JXPathContext context = JXPathContext.newContext(this);
    Pointer rootPtr = context.getPointer("UPNPDevice/root");
    JXPathContext rootCtx = context.getRelativeContext(rootPtr);

    specVersionMajor = Integer.parseInt((String) rootCtx.getValue("specVersion/major"));
    specVersionMinor = Integer.parseInt((String) rootCtx.getValue("specVersion/minor"));

    if (!(specVersionMajor == 1 && specVersionMinor == 0)) {
        throw new IllegalStateException(
                "Unsupported device version (" + specVersionMajor + "." + specVersionMinor + ")");
    }
    boolean buildURLBase = true;
    String base = null;
    try {
        base = (String) rootCtx.getValue("URLBase");
        if (base != null && base.trim().length() > 0) {
            URLBase = new URL(base);
            if (log.isDebugEnabled())
                log.debug("device URLBase " + URLBase);
            buildURLBase = false;
        }
    } catch (JXPathException ex) {
        // URLBase is not mandatory we assume we use the URL of the device
    } catch (MalformedURLException malformedEx) {
        // crappy urlbase provided
        log.warn("Error occured during device baseURL " + base
                + " parsing, building it from device default location", malformedEx);
    }
    if (buildURLBase) {
        String URL = deviceDefLoc.getProtocol() + "://" + deviceDefLoc.getHost() + ":" + deviceDefLoc.getPort();
        String path = deviceDefLoc.getPath();
        if (path != null) {
            int lastSlash = path.lastIndexOf('/');
            if (lastSlash != -1) {
                URL += path.substring(0, lastSlash);
            }
        }
        URLBase = new URL(URL);
    }
    Pointer devicePtr = rootCtx.getPointer("device");
    JXPathContext deviceCtx = rootCtx.getRelativeContext(devicePtr);

    fillUPNPDevice(this, null, deviceCtx, URLBase);
}

From source file:org.firesoa.common.jxpath.XMLModelTestCase.java

protected DocumentContainer createDocumentContainer() {
    return new DocumentContainer(JXPathTestCase.class.getResource("Vendor.xml"), getModel());
}