`

HTML rendering : Java Glossary

    博客分类:
  • Web
阅读更多

Source: http://mindprod.com/jgloss/htmlrendering.html

 

HTML rendering<!-- alias rendering HTML --> HTML rendering refers to analying the tags such as <b> and merging them with the text to produce a formatted display.

In Applets

If you writing Java source code for an Applet , you can persuade the browser you are running

<!-- macro JDisplay htmlrendering.example101.javafrag --><!-- generated -->
java
.
net
.
URL 
url 
= 
new 
java
.
net
.
URL 
(
 "http://mindprod.com"
 )
;

getAppletContext
(
)
.
showDocument
(
 url 
)
;
<!-- /generated by JDisplay -->

That code will fail with a NullPointerException if you attempt it in an application. You can catch all exceptions and just ignore the request in code that sometimes runs as an Applet and sometimes as an application.

There are some catches:

  • The URL must be absolute and must have a protocol e.g. http: or file: . Use getDocumentBase or getCodeBase and the URL context-constructor to make it absolute if you have a relative one. You must always have at least filename in your relative part.
  • Older versions of Opera did not work showDocument if there is a trailing #XXXX on your URL.
  • Netscape 4.79 does not work at all with JApplet s.
  • showDocument ignores the BASE TARGET of the current page. You must specify it explicitly as the second parameter to showDocument .

In Applications

  • Unfortunately showDocument does not allow you to take a hunk of HTML you have in a String, perhaps as the result of a POST, and display it. In that case you somehow have to persuade your server to put that html in a file where you can find it via URL. Alternatively you could use Sun’s not-free HTML rendering classes.
  • In <!-- macro Java 1.2 --><!-- generated -->Java version 1.2 <!-- /generated by Java --> there is javax.swing.text .HTMLEditorKit . which offers primitive HTML 3.2 rendering ability, inching toward HTML 4 compatibility. It also supports surprising amount of CSS. You use it like this: <!-- macro JDisplay htmlrendering.example1.javafrag --><!-- generated -->
    output 
    = 
    new 
    JEditorPane
    (
    )
    ;
    
    output
    .
    setContentType
    (
     "text/html"
     )
    ;
    
    output
    .
    setEditable
    (
     false 
    )
    ;
    
    <!-- /generated by JDisplay --> From then on text in the pane is rendered as HTML. You can also use <!-- macro JDisplay htmlrendering.example2.javafrag --><!-- generated -->
    output
    .
    setPage
    (
     "http://mindprod.com"
     )
    ;
    
    <!-- /generated by JDisplay --> to render a page.
  • Here is how you can render and URL pointing to an HTML page inside Java.
  • /*
     * @(#)TestHTMLRendering.java
     *
     * Summary: Eaxample use of Java HTML Rendering. Renders HTML 3.2 plus some CSS. Does not ignore comments properly. Does not.
     *
     * Copyright: (c) 2009-2010 Roedy Green, Canadian Mind Products, http://mindprod.com
     *
     * Licence: This software may be copied and used freely for any purpose but military.
     *          http://mindprod.com/contact/nonmil.html
     *
     * Requires: JDK 1.6+
     *
     * Created with: IntelliJ IDEA IDE.
     *
     * Version History:
     *  1.0 2009-01-01 - initial version
     */
    package com.mindprod.example;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    import java.awt.Color;
    import java.awt.Container;
    import java.io.IOException;
    
    import static java.lang.System.err;
    import static java.lang.System.out;
    
    /**
     * Eaxample use of Java HTML Rendering. Renders HTML 3.2 plus some CSS. Does not ignore comments properly. Does not.
     * <p/>
     * implement clickable links.
     *
     * @author Roedy Green, Canadian Mind Products
     * @version 1.0 2009-01-01 - initial version
     * @since 2009-01-01
     */
    @SuppressWarnings( { "UnusedDeclaration" } )
    final class TestHTMLRendering
        {
        // ------------------------------ CONSTANTS ------------------------------
    
        /**
         * height of frame in pixels
         */
        private static final int height = 1000;
    
        /**
         * width of frame in pixels
         */
        private static final int width = 1000;
    
        private static final String RELEASE_DATE = "2007-10-04";
    
        /**
         * title for frame
         */
        private static final String TITLE_STRING = "HTML Rendering";
    
        /**
         * URL of page we want to display
         */
        private static final String URL = "http://mindprod.com/index.html";
    
        /**
         * program version
         */
        private static final String VERSION_STRING = "1.0";
    
        // --------------------------- main() method ---------------------------
    
        /**
         * Debugging harness for a JFrame
         *
         * @param args command line arguments are ignored.
         */
        @SuppressWarnings( { "UnusedParameters" } )
        public static void main( String args[] )
            {
            // Invoke the run method on the Swing event dispatch thread
            // Sun now recommends you call ALL your GUI methods on the Swing
            // event thread, even the initial setup.
            // Could also use invokeAndWait and catch exceptions
            SwingUtilities.invokeLater( new Runnable()
            {
            /**
             * } fire up a JFrame on the Swing thread
             */
            public void run()
                {
                out.println( "Starting" );
                final JFrame jframe =
                        new JFrame( TITLE_STRING + " " + VERSION_STRING );
                Container contentPane = jframe.getContentPane();
                jframe.setSize( width, height );
    
                contentPane.setBackground( Color.YELLOW );
                contentPane.setForeground( Color.BLUE );
                jframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                try
                    {
                    out.println( "acquiring URL" );
                    JEditorPane jep = new JEditorPane( URL );
                    out.println( "URL acquired" );
                    JScrollPane jsp =
                            new JScrollPane( jep,
                                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
                    contentPane.add( jsp );
                    }
                catch ( IOException e )
                    {
                    err.println( "can't find URL" );
                    contentPane.add( new JLabel( "can't find URL" ) );
                    }
                jframe.validate();
                jframe.setVisible( true );
                // Shows page, with HTML comments erroneously displayed.
                // The links are not clickable.
                }
            } );
            }// end main
        }// end TestHTMLRendering
     
  • In older JVMs, if you are writing an application you are SOL. For applications, there is no standard HTML rendering class, though BISS-AWT has some primitive HTML rendering code.
  • Flying Saucer is very rigid about standards. It will not render the typical crap you find on the web, only your own carefully validated markup.
  • JDIC Browser is a means to embed Gecko into your Java app, giving pretty much all the rendering engine you might need.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics