/*
 * Copyright 2012 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.NSObject;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.BeanToJsonConverter;
import org.uiautomation.ios.server.application.LanguageDictionary;
import org.uiautomation.iosdriver.ApplicationInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PlistFileUtils {

  private final File source;
  private final Map<String, Object> plistContent;

  public PlistFileUtils(File source) {
    this.source = source;
    this.plistContent = read(source);
  }


  private Map<String, Object> read(File bplist) {
    NSObject object = null;
    try {
      object = BinaryPropertyListParser.parse(bplist);
    } catch (Exception e) {
      throw new WebDriverException("Cannot parse info.plist : " + e.getMessage(), e);
    }
    ApplicationInfo info = new ApplicationInfo(object);
    return info.getProperties();
  }

  public JSONObject toJSON() throws Exception {
    JSONObject res = new JSONObject(plistContent);
    return res;
  }
}

@@ -649,22 +649,21 @@
 d.plist.
-Binary
+ASCII
 Property
@@ -698,55 +698,62 @@
 ist.
-NSObject;%0A%0Aimport org.apache.commons.io.IOUtils
+BinaryPropertyListParser;%0Aimport com.dd.plist.NSObject
 ;%0Aim
@@ -840,331 +840,68 @@
 org.
-openqa.selenium.remote.BeanToJsonConverter;%0Aimport org.uiautomation.ios.server.application.LanguageDictionary;%0Aimport org.uiautomation.iosdriver.ApplicationInfo;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.StringWriter;%0Aimport java.text.Normalizer;%0Aimport java.util.ArrayList;%0Aimport java.util.List
+uiautomation.iosdriver.ApplicationInfo;%0A%0Aimport java.io.File
 ;%0Aim
@@ -1308,24 +1308,125 @@
 eption e) %7B%0A
+      try %7B%0A        object = ASCIIPropertyListParser.parse(bplist);%0A      %7D catch (Exception ex) %7B%0A  
       throw 
@@ -1480,16 +1480,17 @@
  : %22 + e
+x
 .getMess
@@ -1497,19 +1497,28 @@
 age(), e
+x
 );%0A
+      %7D%0A
     %7D%0A  

fix for https://github.com/ios-driver/ios-driver/issues/18