|
| 1 | +package us.codecraft.xsoup.w3c; |
| 2 | + |
| 3 | +import org.jsoup.Jsoup; |
| 4 | +import org.junit.Test; |
| 5 | +import org.w3c.dom.Node; |
| 6 | +import org.w3c.dom.NodeList; |
| 7 | +import us.codecraft.xsoup.Xsoup; |
| 8 | + |
| 9 | +import javax.xml.xpath.*; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author code4crafer@gmail.com |
| 17 | + */ |
| 18 | +public class W3cEvaluatorTest { |
| 19 | + |
| 20 | + private String html = "<html><body><div id='test'>aaa<div><a href=\"https://github.com\">github.com</a></div></div></body></html>"; |
| 21 | + |
| 22 | + private String htmlClass = "<html><body><div class='a b c'><div><a href=\"https://github.com\">github.com</a></div></div><div>b</div></body></html>"; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testSelect() throws XPathExpressionException { |
| 26 | + |
| 27 | + String html = "<html><div><a href='https://github.com'>github.com</a></div>" + |
| 28 | + "<table><tr><td>a</td><td>b</td></tr></table></html>"; |
| 29 | + |
| 30 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html)); |
| 31 | + |
| 32 | + assertThat(getStringValue(document, "//div/a/@href")).isEqualTo("https://github.com"); |
| 33 | + |
| 34 | + List<String> nodeListValue = getNodeListValue(document, "//tr/td"); |
| 35 | + assertThat(nodeListValue.get(0)).isEqualTo("<td>a</td>"); |
| 36 | + assertThat(nodeListValue.get(1)).isEqualTo("<td>b</td>"); |
| 37 | + } |
| 38 | + |
| 39 | + private String getStringValue(org.w3c.dom.Document document, String expression) throws XPathExpressionException { |
| 40 | + XPathExpression xPathExpression = newXPathExpression(expression); |
| 41 | + return xPathExpression.evaluate(document); |
| 42 | + } |
| 43 | + |
| 44 | + private XPathExpression newXPathExpression(String expression) throws XPathExpressionException { |
| 45 | + XPathExpression xPathExpression; |
| 46 | + XPathFactory xPathfactory = XPathFactory.newInstance(); |
| 47 | + XPath target = xPathfactory.newXPath(); |
| 48 | + xPathExpression = target.compile(expression); |
| 49 | + return xPathExpression; |
| 50 | + } |
| 51 | + |
| 52 | + private String getNodeValue(org.w3c.dom.Document document, String expression) throws XPathExpressionException { |
| 53 | + XPathExpression xPathExpression = newXPathExpression(expression); |
| 54 | + Object evaluate = xPathExpression.evaluate(document, XPathConstants.NODE); |
| 55 | + if (evaluate == null) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + Node node = (Node) evaluate; |
| 59 | + return node.getNodeValue(); |
| 60 | + } |
| 61 | + |
| 62 | + private List<String> getNodeListValue(org.w3c.dom.Document document, String expression) throws XPathExpressionException { |
| 63 | + XPathExpression xPathExpression = newXPathExpression(expression); |
| 64 | + Object evaluate = xPathExpression.evaluate(document, XPathConstants.NODESET); |
| 65 | + if (evaluate == null) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + NodeList nodeList = (NodeList) evaluate; |
| 69 | + List<String> nodeStrings = new ArrayList<String>(nodeList.getLength()); |
| 70 | + for (int i = 0; i < nodeList.getLength(); i++) { |
| 71 | + nodeStrings.add(nodeList.item(i).getNodeValue()); |
| 72 | + } |
| 73 | + return nodeStrings; |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testParent() throws XPathExpressionException { |
| 78 | + |
| 79 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html)); |
| 80 | + |
| 81 | + assertThat(getNodeValue(document, "/html/body/div/div/a")).isEqualTo("<a href=\"https://github.com\">github.com</a>"); |
| 82 | + assertThat(getNodeValue(document, "/html//div/div/a")).isEqualTo("<a href=\"https://github.com\">github.com</a>"); |
| 83 | + |
| 84 | + assertThat(getNodeValue(document, "/html/div/div/a")).isNull(); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testByAttribute() throws XPathExpressionException { |
| 90 | + |
| 91 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html)); |
| 92 | + |
| 93 | + assertThat(getNodeValue(document, "//a[@href]")).isEqualTo("<a href=\"https://github.com\">github.com</a>"); |
| 94 | + |
| 95 | + assertThat(getNodeValue(document, "//a[@id]")).isNull(); |
| 96 | + |
| 97 | + String expectedDiv = "<div id=\"test\">\n" + |
| 98 | + " aaa\n" + |
| 99 | + " <div>\n" + |
| 100 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 101 | + " </div>\n" + |
| 102 | + "</div>"; |
| 103 | + |
| 104 | + |
| 105 | + //TODO: illegal |
| 106 | + //assertThat(getNodeValue(document,"//div[@id=test]")).isEqualTo(expectedDiv); |
| 107 | + |
| 108 | + assertThat(getNodeValue(document, "//div[@id='test']")).isEqualTo(expectedDiv); |
| 109 | + assertThat(getNodeValue(document, "//div[@id=\"test\"]")).isEqualTo(expectedDiv); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testClass() throws XPathExpressionException { |
| 114 | + |
| 115 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(htmlClass)); |
| 116 | + |
| 117 | + |
| 118 | + assertThat(getNodeListValue(document,"//div[@class='a b c']").get(0)).isEqualTo("<div class=\"a b c\">\n" + |
| 119 | + " <div>\n" + |
| 120 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 121 | + " </div>\n" + |
| 122 | + "</div>"); |
| 123 | + |
| 124 | + assertThat(getNodeListValue(document, "//div[@class='b']")).isNullOrEmpty(); |
| 125 | + |
| 126 | + assertThat(getNodeListValue(document, "//div[@class='d']")).isNullOrEmpty(); |
| 127 | + |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testNth() throws XPathExpressionException { |
| 133 | + |
| 134 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(htmlClass)); |
| 135 | + |
| 136 | + assertThat(getNodeValue(document, "//body/div[1]")).isEqualTo("<div class=\"a b c\">\n" + |
| 137 | + " <div>\n" + |
| 138 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 139 | + " </div>\n" + |
| 140 | + "</div>"); |
| 141 | + |
| 142 | + assertThat(getNodeValue(document, "//body/div[2]")).isEqualTo("<div>\n" + |
| 143 | + " b\n" + |
| 144 | + "</div>"); |
| 145 | + |
| 146 | + String htmlSVG = "<div><svg>1</svg><svg>2</svg></div>"; |
| 147 | + |
| 148 | + document = Xsoup.convertDocument(Jsoup.parse(htmlSVG)); |
| 149 | + assertThat(getNodeValue(document, "//div/svg[1]")).isEqualTo("<svg>\n" + |
| 150 | + " 1\n" + |
| 151 | + "</svg>"); |
| 152 | + assertThat(getNodeValue(document, "//div/svg[2]")).isEqualTo("<svg>\n" + |
| 153 | + " 2\n" + |
| 154 | + "</svg>"); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testAttribute() throws XPathExpressionException { |
| 159 | + |
| 160 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(htmlClass)); |
| 161 | + |
| 162 | + assertThat(getStringValue(document,"//a/@href")).isEqualTo("https://github.com"); |
| 163 | + |
| 164 | + //TODO: not support |
| 165 | + //assertThat(getStringValue(document,"//a/text()")).isEqualTo("github.com"); |
| 166 | + |
| 167 | + //TODO: not support |
| 168 | + //assertThat(getStringValue(document,"//div[@class=a]/html()")).isEqualTo("<div>\n" + |
| 169 | + // " <a href=\"https://github.com\">github.com</a>\n" + |
| 170 | + // "</div>"); |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testLogicOperation() throws XPathExpressionException { |
| 176 | + |
| 177 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html)); |
| 178 | + |
| 179 | + String expectedDiv = "<div id=\"test\">\n" + |
| 180 | + " aaa\n" + |
| 181 | + " <div>\n" + |
| 182 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 183 | + " </div>\n" + |
| 184 | + "</div>"; |
| 185 | + |
| 186 | + assertThat(getNodeValue(document, "//*[@id='te' or @id='test']")).isEqualTo(expectedDiv); |
| 187 | + |
| 188 | + assertThat(getNodeValue(document, "//*[@id='te' and @id='test']")).isNullOrEmpty(); |
| 189 | + |
| 190 | + assertThat(getNodeValue(document, "//*[@id='te' and @id='test']")).isNullOrEmpty(); |
| 191 | + |
| 192 | + assertThat(getNodeValue(document,"//*[(@id='te' or @id='test') and @id='test']")).isEqualTo(expectedDiv); |
| 193 | + |
| 194 | + assertThat(getNodeValue(document,"//*[@id='te' or (@id='test' and @id='id')]")).isNull(); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testContains() throws XPathExpressionException { |
| 199 | + |
| 200 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html)); |
| 201 | + |
| 202 | + assertThat(getNodeValue(document,"//div[contains(@id,'te')]")).isEqualTo("<div id=\"test\">\n" + |
| 203 | + " aaa\n" + |
| 204 | + " <div>\n" + |
| 205 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 206 | + " </div>\n" + |
| 207 | + "</div>"); |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void testCombingXPath() throws XPathExpressionException { |
| 213 | + |
| 214 | + String html2 = "<html><div id='test2'>aa<a href='https://github.com'>github.com</a></div>"; |
| 215 | + |
| 216 | + String expectedDiv1 = "<div id=\"test\">\n" + |
| 217 | + " aaa\n" + |
| 218 | + " <div>\n" + |
| 219 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 220 | + " </div>\n" + |
| 221 | + "</div>"; |
| 222 | + |
| 223 | + String expectedDiv2 = "<div id=\"test2\">\n" + |
| 224 | + " aa\n" + |
| 225 | + " <a href=\"https://github.com\">github.com</a>\n" + |
| 226 | + "</div>"; |
| 227 | + |
| 228 | + org.w3c.dom.Document document = Xsoup.convertDocument(Jsoup.parse(html)); |
| 229 | + |
| 230 | + assertThat(getNodeValue(document, "//div[@id='test'] | //div[@id='test2']")).isEqualTo(expectedDiv1); |
| 231 | + |
| 232 | + document = Xsoup.convertDocument(Jsoup.parse(html2)); |
| 233 | + |
| 234 | + assertThat(getNodeValue(document, "//div[@id='test'] | //div[@id='test2']")).isEqualTo(expectedDiv2); |
| 235 | + |
| 236 | + document = Xsoup.convertDocument(Jsoup.parse(html+html2)); |
| 237 | + |
| 238 | + assertThat(getNodeListValue(document, "//div[@id='test'] | //div[@id='test2']")).contains(expectedDiv1,expectedDiv2); |
| 239 | + |
| 240 | + } |
| 241 | + |
| 242 | +} |
0 commit comments