edu.xjtu.qxcamerabridge.DescriptorXMLParser.java Source code

Java tutorial

Introduction

Here is the source code for edu.xjtu.qxcamerabridge.DescriptorXMLParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.xjtu.qxcamerabridge;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * A singleton helper class which read/parser the descriptor, and extract the
 * key links contained in the XML.
 *
 * @author ZhipingJiang
 */
public class DescriptorXMLParser {

    private static final DescriptorXMLParser descriptXMLParser = new DescriptorXMLParser();

    /**
     * The singleton entrance.
     *
     * @return
     */
    public static DescriptorXMLParser getInstance() {
        return descriptXMLParser;
    }

    private DescriptorXMLParser() {
    }

    private String pageContent;

    /**
     * Read the http cotent detected by the URL.
     *
     * @param deviceURL suppose to be the URL of device descriptor XML.
     * @return  the working instance for chained code style.
     * @throws IOException
     */
    public DescriptorXMLParser getPageContent(String deviceURL) throws IOException {

        Response reply = Request.Get(deviceURL).execute();
        pageContent = reply.returnContent().asString();
        //System.out.println(pageContent);
        return this;
    }

    /**
     * parse the xml device descriptor. extract the API service URL
     * @return the extracted URLs are stored in the map. The key is the api service name and the value is the dedicated url.
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException 
     */
    public Map<String, String> parseXML() throws ParserConfigurationException, SAXException, IOException {
        Map<String, String> serviceMap = new HashMap();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(IOUtils.toInputStream(pageContent));
        Element root = document.getDocumentElement();
        Element device = (Element) root.getElementsByTagName("device").item(0);
        Element deviceName = (Element) device.getElementsByTagName("friendlyName").item(0);
        serviceMap.put("deviceName", deviceName.getTextContent());
        Element device_info = (Element) device.getElementsByTagName("av:X_ScalarWebAPI_DeviceInfo").item(0);
        Element serviceList = (Element) device_info.getElementsByTagName("av:X_ScalarWebAPI_ServiceList").item(0);
        // extract the servicelist

        for (int i = 0; i < serviceList.getChildNodes().getLength(); i++) {
            Element eachservice = (Element) serviceList.getElementsByTagName("av:X_ScalarWebAPI_Service").item(i);
            Element servicetype = (Element) eachservice.getElementsByTagName("av:X_ScalarWebAPI_ServiceType")
                    .item(0);
            Element actionURL = (Element) eachservice.getElementsByTagName("av:X_ScalarWebAPI_ActionList_URL")
                    .item(0);
            serviceMap.put(servicetype.getTextContent(), actionURL.getTextContent());

        }
        Element imagingDevice = (Element) device_info.getElementsByTagName("av:X_ScalarWebAPI_ImagingDevice")
                .item(0);
        Element liveviewURL = (Element) imagingDevice.getElementsByTagName("av:X_ScalarWebAPI_LiveView_URL")
                .item(0);
        serviceMap.put("liveViewURL", liveviewURL.getTextContent());

        return serviceMap;
    }
}