Author: esjr
Date: Thu Jul 11 16:55:43 2013
New Revision: 1502287
URL: http://svn.apache.org/r1502287
Log:
code cleanup and comments
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap.v11.suo
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Constants.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/DeviceMapClient.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Properties/AssemblyInfo.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Device.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Pattern.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/FileLoader.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/Loader.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/parser/XmlParser.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/util/Util.cs
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMapConsole/Program.cs
Modified: incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap.v11.suo
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap.v11.suo?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
Binary files - no diff available.
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Constants.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Constants.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Constants.cs
(original)
+++ incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Constants.cs
Thu Jul 11 16:55:43 2013
@@ -1,13 +1,27 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+#region "Header"
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
-namespace DeviceMap
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+#endregion
+/// <summary>
+/// Some Constants
+/// </summary>
+public sealed class Constants
{
- public sealed class Constants
- {
- public const string VERSION = "0.1";
- }
-}
+ public const string VERSION = "0.1";
+}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/DeviceMapClient.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/DeviceMapClient.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/DeviceMapClient.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/DeviceMapClient.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,44 +17,63 @@
specific language governing permissions and limitations
under the License.
*/
+#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's DeviceMapClient.java
+/// DeviceMap User-Agent Resolver
/// </summary>
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's DeviceMapClient.java</remarks>
public sealed class DeviceMapClient
{
+ //
internal IDictionary<string, Device> devices;
-
internal IDictionary<string, List<Device>> patterns;
+ #region "Properties"
+ /// <summary>
+ /// Returns number of Devices in Device data
+ /// </summary>
+ /// <returns>Integer</returns>
+ /// <remarks>-</remarks>
public int DeviceCount
{
- get
- {
- return this.devices.Count;
- }
+ get { return devices.Count; }
}
-
+ /// <summary>
+ /// Returns number of Device Patterns in Device pattern data
+ /// </summary>
+ /// <returns>Integer</returns>
+ /// <remarks>-</remarks>
public int PatternCount
{
- get
- {
- return this.patterns.Count;
- }
+ get { return patterns.Count; }
}
+ #endregion
+ #region "Constructor"
+ /// <summary>
+ /// New Device User-Agent Resolver
+ /// </summary>
+ /// <remarks>-</remarks>
public DeviceMapClient()
{
- this.devices = new Dictionary<string, Device>();
- this.patterns = new Dictionary<string, List<Device>>();
+ devices = new Dictionary<string, Device>();
+ patterns = new Dictionary<string, List<Device>>();
}
+ #endregion
- private void createIndex()
+ #region "Methods"
+ /// <summary>
+ /// Create Device and Pattern Index
+ /// </summary>
+ /// <remarks>-</remarks>
+ private void CreateIndex()
{
foreach (Device device in devices.Values)
{
@@ -62,7 +82,7 @@ public sealed class DeviceMapClient
for (int i = 0; i <= patternset.Count - 1; i++)
{
string pattern = patternset[i];
-
+ // deal with duplicates
if (patterns.ContainsKey(pattern))
{
if (i == (patternset.Count - 1) &&
!patterns[pattern].Contains(device))
@@ -72,15 +92,15 @@ public sealed class DeviceMapClient
}
else
{
- List<Device> solo = new List<Device>();
- solo.Add(device);
+ List<Device> subList = new List<Device>();
+ subList.Add(device);
if (patterns.ContainsKey(pattern))
{
- patterns[pattern] = solo;
+ patterns[pattern] = subList;
}
else
{
- patterns.Add(pattern, solo);
+ patterns.Add(pattern, subList);
}
}
@@ -88,27 +108,36 @@ public sealed class DeviceMapClient
}
}
}
-
-
- public void loadFromFolder(string folder)
- {
- this.devices = FileLoader.dataFolder(folder);
- this.createIndex();
- }
-
-
+ /// <summary>
+ /// Load Device and Pattern data from XML files in folder
+ /// </summary>
+ /// <param name="folder">Path the XML resource files</param>
+ /// <remarks>-</remarks>
+ public void LoadFromFolder(string folder)
+ {
+ devices = FileLoader.DataFolder(folder);
+ CreateIndex();
+ }
+ #endregion
+
+ #region "Functions"
+ /// <summary>
+ /// Main Resolver function : Returns Attribute dictionary for device
resolved from useragent
+ /// </summary>
+ /// <param name="useragent">user-agnet string to resolve</param>
+ /// <returns>IDictionary(Of String, String)</returns>
+ /// <remarks>-</remarks>
public IDictionary<string, string> Map(string useragent)
{
if (string.IsNullOrEmpty(useragent))
{
return null;
}
-
Dictionary<string, IList<Device>> hits = new Dictionary<string,
IList<Device>>();
Device winner = null;
string winnerStr = string.Empty;
+ //' added ;
string[] parts = Regex.Split(useragent, "
|-|_|/|\\\\|\\[|\\]|\\(|\\)|;");
-
for (int i = 0; i <= parts.Length - 1; i++)
{
if (!string.IsNullOrEmpty(parts[i]))
@@ -117,16 +146,18 @@ public sealed class DeviceMapClient
int j = 0;
while (j < 4 && (j + i) < parts.Length)
{
- pattern += Util.normalize(parts[i + j]);
+ pattern += Util.Normalize(parts[i + j]);
if (patterns.ContainsKey(pattern))
{
hits[pattern] = patterns[pattern];
+ //'For Each device As Device In dlist
+ //' Util.debugLog((("Hit found: " & pattern & " => ")
+ device.Id & " ") + device.Patterns)
+ //'Next
}
j += 1;
}
}
}
-
foreach (string hit in hits.Keys)
{
foreach (Device device in hits[hit])
@@ -152,9 +183,9 @@ public sealed class DeviceMapClient
winnerStr = hit;
}
}
+ //'Util.debugLog(("Hit candidate: " & hit & " => ") +
device.Id)
}
}
-
if (winner != null)
{
return winner.Attributes;
@@ -164,5 +195,5 @@ public sealed class DeviceMapClient
return null;
}
}
-
+ #endregion
}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Properties/AssemblyInfo.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Properties/AssemblyInfo.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Properties/AssemblyInfo.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/Properties/AssemblyInfo.cs
Thu Jul 11 16:55:43 2013
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision
Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.7.9.15")]
-[assembly: AssemblyFileVersion("1.7.9.15")]
+[assembly: AssemblyVersion("1.7.11.23")]
+[assembly: AssemblyFileVersion("1.7.11.23")]
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Device.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Device.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Device.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Device.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,89 +17,95 @@
specific language governing permissions and limitations
under the License.
*/
-using System;
+#endregion
using System.Collections.Generic;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's Device.java
+/// Device data
/// </summary>
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's Device.java</remarks>
internal sealed class Device
{
- private string deviceId;
-
- private string deviceParent;
-
- private string builderType;
-
+ //
+ private string builderType = string.Empty;
+ private string deviceId = string.Empty;
+ private string deviceParent = string.Empty;
private Pattern pattern;
-
private IDictionary<string, string> properties;
+ #region "Properties"
+ /// <summary>
+ /// Device property dictionary
+ /// </summary>
+ /// <returns>IDictionary(Of String, String)</returns>
+ /// <remarks>-</remarks>
public IDictionary<string, string> Attributes
{
- get
- {
- return this.properties;
- }
- set
- {
- this.properties = value;
- }
+ get { return properties; }
+ set { properties = value; }
}
-
+ /// <summary>
+ /// Unique Device Id
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
public string Id
{
- get
- {
- return this.deviceId;
- }
- set
- {
- this.deviceId = value;
- }
+ get { return deviceId; }
+ set { deviceId = value; }
}
-
+ /// <summary>
+ /// Device Unique Parent Id
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
public string ParentId
{
- get
- {
- return this.deviceParent;
- }
- set
- {
- this.deviceParent = value;
- }
+ get { return deviceParent; }
+ set { deviceParent = value; }
}
-
+ /// <summary cref="Pattern">
+ /// Device Pattern collection
+ /// </summary>
+ /// <returns>Pattern</returns>
+ /// <remarks>Collection of patterns for 'matching' with User-Agent
string</remarks>
public Pattern Patterns
{
- get
- {
- return this.pattern;
- }
+ get { return pattern; }
}
-
+ /// <summary>
+ /// Device Builder type
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>used to destinguish between 'simple' and 'two-step' device
builders</remarks>
public string Type
{
- get
- {
- return this.builderType;
- }
- set
- {
- this.builderType = value;
- }
+ get { return builderType; }
+ set { builderType = value; }
}
+ #endregion
+ #region "Constructor"
+ /// <summary>
+ /// Default new Device
+ /// </summary>
+ /// <remarks>-</remarks>
public Device()
{
- this.deviceParent = string.Empty;
- this.pattern = new Pattern();
+ pattern = new Pattern();
}
+ #endregion
+ #region "Functions"
+ /// <summary>
+ /// To String override
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
public override string ToString()
{
- string[] str = new string[] { "id='", this.deviceId, "',parentId='",
this.deviceParent, "',type='", this.builderType, "',pattern=",
Convert.ToString(this.pattern), ",attribytes=",
Convert.ToString(this.properties), "." };
- return string.Concat(str);
+ return string.Format("Id='{0}', ParentId='{1}', Type='{2}',
Pattern={3}, Attributes={4}", deviceId, deviceParent, builderType,
pattern.ToString(), properties.ToString());
}
+ #endregion
}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Pattern.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Pattern.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Pattern.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/data/Pattern.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,82 +17,113 @@
specific language governing permissions and limitations
under the License.
*/
-using System;
-using System.Collections;
+#endregion
using System.Collections.Generic;
+using System.Text;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's Pattern.java
+/// Device Pattern data
/// </summary>
-internal class Pattern
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's Pattern.java</remarks>
+internal sealed class Pattern
{
+ //
+ private IList<IList<string>> patternList;
- private IList<IList<string>> patterns;
-
- public Pattern()
- {
- patterns = new List<IList<string>>();
- }
-
- public override string ToString()
- {
- return patterns.ToString();
- }
-
- public virtual IList<string> AndPattern
- {
- set
- {
- this.patterns.Add(value);
- }
- }
-
- public virtual IList<string> OrPattern
+ #region "Properties"
+ /// <summary>
+ /// List of Patterns which <em>all</em> must occur in User-Agent string
for a match
+ /// </summary>
+ /// <remarks>-</remarks>
+ public IList<string> AndPattern
+ {
+ set { patternList.Add(value); }
+ }
+ /// <summary>
+ /// List of Patterns which <em>at least one</em> must occur in User-Agent
string for a match
+ /// </summary>
+ /// <remarks>-</remarks>
+ public IList<string> OrPattern
{
set
{
- foreach (string pattern in value)
+ foreach (string patternString in value)
{
- AddPattern = pattern;
+ AddPattern = patternString;
}
}
}
-
- public virtual string AddPattern
+ /// <summary>
+ /// List of Patterns to match with User-Agent string
+ /// </summary>
+ /// <remarks>-</remarks>
+ public string AddPattern
{
set
{
IList<string> subList = new List<string>();
subList.Add(value);
- patterns.Add(subList);
+ patternList.Add(subList);
}
}
-
- public virtual IList<IList<string>> Patterns
+ /// <summary>
+ /// List of Patterns Lists to match with User-Agent string
+ /// </summary>
+ /// <returns>IList(Of IList(Of String))</returns>
+ /// <remarks></remarks>
+ public IList<IList<string>> Patterns
+ {
+ get { return patternList; }
+ }
+ #endregion
+
+ #region "Constructor"
+ /// <summary>
+ /// Default new Device Pattern data
+ /// </summary>
+ /// <remarks>-</remarks>
+ public Pattern()
{
- get
- {
- return patterns;
- }
+ patternList = new List<IList<string>>();
}
+ #endregion
- public virtual bool isValid(List<string> patterns)
+ #region "Functions"
+ /// <summary>
+ /// Returns true if one of the patterns in patternList occurs in Device
Pattern data
+ /// </summary>
+ /// <param name="patternList">List(Of String)</param>
+ /// <returns>Boolean</returns>
+ /// <remarks>-</remarks>
+ public bool isValid(List<string> patternList)
+ {
+ bool found = false;
+ foreach (IList<string> patternset in Patterns) {
+ foreach (string pattern in patternset) {
+ if (!patternList.Contains(pattern)) {
+ goto patternsContinue;
+ }
+ }
+ found = true;
+ break;
+ patternsContinue: ;
+ }
+ return found;
+ }
+ /// <summary>
+ /// To String override
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
+ public override string ToString()
{
- bool found = false;
- foreach (IList<string> patternset in Patterns)
+ StringBuilder builder = new StringBuilder();
+ foreach (List<string> sublist in patternList)
{
- foreach (string pattern in patternset)
- {
- if (!patterns.Contains(pattern))
- {
- goto patternsContinue;
- }
- }
- found = true;
- break;
- patternsContinue: ;
+ builder.AppendFormat("'{0}',", string.Join(",",
sublist.ToArray()));
}
- return found;
+ return builder.ToString().TrimEnd(',');
}
-
+ #endregion
}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/FileLoader.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/FileLoader.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/FileLoader.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/FileLoader.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,56 +17,64 @@
specific language governing permissions and limitations
under the License.
*/
-using System;
+#endregion
using System.Collections.Generic;
-using System.IO;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's FileLoader.java
+/// Load Device and Pattern data from XML resource files
/// </summary>
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's FileLoader.java</remarks>
internal sealed class FileLoader
{
- public FileLoader()
- {
- }
-
- public static IDictionary<string, Device> dataFolder(string folder)
+ /// <summary>
+ /// Load Device and Pattern data from XML resource files in folder
+ /// </summary>
+ /// <param name="folder">Path to XML resource files</param>
+ /// <returns>IDictionary(Of String, Device)</returns>
+ /// <remarks>-</remarks>
+ public static IDictionary<string, Device> DataFolder(string folder)
{
Loader loader = new Loader();
- string deviceData = Path.Combine(folder, "DeviceDataSource.xml");
- string devicePatch = Path.Combine(folder, "DeviceDataSourcePatch.xml");
- string builderData = Path.Combine(folder, "BuilderDataSource.xml");
- string builderPatch = Path.Combine(folder,
"BuilderDataSourcePatch.xml");
- using (StreamReader reader = new StreamReader(deviceData))
+
+ string deviceData = System.IO.Path.Combine(folder,
"DeviceDataSource.xml");
+ string devicePatch = System.IO.Path.Combine(folder,
"DeviceDataSourcePatch.xml");
+ string builderData = System.IO.Path.Combine(folder,
"BuilderDataSource.xml");
+ string builderPatch = System.IO.Path.Combine(folder,
"BuilderDataSourcePatch.xml");
+ // Device data
+ using (System.IO.StreamReader reader = new
System.IO.StreamReader(deviceData))
{
- loader.loadDeviceData(reader);
+ loader.LoadDeviceData(reader);
}
- if (File.Exists(devicePatch))
+ // Desktop data
+ if (!System.IO.File.Exists(devicePatch))
{
- using (StreamReader reader = new StreamReader(devicePatch))
- {
- loader.loadDeviceData(reader);
- }
+ Util.Log("WARNING: file not found " + devicePatch);
}
else
{
- Util.debugLog(string.Concat("WARNING: file not found ",
devicePatch));
+ using (System.IO.StreamReader reader = new
System.IO.StreamReader(devicePatch))
+ {
+ loader.LoadDeviceData(reader);
+ }
}
loader.setParentAttributes();
- using (StreamReader bin = new StreamReader(builderData))
+ // Device Patterns
+ using (System.IO.StreamReader reader = new
System.IO.StreamReader(builderData))
{
- loader.loadDevicePatterns(bin);
+ loader.LoadDevicePatterns(reader);
}
- if (File.Exists(devicePatch))
+ // Desktop Patterns
+ if (!System.IO.File.Exists(devicePatch))
{
- using (StreamReader reader = new StreamReader(builderPatch))
- {
- loader.loadDevicePatterns(reader);
- }
+ Util.Log("WARNING: file not found " + builderPatch);
}
else
{
- Util.debugLog(string.Concat("WARNING: file not found ",
builderPatch));
+ using (System.IO.StreamReader reader = new
System.IO.StreamReader(builderPatch))
+ {
+ loader.LoadDevicePatterns(reader);
+ }
}
return loader.Devices;
}
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/Loader.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/Loader.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/Loader.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/loaders/Loader.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,89 +17,102 @@
specific language governing permissions and limitations
under the License.
*/
+#endregion
using System;
-using System.Collections;
using System.Collections.Generic;
-using System.IO;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's Loader.java
+/// Load Device and Pattern data from XML StreamReader
/// </summary>
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's Loader.java</remarks>
internal sealed class Loader
{
+ //
private IDictionary<string, Device> deviceList;
+ #region "Properties"
+ /// <summary>
+ /// Returns Device data dictionary
+ /// </summary>
+ /// <returns>IDictionary(Of String, Device)</returns>
+ /// <remarks>-</remarks>
public IDictionary<string, Device> Devices
{
- get
- {
- return this.deviceList;
- }
+ get { return deviceList; }
}
+ #endregion
+ #region "Constructor"
+ /// <summary>
+ /// Devfault new Device data Loader
+ /// </summary>
+ /// <remarks>-</remarks>
public Loader()
{
- this.deviceList = new Dictionary<string, Device>();
- }
-
- private static T InlineAssignHelper<T>(ref T target, T value)
- {
- target = value;
- return value;
+ deviceList = new Dictionary<string, Device>();
}
+ #endregion
- public void loadDeviceData(StreamReader inSteam)
+ #region "Methods"
+ /// <summary>
+ /// Load Device data from StreamReader
+ /// </summary>
+ /// <param name="inSteam">StreamReader</param>
+ /// <remarks>-</remarks>
+ public void LoadDeviceData(System.IO.StreamReader inSteam)
{
XmlParser parser = new XmlParser(inSteam);
string tag = string.Empty;
try
{
Device device = new Device();
- IDictionary<string, string> attributes = new Dictionary<string,
string>();
- while (Loader.InlineAssignHelper<string>(ref tag,
parser.NextTag).Length > 0)
+ Dictionary<string, string> attributes = new Dictionary<string,
string>();
+ while ((Util.InlineAssignHelper(ref tag, parser.NextTag)).Length >
0)
{
if (tag.StartsWith("<device "))
{
device.Id = XmlParser.getAttribute(tag, "id");
device.ParentId = XmlParser.getAttribute(tag, "parentId");
}
- else if (!tag.Equals("</device>"))
+ else if (tag.Equals("</device>"))
{
- if (!tag.StartsWith("<property "))
- {
- continue;
- }
- string key = XmlParser.getAttribute(tag, "name");
- attributes[key] = XmlParser.getAttribute(tag, "value");
- }
- else
- {
- if (device.Id != null && !string.IsNullOrEmpty(device.Id))
+ if (!string.IsNullOrEmpty(device.Id))
{
attributes["id"] = device.Id;
device.Attributes = attributes;
- if (!this.Devices.ContainsKey(device.Id))
+ if (Devices.ContainsKey(device.Id))
{
- this.Devices.Add(device.Id, device);
+ Devices[device.Id] = device;
}
else
{
- this.Devices[device.Id] = device;
+ Devices.Add(device.Id, device);
}
}
+ // reset
device = new Device();
attributes = new Dictionary<string, string>();
}
+ else if (tag.StartsWith("<property "))
+ {
+ string key = XmlParser.getAttribute(tag, "name");
+ string value = XmlParser.getAttribute(tag, "value");
+ attributes[key] = value;
+ }
}
}
- catch (Exception exception)
+ catch (Exception ex)
{
- Exception ex = exception;
- Util.debugLog(string.Concat("ERROR: loadDeviceData ",
ex.ToString()), ex);
+ Util.Log("ERROR: loadDeviceData", ex);
}
}
-
- public void loadDevicePatterns(StreamReader inStream)
+ /// <summary>
+ /// Load Device Pattern data from StreamReader
+ /// </summary>
+ /// <param name="inStream">StreamReader</param>
+ /// <remarks></remarks>
+ public void LoadDevicePatterns(System.IO.StreamReader inStream)
{
XmlParser parser = new XmlParser(inStream);
string tag = "";
@@ -107,49 +121,29 @@ internal sealed class Loader
string builder = "";
Device device = null;
string id = "";
- IList<string> patterns = new List<string>();
- while (Loader.InlineAssignHelper<string>(ref tag,
parser.NextTag).Length > 0)
+ List<string> patterns = new List<string>();
+ while ((Util.InlineAssignHelper(ref tag, parser.NextTag)).Length >
0)
{
if (tag.StartsWith("<builder "))
{
builder = XmlParser.getAttribute(tag, "class");
- if (builder.LastIndexOf(".") < 0)
+ if (builder.LastIndexOf(".") >= 0)
{
- continue;
+ builder = builder.Substring(builder.LastIndexOf(".") +
1);
}
- builder =
builder.Substring(checked(builder.LastIndexOf(".") + 1));
}
else if (tag.StartsWith("<device "))
{
- device = this.Devices[XmlParser.getAttribute(tag, "id")];
- }
- else if (!tag.Equals("</device>"))
- {
- if (!tag.Equals("<value>"))
- {
- continue;
- }
- string pattern = Util.normalize(parser.TagValue);
- if (pattern.Length == 0)
- {
- continue;
- }
- patterns.Add(pattern);
+ device = Devices[XmlParser.getAttribute(tag, "id")];
}
- else
+ else if (tag.Equals("</device>"))
{
- if (device == null)
- {
- Util.debugLog(string.Concat("ERROR: device not found:
'", id, "'"));
- }
- else
+ if (device != null)
{
if (builder.Equals("TwoStepDeviceBuilder"))
{
device.Patterns.AndPattern = patterns;
-
string unigram = "";
-
foreach (string pattern in patterns)
{
if (pattern.Contains(unigram))
@@ -161,55 +155,64 @@ internal sealed class Loader
unigram += pattern;
}
}
-
device.Patterns.AddPattern = unigram;
}
else
{
device.Patterns.OrPattern = patterns;
}
-
-
- if (!builder.Equals("SimpleDeviceBuilder"))
+ if (builder.Equals("SimpleDeviceBuilder"))
{
- device.Type = "weak";
+ device.Type = "simple";
}
else
{
- device.Type = "simple";
+ device.Type = "weak";
}
}
+ else
+ {
+ Util.Log("ERROR: device not found: '" + id + "'");
+ }
+ // reset
device = null;
id = "";
patterns = new List<string>();
}
+ else if (tag.Equals("<value>"))
+ {
+ string pattern = Util.Normalize(parser.TagValue);
+ if (string.IsNullOrEmpty(pattern))
+ {
+ continue;
+ }
+ patterns.Add(pattern);
+ }
}
}
- catch (Exception exception)
+ catch (Exception ex)
{
- Exception ex = exception;
- Util.debugLog(string.Concat("ERROR: loadDevicePatterns ",
ex.ToString()), ex);
+ Util.Log("ERROR: loadDevicePatterns " + ex.ToString(), ex);
}
}
-
- private void mergeParent(Device device)
+ /// <summary>
+ /// Recursively add Device's Parent Attributes
+ /// </summary>
+ /// <param name="device">Device</param>
+ /// <remarks>-</remarks>
+ private void MergeParent(Device device)
{
string parentId = device.ParentId;
-
if (string.IsNullOrEmpty(parentId))
{
return;
}
-
Device parent = null;
-
if (!deviceList.TryGetValue(parentId, out parent))
{
return;
}
-
- mergeParent(parent);
-
+ MergeParent(parent);
foreach (string key in parent.Attributes.Keys)
{
if (!device.Attributes.ContainsKey(key))
@@ -218,29 +221,16 @@ internal sealed class Loader
}
}
}
-
-
/// <summary>
- /// Sets attributes from parents
+ /// Sets Parent device attributes
/// </summary>
+ /// <remarks>-</remarks>
public void setParentAttributes()
{
- IEnumerator<Device> enumerator = null;
- try
- {
- enumerator = this.deviceList.Values.GetEnumerator();
- while (enumerator.MoveNext())
- {
- Device device = enumerator.Current;
- this.mergeParent(device);
- }
- }
- finally
+ foreach (Device device in deviceList.Values)
{
- if (enumerator != null)
- {
- enumerator.Dispose();
- }
+ MergeParent(device);
}
}
+ #endregion
}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/parser/XmlParser.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/parser/XmlParser.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/parser/XmlParser.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/parser/XmlParser.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,124 +17,151 @@
specific language governing permissions and limitations
under the License.
*/
+#endregion
using System;
using System.IO;
using System.Text;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's XMLParser.java
+/// XML Parser
/// </summary>
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's XMLParser.java</remarks>
internal sealed class XmlParser
{
- private StreamReader inStream;
-
- private char pre;
-
+ //
+ private System.IO.StreamReader inStream;
+ private char pre = '\0';
+
+ #region "Properties"
+ /// <summary>
+ /// Returns next XML tag in StreamReader
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
public string NextTag
{
get
{
- int i = 0;
StringBuilder localBuilder = new StringBuilder();
+
+ int i = 0;
bool start = false;
- if (this.pre == '<')
+
+ if (pre == '<')
{
- localBuilder.Append(this.pre);
- this.pre = '\0';
+ localBuilder.Append(pre);
+ pre = '\0';
start = true;
}
- while (XmlParser.InlineAssignHelper<int>(ref i,
this.inStream.Read()) != -1)
+
+ while ((Util.InlineAssignHelper(ref i, inStream.Read())) != -1)
{
- char c = (char)i; // Strings.ChrW(i);
+ char c = (char)i;
if (c == '<')
{
start = true;
localBuilder.Append(c);
}
- else if (c != '>')
+ else if (c == '>')
{
- if (!start)
- {
- continue;
- }
localBuilder.Append(c);
+ break;
}
- else
+ else if (start)
{
localBuilder.Append(c);
- break;
}
}
+
return localBuilder.ToString();
}
}
-
+ /// <summary>
+ /// Returns XML tag value from StreamReader
+ /// </summary>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
public string TagValue
{
get
{
- int i = 0;
StringBuilder localBuilder = new StringBuilder();
- while (XmlParser.InlineAssignHelper<int>(ref i,
this.inStream.Read()) != -1)
+ int i = 0;
+ while ((Util.InlineAssignHelper(ref i, inStream.Read())) != -1)
{
char c = (char)i;
- if (c != '<')
+ if (c == '<')
{
- localBuilder.Append(c);
+ pre = '<';
+ break;
}
else
{
- this.pre = '<';
- break;
+ localBuilder.Append(c);
}
}
return localBuilder.ToString().Trim();
}
}
+ #endregion
+ #region "Constructor"
+ /// <summary>
+ /// Prevent parameterless new
+ /// </summary>
+ /// <remarks>-</remarks>
private XmlParser()
{
+ // Nice !
}
-
- public XmlParser(StreamReader str)
- {
- this.inStream = str;
- this.pre = '\0';
- }
-
+ /// <summary>
+ /// New XmlParser for StreamReader
+ /// </summary>
+ /// <param name="stream">StreamReader</param>
+ /// <remarks>-</remarks>
+ public XmlParser(System.IO.StreamReader stream)
+ {
+ inStream = stream;
+ }
+ #endregion
+
+ #region "Functions"
+ /// <summary>
+ /// Returns Attribute (Device property) value of tag with name
+ /// </summary>
+ /// <param name="tag">XML tag</param>
+ /// <param name="name">Attribute name</param>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
public static string getAttribute(string tag, string name)
{
- int retpos = tag.ToLower().IndexOf(string.Concat(name.ToLower(), "="));
+ int retpos = tag.ToLower().IndexOf(name.ToLower() + "=");
if (retpos == -1)
{
return "";
}
- string ret = tag.Substring(checked(checked(retpos + name.Length) + 1));
- if (!ret.StartsWith("\""))
+ string result = tag.Substring(retpos + name.Length + 1);
+ if (result.StartsWith("\""))
{
- int endpos = ret.IndexOf(" ");
+ result = result.Substring(1);
+ int endpos = result.IndexOf("\"");
if (endpos == -1)
{
return "";
}
- ret = ret.Substring(0, endpos);
+ result = result.Substring(0, endpos);
}
else
{
- ret = ret.Substring(1);
- int endpos = ret.IndexOf("\"");
+ int endpos = result.IndexOf(" ");
if (endpos == -1)
{
return "";
}
- ret = ret.Substring(0, endpos);
+ result = result.Substring(0, endpos);
}
- return ret;
- }
-
- private static T InlineAssignHelper<T>(ref T target, T value)
- {
- target = value;
- return value;
+ return result;
}
+ #endregion
}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/util/Util.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/util/Util.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/util/Util.cs
(original)
+++ incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMap/util/Util.cs
Thu Jul 11 16:55:43 2013
@@ -1,3 +1,4 @@
+#region "Header"
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
@@ -16,52 +17,73 @@
specific language governing permissions and limitations
under the License.
*/
+#endregion
using System;
using System.Text;
/// <summary>
-/// @author : eberhard speer jr.
-/// .Net version of Reza Naghibi's Util.java
+/// Utilities
/// </summary>
+/// <author>eberhard speer jr.</author>
+/// <remarks>Apache's DeviceMap Project .Net version
+/// ported from Reza Naghibi's Util.java</remarks>
internal sealed class Util
{
- public Util()
+ /// <summary>
+ /// Inline Assign Helper
+ /// </summary>
+ /// <remarks>-</remarks>
+ public static T InlineAssignHelper<T>(ref T target, T value)
{
+ target = value;
+ return value;
}
-
- public static void debugLog(string msg)
+ /// <summary>
+ /// Console debug messages
+ /// </summary>
+ /// <param name="msg">message</param>
+ /// <remarks>-</remarks>
+ public static void Log(string msg)
{
- Util.debugLog(msg, null);
+ Log(msg, null);
}
-
- public static void debugLog(string msg, Exception e)
+ /// <summary>
+ /// Console debug exception messages
+ /// </summary>
+ /// <param name="msg">message</param>
+ /// <param name="e">Exception</param>
+ /// <remarks>-</remarks>
+ public static void Log(string msg, Exception e)
{
- string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff");
- Console.WriteLine(string.Concat(date, " [dmapjclient] ", msg));
+ Console.WriteLine(string.Format("{0} - {1}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"), msg));
if (e != null)
{
+ Console.WriteLine("Exception");
Console.WriteLine(e.ToString());
Console.Write(e.StackTrace);
}
}
-
- public static string normalize(string p)
+ /// <summary>
+ /// Normailze pattern to letters and digits only
+ /// </summary>
+ /// <param name="dirty">string to normalize</param>
+ /// <returns>String</returns>
+ /// <remarks>-</remarks>
+ public static string Normalize(string dirty)
{
- if (p == null)
+ if (string.IsNullOrEmpty(dirty))
{
- return p;
+ return dirty;
}
- p = p.ToLower().Trim();
- p = p.Replace("[bb]", "b");
- StringBuilder ret = new StringBuilder();
- int length = checked(p.Length - 1);
- for (int i = 0; i <= length; i++)
+ dirty = dirty.ToLower().Trim().Replace("[bb]", "b");
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i <= dirty.Length - 1; i++)
{
- char? c = p[i];
- if (char.IsLetter((char)c) || char.IsDigit((char)c))
+ System.Nullable<char> c = dirty[i];
+ if (char.IsLetter(Convert.ToChar(c)) ||
char.IsDigit(Convert.ToChar(c)))
{
- ret.Append(c);
+ builder.Append(c);
}
}
- return ret.ToString();
+ return builder.ToString();
}
}
\ No newline at end of file
Modified:
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMapConsole/Program.cs
URL:
http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMapConsole/Program.cs?rev=1502287&r1=1502286&r2=1502287&view=diff
==============================================================================
---
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMapConsole/Program.cs
(original)
+++
incubator/devicemap/trunk/devicemapcsharp/DeviceMap/DeviceMapConsole/Program.cs
Thu Jul 11 16:55:43 2013
@@ -10,14 +10,14 @@ namespace DMapConsole
{
static void Main(string[] args)
{
- Console.WriteLine(string.Format("DeviceMap .Net Client {0}",
DeviceMap.Constants.VERSION));
+ Console.WriteLine(string.Format("DeviceMap .Net Client {0}",
Constants.VERSION));
DeviceMapClient client = new DeviceMapClient();
System.Diagnostics.Stopwatch stopWatch = new
System.Diagnostics.Stopwatch();
stopWatch.Start();
// set path to your path to OpenDDR XML resources
- client.loadFromFolder(("C:\\esdev\\Ddr\\OpenDDR\\resources"));
+ client.LoadFromFolder(("C:\\esdev\\Ddr\\OpenDDR\\resources"));
stopWatch.Stop();
Console.WriteLine(string.Format("Loaded {0} devices with {1}
patterns in {2} ms", client.DeviceCount.ToString(),
client.PatternCount.ToString(),
stopWatch.Elapsed.TotalMilliseconds.ToString()));