Android Open Source - sony-camera-remote-java Device Description






From Project

Back to project page sony-camera-remote-java.

License

The source code is released under:

Apache License

If you think the Android project sony-camera-remote-java listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.praetoriandroid.cameraremote;
//  w w w  .  jav  a2s .  c  o  m
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class DeviceDescription {

    public static final String CAMERA = "camera";
    public static final String GUIDE = "guide";

    private Map<String, String> services;

    public static class Fetcher {

        private HttpClient httpClient = new HttpClient();

        public DeviceDescription fetch(String url) throws IOException, ParseException {
            InputStream dataStream = null;
            try {
                dataStream = httpClient.get(url);
                Map<String, String> services = parse(dataStream);
                dataStream.close();
                return new DeviceDescription(services);
            } finally {
                if (dataStream != null) {
                    dataStream.close();
                }
            }
        }

        public Fetcher setConnectionTimeout(int timeout) {
            httpClient.setConnectionTimeout(timeout);
            return this;
        }

        private Map<String, String> parse(InputStream dataStream) throws IOException, ParseException {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse(dataStream);

                Map<String, String> services = new HashMap<String, String>();

            /*
             * This is simplified device description parsing: we assume that the XML document contains the only
             * <device> element with the only <av:X_ScalarWebAPI_DeviceInfo> child element with the only
             * <av:X_ScalarWebAPI_ServiceList> element where all <av:X_ScalarWebAPI_Service> are located.
             */
                NodeList serviceList = document.getElementsByTagName("av:X_ScalarWebAPI_Service");
                for (int i = 0; i < serviceList.getLength(); i++) {
                    Element service = (Element) serviceList.item(i);
                    Element serviceTypeElement = getTheOnlyChild(service, "av:X_ScalarWebAPI_ServiceType");
                    String serviceType = getElementContent(serviceTypeElement);
                    Element actionListUrlElement = getTheOnlyChild(service, "av:X_ScalarWebAPI_ActionList_URL");
                    String actionListUrl = getElementContent(actionListUrlElement);
                    services.put(serviceType, actionListUrl);
                }
                return services;
            } catch (ParserConfigurationException e) {
                throw new ParseException(e);
            } catch (SAXException e) {
                throw new InvalidDataFormatException(e);
            }
        }

        private Element getTheOnlyChild(Element parent, String elementName) throws InvalidDataFormatException {
            NodeList children = parent.getElementsByTagName(elementName);
            if (children.getLength() != 1) {
                throw new InvalidDataFormatException("Element <" + parent.getTagName() + "> should contain the only child <"
                        + elementName + "> element");
            }
            return (Element) children.item(0);
        }

        private String getElementContent(Element element) throws InvalidDataFormatException {
            Node child = element.getFirstChild();
            if (child.getNodeType() != Node.TEXT_NODE) {
                throw new InvalidDataFormatException("Element <" + element.getTagName()
                        + "> should contain the only text child node");
            }
            return child.getNodeValue();
        }

//    private void dumpElement(Element element, int padding) {
//        System.out.print(getPadding(padding));
//        System.out.println('<' + element.getTagName() + '>');
//        NodeList children = element.getChildNodes();
//        for (int i = 0; i < children.getLength(); i++) {
//            Node child = children.item(i);
//            switch (child.getNodeType()) {
//                case Node.ELEMENT_NODE:
//                    dumpElement((Element) child, padding + 2);
//                    break;
//                case Node.TEXT_NODE:
//                    String text = child.getNodeValue();
//                    if (!text.trim().isEmpty()) {
//                        System.out.print(getPadding(padding + 2));
//                        System.out.println(child.getNodeValue());
//                    }
//                    break;
//            }
//        }
//        System.out.print(getPadding(padding));
//        System.out.println("</" + element.getTagName() + '>');
//    }
//
//    private String getPadding(int size) {
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < size; i++) {
//            sb.append(' ');
//        }
//        return sb.toString();
//    }

    }

    private DeviceDescription(Map<String, String> services) throws IOException {
        this.services = services;
    }

    public String getServiceUrl(String serviceType) throws ServiceNotSupportedException {
        String url = services.get(serviceType);
        if (url == null) {
            throw new ServiceNotSupportedException(serviceType);
        }
        return url + '/' + serviceType;
    }

    @Override
    public String toString() {
        return services.toString();
    }

}




Java Source Code List

com.praetoriandroid.cameraremote.BadHttpStatusException.java
com.praetoriandroid.cameraremote.DeviceDescription.java
com.praetoriandroid.cameraremote.HttpClient.java
com.praetoriandroid.cameraremote.InvalidDataFormatException.java
com.praetoriandroid.cameraremote.LiveViewDisconnectedException.java
com.praetoriandroid.cameraremote.LiveViewFetcher.java
com.praetoriandroid.cameraremote.Logger.java
com.praetoriandroid.cameraremote.ParseException.java
com.praetoriandroid.cameraremote.RpcClient.java
com.praetoriandroid.cameraremote.RpcException.java
com.praetoriandroid.cameraremote.ServiceNotSupportedException.java
com.praetoriandroid.cameraremote.SsdpClient.java
com.praetoriandroid.cameraremote.SsdpException.java
com.praetoriandroid.cameraremote.app.LiveView.java
com.praetoriandroid.cameraremote.app.MainActivity.java
com.praetoriandroid.cameraremote.app.Rpc.java
com.praetoriandroid.cameraremote.app.SelfTimerSelector.java
com.praetoriandroid.cameraremote.app.ThisApp.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureResponse.java
com.praetoriandroid.cameraremote.rpc.AwaitTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.BaseRequest.java
com.praetoriandroid.cameraremote.rpc.BaseResponse.java
com.praetoriandroid.cameraremote.rpc.CustomizableEntity.java
com.praetoriandroid.cameraremote.rpc.EventEntity.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoRequest.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetEventRequest.java
com.praetoriandroid.cameraremote.rpc.GetEventResponse.java
com.praetoriandroid.cameraremote.rpc.GetExposureModeRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesResponse.java
com.praetoriandroid.cameraremote.rpc.GetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetVersionsRequest.java
com.praetoriandroid.cameraremote.rpc.GetVersionsResponse.java
com.praetoriandroid.cameraremote.rpc.IllegalResultSizeException.java
com.praetoriandroid.cameraremote.rpc.MethodTypesEntity.java
com.praetoriandroid.cameraremote.rpc.RpcMethod.java
com.praetoriandroid.cameraremote.rpc.SetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.SimpleResponse.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewRequest.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewResponse.java
com.praetoriandroid.cameraremote.rpc.StartMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StartRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.StopLiveviewRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecResponse.java
com.praetoriandroid.cameraremote.rpc.StopRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.ValidationException.java
com.praetoriandroid.cameraremote.rpc._DummyResponse.java
com.praetoriandroid.cameraremote.tool.Cache.java
com.praetoriandroid.cameraremote.tool.RemoteControl.java
com.praetoriandroid.widget.ItemViewCreator.java
com.praetoriandroid.widget.RadialFreeSpace.java
com.praetoriandroid.widget.RadialSelector.java