All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.uiautomation.ios.utils.PlistManager Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012-2013 eBay Software Foundation and ios-driver committers
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.uiautomation.ios.utils;

import com.dd.plist.BinaryPropertyListParser;
import com.dd.plist.BinaryPropertyListWriter;
import com.dd.plist.NSObject;
import com.dd.plist.XMLPropertyListParser;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.openqa.selenium.WebDriverException;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

public class PlistManager {

  private static final Logger log = Logger.getLogger(PlistManager.class.getName());
  public static final String SET_CONNECTION_KEY = "webinspector/setConnectionKey.xml";
  public static final String CONNECT_TO_APP = "webinspector/connectToApp.xml";
  public static final String SET_SENDER_KEY = "webinspector/setSenderKey.xml";
  public static final String SEND_JSON_COMMAND = "webinspector/sendJSONCommand.xml";
  private static final String encoding = "UTF-8";

  private static String cacheTemplate = loadFromTemplate(SEND_JSON_COMMAND);

  public static String loadFromTemplate(String templatePath) {
    if (SEND_JSON_COMMAND.equals(templatePath) && cacheTemplate != null) {
      return cacheTemplate;
    }

    InputStream
        is =
        Thread.currentThread().getContextClassLoader().getResourceAsStream(templatePath);
    if (is == null) {
      throw new WebDriverException("cannot load : " + templatePath);
    }
    StringWriter writer = new StringWriter();
    try {
      IOUtils.copy(is, writer, "UTF-8");
    } catch (IOException e) {
      throw new WebDriverException("Cannot load template " + templatePath);
    }
    String content = writer.toString();
    return content;
  }


  public String JSONCommand(JSONObject command) {
    String json = command.toString();
    String s = null;
    try {
      s = Base64.encodeBase64String(json.getBytes(encoding));
    } catch (UnsupportedEncodingException e) {
      log.warning("encoding not supported :" + encoding);
    }
    String template = loadFromTemplate(SEND_JSON_COMMAND);
    String res = template.replace("$json_encoded", s);
    return res;
  }

  public byte[] plistXmlToBinary(String msg) {
    NSObject object = null;
    try {
      object = XMLPropertyListParser.parse(msg.getBytes());
      return BinaryPropertyListWriter.writeToArray(object);
    } catch (Exception e) {
      throw new WebDriverException(e);
    }


  }

  public String plistBinaryToXml(byte[] binary) throws Exception {
    try {
      NSObject object = BinaryPropertyListParser.parse(binary);
      return object.toXMLPropertyList();
    } catch (Exception e) {
      return null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy