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">
6 '*************************************************************************
8 ' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 ' Copyright
2008 by Sun Microsystems, Inc.
12 ' OpenOffice.org - a multi-platform office productivity suite
14 ' $RCSfile: i18n_XBreakIterator.xba,v $
18 ' This file is part of OpenOffice.org.
20 ' OpenOffice.org is free software: you can redistribute it and/or modify
21 ' it under the terms of the GNU Lesser General Public License version
3
22 ' only, as published by the Free Software Foundation.
24 ' OpenOffice.org is distributed in the hope that it will be useful,
25 ' but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ' GNU Lesser General Public License version
3 for more details
28 ' (a copy is included in the LICENSE file that accompanied this code).
30 ' You should have received a copy of the GNU Lesser General Public License
31 ' version
3 along with OpenOffice.org. If not, see
32 '
<http://www.openoffice.org/license.html
>
33 ' for a copy of the LGPLv3 License.
35 '*************************************************************************
36 '*************************************************************************
40 ' Be sure that all variables are dimensioned:
43 '*************************************************************************
44 ' This Interface/Service test depends on the following GLOBAL variables,
45 ' which must be specified in the object creation:
47 ' - Global oLocale As Object
48 ' - Global UnicodeString As String
49 ' - Global ScriptType(
2,
1) As Integer
51 '*************************************************************************
62 '*************************************************************************
64 ' com.sun.star.i18n.XBreakIterator
65 '*************************************************************************
69 Dim nCharacterIteratorMode As Integer
72 Dim wordType As Integer
73 Dim Boundary As Variant
74 Dim BoundsToTest(arrSize) As new com.sun.star.i18n.Boundary
75 Dim Types(arrSize) As Integer
76 Dim nBoundsToTest As Integer
81 Dim NextCharacters(
2,
3) As Integer
82 Dim PreviousCharacters(
2,
3) As Integer
84 Test.StartMethod(
"nextCharacters()")
86 nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER
88 'Start from position : Travel ... chars : Actual position after : How many chars traveled
89 NextCharacters(
0,
0) =
1 : NextCharacters(
0,
1) =
5000 : NextCharacters(
0,
2) = len(UnicodeString) : NextCharacters(
0,
3) = len(UnicodeString) -
1
90 NextCharacters(
1,
0) =
5 : NextCharacters(
1,
1) = -
5000 : NextCharacters(
1,
2) =
0 : NextCharacters(
1,
3) = -
5
91 NextCharacters(
2,
0) =
10 : NextCharacters(
2,
1) =
6 : NextCharacters(
2,
2) =
16 : NextCharacters(
2,
3) =
6
94 lRes = oObj.nextCharacters(UnicodeString, NextCharacters(i,
0), _
95 oLocale, nCharacterIteratorMode, NextCharacters(i,
1), lDone)
96 Out.Log(
"Expected result is: lRes = " + NextCharacters(i,
2) + _
97 "; lDone = " + NextCharacters(i,
3))
98 Out.Log(
"Actual result is: lRes = " + lRes +
"; lDone = " + lDone)
100 bOK = bOK AND lRes = NextCharacters(i,
2)
101 bOK = bOK AND lDone = NextCharacters(i,
3)
104 Test.MethodTested(
"nextCharacters()", bOK)
106 Test.StartMethod(
"previousCharacters()")
108 nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER
110 'Start from position : Travel ... chars : Actual position after : How many chars traveled
111 PreviousCharacters(
0,
0) =
10 : PreviousCharacters(
0,
1) = -
5000 : PreviousCharacters(
0,
2) = len(UnicodeString) : PreviousCharacters(
0,
3) = len(UnicodeString) -
10
112 PreviousCharacters(
1,
0) =
5 : PreviousCharacters(
1,
1) =
5000 : PreviousCharacters(
1,
2) =
0 : PreviousCharacters(
1,
3) =
5
113 PreviousCharacters(
2,
0) =
10 : PreviousCharacters(
2,
1) =
6 : PreviousCharacters(
2,
2) =
4 : PreviousCharacters(
2,
3) =
6
116 lRes = oObj.previousCharacters(UnicodeString, PreviousCharacters(i,
0), _
117 oLocale, nCharacterIteratorMode, PreviousCharacters(i,
1), lDone)
118 Out.Log(
"Expected result is: lRes = " + PreviousCharacters(i,
2) + _
119 "; lDone = " + PreviousCharacters(i,
3))
120 Out.Log(
"Actual result is: lRes = " + lRes +
"; lDone = " + lDone)
122 bOK = bOK AND lRes = PreviousCharacters(i,
2)
123 bOK = bOK AND lDone = PreviousCharacters(i,
3)
126 Test.MethodTested(
"previousCharacters()", bOK)
129 Test.StartMethod(
"nextWord()")
130 Test.StartMethod(
"previousWord()")
132 wordType = com.sun.star.i18n.WordType.ANYWORD_IGNOREWHITESPACES
133 Dim wordCount1 As Integer
134 Dim wordCount2 As Integer
140 while i
< len(UnicodeString) -
1
141 Boundary = oObj.nextWord(UnicodeString, i, oLocale, wordType)
142 i = Boundary.endPos -
1
143 wordCount1 = wordCount1 +
1
144 if ((nBoundsToTest
< arrSize) AND (Boundary.endPos - Boundary.startPos
> 3)) then
145 BoundsToTest(nBoundsToTest).startPos = Boundary.startPos
146 BoundsToTest(nBoundsToTest).endPos = Boundary.endPos ' Here we find out some words and
147 nBoundsToTest = nBoundsToTest +
1 ' get their bounds to test in future
149 Out.Log(
"Word " + wordCount1 +
"(" + Boundary.startPos +
"," + Boundary.endPos +
"): '" + _
150 mid(UnicodeString, Boundary.startPos +
1, Boundary.endPos - Boundary.startPos) +
"'")
152 Out.Log(
"In text there are " + wordCount1 +
" words, if count from left to right")
155 Boundary = oObj.previousWord(UnicodeString, i, oLocale, wordType)
156 i = Boundary.startPos
157 wordCount2 = wordCount2 +
1
158 Out.Log(
"Word " + wordCount2 +
"(" + Boundary.startPos +
"," + Boundary.endPos +
"): '" + _
159 mid(UnicodeString, Boundary.startPos +
1, Boundary.endPos - Boundary.startPos) +
"'")
161 Out.Log(
"In text there are " + wordCount2 +
" words, if count from right to left")
163 bOK = bOK AND wordCount1 = wordCount2
164 Test.MethodTested(
"previousWord()", bOK)
165 Test.MethodTested(
"nextWord()", bOK)
167 Test.StartMethod(
"getWordBoundary()")
169 if (nBoundsToTest =
0) then
170 Out.Log(
"Couldn't find words longer than 3 letters.")
174 for i =
0 to nBoundsToTest -
1
175 iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) /
2 + BoundsToTest(i).startPos ' calculate middle of the word
176 Boundary = oObj.getWordBoundary(UnicodeString, iPos, oLocale, wordType, true)
177 Out.Log(
"Expected result is: startPos = " + BoundsToTest(i).startPos + _
178 "; endPos = " + BoundsToTest(i).endPos)
179 Out.Log(
"Actual result is: startPos = " + Boundary.startPos +
"; endPos = " + Boundary.endPos + _
180 " Word is: '" + mid(UnicodeString, Boundary.startPos +
1, Boundary.endPos - Boundary.startPos) +
"'")
182 bOK = bOK AND BoundsToTest(i).startPos = Boundary.startPos
183 bOK = bOK AND BoundsToTest(i).endPos = Boundary.endPos
186 Test.MethodTested(
"getWordBoundary()", bOK)
188 Test.StartMethod(
"getWordType()")
190 for i =
0 to nBoundsToTest -
1
191 iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) /
2 + BoundsToTest(i).startPos ' calculate middle of the word
192 wordType = oObj.getWordType(UnicodeString, iPos, oLocale)
193 bOK = bOK AND wordType
>=
0 AND wordType
<=
2
195 Test.MethodTested(
"getWordType()", bOK)
197 Test.StartMethod(
"isBeginWord()")
199 if (nBoundsToTest =
0) then
200 Out.Log(
"Couldn't find words longer than 3 letters.")
204 for i =
0 to nBoundsToTest -
1
205 bOK = bOK AND oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
206 bOK = bOK AND NOT oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos +
1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
208 Test.MethodTested(
"isBeginWord()", bOK)
210 Test.StartMethod(
"isEndWord()")
212 if (nBoundsToTest =
0) then
213 Out.Log(
"Couldn't find words longer than 3 letters.")
217 for i =
0 to nBoundsToTest -
1
218 bOK = bOK AND oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
219 bOK = bOK AND NOT oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos -
1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
221 Test.MethodTested(
"isEndWord()", bOK)
223 Test.StartMethod(
"beginOfSentence()")
224 Test.StartMethod(
"endOfSentence()")
225 Dim sStart As Integer
230 while (iPos
<= len(UnicodeString))' AND bOK)
231 sStart = oObj.beginOfSentence(UnicodeString, iPos, oLocale)
232 sEnd = oObj.endOfSentence(UnicodeString, iPos, oLocale)
233 if (iPos
< sStart OR iPos
> sEnd) then
235 Out.Log(
"From position " + iPos +
" sentence range is [" + sStart +
", " + sEnd +
"]")
236 Out.Log(
"'" + mid(UnicodeString, sStart +
1, sEnd - sStart) +
"'")
242 bOK = oObj.beginOfSentence(UnicodeString, -
10, oLocale) = -
1
243 bOK = oObj.endOfSentence(UnicodeString, -
10, oLocale) = -
1
244 bOK = oObj.beginOfSentence(UnicodeString, len(UnicodeString) +
1, oLocale) = -
1
245 bOK = oObj.endOfSentence(UnicodeString, len(UnicodeString) +
1, oLocale) = -
1
246 if (NOT bOK) then Out.Log(
"When invalid position, returned range is NOT [-1, -1]")
249 Test.MethodTested(
"beginOfSentence()", bOK)
250 Test.MethodTested(
"endOfSentence()", bOK)
252 Test.StartMethod(
"getLineBreak()")
254 Dim LineBreakResults As Variant
255 Dim _LineBreakHyphenationOptions As new com.sun.star.i18n.LineBreakHyphenationOptions
256 Dim _LineBreakUserOptions As new com.sun.star.i18n.LineBreakUserOptions
257 Dim breakPos As Long, pos As Long
258 _LineBreakUserOptions.applyForbiddenRules = false
259 _LineBreakUserOptions.allowHyphenateEnglish = false
261 breakPos =
0 : pos =
0
263 while breakPos =
0 AND pos
< len(UnicodeString)
264 LineBreakResults = oObj.getLineBreak(UnicodeString, pos, oLocale,
0, _LineBreakHyphenationOptions, _LineBreakUserOptions)
265 breakPos = LineBreakResults.BreakIndex
269 ' finally the position of break must be found in the middle and
270 ' it must be before the break position specified
271 bOK = (breakPos
<= pos AND breakPos
> 0)
274 Out.Log(
"The last position was: " + pos +
", and the break position was: " + breakPos)
277 Test.MethodTested(
"getLineBreak()", bOK)
280 Test.StartMethod(
"beginOfScript()")
281 Test.StartMethod(
"endOfScript()")
283 Out.Log(
"Searching for scripts...")
287 while iPos
< len(UnicodeString) AND iPos
> -
1 AND nBoundsToTest
< arrSize
288 iType = oObj.getScriptType(UnicodeString, iPos)
289 BoundsToTest(nBoundsToTest).startPos = oObj.beginOfScript(UnicodeString, iPos, iType)
290 BoundsToTest(nBoundsToTest).endPos = oObj.endOfScript(UnicodeString, iPos, iType)
291 Types(nBoundsToTest) = iType
292 iPos = BoundsToTest(nBoundsToTest).endPos
293 Out.Log(
"Type: " + iType +
"; Bounds: [" + BoundsToTest(nBoundsToTest).startPos +
"," + BoundsToTest(nBoundsToTest).endPos +
"]")
294 nBoundsToTest = nBoundsToTest +
1
296 bOK = bOK AND nBoundsToTest
> 0
298 Test.MethodTested(
"beginOfScript()", bOK)
299 Test.MethodTested(
"endOfScript()", bOK)
302 Test.StartMethod(
"nextScript()")
306 while i
< nBoundsToTest AND bOK
307 iPos = oObj.nextScript(UnicodeString, iPos, Types(i))
308 Out.Log(
"Expected index for Type " + Types(i) +
" for nextScript is " + BoundsToTest(i).startPos)
309 bOK = bOK AND iPos = BoundsToTest(i).startPos
310 Out.Log(
"NextScript returned " + iPos +
";")
313 Test.MethodTested(
"nextScript()", bOK)
315 Test.StartMethod(
"previousScript()")
317 i = nBoundsToTest -
2
318 iPos = len(UnicodeString) +
1
319 while i
>=
0 AND bOK
320 iPos = oObj.previousScript(UnicodeString, iPos, Types(i))
321 Out.Log(
"Expected index for Type " + Types(i) +
" for previousScript is " + BoundsToTest(i).startPos)
322 bOK = bOK AND iPos = BoundsToTest(i).startPos
323 Out.Log(
"PreviousScript returned " + iPos +
";")
326 Test.MethodTested(
"previousScript()", bOK)
328 Test.StartMethod(
"getScriptType()")
331 iPos = oObj.getScriptType(UnicodeString, ScriptType(i,
0))
332 Out.Log(
"Expected result is: ScriptType = " + ScriptType(i,
1))
333 Out.Log(
"Actual result is: ScriptType = " + iPos)
334 bOK = bOK AND iPos = ScriptType(i,
1)
336 Test.MethodTested(
"getScriptType()", bOK)
338 Test.StartMethod(
"beginOfCharBlock()")
339 Test.StartMethod(
"endOfCharBlock()")
341 Out.Log(
"getting CharBlocks bounds...")
345 while iPos
< len(UnicodeString) AND iPos
> -
1 AND nBoundsToTest
< arrSize
346 iType = getCharBlockType(iPos)
347 BoundsToTest(nBoundsToTest).startPos = oObj.beginOfCharBlock(UnicodeString, iPos, oLocale, iType)
348 BoundsToTest(nBoundsToTest).endPos = oObj.endOfCharBlock(UnicodeString, iPos, oLocale, iType)
349 iPos = BoundsToTest(nBoundsToTest).endPos
350 Out.Log(
"" + nBoundsToTest +
"). Bounds: [" + BoundsToTest(nBoundsToTest).startPos +
"," + BoundsToTest(nBoundsToTest).endPos +
"]; Type = " + iType)
351 Types(nBoundsToTest) = iType
352 nBoundsToTest = nBoundsToTest +
1
355 for i =
0 to nBoundsToTest -
2
356 bOK = bOK AND BoundsToTest(i).endPos = BoundsToTest(i +
1).startPos
358 Out.Log(
"Testing for no intersections : " + bOK)
359 bOK = bOK AND BoundsToTest(
0).startPos =
0
360 bOK = bOK AND BoundsToTest(nBoundsToTest -
1).endPos = len(UnicodeString)
361 Out.Log(
"Regions should starts with 0 and ends with " + len(UnicodeString))
363 Test.MethodTested(
"beginOfCharBlock()", bOK)
364 Test.MethodTested(
"endOfCharBlock()", bOK)
366 Test.StartMethod(
"nextCharBlock()")
368 for i =
0 to nBoundsToTest -
1
369 iPos = oObj.nextCharBlock(UnicodeString, BoundsToTest(i).startPos -
1, oLocale, Types(i))
370 if (iPos
<> BoundsToTest(i).startPos) then
372 Out.Log(
"nextCharBlock(UnicodeString, " + BoundsToTest(i).startPos -
1 +
", oLocale, " + Types(i) +
") should return " + BoundsToTest(i).startPos)
373 Out.Log(
"... and actual value is " + iPos)
376 Test.MethodTested(
"nextCharBlock()", bOK)
378 Test.StartMethod(
"previousCharBlock()")
380 for i =
0 to nBoundsToTest -
1
381 iPos = oObj.previousCharBlock(UnicodeString, BoundsToTest(i).endPos +
1, oLocale, Types(i))
382 if (iPos
<> BoundsToTest(i).startPos) then
384 Out.Log(
"previousCharBlock(UnicodeString, " + BoundsToTest(i).startPos -
1 +
", oLocale, " + Types(i) +
") should return " + BoundsToTest(i).startPos)
385 Out.Log(
"... and actual value is " + iPos)
388 Test.MethodTested(
"previousCharBlock()", bOK)
396 Function getCharBlockType(pos As Integer) As Integer
403 if (oObj.beginOfCharBlock(UnicodeString, pos, oLocale, i)
<> -
1) then
410 getCharBlockType = cType