public void AssertXmlEquals(string expected, string actual, string displayMessage) { expected = RemoveEscapeCharacters(expected); actual = RemoveEscapeCharacters(actual); Parser expectedParser = Parser.CreateParser(expected); Parser resultParser = Parser.CreateParser(actual); NodeIterator expectedIterator = expectedParser.GetEnumerator(); NodeIterator actualIterator = resultParser.GetEnumerator(); displayMessage = CreateGenericFailureMessage(displayMessage, expected, actual); Node currentExpectedNode = null, currentActualNode = null; while (expectedIterator.MoveNext()) { Assert.IsTrue(actualIterator.MoveNext()); currentExpectedNode = GetCurrentNodeUsing(expectedIterator); currentActualNode = GetCurrentNodeUsing(actualIterator); AssertStringValueMatches(currentExpectedNode, currentActualNode, displayMessage); FixIfXmlEndTag(resultParser, currentActualNode); FixIfXmlEndTag(expectedParser, currentExpectedNode); AssertSameType(displayMessage, currentExpectedNode, currentActualNode); AssertTagEquals(currentExpectedNode, currentActualNode, displayMessage); } AssertActualXmlHasNoMoreNodes(actualIterator, displayMessage); }
/// <summary> Returns an iterator (enumeration) to the html nodes. Each node can be a tag/endtag/ /// string/link/image<br> /// This is perhaps the most important method of this class. In typical situations, you will need to use /// the parser like this : /// <pre> /// Parser parser = new Parser("http://www.yahoo.com"); /// parser.RegisterScanners(); /// foreach (Node node in parser) { /// if (node instanceof StringNode) { /// // Downcasting to StringNode /// StringNode stringNode = (StringNode)node; /// // Do whatever processing you want with the string node /// System.out.println(stringNode.getText()); /// } /// // Check for the node or tag that you want /// if (node instanceof ...) { /// // Downcast, and process /// } /// } /// </pre> /// </summary> public NodeIterator GetEnumerator() { bool remove_scanner; NodeIterator ret; remove_scanner = false; ret = new NodeIterator(reader, resourceLocn, feedback); ret = CreateNodeIterator(remove_scanner, ret); return(ret); }
private void AssertActualXmlHasNoMoreNodes(NodeIterator actualIterator, string displayMessage) { if (actualIterator.MoveNext()) { string extraTags = "\nExtra Tags\n**********\n"; do { extraTags += ((Node)actualIterator.Current).ToHtml(); } while (actualIterator.MoveNext()); displayMessage = "Actual had more data than expected\n" + extraTags + displayMessage; Assert.Fail(displayMessage); } }
private Node GetCurrentNodeUsing(NodeIterator nodeIterator) { Node currentNode; string text = null; do { if (text != null) { nodeIterator.MoveNext(); } currentNode = (Node)nodeIterator.Current; if (currentNode is StringNode) { text = currentNode.ToPlainTextString().Trim(); } else { text = null; } } while (text != null && text.Length == 0); return(currentNode); }
public NodeIterator CreateNodeIterator(bool remove_scanner, NodeIterator ret) { Node node; MetaTag meta; string httpEquiv; string charset; EndTag end; if (null != url) { try { if (null == scanners["-m"]) { AddScanner(new MetaTagScanner("-m")); remove_scanner = true; } /* Read up to </HEAD> looking for charset directive */ while (ret.MoveNext()) { node = (Node)ret.Current; if (node is MetaTag) { // check for charset on Content-Type meta = (MetaTag)node; httpEquiv = meta["HTTP-EQUIV"]; if ("Content-Type".ToUpper().Equals(httpEquiv.ToUpper())) { charset = getCharset(meta["CONTENT"]); if (!charset.ToUpper().Equals(character_set.ToUpper())) { // oops, different character set, restart character_set = charset; } // once we see the Content-Type meta tag we're finished the pre-read break; } } else if (node is EndTag) { end = (EndTag)node; if (end.TagName.ToUpper().Equals("HEAD".ToUpper())) { // or, once we see the </HEAD> tag we're finished the pre-read break; } } } // Reset the reader RecreateReader(); ret = new NodeIterator(reader, resourceLocn, feedback); } /* TODO catch (UnsupportedEncodingException uee) * { * string msg = "elements() : The content of " + url.RequestUri.ToString() + " has an encoding which is not supported"; * ParserException ex = new ParserException(msg, uee); * feedback.Error(msg, ex); * throw ex; * } */ catch (IOException ioe) { string msg = "elements() : Error in opening a connection to " + url.RequestUri.ToString(); ParserException ex = new ParserException(msg, ioe); feedback.Error(msg, ex); throw ex; } }