You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by bd...@apache.org on 2012/10/02 15:34:35 UTC
svn commit: r1392912 [6/8] - in /incubator/devicemap/trunk/openddr/csharp:
./ DDR-Simple-API/ DDR-Simple-API/Exceptions/ DDR-Simple-API/Models/
DDR-Simple-API/Properties/ DDR-Simple-API/Simple/ OpenDDR-CSharp/
OpenDDR-CSharp/Builders/ OpenDDR-CSharp/Bu...
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/OperatingSystemDatasourceParser.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/OperatingSystemDatasourceParser.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/OperatingSystemDatasourceParser.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/OperatingSystemDatasourceParser.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,168 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Xml;
+using System.Xml.Linq;
+using Oddr.Models.OS;
+using Oddr.Vocabularies;
+using W3c.Ddr.Exceptions;
+
+namespace Oddr.Documenthandlers
+{
+ public class OperatingSystemDatasourceParser
+ {
+ private const string ELEMENT_OPERATING_SYSTEM = "OperatingSystem";
+ private const String ELEMENT_OPERATING_SYSTEM_DESCRIPTION = "operatingSystem";
+ private const String ELEMENT_PROPERTY = "property";
+ private const String ATTRIBUTE_OS_ID = "id";
+ private const String ATTRIBUTE_PROPERTY_NAME = "name";
+ private const String ATTRIBUTE_PROPERTY_VALUE = "value";
+ private XDocument doc;
+ private VocabularyHolder vocabularyHolder;
+ public SortedDictionary<String, Oddr.Models.OS.OperatingSystem> operatingSystems
+ {
+ private set;
+ get;
+ }
+
+ public OperatingSystemDatasourceParser(Stream stream)
+ {
+ Init(stream);
+ }
+
+ public OperatingSystemDatasourceParser(Stream stream, VocabularyHolder vocabularyHolder)
+ {
+ Init(stream);
+ try
+ {
+ vocabularyHolder.ExistVocabulary(ODDRVocabularyService.ODDR_LIMITED_VOCABULARY_IRI);
+ this.vocabularyHolder = vocabularyHolder;
+ }
+ catch (Exception ex)
+ {
+ this.vocabularyHolder = null;
+ }
+
+ }
+
+ private void Init(Stream stream)
+ {
+ this.operatingSystems = new SortedDictionary<string, Oddr.Models.OS.OperatingSystem>(StringComparer.Ordinal);
+ XmlReader reader = null;
+ try
+ {
+ reader = XmlReader.Create(stream);
+ doc = XDocument.Load(reader);
+ }
+ catch (ArgumentNullException ex)
+ {
+ throw new ArgumentNullException(ex.Message, ex);
+ }
+ finally
+ {
+ if (reader != null)
+ {
+ ((IDisposable)reader).Dispose();
+ }
+ }
+ }
+
+ /// <exception cref="Exception">Thrown when...</exception>
+ public void Parse()
+ {
+ if (doc == null)
+ {
+ throw new Exception("Input stream is not valid");
+ }
+
+ OperatingSystemWrapper[] osWrapperArray = (from os in doc.Descendants(ELEMENT_OPERATING_SYSTEM).Descendants(ELEMENT_OPERATING_SYSTEM_DESCRIPTION)
+ select new OperatingSystemWrapper
+ {
+ id = os.Attribute(ATTRIBUTE_OS_ID).Value,
+ properties = (from prop in os.Descendants(ELEMENT_PROPERTY)
+ select new StringPair
+ {
+ key = prop.Attribute(ATTRIBUTE_PROPERTY_NAME).Value,
+ value = prop.Attribute(ATTRIBUTE_PROPERTY_VALUE).Value,
+ }).ToArray<StringPair>(),
+ }).ToArray<OperatingSystemWrapper>();
+
+ foreach (OperatingSystemWrapper osw in osWrapperArray)
+ {
+ Oddr.Models.OS.OperatingSystem os = osw.GetOperatingSystem(vocabularyHolder);
+ operatingSystems.Add(osw.id, os);
+ }
+ }
+
+ private class OperatingSystemWrapper
+ {
+ public string id;
+ public StringPair[] properties;
+
+ public Oddr.Models.OS.OperatingSystem GetOperatingSystem(VocabularyHolder vocabularyHolder)
+ {
+ Dictionary<string, string> dic = new Dictionary<string, string>();
+
+ if (vocabularyHolder != null)
+ {
+ foreach (StringPair sp in properties)
+ {
+ try
+ {
+ //vocabularyHolder.ExistProperty(sp.key, ODDRService.ASPECT_OPERATIVE_SYSTEM, ODDRVocabularyService.ODDR_LIMITED_VOCABULARY_IRI);
+ if (vocabularyHolder.ExistProperty(sp.key, ODDRService.ASPECT_OPERATIVE_SYSTEM, ODDRVocabularyService.ODDR_LIMITED_VOCABULARY_IRI) != null)
+ {
+ dic.Add(sp.key, sp.value);
+ }
+ }
+ //catch (NameException ex)
+ //{
+ // //property non loaded
+ //}
+ catch (ArgumentException ae)
+ {
+ //Console.WriteLine(this.GetType().FullName + " " + sp.key + " already present!!!");
+ }
+ }
+ }
+ else
+ {
+ foreach (StringPair sp in properties)
+ {
+ dic.Add(sp.key, sp.value);
+ }
+ }
+
+ return new Oddr.Models.OS.OperatingSystem(dic);
+ }
+ }
+
+ private class StringPair
+ {
+ public string key;
+ public string value;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/VocabularyParser.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/VocabularyParser.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/VocabularyParser.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Documenthandlers/VocabularyParser.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,144 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Xml.Linq;
+using Oddr.Models.Vocabularies;
+using System.IO;
+using System.Xml;
+
+namespace Oddr.Documenthandlers
+{
+ public class VocabularyParser
+ {
+ private const String ELEMENT_VOCABULARY_DESCRIPTION = "VocabularyDescription";
+ private const String ELEMENT_ASPECTS = "Aspects";
+ private const String ELEMENT_ASPECT = "Aspect";
+ private const String ELEMENT_VARIABLES = "Variables";
+ private const String ELEMENT_VARIABLE = "Variable";
+ private const String ELEMENT_PROPERTIES = "Properties";
+ private const String ELEMENT_PROPERTY = "Property";
+ private const String ATTRIBUTE_PROPERTY_TARGET = "target";
+ private const String ATTRIBUTE_PROPERTY_ASPECT_NAME = "name";
+ private const String ATTRIBUTE_PROPERTY_ASPECT = "aspect";
+ private const String ATTRIBUTE_PROPERTY_NAME = "name";
+ private const String ATTRIBUTE_PROPERTY_VOCABULARY = "vocabulary";
+ private const String ATTRIBUTE_PROPERTY_ID = "id";
+ private const String ATTRIBUTE_PROPERTY_DATA_TYPE = "datatype";
+ private const String ATTRIBUTE_PROPERTY_DATA_TYPE_CAMEL = "datatype";
+ private const String ATTRIBUTE_PROPERTY_EXPR = "expr";
+ private const String ATTRIBUTE_PROPERTY_ASPECTS = "aspects";
+ private const String ATTRIBUTE_PROPERTY_DEFAULT_ASPECT = "defaultAspect";
+ public Vocabulary vocabulary
+ {
+ private set;
+ get;
+ }
+ private XDocument doc;
+
+ /// <exception cref="ArgumentNullException">Thrown when...</exception>
+ public VocabularyParser(Stream stream)
+ {
+ XmlReader reader = null;
+ try
+ {
+ reader = XmlReader.Create(stream);
+ doc = XDocument.Load(reader);
+ }
+ catch (ArgumentNullException ex)
+ {
+ throw new ArgumentNullException(ex.Message, ex);
+ }
+ finally
+ {
+ if (reader != null)
+ {
+ ((IDisposable)reader).Dispose();
+ }
+ }
+ }
+
+ /// <exception cref="Exception">Thrown when...</exception>
+ public void Parse()
+ {
+ if (doc == null)
+ {
+ throw new Exception("Input stream is not valid");
+ }
+
+ vocabulary = (from v in doc.Descendants(ELEMENT_VOCABULARY_DESCRIPTION)
+ select new Vocabulary
+ {
+ vocabularyIRI = (string)v.Attribute(ATTRIBUTE_PROPERTY_TARGET).Value,
+ aspects = (from aspects in v.Descendants(ELEMENT_ASPECTS)
+ select (string)aspects.Attribute(ELEMENT_ASPECT)).ToArray<string>(),
+ properties = new Dictionary<string,VocabularyProperty>(),
+ vocabularyVariables = new Dictionary<string,VocabularyVariable>(),
+ }).First<Vocabulary>();
+
+ XElement vocDescrXElement = doc.Descendants(ELEMENT_VOCABULARY_DESCRIPTION).First<XElement>();
+ XElement propertiesXElement = vocDescrXElement.Descendants(ELEMENT_PROPERTIES).First<XElement>();
+
+ VocabularyProperty[] vocabularyProperties = (from prop in propertiesXElement.Descendants(ELEMENT_PROPERTY)
+ //where prop.Attribute(ATTRIBUTE_PROPERTY_DATA_TYPE) != null
+ select new VocabularyProperty
+ {
+ aspects = prop.Attribute(ATTRIBUTE_PROPERTY_ASPECTS).Value.Replace(" ", "").Split(",".ToCharArray()),
+ defaultAspect = prop.Attribute(ATTRIBUTE_PROPERTY_DEFAULT_ASPECT).Value,
+ //expr = prop.Attribute(ATTRIBUTE_PROPERTY_EXPR).Value,
+ name = prop.Attribute(ATTRIBUTE_PROPERTY_NAME).Value,
+ type = prop.Attribute(ATTRIBUTE_PROPERTY_DATA_TYPE).Value,
+ }).ToArray<VocabularyProperty>();
+
+ foreach (VocabularyProperty vp in vocabularyProperties)
+ {
+ vocabulary.properties.Add(vp.name, vp);
+ }
+
+
+ try
+ {
+ XElement variablesXElement = vocDescrXElement.Descendants(ELEMENT_VARIABLES).First<XElement>();
+
+ VocabularyVariable[] vocabularyVariables = (from var in variablesXElement.Descendants(ELEMENT_VARIABLE)
+ select new VocabularyVariable
+ {
+ aspect = var.Attribute(ATTRIBUTE_PROPERTY_ASPECT).Value,
+ id = var.Attribute(ATTRIBUTE_PROPERTY_ID).Value,
+ name = var.Attribute(ATTRIBUTE_PROPERTY_NAME).Value,
+ vocabulary = var.Attribute(ATTRIBUTE_PROPERTY_VOCABULARY).Value,
+ }).ToArray<VocabularyVariable>();
+
+ foreach (VocabularyVariable vv in vocabularyVariables)
+ {
+ vocabulary.vocabularyVariables.Add(vv.id, vv);
+ }
+ }
+ catch (Exception ex)
+ {
+ //TODO: Handle this Exception
+ }
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/BrowserIdentificator.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/BrowserIdentificator.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/BrowserIdentificator.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/BrowserIdentificator.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,161 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Oddr.Models;
+using W3c.Ddr.Simple;
+using Oddr.Models.Browsers;
+using Oddr.Builders;
+using log4net;
+using log4net.Config;
+
+namespace Oddr.Identificators
+{
+ public class BrowserIdentificator : IIdentificator
+ {
+ protected static readonly ILog logger = LogManager.GetLogger(typeof(BrowserIdentificator));
+ private IBuilder[] builders;
+ private Dictionary<String, Browser> browserCapabilities;
+
+ public BrowserIdentificator(IBuilder[] builders, Dictionary<String, Browser> browserCapabilities)
+ {
+ this.builders = builders;
+ this.browserCapabilities = browserCapabilities;
+ }
+
+ public BuiltObject Get(string userAgent, int confidenceTreshold)
+ {
+ UserAgent ua = UserAgentFactory.newUserAgent(userAgent);
+ return Get(ua, confidenceTreshold);
+ }
+
+ //XXX: to be refined, this should NOT be the main entry point, we should use a set of evidence derivation
+ public BuiltObject Get(IEvidence evdnc, int threshold)
+ {
+ UserAgent ua = UserAgentFactory.newBrowserUserAgent(evdnc);
+
+ if (ua != null)
+ {
+ return Get(ua, threshold);
+ }
+
+ return null;
+ }
+
+ public BuiltObject Get(UserAgent userAgent, int confidenceTreshold)
+ {
+ foreach (IBuilder builder in builders)
+ {
+ if (builder.CanBuild(userAgent))
+ {
+ Browser browser = (Browser)builder.Build(userAgent, confidenceTreshold);
+ if (browser != null)
+ {
+ if (browserCapabilities != null)
+ {
+ String bestID = GetClosestKnownBrowserID(browser.GetId());
+ if (bestID != null)
+ {
+ Browser b = null;
+ if (browserCapabilities.TryGetValue(bestID, out b))
+ {
+ browser.PutPropertiesMap(b.properties);
+ if (!bestID.Equals(browser.GetId()))
+ {
+ browser.confidence = (browser.confidence - 15);
+ }
+
+ }
+ }
+ }
+ return browser;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private String GetClosestKnownBrowserID(String actualBrowserID)
+ {
+ XmlConfigurator.Configure();
+
+ if (actualBrowserID == null)
+ {
+ return null;
+ }
+
+ int idx = actualBrowserID.IndexOf(".");
+
+ if (idx < 0)
+ {
+ logger.Error("SHOULD NOT BE HERE, PLEASE CHECK BROWSER DOCUMENT(1)");
+ logger.Debug(actualBrowserID);
+ return null;
+
+ }
+ else
+ {
+ idx++;
+ }
+ idx = actualBrowserID.IndexOf(".", idx);
+
+ if (idx < 0)
+ {
+ logger.Error("SHOULD NOT BE HERE, PLEASE CHECK BROWSER DOCUMENT(2)" + idx);
+ logger.Debug(actualBrowserID);
+ return null;
+
+ }
+ else
+ {
+ idx++;
+ }
+
+ String bestID = null;
+ foreach (String listBrowserID in browserCapabilities.Keys)
+ {
+ if (listBrowserID.Equals(actualBrowserID))
+ {
+ return actualBrowserID;
+ }
+
+ if (listBrowserID.Length > idx && listBrowserID.Substring(0, idx).Equals(actualBrowserID.Substring(0, idx)))
+ {
+ if (listBrowserID.CompareTo(actualBrowserID) <= 0)
+ {
+ bestID = listBrowserID;
+ }
+ }
+ }
+
+ return bestID;
+ }
+
+ public void CompleteInit()
+ {
+ //does nothing
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/DeviceIdentificator.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/DeviceIdentificator.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/DeviceIdentificator.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/DeviceIdentificator.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,131 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using W3c.Ddr.Simple;
+using Oddr.Models;
+using Oddr.Models.Devices;
+using Oddr.Builders.Devices;
+
+namespace Oddr.Identificators
+{
+ public class DeviceIdentificator : IIdentificator
+ {
+ private IDeviceBuilder[] builders;
+ private Dictionary<String, Device> devices;
+
+ public DeviceIdentificator(IDeviceBuilder[] builders, Dictionary<String, Device> devices)
+ {
+ this.builders = builders;
+ this.devices = devices;
+ }
+
+ public BuiltObject Get(string userAgent, int confidenceTreshold)
+ {
+ return Get(UserAgentFactory.newUserAgent(userAgent), confidenceTreshold);
+ }
+
+ //XXX: to be refined, this should NOT be the main entry point, we should use a set of evidence derivation
+ public BuiltObject Get(IEvidence evdnc, int threshold)
+ {
+ UserAgent ua = UserAgentFactory.newDeviceUserAgent(evdnc);
+ if (ua != null)
+ {
+ return Get(ua, threshold);
+ }
+ return null;
+ }
+
+ public BuiltObject Get(UserAgent userAgent, int confidenceTreshold)
+ {
+ List<Device> foundDevices = new List<Device>();
+ Device foundDevice = null;
+ foreach (IDeviceBuilder deviceBuilder in builders)
+ {
+ if (deviceBuilder.CanBuild(userAgent))
+ {
+ Device device = (Device)deviceBuilder.Build(userAgent, confidenceTreshold);
+ if (device != null)
+ {
+ String parentId = device.parentId;
+ Device parentDevice = null;
+ while (!"root".Equals(parentId))
+ {
+ if (devices.TryGetValue(parentId, out parentDevice))
+ {
+ foreach (KeyValuePair<string, string> entry in parentDevice.properties)
+ {
+ if (!device.ContainsProperty(entry.Key))
+ {
+ device.PutProperty(entry.Key, entry.Value);
+ }
+ }
+ parentId = parentDevice.parentId;
+ }
+ }
+
+ foundDevices.Add(device);
+ if (device.confidence >= confidenceTreshold)
+ {
+ foundDevice = device;
+ break;
+ }
+ }
+ }
+ }
+
+ if (foundDevice != null)
+ {
+ return foundDevice;
+
+ }
+ else
+ {
+ if (foundDevices.Count > 0)
+ {
+ foundDevices.Sort();
+ foundDevices.Reverse();
+ return foundDevices[0];
+ }
+
+ return null;
+ }
+ }
+
+ public void CompleteInit()
+ {
+ foreach (IDeviceBuilder deviceBuilder in builders)
+ {
+ try
+ {
+ deviceBuilder.CompleteInit(devices);
+ }
+ catch (Exception ex)
+ {
+ //Console.WriteLine(this.GetType().FullName + " " + ex.Message);
+ }
+ }
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/IIdentificator.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/IIdentificator.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/IIdentificator.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/IIdentificator.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,41 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Oddr.Models;
+using W3c.Ddr.Simple;
+
+namespace Oddr.Identificators
+{
+ public interface IIdentificator
+ {
+ BuiltObject Get(String userAgent, int confidenceTreshold);
+
+ BuiltObject Get(IEvidence evdnc, int threshold);
+
+ BuiltObject Get(UserAgent userAgent, int confidenceTreshold);
+
+ void CompleteInit();
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/OSIdentificator.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/OSIdentificator.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/OSIdentificator.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Identificators/OSIdentificator.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,146 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Oddr.Models;
+using W3c.Ddr.Simple;
+using Oddr.Builders;
+using log4net;
+using Oddr.Models.Browsers;
+using OSModel = Oddr.Models.OS;
+using log4net.Config;
+
+namespace Oddr.Identificators
+{
+ public class OSIdentificator : IIdentificator
+ {
+ protected static readonly ILog logger = LogManager.GetLogger(typeof(OSIdentificator));
+ private IBuilder[] builders;
+ private Dictionary<String, OSModel.OperatingSystem> operatingSystemCapabilities;
+
+ public OSIdentificator(IBuilder[] builders, Dictionary<String, OSModel.OperatingSystem> operatingSystemCapabilities)
+ {
+ this.builders = builders;
+ this.operatingSystemCapabilities = operatingSystemCapabilities;
+ }
+
+ public BuiltObject Get(string userAgent, int confidenceTreshold)
+ {
+ return Get(UserAgentFactory.newUserAgent(userAgent), confidenceTreshold);
+ }
+
+ //XXX: to be refined, this should NOT be the main entry point, we should use a set of evidence derivation
+ public BuiltObject Get(IEvidence evdnc, int threshold)
+ {
+ UserAgent ua = UserAgentFactory.newDeviceUserAgent(evdnc);
+ if (ua != null)
+ {
+ return Get(ua, threshold);
+ }
+ return null;
+ }
+
+ public BuiltObject Get(UserAgent userAgent, int confidenceTreshold)
+ {
+ foreach (IBuilder builder in builders)
+ {
+ if (builder.CanBuild(userAgent))
+ {
+ OSModel.OperatingSystem os = (OSModel.OperatingSystem)builder.Build(userAgent, confidenceTreshold);
+ if (os != null)
+ {
+ if (operatingSystemCapabilities != null)
+ {
+ String bestID = GetClosestKnownBrowserID(os.GetId());
+ if (bestID != null)
+ {
+ OSModel.OperatingSystem operatingSystem = null;
+ if (operatingSystemCapabilities.TryGetValue(bestID, out operatingSystem))
+ {
+ os.PutPropertiesMap(operatingSystem.properties);
+
+ if (!bestID.Equals(os.GetId()))
+ {
+ os.confidence = (os.confidence - 15);
+ }
+ }
+ }
+ }
+ return os;
+ }
+ }
+ }
+ return null;
+ }
+
+ private String GetClosestKnownBrowserID(String actualOperatingSystemID) {
+ XmlConfigurator.Configure();
+
+ if (actualOperatingSystemID == null) {
+ return null;
+ }
+
+ int idx = actualOperatingSystemID.IndexOf(".");
+
+ if (idx < 0) {
+ logger.Error("SHOULD NOT BE HERE, PLEASE CHECK BROWSER DOCUMENT(1)");
+ logger.Debug(actualOperatingSystemID);
+ return null;
+
+ } else {
+ idx++;
+ }
+ idx = actualOperatingSystemID.IndexOf(".", idx);
+
+ if (idx < 0) {
+ logger.Error("SHOULD NOT BE HERE, PLEASE CHECK BROWSER DOCUMENT(2)" + idx);
+ logger.Debug(actualOperatingSystemID);
+ return null;
+
+ } else {
+ idx++;
+ }
+
+ String bestID = null;
+ foreach (String listOperatingSystemID in operatingSystemCapabilities.Keys) {
+ if (listOperatingSystemID.Equals(actualOperatingSystemID)) {
+ return actualOperatingSystemID;
+ }
+
+ if (listOperatingSystemID.Length > idx && listOperatingSystemID.Substring(0, idx).Equals(actualOperatingSystemID.Substring(0, idx))) {
+ if (listOperatingSystemID.CompareTo(actualOperatingSystemID) <= 0) {
+ bestID = listOperatingSystemID;
+ }
+ }
+ }
+
+ return bestID;
+ }
+
+ public void CompleteInit()
+ {
+ //does nothing
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Browsers/Browser.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Browsers/Browser.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Browsers/Browser.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Browsers/Browser.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,324 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Oddr.Models.Browsers
+{
+ /// <summary>
+ /// Identified Browser model object.
+ /// </summary>
+ public class Browser : BuiltObject, IComparable, ICloneable
+ {
+ public String majorRevision
+ {
+ get; set;
+ }
+ public String minorRevision
+ {
+ get; set;
+ }
+ public String microRevision
+ {
+ get; set;
+ }
+ public String nanoRevision
+ {
+ get;
+ set;
+ }
+
+ public Browser()
+ : base()
+ {
+ Init();
+ }
+
+ public Browser(Dictionary<String, String> properties)
+ : base(properties)
+ {
+ Init();
+ }
+
+ private void Init()
+ {
+ this.majorRevision = "0";
+ this.minorRevision = "0";
+ this.microRevision = "0";
+ this.nanoRevision = "0";
+ }
+
+ public String GetId()
+ {
+ if (GetModel() == null || GetVendor() == null) {
+ return null;
+ }
+ String id = GetVendor() + "." + GetModel() + "." + this.majorRevision + "." + this.minorRevision + "." + this.microRevision + "." + this.nanoRevision;
+ return id;
+ }
+
+ //GETTERS
+ //utility getter for core ddr properties
+ public String GetCookieSupport()
+ {
+ return Get("cookieSupport");
+ }
+
+ public int GetDisplayHeight()
+ {
+ try {
+ return int.Parse(Get("displayHeight"));
+
+ } catch (Exception ex) {
+ return -1;
+ }
+ }
+
+ public int GetDisplayWidth()
+ {
+ try {
+ return int.Parse(Get("displayWidth"));
+
+ } catch (Exception ex) {
+ return -1;
+ }
+ }
+
+ public String GetImageFormatSupport()
+ {
+ return Get("imageFormatSupport");
+ }
+
+ public String GetInputModeSupport()
+ {
+ return Get("inputModeSupport");
+ }
+
+ public String GetMarkupSupport()
+ {
+ return Get("markupSupport");
+ }
+
+ public String GetModel()
+ {
+ return Get("model");
+ }
+
+ public String GetScriptSupport()
+ {
+ return Get("scriptSupport");
+ }
+
+ public String GetStylesheetSupport()
+ {
+ return Get("stylesheetSupport");
+ }
+
+ public String GetVendor()
+ {
+ return Get("vendor");
+ }
+
+ public String GetVersion()
+ {
+ return Get("version");
+ }
+
+ //utility getter for significant oddr browser properties
+ public String GetRenderer()
+ {
+ return Get("layoutEngine");
+ }
+
+ public String GetRendererVersion()
+ {
+ return Get("layoutEngineVersion");
+ }
+
+ public String GetClaimedReference()
+ {
+ return Get("referencedBrowser");
+ }
+
+ public String GetClaimedReferenceVersion()
+ {
+ return Get("referencedBrowserVersion");
+ }
+
+ public String GetBuild()
+ {
+ return Get("build");
+ }
+
+ //SETTERS
+ //utility setter for core ddr properties
+ public void SetCookieSupport(String cookieSupport)
+ {
+ PutProperty("cookieSupport", cookieSupport);
+ }
+
+ public void SetDisplayHeight(int displayHeight)
+ {
+ PutProperty("displayHeight", displayHeight.ToString());
+ }
+
+ public void SetDisplayWidth(int displayWidth)
+ {
+ PutProperty("displayWidth", displayWidth.ToString());
+ }
+
+ public void SetImageFormatSupport(String imageFormatSupport)
+ {
+ PutProperty("imageFormatSupport", imageFormatSupport);
+ }
+
+ public void SetInputModeSupport(String inputModeSupport)
+ {
+ PutProperty("inputModeSupport", inputModeSupport);
+ }
+
+ public void SetMarkupSupport(String markupSupport)
+ {
+ PutProperty("markupSupport", markupSupport);
+ }
+
+ public void SetModel(String model)
+ {
+ PutProperty("model", model);
+ }
+
+ public void SetScriptSupport(String scriptSupport)
+ {
+ PutProperty("scriptSupport", scriptSupport);
+ }
+
+ public void SetStylesheetSupport(String stylesheetSupport)
+ {
+ PutProperty("stylesheetSupport", stylesheetSupport);
+ }
+
+ public void SetVendor(String vendor)
+ {
+ PutProperty("vendor", vendor);
+ }
+
+ public void SetVersion(String version)
+ {
+ PutProperty("version", version);
+ }
+
+ //utility setter for significant oddr browser properties
+ public void SetLayoutEngine(String layoutEngine)
+ {
+ PutProperty("layoutEngine", layoutEngine);
+ }
+
+ public void SetLayoutEngineVersion(String layoutEngineVersion)
+ {
+ PutProperty("layoutEngineVersion", layoutEngineVersion);
+ }
+
+ public void SetReferenceBrowser(String referenceBrowser)
+ {
+ PutProperty("referenceBrowser", referenceBrowser);
+ }
+
+ public void SetReferenceBrowserVersion(String referenceBrowserVersion)
+ {
+ PutProperty("referenceBrowserVersion", referenceBrowserVersion);
+ }
+
+ public void SetBuild(String build)
+ {
+ PutProperty("build", build);
+ }
+
+ //Comparable
+ public int CompareTo(object obj)
+ {
+ if (obj == null || !(obj is Browser))
+ {
+ return int.MaxValue;
+ }
+
+ Browser bd = (Browser) obj;
+ return this.confidence - bd.confidence;
+ }
+
+
+ // Cloneable
+ public object Clone()
+ {
+ Browser b = new Browser();
+ b.majorRevision = this.majorRevision;
+ b.minorRevision = this.minorRevision;
+ b.microRevision = this.microRevision;
+ b.nanoRevision = this.nanoRevision;
+ b.confidence = this.confidence;
+ b.PutPropertiesMap(this.properties);
+ return b;
+ }
+
+ //Utility
+ public override string ToString()
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.Append(GetVendor());
+ sb.Append(" ");
+ sb.Append(GetModel());
+
+ sb.Append("(");
+ if (GetRenderer() != null && GetRenderer().Length > 0) {
+ sb.Append(" ");
+ sb.Append(GetRenderer());
+ sb.Append(" ");
+ sb.Append(GetRendererVersion());
+ sb.Append(" ");
+ }
+ if (GetClaimedReference() != null && GetClaimedReference().Length > 0) {
+ sb.Append(" ");
+ sb.Append(GetClaimedReference());
+ sb.Append(" ");
+ sb.Append(GetClaimedReferenceVersion());
+ sb.Append(" ");
+ }
+ sb.Append(GetVersion()).Append(")");
+
+ sb.Append(" [").Append(this.majorRevision).Append(".").Append(this.minorRevision).Append(".").Append(this.microRevision).Append(".").Append(this.nanoRevision).Append("]");
+ if (GetBuild() != null) {
+ sb.Append(" - ").Append(GetBuild());
+ }
+ if (GetDisplayWidth() > 0 && GetDisplayHeight() > 0) {
+ sb.Append("<");
+ sb.Append(GetDisplayWidth());
+ sb.Append("x");
+ sb.Append(GetDisplayHeight());
+ sb.Append(">");
+ }
+ sb.Append(" ").Append(this.confidence).Append("%");
+ String toRet = sb.ToString();
+ return toRet;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BufferedODDRHTTPEvidence.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BufferedODDRHTTPEvidence.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BufferedODDRHTTPEvidence.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BufferedODDRHTTPEvidence.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,83 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Oddr.Models.Browsers;
+using Oddr.Models.Devices;
+using System.Runtime.CompilerServices;
+using OSModel = Oddr.Models.OS;
+
+namespace Oddr.Models
+{
+ /// <summary>
+ /// Extends ODDRHTTPEvidence. Contains the reference to identified Bowser, Device and OperatingSystem.
+ /// It can be used to retrieve back model object in order to get properties directly.
+ /// </summary>
+ public class BufferedODDRHTTPEvidence : ODDRHTTPEvidence
+ {
+ /// <summary>
+ /// Identified Browser object. Null if not yet identified.
+ /// </summary>
+ public Browser browserFound
+ {
+ [MethodImpl(MethodImplOptions.Synchronized)]
+ get;
+ [MethodImpl(MethodImplOptions.Synchronized)]
+ set;
+ }
+
+ /// <summary>
+ /// Identified Device object. Null if not yet identified.
+ /// </summary>
+ public Device deviceFound
+ {
+ [MethodImpl(MethodImplOptions.Synchronized)]
+ get;
+ [MethodImpl(MethodImplOptions.Synchronized)]
+ set;
+ }
+
+ /// <summary>
+ /// Identified OperatingSystem object. Null if not yet identified.
+ /// </summary>
+ public OSModel.OperatingSystem osFound
+ {
+ [MethodImpl(MethodImplOptions.Synchronized)]
+ get;
+ [MethodImpl(MethodImplOptions.Synchronized)]
+ set;
+ }
+
+ /// <summary>
+ /// When Evidence change, stored model object are removed in order to allow new identification.
+ /// </summary>
+ /// <param name="key">Header name</param>
+ /// <param name="value">Header value</param>
+ public override void Put(String key, String value) {
+ this.osFound = null;
+ this.browserFound = null;
+ base.Put(key, value);
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BuiltObject.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BuiltObject.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BuiltObject.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/BuiltObject.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,99 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Oddr.Models
+{
+ /// <summary>
+ /// Superclass of identified model object.
+ /// </summary>
+ public class BuiltObject
+ {
+ /// <summary>
+ /// Confidence of identified model object.
+ /// </summary>
+ public int confidence
+ {
+ set;
+ get;
+ }
+ /// <summary>
+ /// Dictionary of properties of identified model object.
+ /// </summary>
+ public Dictionary<String, String> properties
+ {
+ protected set;
+ get;
+ }
+
+ public BuiltObject() {
+ this.properties = new Dictionary<String, String>();
+ this.confidence = 0;
+ }
+
+ public BuiltObject(int confidence, Dictionary<String, String> properties)
+ {
+ this.confidence = confidence;
+ this.properties = properties;
+ }
+
+ public BuiltObject(Dictionary<String, String> properties)
+ {
+ this.confidence = 0;
+ this.properties = properties;
+ }
+
+ /// <summary>
+ /// Retrieve a property from properties dictionary.
+ /// </summary>
+ /// <param name="property">The name of requested properties.</param>
+ /// <returns>Return the value of requested property.</returns>
+ public String Get(String property)
+ {
+ if (properties.ContainsKey(property))
+ {
+ return properties[property];
+ }
+ return null;
+ }
+
+ /// <summary>
+ /// Add a property to properties dictionary.
+ /// </summary>
+ /// <param name="name">The name of the property.</param>
+ /// <param name="value">The value of the property.</param>
+ public void PutProperty(String name, String value) {
+ this.properties[name] = value;
+ }
+
+ public void PutPropertiesMap(Dictionary<String, String> properties)
+ {
+ foreach (KeyValuePair<string, string> kvp in properties)
+ {
+ this.properties[kvp.Key] = kvp.Value;
+ }
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Devices/Device.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Devices/Device.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Devices/Device.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/Devices/Device.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,85 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Oddr.Models.Devices
+{
+ /// <summary>
+ /// Identified Device model object.
+ /// </summary>
+ public class Device : BuiltObject, IComparable, ICloneable
+ {
+ public String id
+ {
+ get;
+ set;
+ }
+ public String parentId
+ {
+ get;
+ set;
+ }
+
+ public Device()
+ : base()
+ {
+ this.parentId = "root";
+ }
+
+ public int CompareTo(object obj)
+ {
+ if (obj == null || !(obj is Device))
+ {
+ return int.MaxValue;
+ }
+
+ Device bd = (Device) obj;
+ return this.confidence - bd.confidence;
+ }
+
+ public object Clone()
+ {
+ Device d = new Device();
+ d.id = this.id;
+ d.parentId = this.parentId;
+ d.confidence = this.confidence;
+ d.PutPropertiesMap(this.properties);
+ return d;
+ }
+
+ public bool ContainsProperty(String propertyName)
+ {
+ try
+ {
+ return properties.ContainsKey(propertyName);
+
+ }
+ catch (ArgumentNullException ex)
+ {
+ return false;
+ }
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRHTTPEvidence.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRHTTPEvidence.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRHTTPEvidence.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRHTTPEvidence.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,76 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using W3c.Ddr.Simple;
+
+namespace Oddr.Models
+{
+ /// <summary>
+ /// Evidence consisting of HTTP Header name and value pairs.
+ /// </summary>
+ public class ODDRHTTPEvidence : IEvidence
+ {
+ /// <summary>
+ /// Headers dictionary.
+ /// </summary>
+ Dictionary<String, String> headers;
+
+ /// <summary>
+ /// Empty constructor.
+ /// </summary>
+ public ODDRHTTPEvidence() {
+ headers = new Dictionary<String, String>();
+ }
+
+ /// <summary>
+ /// Headers Dictionary parameterizable costructor.
+ /// </summary>
+ /// <param name="map">Headers Dictionary.</param>
+ public ODDRHTTPEvidence(Dictionary<String, String> map) {
+ headers = new Dictionary<String, String>(map);
+ }
+
+ public bool Exist(string property)
+ {
+ if (property == null)
+ {
+ return false;
+ }
+ return headers.ContainsKey(property.ToLower());
+ }
+
+ public String Get(String header)
+ {
+ string toRet = null;
+ headers.TryGetValue(header.ToLower(), out toRet);
+ return toRet;
+ }
+
+ public virtual void Put(String key, String value)
+ {
+ headers.Add(key.ToLower(), value);
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyName.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyName.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyName.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyName.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,52 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using W3c.Ddr.Simple;
+
+namespace Oddr.Models
+{
+ public class ODDRPropertyName : IPropertyName
+ {
+
+ private String localPropertyName;
+ private String nameSpace;
+
+ public ODDRPropertyName(String localPropertyName, String nameSpace)
+ {
+ this.localPropertyName = localPropertyName;
+ this.nameSpace = nameSpace;
+ }
+
+ public string LocalPropertyName()
+ {
+ return this.localPropertyName;
+ }
+
+ public string Namespace()
+ {
+ return this.nameSpace;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyRef.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyRef.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyRef.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyRef.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,84 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using W3c.Ddr.Simple;
+
+namespace Oddr.Models
+{
+ public class ODDRPropertyRef : IPropertyRef
+ {
+ private readonly IPropertyName propertyName;
+ private readonly String aspectName;
+
+ public ODDRPropertyRef(IPropertyName propertyName, String aspectName)
+ {
+ this.propertyName = propertyName;
+ this.aspectName = aspectName;
+ }
+
+ public string AspectName()
+ {
+ return aspectName;
+ }
+
+ public string LocalPropertyName()
+ {
+ return propertyName.LocalPropertyName();
+ }
+
+ public string Namespace()
+ {
+ return propertyName.Namespace();
+ }
+
+ public string NullAspect()
+ {
+ return "__NULL";
+ }
+
+ public override bool Equals(object obj)
+ {
+ // If parameter is null return false.
+ if (obj == null)
+ {
+ return false;
+ }
+
+ // If parameter cannot be cast to Point return false.
+ ODDRPropertyRef oddr = obj as ODDRPropertyRef;
+ if ((System.Object)oddr == null)
+ {
+ return false;
+ }
+
+ return (oddr.AspectName().Equals(this.aspectName) && oddr.LocalPropertyName().Equals(this.LocalPropertyName()) && oddr.Namespace().Equals(this.Namespace()));
+ }
+
+ public override int GetHashCode()
+ {
+ int hash = 3;
+ hash = 73 * hash + (this.propertyName != null ? this.propertyName.GetHashCode() : 0);
+ hash = 73 * hash + (this.aspectName != null ? this.aspectName.GetHashCode() : 0);
+ return hash;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValue.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValue.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValue.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValue.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,235 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using W3c.Ddr.Simple;
+using W3c.Ddr.Exceptions;
+
+namespace Oddr.Models
+{
+ public class ODDRPropertyValue : IPropertyValue
+ {
+ private const String TYPE_BOOLEAN = "xs:boolean";
+ private const String TYPE_DOUBLE = "xs:double";
+ private const String TYPE_ENUMERATION = "xs:enumeration";
+ private const String TYPE_FLOAT = "xs:float";
+ private const String TYPE_INT = "xs:integer";
+ private const String TYPE_NON_NEGATIVE_INTEGER = "xs:nonNegativeInteger";
+ private const String TYPE_LONG = "xs:long";
+ private readonly String value;
+ private readonly String type;
+ private readonly IPropertyRef propertyRef;
+
+ public ODDRPropertyValue(String value, String type, IPropertyRef propertyRef)
+ {
+ this.value = value == null ? value : value.Trim();
+ this.type = type;
+ this.propertyRef = propertyRef;
+ }
+
+ public bool Exists()
+ {
+ if (value != null && value.Length > 0 && !"-".Equals(value))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public bool GetBoolean()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+ if (type.Equals(TYPE_BOOLEAN))
+ {
+ try
+ {
+ return bool.Parse(value);
+
+ }
+ catch (FormatException ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, "Not " + TYPE_BOOLEAN + " value");
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public double GetDouble()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+
+ if (type.Equals(TYPE_DOUBLE) || type.Equals(TYPE_FLOAT))
+ {
+ try
+ {
+ return double.Parse(value);
+
+ }
+ catch (FormatException ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, "Not " + TYPE_DOUBLE + " value");
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public string[] GetEnumeration()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+
+ if (type.Equals(TYPE_ENUMERATION))
+ {
+ try
+ {
+ String[] splitted = value.Split(",".ToCharArray());
+ for (int i = 0; i < splitted.Length; i++)
+ {
+ splitted[i] = splitted[i].Trim();
+ }
+
+ return splitted;
+
+ }
+ catch (Exception ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, "Not " + TYPE_ENUMERATION + " value");
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public float GetFloat()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+
+ if (type.Equals(TYPE_FLOAT))
+ {
+ try
+ {
+ return float.Parse(value);
+
+ }
+ catch (FormatException ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, "Not " + TYPE_FLOAT + " value");
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public int GetInteger()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+
+ if (type.Equals(TYPE_INT))
+ {
+ try
+ {
+ return int.Parse(value);
+
+ }
+ catch (FormatException ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+
+ if (type.Equals(TYPE_NON_NEGATIVE_INTEGER))
+ {
+ try
+ {
+ int integer = int.Parse(value);
+
+ if (integer >= 0)
+ {
+ return integer;
+ }
+
+ }
+ catch (FormatException ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, "Not " + TYPE_INT + " value");
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public long GetLong()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+ if (type.Equals(TYPE_LONG) || type.Equals(TYPE_INT) || type.Equals(TYPE_NON_NEGATIVE_INTEGER))
+ {
+ try
+ {
+ return long.Parse(value);
+
+ }
+ catch (FormatException ex)
+ {
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, ex);
+ }
+ }
+ throw new ValueException(ValueException.INCOMPATIBLE_TYPES, "Not " + TYPE_LONG + " value");
+ }
+
+ /// <exception cref="W3c.Ddr.Exceptions.ValueException">Thrown when...</exception>
+ public string GetString()
+ {
+ if (!Exists())
+ {
+ throw new ValueException(ValueException.NOT_KNOWN, type);
+ }
+ return value;
+ }
+
+ public IPropertyRef PropertyRef()
+ {
+ return this.propertyRef;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValues.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValues.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValues.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/ODDRPropertyValues.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,70 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using W3c.Ddr.Simple;
+
+namespace Oddr.Models
+{
+ public class ODDRPropertyValues : IPropertyValues
+ {
+ List<IPropertyValue> properties;
+
+ public ODDRPropertyValues()
+ {
+ this.properties = new List<IPropertyValue>();
+ }
+
+ public void addProperty(IPropertyValue v)
+ {
+ properties.Add(v);
+ }
+
+
+ public IPropertyValue[] GetAll()
+ {
+ try
+ {
+ return properties.ToArray();
+
+ }
+ catch (ArgumentNullException ex)
+ {
+ return new IPropertyValue[0];
+ }
+ }
+
+ public IPropertyValue GetValue(IPropertyRef pr)
+ {
+ foreach (IPropertyValue propertyValue in properties)
+ {
+ if (propertyValue.PropertyRef().Equals(pr))
+ {
+ return propertyValue;
+ }
+ }
+ return null;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/OS/OperatingSystem.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/OS/OperatingSystem.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/OS/OperatingSystem.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/OS/OperatingSystem.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,181 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Oddr.Models.OS
+{
+ /// <summary>
+ /// Identified OperatingSystem model object.
+ /// </summary>
+ public class OperatingSystem : BuiltObject, IComparable, ICloneable
+ {
+ public String majorRevision
+ {
+ get;
+ set;
+ }
+ public String minorRevision
+ {
+ get;
+ set;
+ }
+ public String microRevision
+ {
+ get;
+ set;
+ }
+ public String nanoRevision
+ {
+ get;
+ set;
+ }
+
+ public OperatingSystem() : base()
+ {
+ this.majorRevision = "0";
+ this.minorRevision = "0";
+ this.microRevision = "0";
+ this.nanoRevision = "0";
+ }
+
+ public OperatingSystem(Dictionary<String, String> properties) : base(properties)
+ {
+ this.majorRevision = "0";
+ this.minorRevision = "0";
+ this.microRevision = "0";
+ this.nanoRevision = "0";
+ }
+
+ public String GetId()
+ {
+ if (GetModel() == null || GetVendor() == null) {
+ return null;
+ }
+ String id = GetVendor() + "." + GetModel() + "." + this.majorRevision + "." + this.minorRevision + "." + this.microRevision + "." + this.nanoRevision;
+ return id;
+ }
+
+ //GETTERS
+ //utility getter for significant oddr OS properties
+ public String GetModel()
+ {
+ return Get("model");
+ }
+
+ public String GetVendor()
+ {
+ return Get("vendor");
+ }
+
+ public String GetVersion()
+ {
+ return Get("version");
+ }
+
+ public String GetBuild()
+ {
+ return Get("build");
+ }
+
+ public String GetDescription()
+ {
+ return Get("description");
+ }
+
+ //SETTERS
+ //utility setter for significant oddr OS properties
+ public void SetModel(String model)
+ {
+ PutProperty("model", model);
+ }
+
+ public void SetVendor(String vendor)
+ {
+ PutProperty("vendor", vendor);
+ }
+
+ public void SetVersion(String version)
+ {
+ PutProperty("version", version);
+ }
+
+ public void SetBuild(String build)
+ {
+ PutProperty("build", build);
+ }
+
+ public void SetDescription(String description)
+ {
+ PutProperty("description", description);
+ }
+
+ //Comparable
+ public int CompareTo(object obj)
+ {
+ if (obj == null || !(obj is OperatingSystem)) {
+ return int.MaxValue;
+ }
+
+ OperatingSystem bd = (OperatingSystem) obj;
+ return this.confidence - bd.confidence;
+ }
+
+ // Cloneable
+ public object Clone()
+ {
+ OperatingSystem os = new OperatingSystem();
+ os.majorRevision = this.majorRevision;
+ os.minorRevision = this.minorRevision;
+ os.microRevision = this.microRevision;
+ os.nanoRevision = this.nanoRevision;
+ os.confidence = this.confidence;
+ os.PutPropertiesMap(this.properties);
+ return os;
+ }
+
+ //Utility
+ public override string ToString()
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.Append(GetVendor());
+ sb.Append(" ");
+ sb.Append(GetModel());
+
+ if (GetDescription() != null && GetDescription().Length > 0) {
+ sb.Append("(");
+ sb.Append(GetDescription());
+ sb.Append(GetVersion()).Append(")");
+ }
+
+ sb.Append(" [").Append(this.majorRevision).Append(".").Append(this.minorRevision).Append(".").Append(this.microRevision).Append(".").Append(this.nanoRevision).Append("]");
+ if (GetBuild() != null) {
+ sb.Append(" - ").Append(GetBuild());
+ }
+ sb.Append(" ").Append(this.confidence).Append("%");
+ String toRet = sb.ToString();
+ return toRet;
+ }
+ }
+}
Added: incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/UserAgent.cs
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/UserAgent.cs?rev=1392912&view=auto
==============================================================================
--- incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/UserAgent.cs (added)
+++ incubator/devicemap/trunk/openddr/csharp/OpenDDR-CSharp/Models/UserAgent.cs Tue Oct 2 13:34:31 2012
@@ -0,0 +1,244 @@
+/**
+ * Copyright 2011 OpenDDR LLC
+ * This software is distributed under the terms of the GNU Lesser General Public License.
+ *
+ *
+ * This file is part of OpenDDR Simple APIs.
+ * OpenDDR Simple APIs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * OpenDDR Simple APIs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace Oddr.Models
+{
+ public class UserAgent
+ {
+
+ public const String MOZILLA_AND_OPERA_PATTERN = "(.*?)((?:Mozilla)|(?:Opera))[/ ](\\d+\\.\\d+).*?\\(((?:.*?)(?:.*?\\(.*?\\))*(?:.*?))\\)(.*)";
+ private const String VERSION_PATTERN = ".*Version/(\\d+.\\d+).*";
+ public const int INDEX_MOZILLA_PATTERN_GROUP_PRE = 1;
+ public const int INDEX_MOZILLA_PATTERN_GROUP_INSIDE = 4;
+ public const int INDEX_MOZILLA_PATTERN_GROUP_POST = 5;
+ public const int INDEX_MOZILLA_PATTERN_GROUP_MOZ_VER = 3;
+ public const int INDEX_OPERA_OR_MOZILLA = 2;
+ private static Regex mozillaPatternCompiled = new Regex(MOZILLA_AND_OPERA_PATTERN, RegexOptions.Compiled);
+ private static Regex versionPatternCompiled = new Regex(VERSION_PATTERN, RegexOptions.Compiled);
+ private static Regex iPadRegex = new Regex(".*(?!like).iPad.*", RegexOptions.Compiled);
+ private static Regex iPodRegex = new Regex(".*(?!like).iPod.*", RegexOptions.Compiled);
+ private static Regex iPhoneRegex = new Regex(".*(?!like).iPhone.*", RegexOptions.Compiled);
+ private static Regex blackBerryRegex = new Regex(".*[Bb]lack.?[Bb]erry.*|.*RIM.?Tablet.?OS.*", RegexOptions.Compiled);
+ private static Regex symbianRegex = new Regex(".*Symbian.*|.*SymbOS.*|.*Series.?60.*", RegexOptions.Compiled);
+ private static Regex windowsRegex = new Regex(".*Windows.?(?:(?:CE)|(?:Phone)|(?:NT)|(?:Mobile)).*", RegexOptions.Compiled);
+ private static Regex internetExplorerRegex = new Regex(".*MSIE.([0-9\\.b]+).*", RegexOptions.Compiled);
+ public String completeUserAgent
+ {
+ private set;
+ get;
+ }
+
+ public bool mozillaPattern
+ {
+ private set;
+ get;
+ }
+
+ public bool operaPattern
+ {
+ private set;
+ get;
+ }
+
+ public String mozillaVersion
+ {
+ private set;
+ get;
+ }
+
+ public String operaVersion
+ {
+ private set;
+ get;
+ }
+
+ public bool containsAndroid
+ {
+ private set;
+ get;
+ }
+
+ public bool containsBlackBerryOrRim
+ {
+ private set;
+ get;
+ }
+
+ public bool containsIOSDevices
+ {
+ private set;
+ get;
+ }
+
+ public bool containsMSIE
+ {
+ private set;
+ get;
+ }
+
+ public bool containsSymbian
+ {
+ private set;
+ get;
+ }
+
+ public bool containsWindowsPhone
+ {
+ private set;
+ get;
+ }
+
+ private String[] patternElements;
+
+ internal UserAgent(String userAgent)
+ {
+ if (userAgent == null)
+ {
+ throw new ArgumentNullException("userAgent can not be null");
+ }
+ completeUserAgent = userAgent;
+
+ Match result = mozillaPatternCompiled.Match(userAgent);
+
+ if (result.Success)
+ {
+ patternElements = new String[]{
+ result.Groups[INDEX_MOZILLA_PATTERN_GROUP_PRE].Value,
+ result.Groups[INDEX_MOZILLA_PATTERN_GROUP_INSIDE].Value,
+ result.Groups[INDEX_MOZILLA_PATTERN_GROUP_POST].Value
+ };
+ String version = result.Groups[INDEX_MOZILLA_PATTERN_GROUP_MOZ_VER].Value;
+ if (result.Groups[INDEX_OPERA_OR_MOZILLA].Value.Contains("Opera"))
+ {
+ mozillaPattern = false;
+ operaPattern = true;
+ operaVersion = version;
+
+ if (operaVersion.Equals("9.80") && patternElements[2] != null)
+ {
+ Match result2 = versionPatternCompiled.Match(patternElements[2]);
+
+ if (result2.Success)
+ {
+ operaVersion = result2.Groups[1].Value;
+ }
+ }
+
+ }
+ else
+ {
+ mozillaPattern = true;
+ mozillaVersion = version;
+ }
+
+ }
+ else
+ {
+ mozillaPattern = false;
+ operaPattern = false;
+ patternElements = new String[]{
+ null,
+ null,
+ null};
+ mozillaVersion = null;
+ operaVersion = null;
+ }
+
+ if (userAgent.Contains("Android"))
+ {
+ containsAndroid = true;
+
+ }
+ else
+ {
+ containsAndroid = false;
+
+ if (iPadRegex.IsMatch(userAgent) || iPodRegex.IsMatch(userAgent) || iPhoneRegex.IsMatch(userAgent))
+ {
+ containsIOSDevices = true;
+
+ }
+ else
+ {
+ containsIOSDevices = false;
+ if (blackBerryRegex.IsMatch(userAgent))
+ {
+ containsBlackBerryOrRim = true;
+
+ }
+ else
+ {
+ containsBlackBerryOrRim = false;
+ if (symbianRegex.IsMatch(userAgent))
+ {
+ containsSymbian = true;
+
+ }
+ else
+ {
+ containsSymbian = false;
+ if (windowsRegex.IsMatch(userAgent))
+ {
+ containsWindowsPhone = true;
+
+ }
+ else
+ {
+ containsWindowsPhone = false;
+ }
+
+ if (internetExplorerRegex.IsMatch(userAgent))
+ {
+ containsMSIE = true;
+
+ }
+ else
+ {
+ containsMSIE = false;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ public String GetPatternElementsPre()
+ {
+ return patternElements[0];
+ }
+
+ public String GetPatternElementsInside()
+ {
+ return patternElements[1];
+ }
+
+ public String GetPatternElementsPost()
+ {
+ return patternElements[2];
+ }
+ }
+}