'#Reference {420B2830-E718-11CF-893D-00A0C9054228}#1.0#0#C:\WINDOWS\System32\scrrun.dll#Microsoft Scripting Runtime
'' Implements handling of Java Property Resource Bundles

'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND
'EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
'OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE

'Version history
'12/2002 Some Unicode issues fixed
'09/2003 Treat IDs as string to prevent conversion from hex to int
'09/2003 Unused reference to regular expressions deleted

Option Explicit
Function GuessLanguage(s As String) As Integer
	Dim pos As Long

	GuessLanguage = 0
	pos = InStrRev(s, "_")
	If pos = 0 Then Exit Function

	GuessLanguage = PSL.GetLangID(Mid(s, pos + 1, 2), pslCodeISO)

	If GuessLanguage < 0 Then Let GuessLanguage = 0
End Function

Function GetCodePage(rd As PslResData)
	GetCodePage = 1252

	Dim lang As Integer

	If rd.Action = pslResUpdScanTarget Or rd.Action = pslResUpdGenerate Then
		lang = rd.TargetLanguage
	Else
		lang = rd.SourceLanguage
	End If

	If lang = 1041 Then Let GetCodePage = 932

End Function


Function Readline(s As String, tsi As Scripting.TextStream) As String
	Dim tempstr As String
	Dim pos As Long

	Readline = s

	While Right(Trim(Readline), 1) = "\"
		tempstr = Trim(tsi.ReadLine)
		pos = InStrRev(Readline, "\")
		Readline = Left(Readline, pos - 1) + tempstr
	Wend
End Function

' Update translation list and generate target file
Function JavaUpdate(rd As PslResData) As Long
    Dim fso As Scripting.FileSystemObject, tsi As Scripting.TextStream, tso As Scripting.TextStream
    Dim s As String, i As Integer, colpos As Integer, eqpos As Integer, id as String

	Set fso = New Scripting.FileSystemObject
    Set tsi = fso.OpenTextFile(rd.SourceFile)

	' Generating target file
	If rd.Action = pslResUpdGenerate Then
		Set tso = fso.CreateTextFile(rd.TargetFile)
	End If

	JavaUpdate = 0
	rd.ProcessResource "Java Property Resource Bundle", "Strings"

	On Error GoTo ende
	While True
		s = tsi.ReadLine

		' Handle string spanning over multiple lines
		s = Readline(s, tsi)

		' Handle only lines which are not comments
		If Left(s, 1) <> "#" And Left(s, 1) <> "!" Then
			' "'" or "=" can be used as separators, the first one will be used
			colpos =InStr(s, ":")
			eqpos = InStr(s, "=")
			If colpos = 0 Then
				colpos = eqpos
			End If

			If eqpos = 0 Then
				eqpos = colpos
			End If

			If colpos < eqpos Then
				eqpos = colpos
			Else
				colpos = eqpos
			End If

			' Handle only lines with a separator
			If colpos > 0 Then
				s = PSL.ConvertASCII2Unicode(s, GetCodePage(rd))

				id = Trim(Left(s, colpos - 1))
				rd.SetEntryData(pslResUpdId, id)
				rd.SetEntryData(pslResUpdText, Mid(s, colpos + 1))
				rd.ProcessEntry

				' Generate changed line
				If rd.Action = pslResUpdGenerate Then
					s = CStr(id & "=" & rd.GetEntryData(pslResUpdText))
					s = PSL.ConvertUnicode2ASCII(s, GetCodePage(rd))

				End If
			End If
		End If

		' Write data to target file
		If rd.Action = 1 Then
			tso.WriteLine(s)
		End If
	Wend
ende:
End Function

Function JavaListContent(rd As PslResData) As Long

	JavaListContent = 0
	rd.ListResource "Java Property Resource Bundle", "Strings", GuessLanguage(rd.SourceFile)
End Function


Function JavaCheckFiles(rd As PslResData) As Long
    Dim fso As Scripting.FileSystemObject, ts As Scripting.TextStream, s As String
    Set fso = New Scripting.FileSystemObject

	' No property file
	If InStr(UCase(rd.SourceFile), "PROPERTIES") = 0 Then GoTo failure

	' File does not exists
    On Error GoTo failure
    Set ts = fso.OpenTextFile(rd.SourceFile)

	' If we also need to check target file, should not be the same as source file
	If rd.TargetFile <> "" Then
    	If UCase(rd.SourceFile) = UCase(rd.TargetFile) Then GoTo failure
	End If

	JavaCheckFiles = 0
	Exit Function

failure:
	JavaCheckFiles = 1

End Function

Function JavaGetLanguages(rd As PslResData) As Long
	' Only Language neutral
	JavaGetLanguages = 0
	rd.AddLanguage(GuessLanguage(rd.SourceFile))
End Function

Public Sub PSL_OnProcessUserFile(rd As PslResData)

	rd.Error = 0

	If rd.Action = pslResUpdGetFileExtensions Then
		rd.AddFileExtension("Java Property Files", "*.properties")
		Exit Sub
	End If

	' No property file
	If InStr(UCase(rd.SourceFile), "PROPERTIES") = 0 Then
		rd.Error = 1
		Exit Sub
	End If

	Select Case rd.Action
		Case pslResUpdScanTarget
			rd.Error = JavaUpdate(rd)
		Case pslResUpdGenerate
			rd.Error = JavaUpdate(rd)
		Case pslResUpdScanData
			rd.Error = JavaUpdate(rd)
		Case pslResUpdListContent
			rd.Error = JavaListContent(rd)
		Case pslResUpdCheckSourceTarget
			rd.Error = JavaCheckFiles(rd)
		Case pslResUpdGetLanguages
			rd.Error = JavaGetLanguages(rd)
		Case Else
			rd.Error = 1
	End Select
End Sub


