Exemplo n.º 1
0
 internal static void ParseWurflFiles(Provider devices)
 {
     // Parse the wurfl files.
     ParseWurflFiles(
         devices,
         Manager.WurflFilePath,
         Manager.CapabilitiesWhiteList,
         Manager.WurflPatchFiles);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Creates an instance of DeviceInfo.
 /// </summary>
 /// <param name="userAgent">User agent string used to identify this device.</param>
 /// <param name="deviceId">A unique Identifier of the device.</param>
 /// <param name="devices">A reference to the complete index of devices.</param>
 internal DeviceInfo(
     Provider devices,
     string deviceId,
     string userAgent)
 {
     Init(devices, deviceId, userAgent);
 }
Exemplo n.º 3
0
        private void Init(
            Provider devices,
            string deviceId,
            string userAgent)
        {
            if (userAgent == null)
                throw new ArgumentNullException("userAgent");

            _userAgent = UserAgentParser.Parse(userAgent);

            Init(devices, deviceId);
        }
Exemplo n.º 4
0
        private void Init(
            Provider devices,
            string deviceId)
        {
            if (string.IsNullOrEmpty(deviceId))
                throw new ArgumentNullException("deviceId");

            if (devices == null)
                throw new ArgumentNullException("devices");

            _devices = devices;
            _deviceId = deviceId;
            _deviceCapabilities = new Collection();
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates an instance of DeviceInfo.
 /// </summary>
 /// <param name="deviceId">A unique Identifier of the device.</param>
 /// <param name="devices">A reference to the complete index of devices.</param>
 internal DeviceInfo(
     Provider devices,
     string deviceId)
 {
     Init(devices, deviceId);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Creates an instance of DeviceInfo.
 /// </summary>
 /// <param name="userAgent">User agent string used to identify this device.</param>
 /// <param name="deviceId">A unique Identifier of the device.</param>
 /// <param name="devices">A reference to the complete index of devices.</param>
 /// <param name="fallbackDevice">The fallback device to use for this device if any.</param>
 internal DeviceInfo(
     Provider devices,
     string deviceId,
     string userAgent,
     DeviceInfo fallbackDevice)
 {
     if (fallbackDevice == null)
         throw new ArgumentNullException("fallbackDevice");
     Init(devices, deviceId, userAgent);
     _fallbackDevice = fallbackDevice;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Parses the wurfl file into a instance of WurflFile.
        /// </summary>
        /// <param name="devices">Instance of Devices to store data.</param>
        /// <param name="wurflFilePath">Wurfl file path.</param>
        /// <param name="capabilitiesWhiteList">List of capabilities to be used. If none, all capabilities will be loaded into the memory.</param>
        /// <param name="wurflPatchFiles">Null, string or array of strings representing the wurfl patch files
        /// which must be applied against the main file.</param>
        /// <returns>Returns an instance of WurflFile. 
        /// <remarks>If none file is found a null value will be returned.</remarks>
        /// </returns>
        /// <exception cref="System.IO.FileNotFoundException">Thrown if the parameter <paramref name="wurflFilePath"/> 
        /// referes to a file that does not exists.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if the parameter <paramref name="wurflFilePath"/> 
        /// is an empty string or a null value.</exception>
        private static void ParseWurflFiles(
            Provider devices,
            string wurflFilePath,
            StringCollection capabilitiesWhiteList,
            params string[] wurflPatchFiles)
        {
            if (string.IsNullOrEmpty(wurflFilePath))
                throw new ArgumentNullException("wurflFilePath");

            if (!File.Exists(wurflFilePath))
                throw new FileNotFoundException(Constants.WurflFileNotFound, wurflFilePath);

            // Load white listed capabilities
            if (capabilitiesWhiteList != null)
            {
                _loadOnlyCapabilitiesWhiteListed = capabilitiesWhiteList.Count > 0;
            #if VER4
                foreach (string capability in
                    capabilitiesWhiteList.Cast<string>().Where(capability => !_capabilitiesWhiteListed.Contains(capability)))
                {
                    _capabilitiesWhiteListed.Add(capability);
                }
            #elif VER2
                foreach (string capability in capabilitiesWhiteList)
                    if (!_capabilitiesWhiteListed.Contains(capability))
                        _capabilitiesWhiteListed.Add(capability);
            #endif
            }

            StringCollection wurflFilePaths = new StringCollection();
            wurflFilePaths.Add(wurflFilePath);
            wurflFilePaths.AddRange(wurflPatchFiles);

            ParseWurflFiles(devices, wurflFilePaths, File.GetCreationTimeUtc(wurflFilePath));
        }
Exemplo n.º 8
0
 /// <summary>
 /// Parses the wurfl file into a instance of Devices.
 /// </summary>
 /// <param name="devices">Instance of Devices to store data.</param>
 /// <param name="wurflFilePath">Wurfl file path.</param>
 /// <param name="wurflPatchFiles">Null, string or array of strings representing the wurfl patch files
 /// which must be applied against the main file.</param>
 /// <returns>Returns an instance of Devices. 
 /// <remarks>If none file is found a null value will be returned.</remarks>
 /// </returns>
 /// <exception cref="System.IO.FileNotFoundException">Thrown if the parameter <paramref name="wurflFilePath"/> 
 /// referes to a file that does not exists.</exception>
 /// <exception cref="System.ArgumentNullException">Thrown if the parameter <paramref name="wurflFilePath"/> 
 /// is an empty string or a null value.</exception>
 private static void ParseWurflFiles(
     Provider devices,
     string wurflFilePath,
     params string[] wurflPatchFiles)
 {
     ParseWurflFiles(
         devices,
         wurflFilePath,
         null,
         wurflPatchFiles);
 }
Exemplo n.º 9
0
        /// <summary>
        /// Parses the Wurfl files and updates the devices object with data found.
        /// </summary>
        /// <param name="wurflFilePaths">Paths of the Wurfl xml and patch files.</param>
        /// <param name="devices">Collection of mobile devices.</param>
        /// <param name="masterFileDate">If a device has a creation date element only include it in the
        /// device database if it was created after the master file's date and time.</param>
        /// <remarks>If no files are found devices will remain unchanged.</remarks>
        private static void ParseWurflFiles(Provider devices, StringCollection wurflFilePaths,
            DateTime masterFileDate)
        {
            StringCollection availableCapabilities = new StringCollection();

            foreach (string filePath in wurflFilePaths)
            {
                // Load the data from the XML file into the devices collection.
                LoadXmlData(availableCapabilities,
                            devices,
                            filePath,
                            masterFileDate);
            }
        }
Exemplo n.º 10
0
        private static void LoadXmlData(
            StringCollection availableCapabilities,
            Provider devices,
            string filePath,
            DateTime masterFileDate)
        {
            DeviceInfo device = null;
            FileInfo file = new FileInfo(filePath);

            if (file.Exists)
            {
                // Open the reader using decompression if the file has an extension
                // that indicates it's compressed.
                using (XmlReader reader = GetReader(file))
                {
                    try
                    {
                        // Process the data file.
                        while (reader.Read())
                        {
                            switch (reader.Name)
                            {
                                    // Load Device Data
                                case Constants.DeviceNodeName:
                                    if (reader.IsStartElement())
                                    {
                                        // If a device has already been created ensure it's saved.
                                        if (device != null)
                                            devices.Set(device);

                                        // Create or get the device related to the current XML element.
                                        device = LoadDevice(devices, reader);
                                    }
                                    break;

                                    // Load the device capability.
                                case Constants.CapabilityNodeName:
                                    if (reader.IsStartElement())
                                    {
                                        LoadCapabilityData(
                                            reader,
                                            device,
                                            availableCapabilities);
                                    }
                                    break;
                            }
                        }

                        // If a device has not been written ensure it's added to the device dataset.
                        if (device != null)
                            devices.Set(device);
                    }
                    catch (XmlException ex)
                    {
                        throw new WurflException(
                            String.Format("XML exception processing wurfl file '{0}'.", filePath),
                            ex);
                    }
                    catch (IOException ex)
                    {
                        throw new WurflException(
                            String.Format("IO exception processing wurfl file '{0}'.", filePath),
                            ex);
                    }
                    catch (Exception ex)
                    {
                        throw new WurflException(
                            String.Format("Exception processing wurfl file '{0}'.", filePath),
                            ex);
                    }
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Processes the XML element containing the device attributes.
        /// </summary>
        /// <param name="devices">A list of loaded devices.</param>
        /// <param name="reader">The XML stream readers.</param>
        /// <returns>An empty device.</returns>
        private static DeviceInfo LoadDevice(Provider devices, XmlReader reader)
        {
            // Create the next device using the fallback device if available.
            string deviceId = reader.GetAttribute(Constants.IdAttributeName, string.Empty);
            string userAgent = reader.GetAttribute(Constants.UserAgentAttributeName, string.Empty);
            string fallbackDeviceId = reader.GetAttribute(Constants.FallbackAttributeName, string.Empty);

            // If the device already exists then use the previous one. This may happen
            // when an earlier device referenced a fallback device that had not yet
            // been created.
            DeviceInfo device = devices.GetDeviceInfoFromID(deviceId);
            if (device == null)
            {
                // Create the new device.
                device = new DeviceInfo(devices, deviceId, userAgent ?? String.Empty);
            }
            else if (userAgent != null)
            {
                // Ensure the correct UserAgent string is assigned to this device.
                device.SetUserAgent(userAgent);
            }

            // If the Actual Device Root attribute is specified then set the value
            // for this device.
            bool isActualDeviceRoot;
            if (bool.TryParse(reader.GetAttribute(Constants.ActualDeviceRoot, string.Empty), out isActualDeviceRoot))
                device.IsActualDeviceRoot = isActualDeviceRoot;

            // Check the fallback device is different to the device being loaded.
            if (fallbackDeviceId != null && device.DeviceId != fallbackDeviceId)
            {
                // Does the fallback device already exist?
                device.FallbackDevice = devices.GetDeviceInfoFromID(fallbackDeviceId);
                if (device.FallbackDevice == null)
                {
                    // No. So create new fallback device.
                    device.FallbackDevice = new DeviceInfo(devices, fallbackDeviceId);
                    // Add it to the available devices.
                    devices.Set(device.FallbackDevice);
                }
            }
            return device;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates a single instance of this class to be used by all
        /// clients within the AppDomain.
        /// </summary>
        private static void LoadSingleton()
        {
            Provider newInstance = new Provider();
            try
            {
                long startTicks = DateTime.Now.Ticks;

                // Load data from all available sources.
                Processor.ParseWurflFiles(newInstance);

                long duration = TimeSpan.FromTicks(DateTime.Now.Ticks - startTicks + 1).Milliseconds;

                // Log the length of time taken to load the device data.
                EventLog.Info(String.Format("Loaded {0} devices using {1} strings in {2}ms",
                                            newInstance._deviceIDs.Count,
                                            Strings.Count,
                                            duration));

                // Log the number of devices assigned to each handler if debugging is enabled.
            #if DEBUG
                // Display the handler results.
                //tomquery
                /*if (newInstance._handlers != null && EventLog.IsDebug)
                {
                    for (int i = 0; i < newInstance._handlers.Length; i++)
                    {
                        EventLog.Debug(String.Format("Handler '{0}' loaded with {1} devices.",
                                                     newInstance._handlers[i].GetType().Name,
                                                     newInstance._handlers[i].UserAgents.Count));
                    }
                }*/
            #endif

                // Store the single instance and change the status to show the
                // data has finished loading.
                newInstance.IsLoaded = true;
                _instance = newInstance;
            }
            catch (WurflException ex)
            {
                // Record the exception.
                EventLog.Fatal(ex);
                // Set an empty Devices instance as it's not possible
                // to reliably load the data files specified.
                _instance = new Provider();
            }
        }