Menu

#257 The example for setTextPrototype does not work

open
5
2008-03-26
2008-03-26
Jay
No

The example for setTextPrototype does not work

PrototypicalNodeFactory factory = new PrototypicalNodeFactory();
factory.setTextPrototype (new TextNode () {
public String toPlainTextString()
{
return (org.htmlparser.util.Translate.decode (super.toPlainTextString ()));
}
});

Discussion

  • Jay

    Jay - 2008-03-26

    Logged In: YES
    user_id=154504
    Originator: YES

    how do you call this one? I need to override text nodes

     
  • Derrick Oswald

    Derrick Oswald - 2008-03-27

    Logged In: YES
    user_id=605407
    Originator: NO

    Can you be more explicit about what doesn't work.
    Like it doesn't compile, or it doesn't generate text nodes of your class, or it throws an exception.

    Your class needs to be clonable. Perhaps that is it?

     
  • Jay

    Jay - 2008-03-28

    Logged In: YES
    user_id=154504
    Originator: YES

    // This code doesn't compile. It complains "The constructor TextNode() is undefined". I got this in the documentation and thought it was a way to override textnodes?
    // My goal is to override TextNode so that I can process text and turn http://link.com into a real link <a href="link.com">link.com</a>

    import org.htmlparser.Node;
    import org.htmlparser.Parser;
    import org.htmlparser.Text;
    import org.htmlparser.lexer.Page;
    import org.htmlparser.tags.LinkTag;
    import org.htmlparser.util.NodeList;
    import org.htmlparser.util.ParserException;
    import org.htmlparser.PrototypicalNodeFactory;
    import org.htmlparser.tags.*;
    import org.htmlparser.nodes.TextNode;

    public static void main (String[] args) throws ParserException
    {
    String html = "<html><body>\n<script>alert('hi');</script><select id=\"da\"></select>" +
    "<p><a href=\"http://googlelink.com\">123456</a><br/>" +
    "<h1>hello</h1><a href=cnn.com></a>\n" +
    "http://google.com</br>" +
    "<b>https://cnn.com/?test=3&amp;2=d</b></p>\n" +
    "<table><tr><td>http://table.com</td></tr></table>" +
    "<a href=\"a.html\">123</a>\n<a href=\"http://www.alreadylinkified.com/\">http://www.alreadylinkified.com</a>\n</body></html>";

    PrototypicalNodeFactory factory = new PrototypicalNodeFactory();
    factory.setTextPrototype (new TextNode () {
    public String toPlainTextString()
    {
    return (org.htmlparser.util.Translate.decode (super.toPlainTextString ()));
    }
    });

    Parser parser = new Parser(html);
    parser.setNodeFactory(factory);
    NodeList all = parser.parse(null);
    System.out.println( all.toHtml());
    }

    }

     

Log in to post a comment.