#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.Text ''' ''' XML Parser ''' ''' eberhard speer jr. ''' Apache's DeviceMap Project .Net version
''' ported from Reza Naghibi's XMLParser.java
Friend NotInheritable Class XmlParser ' Private inStream As IO.StreamReader Private pre As Char = ChrW(0) #Region "Properties" ''' ''' Returns next XML tag in StreamReader ''' ''' String ''' - Public ReadOnly Property NextTag() As String Get Dim localBuilder As New StringBuilder() Dim i As Integer Dim start As Boolean = False If pre = "<"c Then localBuilder.Append(pre) pre = ChrW(0) start = True End If While (Util.InlineAssignHelper(i, inStream.Read())) <> -1 Dim c As Char = ChrW(i) If c = "<"c Then start = True localBuilder.Append(c) ElseIf c = ">"c Then localBuilder.Append(c) Exit While ElseIf start Then localBuilder.Append(c) End If End While Return localBuilder.ToString() End Get End Property ''' ''' Returns XML tag value from StreamReader ''' ''' String ''' - Public ReadOnly Property TagValue() As String Get Dim localBuilder As New StringBuilder() Dim i As Integer While (Util.InlineAssignHelper(i, inStream.Read())) <> -1 Dim c As Char = ChrW(i) If c = "<"c Then pre = "<"c Exit While Else localBuilder.Append(c) End If End While Return localBuilder.ToString.Trim() End Get End Property #End Region ' Properties #Region "Constructor" ''' ''' Prevent parameterless new ''' ''' - Private Sub New() ' Nice ! End Sub ''' ''' New XmlParser for StreamReader ''' ''' StreamReader ''' - Public Sub New(stream As IO.StreamReader) inStream = stream End Sub #End Region ' Constructor #Region "Functions" ''' ''' Returns Attribute (Device property) value of tag with name ''' ''' XML tag ''' Attribute name ''' String ''' - Public Shared Function getAttribute(tag As String, name As String) As String Dim retpos As Integer = tag.ToLower.IndexOf(name.ToLower() & "=") If retpos = -1 Then Return "" End If Dim result As String = tag.Substring(retpos + name.Length + 1) If result.StartsWith("""") Then result = result.Substring(1) Dim endpos As Integer = result.IndexOf("""") If endpos = -1 Then Return "" End If result = result.Substring(0, endpos) Else Dim endpos As Integer = result.IndexOf(" ") If endpos = -1 Then Return "" End If result = result.Substring(0, endpos) End If Return result End Function #End Region ' Functions End Class