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

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

Introduction

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

Prototype

String MODEL_DOM

To view the source code for org.apache.commons.jxpath.xml DocumentContainer MODEL_DOM.

Click Source Link

Document

DOM constant

Usage

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

private void parseSCPD() {
    try {/*from  ww  w.  ja v  a 2s  .  c om*/
        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  w  w  w . j a  v  a2s .  c  o  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);
}