📄 htmlparserutilstest.java
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2004 Somik Raha//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/utilTests/HTMLParserUtilsTest.java,v $// $Author: anul $// $Date: 2004/08/27 09:56:56 $// $Revision: 1.19 $//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package org.htmlparser.tests.utilTests;import org.htmlparser.NodeFilter;import org.htmlparser.filters.*;import org.htmlparser.tags.*;import org.htmlparser.tests.ParserTestCase;import org.htmlparser.util.ParserUtils;public class HTMLParserUtilsTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.utilTests.HTMLParserUtilsTest", "HTMLParserUtilsTest"); } public HTMLParserUtilsTest(String name) { super(name); } public void testRemoveTrailingSpaces() { String text = "Hello World "; assertStringEquals( "modified text", "Hello World", ParserUtils.removeTrailingBlanks(text) ); } public void testButCharsMethods() { String[] tmpSplitButChars = ParserUtils.splitButChars("<DIV> +12.5, +3.4 </DIV>", "+.1234567890"); assertStringEquals( "modified text", "+12.5*+3.4", new String(tmpSplitButChars[0] + '*' + tmpSplitButChars[1]) ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimButChars("<DIV> +12.5 </DIV>", "+.1234567890") ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimButChars("<DIV> +1 2 . 5 </DIV>", "+.1234567890") ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimButCharsBeginEnd("<DIV> +12.5 </DIV>", "+.1234567890") ); assertStringEquals( "modified text", "+1 2 . 5", ParserUtils.trimButCharsBeginEnd("<DIV> +1 2 . 5 </DIV>", "+.1234567890") ); } public void testButDigitsMethods() { String[] tmpSplitButDigits = ParserUtils.splitButDigits("<DIV> +12.5, +3.4 </DIV>", "+."); assertStringEquals( "modified text", "+12.5*+3.4", new String(tmpSplitButDigits[0] + '*' + tmpSplitButDigits[1]) ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimButDigits("<DIV> +12.5 </DIV>", "+.") ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimButDigits("<DIV> +1 2 . 5 </DIV>", "+.") ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimButDigitsBeginEnd("<DIV> +12.5 </DIV>", "+.") ); assertStringEquals( "modified text", "+1 2 . 5", ParserUtils.trimButDigitsBeginEnd("<DIV> +1 2 . 5 </DIV>", "+.") ); } public void testCharsMethods() { String[] tmpSplitChars = ParserUtils.splitChars("<DIV> +12.5, +3.4 </DIV>", " <>DIV/,"); assertStringEquals( "modified text", "+12.5*+3.4", new String(tmpSplitChars[0] + '*' + tmpSplitChars[1]) ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimChars("<DIV> +12.5 </DIV>", "<>DIV/ ") ); assertStringEquals( "modified text", "Trimallchars", ParserUtils.trimChars("<DIV> Trim all chars </DIV>", "<>DIV/ ") ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimCharsBeginEnd("<DIV> +12.5 </DIV>", "<>DIV/ ") ); assertStringEquals( "modified text", "Trim all spaces but not the ones inside the string", ParserUtils.trimCharsBeginEnd("<DIV> Trim all spaces but not the ones inside the string </DIV>", "<>DIV/ ") ); } public void testSpacesMethods() { String[] tmpSplitSpaces = ParserUtils.splitSpaces("<DIV> +12.5, +3.4 </DIV>", "<>DIV/,"); assertStringEquals( "modified text", "+12.5*+3.4", new String(tmpSplitSpaces[0] + '*' + tmpSplitSpaces[1]) ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimSpaces("<DIV> +12.5 </DIV>", "<>DIV/") ); assertStringEquals( "modified text", "Trimallspaces", ParserUtils.trimSpaces("<DIV> Trim all spaces </DIV>", "<>DIV/") ); assertStringEquals( "modified text", "+12.5", ParserUtils.trimSpacesBeginEnd("<DIV> +12.5 </DIV>", "<>DIV/") ); assertStringEquals( "modified text", "Trim all spaces but not the ones inside the string", ParserUtils.trimSpacesBeginEnd("<DIV> Trim all spaces but not the ones inside the string </DIV>", "<>DIV/") ); assertStringEquals( "modified text", "0", ParserUtils.trimSpacesBeginEnd("0", "") ); assertStringEquals( "modified text", "verifying the last char x", ParserUtils.trimSpacesBeginEnd("verifying the last char x", "") ); assertStringEquals( "modified text", "verifying the last char x", ParserUtils.trimSpacesBeginEnd("verifying the last char x ", "") ); assertStringEquals( "modified text", "x verifying the first char", ParserUtils.trimSpacesBeginEnd("x verifying the first char", "") ); assertStringEquals( "modified text", "x verifying the first char", ParserUtils.trimSpacesBeginEnd(" x verifying the first char", "") ); } public void testTagsMethods() { try { String[] tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}); assertStringEquals( "modified text", "Begin * ALL OK", new String(tmpSplitTags[0] + '*' + tmpSplitTags[1]) ); tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}, false, false); assertStringEquals( "modified text", "Begin *<DIV> +12.5 </DIV>* ALL OK", new String(tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) ); tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}, true, false); assertStringEquals( "modified text", "Begin * +12.5 * ALL OK", new String(tmpSplitTags[0] + '*' + tmpSplitTags[1] + '*' + tmpSplitTags[2]) ); tmpSplitTags = ParserUtils.splitTags("Begin <DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}, false, true); assertStringEquals( "modified text", "Begin * ALL OK", new String(tmpSplitTags[0] + '*' + tmpSplitTags[1]) ); assertStringEquals( "modified text", " ALL OK", ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}) ); assertStringEquals( "modified text", "<DIV> +12.5 </DIV> ALL OK", ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}, false, false) ); assertStringEquals( "modified text", " +12.5 ALL OK", ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}, true, false) ); assertStringEquals( "modified text", " ALL OK", ParserUtils.trimTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", new String[] {"DIV"}, false, true) ); // Test trimAllTags method assertStringEquals( "modified text", " +12.5 ALL OK", ParserUtils.trimAllTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", false) ); assertStringEquals( "modified text", " ALL OK", ParserUtils.trimAllTags("<DIV><DIV> +12.5 </DIV></DIV> ALL OK", true)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -