1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package ifc.util; 29 30 import lib.MultiMethodTest; 31 32 import com.sun.star.util.SearchAlgorithms; 33 import com.sun.star.util.SearchFlags; 34 import com.sun.star.util.SearchOptions; 35 import com.sun.star.util.SearchResult; 36 import com.sun.star.util.XTextSearch; 37 38 /** 39 * Testing <code>com.sun.star.util.XTextSearch</code> 40 * interface methods : 41 * <ul> 42 * <li><code> setOptions()</code></li> 43 * <li><code> searchForward()</code></li> 44 * <li><code> searchBackward()</code></li> 45 * </ul> <p> 46 * Test is <b> NOT </b> multithread compilant. <p> 47 * @see com.sun.star.util.XTextSearch 48 */ 49 public class _XTextSearch extends MultiMethodTest { 50 51 // oObj filled by MultiMethodTest 52 public XTextSearch oObj = null ; 53 54 protected final String str = "acababaabcababadcdaa" ; 55 protected final int startPos = 2 , endPos = 20 ; 56 protected final String searchStr = "(ab)*a(c|d)+" ; 57 protected final int fStartRes = 10, fEndRes = 18 ; 58 protected final int bStartRes = 18, bEndRes = 14 ; 59 60 /** 61 * Sets options for searching regular expression in a string, 62 * ignoring case. <p> 63 * Has <b>OK</b> status if no runtime exceptions occured. 64 */ 65 public void _setOptions() { 66 67 SearchOptions opt = new SearchOptions() ; 68 opt.algorithmType = SearchAlgorithms.REGEXP ; 69 opt.searchFlag = SearchFlags.ALL_IGNORE_CASE ; 70 opt.searchString = searchStr ; 71 72 oObj.setOptions(opt) ; 73 74 tRes.tested("setOptions()", true) ; 75 } 76 77 78 /** 79 * Tries to find a substring matching regular expression. <p> 80 * Has <b>OK</b> if the correct substring position returned. 81 */ 82 public void _searchForward() { 83 requiredMethod("setOptions()") ; 84 85 SearchResult res = oObj.searchForward(str, startPos, endPos) ; 86 87 log.println("Result of searching '" + searchStr + "' substring in \n'" + 88 str + "' string (" + res.subRegExpressions + " matches):") ; 89 90 for (int i = 0; i < res.subRegExpressions; i++) 91 log.println(" (" + res.startOffset[i] + ", " + res.endOffset[i] + ")") ; 92 93 tRes.tested("searchForward()", res.subRegExpressions > 0 && 94 res.startOffset[0] == fStartRes && res.endOffset[0] == fEndRes) ; 95 } 96 97 /** 98 * Tries to find a substring matching regular expression walking 99 * backward. <p> 100 * Has <b>OK</b> if the correct substring position returned. 101 */ 102 public void _searchBackward() { 103 requiredMethod("setOptions()") ; 104 105 SearchResult res = oObj.searchBackward(str, endPos, startPos) ; 106 107 log.println("Result of searching '" + searchStr + "' substring in \n'" + 108 str + "' string (" + res.subRegExpressions + " matches):") ; 109 110 for (int i = 0; i < res.subRegExpressions; i++) 111 log.println(" (" + res.startOffset[i] + ", " + res.endOffset[i] + ")") ; 112 113 tRes.tested("searchBackward()", res.subRegExpressions > 0 && 114 res.startOffset[0] == bStartRes && res.endOffset[0] == bEndRes) ; 115 } 116 117 } 118 119 120