#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
'
' 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.
#End Region ' Header
'
Imports System.Configuration
Imports System.IO
'''
''' Load Device and Pattern data from StreamReader
'''
''' eberhard speer jr.
''' -
Friend NotInheritable Class Loader
'
Private Property deviceList As IDictionary(Of String, Device)
#Region "Properties"
'''
''' Returns Device data dictionary
'''
''' IDictionary(Of String, Device)
''' -
Public ReadOnly Property Devices() As IDictionary(Of String, Device)
Get
Return deviceList
End Get
End Property
#End Region ' Properties
#Region "Constructor"
'''
''' Default new Device data Loader
'''
''' Thrown when (InnerException)
''' - NullReferenceException : DeviceMap ConnectionStrings missing in config file
''' - WebException : URL Loader exception
''' - ArgumentException : File Loader exception
''' - ArgumentException : Loader exception
'''
'''
''' -
Public Sub New()
deviceList = New Dictionary(Of String, Device)
Try
Dim folder As String = ConfigurationManager.ConnectionStrings(Constants.APP_NAME).ToString.Trim.ToLowerInvariant
If folder.StartsWith(Constants.HTTP_PREFIX) Then
folder = folder.TrimEnd(CChar("/"))
' Devices
For Each xmlFile In {Constants.DEVICE_DATA, Constants.DEVICE_DATA_PATCH}
LoadDeviceData(New UrlLoader(String.Format("{0}/{1}", folder, xmlFile)).Reader)
Next
' Patterns
For Each xmlFile In {Constants.BUILDER_DATA, Constants.BUILDER_DATA_PATCH}
LoadDevicePatterns(New UrlLoader(String.Format("{0}/{1}", folder, xmlFile)).Reader)
Next
Else
folder = folder.TrimEnd(CChar("\"))
' Devices
For Each xmlFile In {Constants.DEVICE_DATA, Constants.DEVICE_DATA_PATCH}
LoadDeviceData(New FileLoader(String.Format("{0}/{1}", folder, xmlFile)).Reader)
Next
' Patterns
For Each xmlFile In {Constants.BUILDER_DATA, Constants.BUILDER_DATA_PATCH}
LoadDevicePatterns(New FileLoader(String.Format("{0}/{1}", folder, xmlFile)).Reader)
Next
End If
Catch ex As NullReferenceException
Throw New DeviceMapException(String.Format(Constants.CONFIG_ERROR_CONN_FORMAT, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile), ex)
Catch ex As System.Net.WebException
Throw New DeviceMapException(String.Format(Constants.WEB_ERROR_FORMAT, ex.Message), ex)
Catch ex As ArgumentException
Throw New DeviceMapException(ex.Message, ex)
Catch ex As Exception
Throw New DeviceMapException(ex.Message, ex)
End Try
End Sub
#End Region ' Constructor
#Region "Methods"
'''
''' Load Device data from StreamReader
'''
''' StreamReader
''' -
Private Sub LoadDeviceData(inSteam As IO.StreamReader)
Dim parser As New XmlParser(inSteam)
Dim tag As String = String.Empty
Try
Dim device As New Device()
Dim attributes As New Dictionary(Of String, String)
While (Util.InlineAssignHelper(tag, parser.NextTag)).Length > 0
If tag.StartsWith("") Then
If Not String.IsNullOrEmpty(device.Id) Then
attributes("id") = device.Id
device.Attributes = attributes
If Devices.ContainsKey(device.Id) Then
Devices(device.Id) = device
Else
Devices.Add(device.Id, device)
End If
End If
' reset
device = New Device()
attributes = New Dictionary(Of String, String)
ElseIf tag.StartsWith("
''' Load Device Pattern data from StreamReader
'''
''' StreamReader
'''
Private Sub LoadDevicePatterns(inStream As IO.StreamReader)
Dim parser As New XmlParser(inStream)
Dim tag As String = ""
Try
Dim builder As String = ""
Dim device As Device = Nothing
Dim id As String = ""
Dim patterns As New List(Of String)
While (Util.InlineAssignHelper(tag, parser.NextTag)).Length > 0
If tag.StartsWith("= 0 Then
builder = builder.Substring(builder.LastIndexOf(".") + 1)
End If
ElseIf tag.StartsWith("") Then
If device IsNot Nothing Then
If builder.Equals("TwoStepDeviceBuilder") Then
device.Patterns.AndPattern = patterns
Dim unigram As String = ""
For Each pattern As String In patterns
If pattern.Contains(unigram) Then
unigram = pattern
Else
unigram &= pattern
End If
Next
device.Patterns.Pattern = unigram
Else
device.Patterns.OrPattern = patterns
End If
If builder.Equals("SimpleDeviceBuilder") Then
device.Type = "simple"
Else
device.Type = "weak"
End If
Else
Util.Log("ERROR: device not found: '" & id & "'")
End If
' reset
device = Nothing
id = ""
patterns = New List(Of String)()
ElseIf tag.Equals("") Then
Dim pattern As String = Util.Normalize(parser.TagValue)
If String.IsNullOrEmpty(pattern) Then
Continue While
End If
patterns.Add(pattern)
End If
End While
Catch ex As Exception
Throw New ArgumentException(String.Format("loadDevicePatterns : {0}", ex.Message), ex)
End Try
End Sub
'''
''' Recursively add Device's Parent Attributes
'''
''' Device
''' -
Private Sub MergeParent(device As Device)
Dim parentId As String = device.ParentId
If String.IsNullOrEmpty(parentId) Then
Return
End If
Dim parent As Device = Nothing
If Not deviceList.TryGetValue(parentId, parent) Then
Return
End If
MergeParent(parent)
For Each key As String In parent.Attributes.Keys
If Not device.Attributes.ContainsKey(key) Then
device.Attributes(key) = parent.Attributes(key)
End If
Next
End Sub
'''
''' Sets Parent device attributes
'''
''' -
Private Sub setParentAttributes()
For Each device As Device In deviceList.Values
MergeParent(device)
Next
End Sub
#End Region ' Methods
End Class