1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="i18n_XBreakIterator" script:language="StarBasic">
4
5
6'*************************************************************************
7'
8'  Licensed to the Apache Software Foundation (ASF) under one
9'  or more contributor license agreements.  See the NOTICE file
10'  distributed with this work for additional information
11'  regarding copyright ownership.  The ASF licenses this file
12'  to you under the Apache License, Version 2.0 (the
13'  "License"); you may not use this file except in compliance
14'  with the License.  You may obtain a copy of the License at
15'
16'    http://www.apache.org/licenses/LICENSE-2.0
17'
18'  Unless required by applicable law or agreed to in writing,
19'  software distributed under the License is distributed on an
20'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21'  KIND, either express or implied.  See the License for the
22'  specific language governing permissions and limitations
23'  under the License.
24'
25'*************************************************************************
26
27
28
29
30
31' Be sure that all variables are dimensioned:
32option explicit
33
34'*************************************************************************
35' This Interface/Service test depends on the following GLOBAL variables,
36' which must be specified in the object creation:
37
38'     - Global oLocale As Object
39'     - Global UnicodeString As String
40'     - Global ScriptType(2, 1) As Integer
41
42'*************************************************************************
43
44
45
46
47
48const arrSize = 300
49
50
51Sub RunTest()
52
53'*************************************************************************
54' INTERFACE:
55' com.sun.star.i18n.XBreakIterator
56'*************************************************************************
57On Error Goto ErrHndl
58    Dim bOK As Boolean
59    Dim i As Integer
60    Dim nCharacterIteratorMode As Integer
61    Dim lDone As Long
62    Dim lRes As Long
63    Dim wordType As Integer
64    Dim Boundary As Variant
65    Dim BoundsToTest(arrSize) As new com.sun.star.i18n.Boundary
66    Dim Types(arrSize) As Integer
67    Dim nBoundsToTest As Integer
68    Dim iPos As Integer
69    Dim iType As Integer
70    Dim bFound As Boolean
71    Dim j As Integer
72    Dim NextCharacters(2, 3) As Integer
73    Dim PreviousCharacters(2, 3) As Integer
74
75    Test.StartMethod("nextCharacters()")
76    bOK = true
77    nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER
78
79    'Start from position     : Travel ... chars         : Actual position after    : How many chars traveled
80    NextCharacters(0, 0) =  1 : NextCharacters(0, 1) =  5000 : NextCharacters(0, 2) = len(UnicodeString) : NextCharacters(0, 3) = len(UnicodeString) - 1
81    NextCharacters(1, 0) =  5 : NextCharacters(1, 1) = -5000 : NextCharacters(1, 2) =  0 : NextCharacters(1, 3) = -5
82    NextCharacters(2, 0) = 10 : NextCharacters(2, 1) =     6 : NextCharacters(2, 2) = 16 : NextCharacters(2, 3) = 6
83
84    for i = 0 to 2
85        lRes = oObj.nextCharacters(UnicodeString, NextCharacters(i, 0), _
86                oLocale, nCharacterIteratorMode, NextCharacters(i, 1), lDone)
87        Out.Log("Expected result is: lRes = " + NextCharacters(i, 2) + _
88                                 "; lDone = " + NextCharacters(i, 3))
89        Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone)
90
91        bOK = bOK AND lRes = NextCharacters(i, 2)
92        bOK = bOK AND lDone = NextCharacters(i, 3)
93    next i
94
95    Test.MethodTested("nextCharacters()", bOK)
96
97    Test.StartMethod("previousCharacters()")
98    bOK = true
99    nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER
100
101    'Start from position     : Travel ... chars         : Actual position after    : How many chars traveled
102    PreviousCharacters(0, 0) = 10 : PreviousCharacters(0, 1) = -5000 : PreviousCharacters(0, 2) = len(UnicodeString) : PreviousCharacters(0, 3) = len(UnicodeString) - 10
103    PreviousCharacters(1, 0) =  5 : PreviousCharacters(1, 1) =  5000 : PreviousCharacters(1, 2) = 0 : PreviousCharacters(1, 3) = 5
104    PreviousCharacters(2, 0) = 10 : PreviousCharacters(2, 1) =     6 : PreviousCharacters(2, 2) = 4 : PreviousCharacters(2, 3) = 6
105
106    for i = 0 to 2
107        lRes = oObj.previousCharacters(UnicodeString, PreviousCharacters(i, 0), _
108                oLocale, nCharacterIteratorMode, PreviousCharacters(i, 1), lDone)
109        Out.Log("Expected result is: lRes = " + PreviousCharacters(i, 2) + _
110                                 "; lDone = " + PreviousCharacters(i, 3))
111        Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone)
112
113        bOK = bOK AND lRes = PreviousCharacters(i, 2)
114        bOK = bOK AND lDone = PreviousCharacters(i, 3)
115    next i
116
117    Test.MethodTested("previousCharacters()", bOK)
118
119
120    Test.StartMethod("nextWord()")
121    Test.StartMethod("previousWord()")
122    bOK = true
123    wordType = com.sun.star.i18n.WordType.ANYWORD_IGNOREWHITESPACES
124    Dim wordCount1 As Integer
125    Dim wordCount2 As Integer
126    wordCount1 = 0
127    wordCount2 = 0
128    i = 0
129    nBoundsToTest = 0
130
131    while i &lt; len(UnicodeString) - 1
132        Boundary = oObj.nextWord(UnicodeString, i, oLocale, wordType)
133        i = Boundary.endPos - 1
134        wordCount1 = wordCount1 + 1
135        if ((nBoundsToTest &lt; arrSize) AND (Boundary.endPos - Boundary.startPos &gt; 3)) then
136            BoundsToTest(nBoundsToTest).startPos = Boundary.startPos
137            BoundsToTest(nBoundsToTest).endPos = Boundary.endPos  ' Here we find out some words and
138            nBoundsToTest = nBoundsToTest + 1       ' get their bounds to test in future
139        end if
140        Out.Log("Word " + wordCount1 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" +  _
141            mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
142    wend
143    Out.Log("In text there are " + wordCount1 + " words, if count from left to right")
144
145    while i &gt; 0
146        Boundary = oObj.previousWord(UnicodeString, i, oLocale, wordType)
147        i = Boundary.startPos
148        wordCount2 = wordCount2 + 1
149        Out.Log("Word " + wordCount2 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" + _
150            mid(UnicodeString,  Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
151    wend
152    Out.Log("In text there are " + wordCount2 + " words, if count from right to left")
153
154    bOK = bOK AND wordCount1 = wordCount2
155    Test.MethodTested("previousWord()", bOK)
156    Test.MethodTested("nextWord()", bOK)
157
158    Test.StartMethod("getWordBoundary()")
159    bOK = true
160    if (nBoundsToTest = 0) then
161        Out.Log("Couldn't find words longer than 3 letters.")
162        bOK = false
163    end if
164
165    for i = 0 to nBoundsToTest - 1
166        iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word
167        Boundary = oObj.getWordBoundary(UnicodeString, iPos, oLocale, wordType, true)
168        Out.Log("Expected result is: startPos = " + BoundsToTest(i).startPos + _
169                                 "; endPos = " + BoundsToTest(i).endPos)
170        Out.Log("Actual result is: startPos = " + Boundary.startPos + "; endPos = " + Boundary.endPos + _
171               " Word is: '" + mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
172
173        bOK = bOK AND BoundsToTest(i).startPos = Boundary.startPos
174        bOK = bOK AND BoundsToTest(i).endPos = Boundary.endPos
175    next i
176
177    Test.MethodTested("getWordBoundary()", bOK)
178
179    Test.StartMethod("getWordType()")
180    bOK = true
181    for i = 0 to nBoundsToTest - 1
182        iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word
183        wordType = oObj.getWordType(UnicodeString, iPos, oLocale)
184        bOK = bOK AND wordType &gt;= 0 AND wordType &lt;= 2
185    next i
186    Test.MethodTested("getWordType()", bOK)
187
188    Test.StartMethod("isBeginWord()")
189    bOK = true
190    if (nBoundsToTest = 0) then
191        Out.Log("Couldn't find words longer than 3 letters.")
192        bOK = false
193    end if
194
195    for i = 0 to nBoundsToTest - 1
196        bOK = bOK AND oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
197        bOK = bOK AND NOT oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos + 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
198    next i
199    Test.MethodTested("isBeginWord()", bOK)
200
201    Test.StartMethod("isEndWord()")
202    bOK = true
203    if (nBoundsToTest = 0) then
204        Out.Log("Couldn't find words longer than 3 letters.")
205        bOK = false
206    end if
207
208    for i = 0 to nBoundsToTest - 1
209        bOK = bOK AND oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
210        bOK = bOK AND NOT oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos - 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
211    next i
212    Test.MethodTested("isEndWord()", bOK)
213
214    Test.StartMethod("beginOfSentence()")
215    Test.StartMethod("endOfSentence()")
216    Dim sStart As Integer
217    Dim sEnd As Integer
218
219    bOK = true
220    iPos = 0
221    while (iPos &lt;= len(UnicodeString))' AND bOK)
222        sStart = oObj.beginOfSentence(UnicodeString, iPos, oLocale)
223        sEnd = oObj.endOfSentence(UnicodeString, iPos, oLocale)
224        if (iPos &lt; sStart OR iPos &gt; sEnd) then
225            bOK = false
226            Out.Log("From position " + iPos + " sentence range is [" + sStart + ", " + sEnd + "]")
227            Out.Log("'" + mid(UnicodeString, sStart + 1, sEnd - sStart) + "'")
228        end if
229        iPos = iPos + 1
230    wend
231
232    if (bOK) then
233        bOK = oObj.beginOfSentence(UnicodeString, -10, oLocale) = -1
234        bOK = oObj.endOfSentence(UnicodeString, -10, oLocale) = -1
235        bOK = oObj.beginOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1
236        bOK = oObj.endOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1
237        if (NOT bOK) then Out.Log("When invalid position, returned range is NOT [-1, -1]")
238    end if
239
240    Test.MethodTested("beginOfSentence()", bOK)
241    Test.MethodTested("endOfSentence()", bOK)
242
243    Test.StartMethod("getLineBreak()")
244    bOK = true
245    Dim LineBreakResults As Variant
246    Dim _LineBreakHyphenationOptions As new com.sun.star.i18n.LineBreakHyphenationOptions
247    Dim _LineBreakUserOptions As new com.sun.star.i18n.LineBreakUserOptions
248    Dim breakPos As Long, pos As Long
249    _LineBreakUserOptions.applyForbiddenRules = false
250    _LineBreakUserOptions.allowHyphenateEnglish = false
251
252    breakPos = 0  : pos = 0
253
254    while breakPos = 0 AND pos &lt; len(UnicodeString)
255        LineBreakResults = oObj.getLineBreak(UnicodeString, pos, oLocale, 0, _LineBreakHyphenationOptions, _LineBreakUserOptions)
256        breakPos = LineBreakResults.BreakIndex
257        pos = pos + 1
258    wend
259
260    ' finally the position of break must be found in the middle and
261    ' it must be before the break position specified
262    bOK = (breakPos &lt;= pos AND breakPos > 0)
263
264    if (NOT bOK) then
265        Out.Log("The last position was: " + pos + ", and the break position was: " + breakPos)
266    end if
267
268    Test.MethodTested("getLineBreak()", bOK)
269
270
271    Test.StartMethod("beginOfScript()")
272    Test.StartMethod("endOfScript()")
273    bOK = true
274    Out.Log("Searching for scripts...")
275    iPos = 0
276    nBoundsToTest = 0
277
278    while iPos &lt; len(UnicodeString) AND iPos &gt; -1 AND nBoundsToTest &lt; arrSize
279        iType = oObj.getScriptType(UnicodeString, iPos)
280        BoundsToTest(nBoundsToTest).startPos = oObj.beginOfScript(UnicodeString, iPos, iType)
281        BoundsToTest(nBoundsToTest).endPos = oObj.endOfScript(UnicodeString, iPos, iType)
282        Types(nBoundsToTest) = iType
283        iPos = BoundsToTest(nBoundsToTest).endPos
284        Out.Log("Type: " + iType + "; Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]")
285        nBoundsToTest = nBoundsToTest + 1
286    wend
287    bOK = bOK AND nBoundsToTest &gt; 0
288
289    Test.MethodTested("beginOfScript()", bOK)
290    Test.MethodTested("endOfScript()", bOK)
291
292
293    Test.StartMethod("nextScript()")
294    bOK = true
295    i = 1
296    iPos = -1
297    while i &lt; nBoundsToTest AND bOK
298        iPos = oObj.nextScript(UnicodeString, iPos, Types(i))
299        Out.Log("Expected index for Type " + Types(i) + " for nextScript is " + BoundsToTest(i).startPos)
300        bOK = bOK AND iPos = BoundsToTest(i).startPos
301        Out.Log("NextScript returned " + iPos + ";")
302        i = i + 1
303    wend
304    Test.MethodTested("nextScript()", bOK)
305
306    Test.StartMethod("previousScript()")
307    bOK = true
308    i = nBoundsToTest - 2
309    iPos = len(UnicodeString) + 1
310    while i &gt;= 0  AND bOK
311        iPos = oObj.previousScript(UnicodeString, iPos, Types(i))
312        Out.Log("Expected index for Type " + Types(i) + " for previousScript is " + BoundsToTest(i).startPos)
313        bOK = bOK AND iPos = BoundsToTest(i).startPos
314        Out.Log("PreviousScript returned " + iPos + ";")
315        i = i - 1
316    wend
317    Test.MethodTested("previousScript()", bOK)
318
319    Test.StartMethod("getScriptType()")
320    bOK = true
321    for i = 0 to 2
322        iPos = oObj.getScriptType(UnicodeString, ScriptType(i, 0))
323        Out.Log("Expected result is: ScriptType = " + ScriptType(i, 1))
324        Out.Log("Actual result is: ScriptType = " + iPos)
325        bOK = bOK AND iPos = ScriptType(i, 1)
326    next i
327    Test.MethodTested("getScriptType()", bOK)
328
329    Test.StartMethod("beginOfCharBlock()")
330    Test.StartMethod("endOfCharBlock()")
331    bOK = true
332    Out.Log("getting CharBlocks bounds...")
333    iPos = 0
334    nBoundsToTest = 0
335
336    while iPos &lt; len(UnicodeString) AND iPos &gt; -1 AND nBoundsToTest &lt; arrSize
337        iType = getCharBlockType(iPos)
338        BoundsToTest(nBoundsToTest).startPos = oObj.beginOfCharBlock(UnicodeString, iPos, oLocale, iType)
339        BoundsToTest(nBoundsToTest).endPos = oObj.endOfCharBlock(UnicodeString, iPos, oLocale, iType)
340        iPos = BoundsToTest(nBoundsToTest).endPos
341        Out.Log("" + nBoundsToTest + "). Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]; Type = " + iType)
342        Types(nBoundsToTest) = iType
343        nBoundsToTest = nBoundsToTest + 1
344    wend
345
346    for i = 0 to nBoundsToTest - 2
347        bOK = bOK AND BoundsToTest(i).endPos = BoundsToTest(i + 1).startPos
348    next
349    Out.Log("Testing for no intersections : " + bOK)
350    bOK = bOK AND BoundsToTest(0).startPos = 0
351    bOK = bOK AND BoundsToTest(nBoundsToTest - 1).endPos = len(UnicodeString)
352    Out.Log("Regions should starts with 0 and ends with " + len(UnicodeString))
353
354    Test.MethodTested("beginOfCharBlock()", bOK)
355    Test.MethodTested("endOfCharBlock()", bOK)
356
357    Test.StartMethod("nextCharBlock()")
358    bOK = true
359    for i = 0 to nBoundsToTest - 1
360        iPos = oObj.nextCharBlock(UnicodeString, BoundsToTest(i).startPos - 1, oLocale, Types(i))
361        if (iPos &lt;&gt; BoundsToTest(i).startPos) then
362            bOK = false
363            Out.Log("nextCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 + ", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos)
364            Out.Log("... and actual value is " + iPos)
365        end if
366    next i
367    Test.MethodTested("nextCharBlock()", bOK)
368
369    Test.StartMethod("previousCharBlock()")
370    bOK = true
371    for i = 0 to nBoundsToTest - 1
372        iPos = oObj.previousCharBlock(UnicodeString, BoundsToTest(i).endPos + 1, oLocale, Types(i))
373        if (iPos &lt;&gt; BoundsToTest(i).startPos) then
374            bOK = false
375            Out.Log("previousCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 +", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos)
376            Out.Log("... and actual value is " + iPos)
377        end if
378    next i
379    Test.MethodTested("previousCharBlock()", bOK)
380
381Exit Sub
382ErrHndl:
383    Test.Exception()
384    bOK = false
385    resume next
386End Sub
387Function getCharBlockType(pos As Integer) As Integer
388    Dim i As Integer
389    Dim cType As Integer
390
391    cType = 0
392    i = 1
393    while (i &lt; 31)
394        if (oObj.beginOfCharBlock(UnicodeString, pos, oLocale, i) &lt;&gt; -1) then
395            cType = i
396            i = 100
397        end if
398        i = i + 1
399    wend
400
401    getCharBlockType = cType
402End Function
403</script:module>
404