<commit_before>/*
 * 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.server.application;

import static org.uiautomation.ios.IOSCapabilities.DEVICE_FAMILLY;
import static org.uiautomation.ios.IOSCapabilities.ICON;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.WebDriverException;
import org.uiautomation.ios.communication.IOSDevice;
import org.uiautomation.ios.exceptions.IOSAutomationException;
import org.uiautomation.ios.server.utils.PlistFileUtils;

import com.dd.plist.BinaryPropertyListParser;
import com.dd.plist.BinaryPropertyListWriter;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSNumber;

// TODO freynaud create IOSApp vs Running App that has locale + language
public class IOSApplication {

  private final JSONObject metadata;
  private final File app;
  private Localizable currentLanguage;
  private final List<LanguageDictionary> dictionaries = new ArrayList<LanguageDictionary>();

  /**
   * 
   * @param currentLanguage
   * @param pathToApp
   * @throws IOSAutomationException
   */
  public IOSApplication(String pathToApp) throws IOSAutomationException {
    this.app = new File(pathToApp);
    if (!app.exists()) {
      throw new IOSAutomationException(pathToApp + "isn't an IOS app.");
    }
    loadAllContent();
    try {
      metadata = getFullPlist();
    } catch (Exception e) {
      throw new IOSAutomationException("cannot load the metadata from the Info.plist file for " + pathToApp);
    }
  }

  /**
   * the content of the Info.plist for the app, as a json object.
   * 
   * @return
   */
  public JSONObject getMetadata() {
    return metadata;
  }

  /**
   * get the list of languages the application if localized to.
   * 
   * @return
   * @throws Exception
   */
  public List<Localizable> getSupportedLanguages() {
    Set<Localizable> res = new HashSet<Localizable>();
    List<File> l10ns = LanguageDictionary.getL10NFiles(app);
    for (File f : l10ns) {
      String name = LanguageDictionary.extractLanguageName(f);
      res.add(new LanguageDictionary(name).getLanguage());
    }
    return new ArrayList<Localizable>(res);
  }

  public LanguageDictionary getDictionary(Localizable language) throws IOSAutomationException {
    for (LanguageDictionary dict : dictionaries) {
      if (dict.getLanguage() == language) {
        return dict;
      }
    }
    throw new IOSAutomationException("Cannot find dictionary for " + language);
  }

  /**
   * Load all the dictionaries for the application.
   * 
   * @throws Exception
   */
  private void loadAllContent() throws IOSAutomationException {
    if (!dictionaries.isEmpty()) {
      throw new IOSAutomationException("Content already present.");
    }
    Map<String, LanguageDictionary> dicts = new HashMap<String, LanguageDictionary>();

    List<File> l10nFiles = LanguageDictionary.getL10NFiles(app);
    for (File f : l10nFiles) {
      String name = LanguageDictionary.extractLanguageName(f);
      LanguageDictionary res = dicts.get(name);
      if (res == null) {
        res = new LanguageDictionary(name);
        dicts.put(name, res);
      }
      try {
        // and load the content.
        JSONObject content = res.readContentFromBinaryFile(f);
        res.addJSONContent(content);
      } catch (Exception e) {
        throw new IOSAutomationException("error loading content for l10n", e);
      }

    }
    dictionaries.addAll(dicts.values());

  }

  public String translate(ContentResult res, Localizable language) throws IOSAutomationException {
    LanguageDictionary destinationLanguage = getDictionary(language);
    return destinationLanguage.translate(res);

  }

  public JSONObject getTranslations(String name) throws JSONException {

    JSONObject l10n = new JSONObject();
    l10n.put("matches", 0);
    if (name != null && !name.isEmpty() && !"null".equals(name)) {
      try {
        List<ContentResult> results = getPotentialMatches(name);

        int size = results.size();
        if (size != 0) {<commit_msg>more TODOs<commit_after>/*
 * 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.server.application;

import static org.uiautomation.ios.IOSCapabilities.DEVICE_FAMILLY;
import static org.uiautomation.ios.IOSCapabilities.ICON;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.WebDriverException;
import org.uiautomation.ios.communication.IOSDevice;
import org.uiautomation.ios.exceptions.IOSAutomationException;
import org.uiautomation.ios.server.utils.PlistFileUtils;

import com.dd.plist.BinaryPropertyListParser;
import com.dd.plist.BinaryPropertyListWriter;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSNumber;

// TODO freynaud create IOSApp vs Running App that has locale + language
public class IOSApplication {

  private final JSONObject metadata;
  private final File app;
  private Localizable currentLanguage;
  private final List<LanguageDictionary> dictionaries = new ArrayList<LanguageDictionary>();

  /**
   * 
   * @param currentLanguage
   * @param pathToApp
   * @throws IOSAutomationException
   */
  public IOSApplication(String pathToApp) throws IOSAutomationException {
    this.app = new File(pathToApp);
    if (!app.exists()) {
      throw new IOSAutomationException(pathToApp + "isn't an IOS app.");
    }
    loadAllContent();
    try {
      metadata = getFullPlist();
    } catch (Exception e) {
      throw new IOSAutomationException("cannot load the metadata from the Info.plist file for " + pathToApp);
    }
  }

  /**
   * the content of the Info.plist for the app, as a json object.
   * 
   * @return
   */
  public JSONObject getMetadata() {
    return metadata;
  }

  /**
   * get the list of languages the application if localized to.
   * 
   * @return
   * @throws Exception
   */
  public List<Localizable> getSupportedLanguages() {
    Set<Localizable> res = new HashSet<Localizable>();
    List<File> l10ns = LanguageDictionary.getL10NFiles(app);
    for (File f : l10ns) {
      String name = LanguageDictionary.extractLanguageName(f);
      res.add(new LanguageDictionary(name).getLanguage());
    }
    return new ArrayList<Localizable>(res);
  }

  public LanguageDictionary getDictionary(Localizable language) throws IOSAutomationException {
    for (LanguageDictionary dict : dictionaries) {
      if (dict.getLanguage() == language) {
        return dict;
      }
    }
    throw new IOSAutomationException("Cannot find dictionary for " + language);
  }

  /**
   * Load all the dictionaries for the application.
   * 
   * @throws Exception
   */
  private void loadAllContent() throws IOSAutomationException {
    if (!dictionaries.isEmpty()) {
      throw new IOSAutomationException("Content already present.");
    }
    Map<String, LanguageDictionary> dicts = new HashMap<String, LanguageDictionary>();

    List<File> l10nFiles = LanguageDictionary.getL10NFiles(app);
    for (File f : l10nFiles) {
      String name = LanguageDictionary.extractLanguageName(f);
      LanguageDictionary res = dicts.get(name);
      if (res == null) {
        res = new LanguageDictionary(name);
        dicts.put(name, res);
      }
      try {
        // and load the content.
        JSONObject content = res.readContentFromBinaryFile(f);
        res.addJSONContent(content);
      } catch (Exception e) {
        throw new IOSAutomationException("error loading content for l10n", e);
      }

    }
    dictionaries.addAll(dicts.values());

  }

  public String translate(ContentResult res, Localizable language) throws IOSAutomationException {
    LanguageDictionary destinationLanguage = getDictionary(language);
    return destinationLanguage.translate(res);

  }

  // TODO freynaud return a Map
  public JSONObject getTranslations(String name) throws JSONException {

    JSONObject l10n = new JSONObject();
    l10n.put("matches", 0);
    if (name != null && !name.isEmpty() && !"null".equals(name)) {
      try {
        List<ContentResult> results = getPotentialMatches(name);

        int size = results.size();
        if (size != 0) {
{'fraction_non_alphanumeric': 0.09743536924503038, 'fraction_numerical': 0.005767844268204758, 'mean_word_length': 3.650383141762452, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7395076', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2736, 'n_words': 782}