context: /*==============================================================================
  Copyright (c) 2010-2013 QUALCOMM Austria Research Center GmbH.
  All Rights Reserved.
  Qualcomm Confidential and Proprietary
  ==============================================================================*/
  
  using System.Collections.Generic;
  using System.IO;
  using System.Xml;
  using UnityEngine;
  
  // This class is used to parse the config.xml file into a ConfigData file and
  // vice versa. The config.xml file is used to configure Trackables and
  // Virtual Buttons.
  // Implements a non-thread safe singleton pattern.
  public class ConfigParser
  {
      #region PROPERTIES
  
      // Returns the one and only ConfigParser instance.
      public static ConfigParser Instance
      {
          get
          {
              if (mInstance == null)
                  mInstance = new ConfigParser();
  
              return mInstance;
          }
      }
  
      #endregion // PROPERTIES
  
  
  
      #region PRIVATE_MEMBER_VARIABLES
  
      private static ConfigParser mInstance = null;
  
      #endregion // PRIVATE_MEMBER_VARIABLES
  
  
  
      #region PUBLIC_METHODS
      
      // This method reads a config.xml file at the given path and fills the
      // ConfigData object with the data.
      public bool fileToStruct(string configXMLPath, ConfigData configData)
      {
          if (!File.Exists(configXMLPath))
              return false;
  
          using (XmlTextReader configReader = new XmlTextReader(configXMLPath))
          {
              while (configReader.Read())
              {
                  if (configReader.NodeType == XmlNodeType.Element)
                  {
                      // "Global" Attributes
                      string itNameAttr = "";
  
                      switch (configReader.Name)
                      {
                          case "ImageTarget":
  
                              // Parse name from config file
                              itNameAttr = configReader.GetAttribute("name");
                              if (itNameAttr == null)
                              {
                                  Debug.LogWarning("Found ImageTarget without " +
                                                   "name attribute in " +
                                                   "config.xml. Image Target " +
                                                   "will be ignored.");
                                  continue;
                              }
  
                              // Parse itSize from config file
                              Vector2 itSize = Vector2.zero;
                              string[] itSizeAttr =
                                  configReader.GetAttribute("size").Split(' ');
                              if (itSizeAttr != null)
                              {
                                  if (!QCARUtilities.SizeFromStringArray(
                                      out itSize, itSizeAttr))
                                  {
                                      Debug.LogWarning("Found illegal itSize " +
                                                       "attribute for Image " +
                                                       "Target " + itNameAttr +
                                                       " in config.xml. " +
                                                       "Image Target will be " +
                                                       "ignored.");
                                      continue;
                                  }
                              }
                              else
                              {
                                  Debug.LogWarning("Image Target " + itNameAttr +
                                                   " is missing a itSize " +
                                                   "attribut in config.xml. " +
                                                   "Image Target will be " +
                                                   "ignored.");
                                  continue;
                              }
                              configReader.MoveToElement();
  
                              ConfigData.ImageTargetData imageTarget =
                                  new ConfigData.ImageTargetData();
  
                              imageTarget.size = itSize;
                              imageTarget.virtualButtons =
                                  new List<ConfigData.VirtualButtonData>();
  
                              configData.SetImageTarget(imageTarget, itNameAttr);
  
                              break;
  
  
                          case "VirtualButton":
  
                              // Parse name from config file
                              string vbNameAttr =
                                  configReader.GetAttribute("name");
                              if (vbNameAttr == null)
                              {
                                  Debug.LogWarning("Found VirtualButton " +
                                                   "without name attribute in " +
                                                   "config.xml. Virtual Button " +
                                                   "will be ignored.");
                                  continue;
                              }
  
                              // Parse rectangle from config file
                              Vector4 vbRectangle = Vector4.zero;
                              string[] vbRectangleAttr =
                                  configReader.GetAttribute("rectangle").Split(' ');
                              if (vbRectangleAttr != null)
                              {
                                  if (!QCARUtilities.RectangleFromStringArray(
                                      out vbRectangle, vbRectangleAttr))
                                  {
                                      Debug.LogWarning("Found invalid " +
                                                       "rectangle attribute " +
                                                       "for Virtual Button " +
                                                       vbNameAttr +
                                                       " in config.xml. " +
                                                       "Virtual Button will " +
                                                       "be ignored.");
                                      continue;
                                  }
                              }
                              else
                              {
                                  Debug.LogWarning("Virtual Button " +
                                                   vbNameAttr +
                                                   " has no rectangle " +
                                                   "attribute in config.xml. " +
                                                   "Virtual Button will be " +
                                                   "ignored.");
                                  continue;
                              }
  
                              // Parse enabled boolean from config file
                              bool vbEnabled = true;
                              string enabledAttr =
                                  configReader.GetAttribute("enabled");
                              if (enabledAttr != null)
                              {
                                  if (string.Compare(enabledAttr,
                                      "true", true) == 0)
                                  {
                                      vbEnabled = true;
                                  }
                                  else if (string.Compare(enabledAttr,
                                      "false", true) == 0)
                                  {
                                      vbEnabled = false;
                                  }
                                  else
                                  {
                                      Debug.LogWarning("Found invalid enabled " +
                                                       "attribute for Virtual " +
                                                       "Button " + vbNameAttr +
                                                       " in config.xml. " +
                                                       "Default setting will " +
                                                       "be used.");
                                  }
                              }
  
                              // Parse sensitivity from config file
                              VirtualButton.Sensitivity vbSensitivity =
                                  VirtualButton.DEFAULT_SENSITIVITY;
                              string vbSensitivityAttr =
                                  configReader.GetAttribute("sensitivity");
                              if (vbSensitivityAttr != null)
                              {
                                  if (string.Compare(vbSensitivityAttr,
                                      "low", true) == 0)
                                  {
                                      vbSensitivity =
                                      VirtualButton.Sensitivity.LOW;
                                  }
                                  else if (string.Compare(vbSensitivityAttr,
                                      "medium", true) == 0)
                                  {
                                      vbSensitivity =
                                      VirtualButton.Sensitivity.MEDIUM;
                                  }
                                  else if (string.Compare(vbSensitivityAttr,
                                      "high", true) == 0)
                                  {
                                      vbSensitivity =
                                      VirtualButton.Sensitivity.HIGH;
                                  }
                                  else
                                  {
                                      Debug.LogWarning("Found illegal " +
                                                       "sensitivity attribute " +
                                                       "for Virtual Button " +
                                                       vbNameAttr +
                                                       " in config.xml. " +
                                                       "Default setting will " +
                                                       "be used.");
                                  }
                              }
  
                              configReader.MoveToElement();
  
                              ConfigData.VirtualButtonData virtualButton =
                                  new ConfigData.VirtualButtonData();
  
                              string latestITName = GetLatestITName(configData);
  
                              virtualButton.name = vbNameAttr;
                              virtualButton.rectangle = vbRectangle;
                              virtualButton.enabled = vbEnabled;
                              virtualButton.sensitivity = vbSensitivity;
  
                              // Since the XML Reader runs top down we can assume
                              // that the Virtual Button that has been found is
                              // part of the latest Image Target.
                              if (configData.ImageTargetExists(latestITName))
                              {
                                  configData.AddVirtualButton(virtualButton,
                                                               latestITName);
                              }
                              else
                              {
                                  Debug.LogWarning("Image Target with name " +
                                                   latestITName +
                                                   " could not be found. " +
                                                   "Virtual Button " +
                                                   vbNameAttr +
                                                   "will not be added.");
                              }
                              break;
  
                          case "MultiTarget":
  
                              // Parse name from config file
                              string mtNameAttr =
                                  configReader.GetAttribute("name");
                              if (mtNameAttr == null)
                              {
                                  Debug.LogWarning("Found Multi Target without " +
                                                   "name attribute in " +
                                                   "config.xml. Multi Target " +
                                                   "will be ignored.");
                                  continue;
                              }
                              configReader.MoveToElement();
  
                              ConfigData.MultiTargetData multiTarget =
                                  new ConfigData.MultiTargetData();
  
                              multiTarget.parts =
                                  new List<ConfigData.MultiTargetPartData>();
  
                              configData.SetMultiTarget(multiTarget, mtNameAttr);
                              break;
  
  
                          case "Part":
  
                              // Parse name from config file
                              string prtNameAttr =
                                  configReader.GetAttribute("name");
  
gt:                             if (prtNameAttr == null)