JDK-4229279 : JEditorPane with HTMLEditorKit is unusable in an Applet
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0,1.2.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_95,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-04-13
  • Updated: 1999-06-25
  • Resolved: 1999-06-25
Related Reports
Duplicate :  
Description

Name: dbT83986			Date: 04/13/99


Displaying HTML is not (easy and portable) possible in Applets (see workaround).
But it should be a basic functionality!

Displaying HTML in a swing Applet under Netscape 4.5 (and MS-IE?) is not
possible, the Netscape browser throws following error:

  netscape.security.AppletSecurityException: security.checkResourceAccess

This happens when using HTMLEditorKit with JEditorPane.

I believe its because of the 2 files:
  javax.swing.test.html.default.css
  javax.swing.test.html.parser.html32.bdtd
which are loaded localy with getResourceAsStream() instead
of accessing them from the applet URL (-> no security problem)

'default.css' can by forced to load from the URL by creating the
HTMLEditorKit:

    URL myCSS_URL = new URL(getCodeBase()+"javax/swing/text/html/default.css");
    javax.swing.text.html.StyleSheet styleSheet = new javax.swing.text.html.StyleSheet();
    try {
      java.io.InputStream istr = myCSS_URL.openStream();
      java.io.InputStreamReader in = new java.io.InputStreamReader(istr);
      styleSheet.loadRules(in, myCSS_URL);
    } catch (IOException e) { Log.error(myInstanceName, "Error in loading CSS from URL:" + e); }    
    javax.swing.text.html.HTMLEditorKit kit =  new javax.swing.text.html.HTMLEditorKit();
    kit.setStyleSheet(styleSheet);
    jEditorPane.setEditorKit(kit); //  Sets the currently installed kit for handling content.

but for 'html32.bdtd' there is no workaround to load it from URL
(see code in javax.swing.text.parser.ParserDelegator.java)

Testing localy with appletviewer always works fine (displaying HTML text)
Testing with Netscape: The HTML area is empty (no text is displayed)


Please add a simple possibility to load these files from the URL!
(Review ID: 56926) 
======================================================================

Name: skT88420			Date: 04/26/99

=20
I'm writing a small program that will allow a user to edit a
HTML document over the net, in his/her webbrowser. For this I'm
using the JEditorPane class. But the HTML document will show up
without any formatting when displayed inside the applet (using
appletviewer), but when I remade the program as an application,
it worked as expected.

Here is the code:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.*;

public class JED extends JApplet {
  public JED() {
  }

  public void init() {
    JEditorPane editorPane =3D new JEditorPane();
    editorPane.setEditable(true);

// This is for testing, will use setPage() later
    editorPane.setEditorKit(new HTMLEditorKit());
    editorPane.setText("<html><body><center><h1>Test</h1></center><u>Some t=
ext...</u><br></body></html>");

    JScrollPane editorScrollPane =3D new JScrollPane(editorPane);
    editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLL=
BAR_ALWAYS);
    editorScrollPane.setPreferredSize(new Dimension(600, 500));

    getContentPane().add(editorScrollPane, BorderLayout.CENTER);
  }
}
(Review ID: 57471)
======================================================================

Name: skT88420			Date: 04/26/99


JEditorPane (usually) recognizes and observes HTML markup when 
displaying text in applications and in applets run under 
the appletviewer without the plugin. However when JEditorPane runs 
in an applet under plugin 1.2 in NS (4.5) or IE (5), JEditorPane 
ignores the HTML markup and gives a flat display of the text.  

The problem can be seen by running a test applet I have set up at:

http://sunsite.berkeley.edu/apps/dev/jeditortest/AppletTest.html

This applet is derived from the JEditorPane example in chapter 22 
of David Geary's book. The source code is as follows:

public class AppletTest extends JApplet {
    
	//private JEditorPane editorPane = new JEditorPane();

	public void init() {
	    System.out.println("Initing applet");
	    JEditorPane editorPane  = new JEditorPane();
		Container contentPane = getContentPane();
		String url = "http://sunsite.berkeley.edu/apps/dev/jeditortest/MOA2Tour.html";
		try { 
			editorPane.setPage(url);
		}
		catch(IOException ex) { ex.printStackTrace(); }
		contentPane.add(new JScrollPane(editorPane), 
						BorderLayout.CENTER);
		editorPane.setEditable(false);
	}
}

There is no error message.
(Review ID: 57502)
======================================================================

Name: skT88420			Date: 05/06/99


I took some code of the bug reports which allowed someone to
convert HTML to text and back.  It works fine when run as an
application with java.exe but using appletviewer.exe and the
newest Java plugin 1.2, it shows nothing.  In a application frame
you can specify <B>Hello</B> and it will work, in an applet
it will show nothing.  But in an applet <HTML><BODY><B>Hello
</B></BODY></HTML> will work but produce absolutely no
attribute changes like bold, color or anything.

Here is the code, to run using appletviewer.exe, then change
some code around and run in java.exe:

Thanks.

//==============//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.net.*;
import java.io.*;

class GetHTML extends JFrame implements ActionListener {
   HTMLEditorKit htmlKit;
   JTextPane targetPane;
   JTextPane sourcePane;
   JButton targetButton;
   JButton sourceButton;
   JButton csButton;
   JButton ctButton;

   public GetHTML() {
      super("GetHTML");
      htmlKit = new HTMLEditorKit();

      // target pane contains the formatted HTML
      targetPane = new JTextPane();
      targetPane.setEditable(false);
      targetPane.setEditorKit(htmlKit);

      // source pane contains the unformatted HTML
      sourcePane = new JTextPane();

      // formats HTML source from source pane into target pane
      targetButton = new JButton("SOURCE -> TARGET");
      targetButton.setFont(new Font("Monospaced", Font.PLAIN, 10));
      targetButton.addActionListener(this);

      // "unformats" HTML from target pane to source pane
      sourceButton = new JButton("TARGET -> SOURCE");
      sourceButton.setFont(new Font("Monospaced", Font.PLAIN, 10));
      sourceButton.addActionListener(this);

      // clears the source pane
      csButton = new JButton("CLEAR SOURCE");
      csButton.setFont(new Font("Monospaced", Font.PLAIN, 10));
      csButton.addActionListener(this);

      // clears the target pane
      ctButton = new JButton("CLEAR TARGET");
      ctButton.setFont(new Font("Monospaced", Font.PLAIN, 10));
      ctButton.addActionListener(this);

      // assemble all of it

      JPanel buttonP = new JPanel();
      buttonP.setLayout(new GridLayout(2, 2, 2, 2));
      buttonP.add(targetButton);
      buttonP.add(sourceButton);
      buttonP.add(csButton);
      buttonP.add(ctButton);

      JPanel targetP = new JPanel();
      targetP.setLayout(new BorderLayout(0, 0));
      targetP.add(BorderLayout.NORTH, new JLabel("TARGET",
                                          SwingConstants.CENTER));
      JScrollPane tScroller = new JScrollPane(targetPane);
      targetP.add(BorderLayout.CENTER, tScroller);

      JPanel sourceP = new JPanel();
      sourceP.setLayout(new BorderLayout(0, 0));
      sourceP.add(BorderLayout.NORTH, new JLabel("SOURCE",SwingConstants.CENTER));
      JScrollPane sScroller = new JScrollPane(sourcePane);
      sourceP.add(BorderLayout.CENTER, sScroller);
                  
      JPanel panesPanel = new JPanel();
      panesPanel.setLayout(new GridLayout(2, 1, 2, 2));
      panesPanel.add(targetP);
      panesPanel.add(sourceP);

      this.getContentPane().setLayout(new BorderLayout(0, 0));
      this.getContentPane().add(BorderLayout.CENTER, panesPanel);
      this.getContentPane().add(BorderLayout.SOUTH, buttonP);

      this.addWindowListener(
         new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
               System.out.println("closing...");
               System.exit(0);
            }
         }
      );
   }

   private final void p(Object o) {
      System.out.println(o); 
   }

   // handle button clicks
   public void actionPerformed(ActionEvent evt) {
      String cmd = evt.getActionCommand();
      if (cmd.equals("SOURCE -> TARGET")) {
         p("to target...");
         dumpSourceToTarget();
      }
      else if (cmd.equals("TARGET -> SOURCE")) {
         p("to source...");
         dumpTargetToSource();
      }
      else if (cmd.equals("CLEAR SOURCE")) {
         clearSource();
      }
      else if (cmd.equals("CLEAR TARGET")) {
         clearTarget();
      }
   }

   // Puts the HTML source from the target pane into the source pane. The
   // crucial step here is that it takes the Document associated with
   // the target pane (formatted HTML) and writes it into a buffer (the
   // StringWriter object, which the HTMLEditorKit requires for a write).
   // We can now get the HTML from the writer.
   private void dumpTargetToSource() {
      clearSource();
      try {
         Document dstDoc = targetPane.getDocument();
         StringWriter writer = new StringWriter();
         int len = dstDoc.getLength();
         htmlKit.write(writer, dstDoc, 0, len);
         sourcePane.setText(writer.toString());
      } 
      catch (Exception ex) {
         p("Exception occurred...");
         ex.printStackTrace();
      }
   }

   // Puts the HTML source from the source pane into the target pane as
   // formatted HTML. Here we simply take the text from the source pane,
   // assume it's HTML, and read it into the target pane using the
   // target pane's HTMLEditorKit. The StringReader simply wraps a Reader
   // object around the source text so that the HTMLEditorKit can use it.
   private void dumpSourceToTarget() {
      clearTarget();
      try {
         String txt = sourcePane.getText();
         if ((txt == null) || (txt.trim().length() == 0)) { return; }
         Document dstDoc = targetPane.getDocument();
         StringReader reader = new StringReader(txt);
         htmlKit.read(reader, dstDoc, 0);
      } 
      catch (Exception ex) {
         p("Exception occurred...");
         ex.printStackTrace();
      }
   }

   // Clears the source pane
   private void clearSource() {
      sourcePane.setText("");
   }

   // Clears the document pane. I couldn't just set the text
   private void clearTarget() {
      targetPane.setDocument(htmlKit.createDefaultDocument());
   }

   public static void main(String[] args) {
      GetHTML f = new GetHTML();
      f.setSize(400, 400);
      f.setVisible(true);
      }
   }


//The JApplet used to show frame GetHTML
public class Test extends JApplet {
   public Test() {}
   
   public void init() {
      GetHTML f=new GetHTML();
      f.show();
   }
}

//The Java Plug-In 1.2 put this in an HTML file to test
/*
<HTML><BODY>

<OBJECT CLASSID="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH=250 HEIGHT=300 ALT="Plasma Duckhunt v3.0" CODEBASE="http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,1,0">
<PARAM NAME=TYPE VALUE="application/x-java-applet;version=1.2.1">
<PARAM NAME=CODE VALUE="Test.class">
</OBJECT>

</BODY></HTML>
*/
(Review ID: 57882)
======================================================================

Comments
WORK AROUND Name: dbT83986 Date: 04/13/99 A solution could be using the Netscape Privilege Manager: netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead"); but this a lot of coding (signing the applet?) and it is not portable to the Microsoft Internet Explorer. ====================================================================== Name: skT88420 Date: 04/26/99 For time being I am using a 3rd party HTML component that works just fine in our applet; but this exceeds our needs and more than doubles the size of the jar file that must be loaded to run the applet. (Review ID: 57502) ======================================================================
11-06-2004

PUBLIC COMMENTS This is a dup of 4181905, refer to it for more info on this. scott.violet@eng 1999-06-25
25-06-1999

EVALUATION This is a dup of 4181905, refer to it for more info on this. scott.violet@eng 1999-06-25
25-06-1999