Imports System
Imports System.Text
Imports System.Diagnostics
Imports Xmlsq

'  
' * $Id: TestXmlsq.vb $
' * Last updated:
' *   $Date: 2021-07-17 09:13 $
' *   $Version: 1.0.0 $
' 

' Some tests using the XMLSQ .NET interface.
' * 
' * Requires `xmlsq` to be installed on your system: available from <http://cryptosys.net/xmlsq/>
' * Add a reference to `diXmlsqNet.dll`
' * 
' * Test files, e.g. `bookstore.xml`, are in `xmlsq-testfiles.zip`. These must be in the CWD.
' * 
' * This is a Console Application written for target .NET Framework 4.0 and above 
' * Please report any bugs to <https://cryptosys.net/contact>
' 

'****************************** LICENSE ***********************************
' * Copyright (C) 2020-21 David Ireland, DI Management Services Pty Limited.
' * All rights reserved. <www.di-mgt.com.au> <www.cryptosys.net>
' * The code in this module is licensed under the terms of the MIT license.  
' * For a copy, see <http://opensource.org/licenses/MIT>
'****************************************************************************
'


' Ported from C# to VB.NET using icsharpcode.net's SharpDevelop.

Namespace TestXmlsq
	Class TestXmlsq
        Private Const MIN_VERSION As Integer = 10000

		Private Shared Function quote_str(s As String) As String
			Return "'" & s & "'"
		End Function

		Private Shared Sub test_Bookstore()
			Dim n As Integer
			Dim s As String, query__1 As String
			Dim xmlfile As String

			xmlfile = "bookstore.xml"
			Console.WriteLine("FILE: {0}", xmlfile)
			Console.WriteLine("Get the root element...")
			query__1 = "/"
			s = Query.GetText(xmlfile, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(s)

			Console.WriteLine("Get text of 2nd title element...")
			query__1 = "(//title)[2]"
			s = Query.GetText(xmlfile, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(s)

			Console.WriteLine("Get count of book elements...")
			query__1 = "//book"
			n = Xmlsq.Query.Count(xmlfile, query__1)
			Console.WriteLine("COUNT: Query: {0}", query__1)
			Console.WriteLine(n)

			Console.WriteLine("Get text of first book element...")
			query__1 = "//book"
			s = Query.GetText(xmlfile, query__1)
			Console.WriteLine("GETTEXT: Query: {0}", query__1)
			Console.WriteLine(s)

			Console.WriteLine("Full Xpath query...")
			query__1 = "//book"
			s = Query.FullQuery(xmlfile, query__1)
			Console.WriteLine("FULLQUERY: Query: {0}", query__1)
			Console.WriteLine(s)

			Console.WriteLine("Get text of first title element...")
			query__1 = "//title"
			s = Query.GetText(xmlfile, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(s)

			Console.WriteLine("Full Xpath query...")
			query__1 = "//title"
			Console.WriteLine("Query: {0}", query__1)
			s = Query.FullQuery(xmlfile, query__1)
			Console.WriteLine(s)

			Console.WriteLine("Full Xpath query with `raw` option...")
			query__1 = "//title"
			Console.WriteLine("Query: {0}", query__1)
			s = Query.FullQuery(xmlfile, query__1, Query.Opts.Raw)
			Console.WriteLine(s)

			Console.WriteLine("Get text of 3rd title element (UTF-8-encoded)...")
			query__1 = "(//title)[3]"
			Console.WriteLine("Query: {0}", query__1)
			s = Query.GetText(xmlfile, query__1)
			Console.WriteLine(s)

			Console.WriteLine("Get text of 3rd title element asciified...")
			query__1 = "(//title)[3]"
			Console.WriteLine("Query: {0}", query__1)
			s = Query.GetText(xmlfile, query__1, Query.Opts.Asciify)
			Console.WriteLine(s)

			Console.WriteLine("Get text of 3rd title element converted to native .NET UTF-16 string...")
			query__1 = "(//title)[3]"
			Console.WriteLine("Query: {0}", query__1)
			s = Query.GetText(xmlfile, query__1)
			' Convert a string containing UTF-8 to a native .NET UTF-16 string
			s = Encoding.UTF8.GetString(Encoding.[Default].GetBytes(s))
			' Should now print OK in .NET
			Console.WriteLine(s)
		End Sub

		Private Shared Sub test_UseCount()
			Dim n As Integer
			Dim s As String, query__1 As String
			Dim xmlfile As String

			Console.WriteLine(vbLf & "USE THE COUNT TO QUERY EACH MATCHING ELEMENT...")

			xmlfile = "bookstore.xml"
			Console.WriteLine("FILE: {0}", xmlfile)

			Console.WriteLine("Use the count to query each matching element in turn...")
			query__1 = "//title"
			n = Xmlsq.Query.Count(xmlfile, query__1)
			Console.WriteLine("COUNT: Query: {0}", query__1)
			Console.WriteLine(n)

			For i As Integer = 1 To n
				' Compose query
				query__1 = String.Format("(//title)[{0}]", i)
				Console.WriteLine("Query: {0}", query__1)
				' then use it
				s = Query.GetText(xmlfile, query__1, Query.Opts.Asciify)

				Console.WriteLine(s)
			Next


		End Sub

		Private Shared Sub test_Errors()
			Dim n As Integer
			Dim s As String, query__1 As String
			Dim xmlfile As String

			Console.WriteLine(vbLf & "EXPECTING ERRORS...")

			xmlfile = "bookstore.xml"
			query__1 = "///badquery"
			Console.WriteLine("Query: {0}", query__1)
			Try
				s = Query.GetText(xmlfile, query__1)
			Catch e As XmlsqErrorException
				Console.WriteLine(e.Message)
			End Try

			xmlfile = "missing.xml"
			query__1 = "//title"
			Console.WriteLine("FILE: {0}", xmlfile)
			Console.WriteLine("Query: {0}", query__1)
			Try
				s = Query.GetText(xmlfile, query__1)
			Catch e As XmlsqErrorException
				Console.WriteLine(e.Message)
			End Try
			xmlfile = "notxml.txt"
			query__1 = "/a"
			Console.WriteLine("FILE: {0}", xmlfile)
			Console.WriteLine("Query: {0}", query__1)
			Try
				s = Query.GetText(xmlfile, query__1)
			Catch e As XmlsqErrorException
				Console.WriteLine(e.Message)
			End Try
			xmlfile = "missing.xml"
			query__1 = "//title"
			Console.WriteLine("FILE: {0}", xmlfile)
			Console.WriteLine("Query: {0}", query__1)
			Try
				n = Query.Count(xmlfile, query__1)
			Catch e As XmlsqErrorException
				Console.WriteLine(e.Message)
			End Try

		End Sub

		Private Shared Sub test_Empty()
			Dim n As Integer
			Dim s As String, query__1 As String

			Console.WriteLine(vbLf & "TESTING FOR EMPTY VALUES...")
			' Pass XML as a string
			Dim xml As String = "<a><b foo=''></b><c>test1</c><e /></a>"
			Console.WriteLine("xml={0}", xml)

			Console.WriteLine("Get text of element b (empty, so add quote delimiters to see something)...")
			query__1 = "a/b"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))

			Console.WriteLine("Similarly for empty element <e />...")
			query__1 = "//e"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))

			Console.WriteLine("And for empty attribute foo=""""...")
			query__1 = "//b/@foo"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))

			Console.WriteLine("But we get the same result if the element is missing...")
			query__1 = "//notthere"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))

			Console.WriteLine("And if the attribute is not there...")
			query__1 = "//b/@baz"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))

			Console.WriteLine("To differentiate: use the Query.Count method...")
			query__1 = "//e"
			n = Query.Count(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine("Count={0}", n)
			query__1 = "//notthere"
			n = Query.Count(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine("Count={0}", n)
			query__1 = "//b/@foo"
			n = Query.Count(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine("Count={0}", n)
			query__1 = "//b/@baz"
			n = Query.Count(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine("Count={0}", n)

		End Sub

		Private Shared Sub test_Whitespace()
			Dim s As String, query__1 As String

			Console.WriteLine(vbLf & "IGNORABLE WHITESPACE...")
			' Pass XML as a string
			Dim xml As String = "<a foo = '   val   de  ri '>  hello   world </a>"
			Console.WriteLine("xml={0}", xml)

			query__1 = "/a"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))
			Console.WriteLine("-- with Trim option (note whitespace in middle of element content is unchanged)")
			s = Query.GetText(xml, query__1, Query.Opts.Trim)
			Console.WriteLine(quote_str(s))

			query__1 = "/a/@foo"
			s = Query.GetText(xml, query__1)
			Console.WriteLine("Query: {0}", query__1)
			Console.WriteLine(quote_str(s))
			Console.WriteLine("-- with Trim option (note whitespace inside attribute value is collapsed)")
			s = Query.GetText(xml, query__1, Query.Opts.Trim)
			Console.WriteLine(quote_str(s))
		End Sub

		Public Shared Sub Main(args As String())

			' Make sure minimum required version of core DLL is installed...
            Console.WriteLine("Xmlsq core DLL Version={0:D5}", Xmlsq.Gen.Version())

			If Gen.Version() < MIN_VERSION Then
				Console.WriteLine("FATAL ERROR: Require DLL version " & MIN_VERSION & " or later.")
				Return
			End If

            Console.WriteLine("This .NET module Version={0}", Xmlsq.Gen.NetVersion())

            ' Handle command-line arguments
			' [some]      just do some tests (default = do all)
			Dim doSome As Boolean = False
			For iarg As Integer = 0 To args.Length - 1
				If args(iarg) = "some" Then
					doSome = True
				End If
			Next

			'*************
			' DO THE TESTS
			'*************
			If doSome Then
				' Use "some" in the command line
				' Do some tests - comment these out as required
				'test_Bookstore();
				'test_Errors();
				'test_Empty();
				'test_UseCount();
				test_Whitespace()
			Else
				' Do all the test modules (default)
				test_Bookstore()
				test_Errors()
				test_Empty()
				test_UseCount()
				test_Whitespace()
			End If

			' FINALLY, DISPLAY QUICK INFO ABOUT THE CORE DLL
			Console.WriteLine(vbLf & "DETAILS OF CORE DLL...")
			Console.WriteLine("DLL Version={0:D5} [{1}] Compiled=[{2}] ", Gen.Version(), Gen.Platform(), Gen.CompileTime())
			Console.WriteLine("[{0}]", Gen.ModuleName())
		End Sub
	End Class
End Namespace