HTMLEditorKit is throwing exception

Discussion in 'Java' started by Mike Mimic, Apr 20, 2004.

  1. Mike Mimic

    Mike Mimic Guest

    Hi!

    What is wrong with this code:

    HTMLDocument html = new HTMLDocument();
    try {
    HTMLEditorKit kit = new HTMLEditorKit();
    URL url = new URL("http://www.google.com/");
    kit.read(new BufferedReader(new
    InputStreamReader(url.openStream())), html, 0);
    }
    catch (Exception e) {
    System.err.println("Error");
    }

    Exception is thrown every time. (Yes, I am connected to the Internet
    and I can read from reader - I have tryed reading from new
    BufferedReader(new InputStreamReader(url.openStream())) Reader and it
    works.)


    Mike
     
    Mike Mimic, Apr 20, 2004
    #1
    1. Advertising

  2. Mike Mimic

    Bryce (Work) Guest

    On Tue, 20 Apr 2004 18:37:49 +0200, Mike Mimic <>
    wrote:

    >Hi!
    >
    >What is wrong with this code:
    >
    >HTMLDocument html = new HTMLDocument();
    >try {
    > HTMLEditorKit kit = new HTMLEditorKit();
    > URL url = new URL("http://www.google.com/");
    > kit.read(new BufferedReader(new
    > InputStreamReader(url.openStream())), html, 0);
    >}
    >catch (Exception e) {
    > System.err.println("Error");
    >}
    >
    >Exception is thrown every time. (Yes, I am connected to the Internet
    >and I can read from reader - I have tryed reading from new
    >BufferedReader(new InputStreamReader(url.openStream())) Reader and it
    >works.)


    What's the Exception? Just printing "error" doesn't tell you much. Try
    e.printStackTrace();

    --
    now with more cowbell
     
    Bryce (Work), Apr 20, 2004
    #2
    1. Advertising

  3. Mike Mimic

    Mike Mimic Guest

    Hi!

    Bryce (Work) wrote:
    > What's the Exception? Just printing "error" doesn't tell you much. Try
    > e.printStackTrace();


    Good idea. :)

    I have been reading message but it was null.

    Here it is:

    javax.swing.text.ChangedCharSetException
    at javax.swing.text.html.parser.DocumentParser.handleEmptyTag(Unknown
    Source)
    at javax.swing.text.html.parser.Parser.startTag(Unknown Source)
    at javax.swing.text.html.parser.Parser.parseTag(Unknown Source)
    at javax.swing.text.html.parser.Parser.parseContent(Unknown Source)
    at javax.swing.text.html.parser.Parser.parse(Unknown Source)
    at javax.swing.text.html.parser.DocumentParser.parse(Unknown Source)
    at javax.swing.text.html.parser.ParserDelegator.parse(Unknown Source)
    at javax.swing.text.html.HTMLEditorKit.read(Unknown Source)
    at test.main(test.java:18)

    Does not tell me much. Empty tag?


    Mike
     
    Mike Mimic, Apr 20, 2004
    #3
  4. Mike Mimic

    Chris Smith Guest

    Mike Mimic wrote:
    > Here it is:
    >
    > javax.swing.text.ChangedCharSetException


    Mike,

    When you provide a Reader to HTMLEditorKit to retrieve an HTML page,
    you're giving it a character stream that isn't encoded at all. If it
    encounters a meta tag in the HTML source that tries to change the
    encoding, it will therefore throw an exception, because it has no way to
    honor that request.

    You can set a property on the document to tell it to ignore this. The
    code looks like this:

    doc.putProperty("IgnoreCharsetDirective", new Boolean(true));

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 20, 2004
    #4
  5. Mike Mimic

    Mike Mimic Guest

    Hi!

    Chris Smith wrote:
    > When you provide a Reader to HTMLEditorKit to retrieve an HTML page,
    > you're giving it a character stream that isn't encoded at all.


    Encoded to what?

    How can I then encode stream so that HTMLEditorKit will be able to
    change the encoding?


    Mike
     
    Mike Mimic, Apr 20, 2004
    #5
  6. Mike Mimic

    Chris Smith Guest

    Mike Mimic wrote:
    > Encoded to what?
    >
    > How can I then encode stream so that HTMLEditorKit will be able to
    > change the encoding?


    Though I haven't tried it, I believe you may have better luck if you
    used InputStream instead of Reader. Alternatively, just set that
    property.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 20, 2004
    #6
  7. Mike Mimic

    Mike Mimic Guest

    Hi!

    Chris Smith wrote:
    > Though I haven't tried it, I believe you may have better luck if you
    > used InputStream instead of Reader. Alternatively, just set that
    > property.


    I had only BufferStream (chained to InputStream) but it was the same.


    Mike
     
    Mike Mimic, Apr 20, 2004
    #7
  8. Mike Mimic

    Chris Smith Guest

    Mike Mimic wrote:
    > Chris Smith wrote:
    > > Though I haven't tried it, I believe you may have better luck if you
    > > used InputStream instead of Reader. Alternatively, just set that
    > > property.

    >
    > I had only BufferStream (chained to InputStream) but it was the same.


    Yep, after looking into it, turns out that the default parser just gives
    up anyway, rather than switching encodings. Set that property and avoid
    the whole mess.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 21, 2004
    #8
  9. Mike Mimic

    Mike Mimic Guest

    Hi!

    Chris Smith wrote:
    > Yep, after looking into it, turns out that the default parser just gives
    > up anyway, rather than switching encodings. Set that property and avoid
    > the whole mess.


    I have. It works now. Thanks.


    Mike
     
    Mike Mimic, Apr 21, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plutian
    Replies:
    1
    Views:
    473
  2. Bill Tschumy
    Replies:
    1
    Views:
    3,253
  3. Gerrit Hulleman
    Replies:
    3
    Views:
    1,430
    Gerrit Hulleman
    Jul 27, 2004
  4. Nebojsa Topolscak
    Replies:
    3
    Views:
    588
    Jon Caldwell
    Jan 14, 2005
  5. tintac
    Replies:
    1
    Views:
    786
    Andrew Thompson
    Jan 29, 2005
Loading...

Share This Page