⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingparseradaptor.java

📁 jetspeed源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jetspeed.util.rewriter;

// javax.swing.text
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.HTMLEditorKit;

// java.io
import java.io.*;

// java.util
import java.util.*;

// java.net
import java.net.*;
import org.apache.turbine.util.Log;//AAAtogli!


/*
 * HTML Parser Adaptor for the Swing 'HotJava' parser.
 *
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @version $Id: SwingParserAdaptor.java,v 1.6 2004/02/23 03:18:59 jford Exp $
 */

public class SwingParserAdaptor implements HTMLParserAdaptor
{

    private SwingParserAdaptor.Callback cb = new SwingParserAdaptor.Callback();
    private String lineSeparator;
    private boolean skippingImplied = false;
    private Rewriter rewriter;
    /*
     * Construct a swing (hot java) parser adaptor
     * Receives a Rewriter parameter, which is used as a callback when rewriting URLs.
     * The rewriter object executes the implementation specific URL rewriting.
     *
     * @param rewriter The rewriter object that is called back during URL rewriting
     */
    public SwingParserAdaptor(Rewriter rewriter)
    {
        this.rewriter = rewriter;
        lineSeparator = System.getProperty("line.separator", "\r\n");         
    }

    /*
     * Parses and an HTML document, rewriting all URLs as determined by the Rewriter callback
     *
     *
     * @param reader The input stream reader 
     *
     * @throws MalformedURLException 
     *
     * @return An HTML-String with rewritten URLs.
     */    
    public String run(Reader reader)
    throws MalformedURLException
    {
        HTMLEditorKit.Parser parser = new SwingParserAdaptor.ParserGetter().getParser();        

        String res ="";
        try
        {
            parser.parse(reader, cb, true);
            res = cb.getResult(); 
        } catch (Exception e)
        {
            e.printStackTrace();
      //Log.info("Exception occurred:" + e.toString());AAAtogli!!!
      //Log.info("Exception occurred:" + e.printStackTrace());
            throw new MalformedURLException(e.toString());
        }
        return res;
    }


    /*
     * This Class is needed, because getParser is protected and therefore 
     *  only accessibly by a subclass
     */
    class ParserGetter extends HTMLEditorKit
    {

        public HTMLEditorKit.Parser getParser(){
            return super.getParser();
        }
    } 


    /*
     *  Swing Parser Callback from the HTMLEditorKit.
     * This class handles all SAX-like events during parsing.
     *
     */
    class Callback extends HTMLEditorKit.ParserCallback
    {


        // either handling of <FORM> is buggy, or I made some weird mistake ... 
        // ... JDK 1.3 sends double "</form>"-tags on closing <form>
        private boolean inForm = false; 
        private boolean inScript = false; 
        private boolean emit = true;
        private boolean simpleTag = false;

        private StringWriter result = new StringWriter();

        private Callback () 
        {
        }

        //
        // -------------- Hot Java event callbacks... --------------------
        //

        /*
         *  Hot Java event callback for text (all data in between tags)
         * 
         * @param values The array of characters containing the text.
         */
        public void handleText(char[] values,int param) 
        {
             if (false == emit)                               
                 return;                                      
             if (values[0] == '>')                            
                 return;     
             if (false == rewriter.enterText(values, param))
                return;                    

            addToResult(values);
        }

        /*
         * Hot Java event callback for handling a simple tag (without begin/end)
         *
         * @param tag The HTML tag being handled.
         * @param attrs The mutable HTML attribute set for the current HTML element.         
         * @param position the position of the tag.         
         *
         */
        public void handleSimpleTag(HTML.Tag tag,MutableAttributeSet attrs,int param) 
        {
            simpleTag = true;
            if (false == rewriter.enterSimpleTagEvent(tag, attrs))
                return;

            if (false == isValidFragmentTag(tag))
                return;

            appendTagToResult(tag,attrs);        
            if (tag.toString().equalsIgnoreCase("param") ||
                tag.toString().equalsIgnoreCase("object") ||
                tag.toString().equalsIgnoreCase("embed"))
            {
                result.write(lineSeparator);
            }
            simpleTag = false;
            String appended = rewriter.exitSimpleTagEvent(tag, attrs);
            if (null != appended)
                result.write(appended);
        }

        /*
         * Hot Java event callback for handling a start tag.
         *
         * @param tag The HTML tag being handled.
         * @param attrs The mutable HTML attribute set for the current HTML element.         
         * @param position the position of the tag.         
         *
         */
        public void handleStartTag(HTML.Tag tag,  MutableAttributeSet attrs, int position) 
        {
            if (false == rewriter.enterStartTagEvent(tag, attrs))
                return;

            if (tag == HTML.Tag.HEAD)
            {
                emit = false;
                return;
            }

           if (false == isValidFragmentTag(tag))
                return;

            appendTagToResult(tag,attrs);
            formatLine(tag);
            String appended = rewriter.exitStartTagEvent(tag, attrs);
            if (null != appended)
                result.write(appended);
        }


        boolean isValidFragmentTag(HTML.Tag tag)
        {                    
            /*
            if (false == emit)
                return false;

            if (tag == HTML.Tag.HTML)  // always strip out HTML tag for fragments
                return false;

            if (tag == HTML.Tag.BODY)
                return false;

            if (tag == HTML.Tag.FRAMESET)  // always strip out FRAMESET tag for fragments
                return false;

            if (tag == HTML.Tag.FRAME)  
                return false;

            if (tag == HTML.Tag.NOFRAMES)  
                return false;
              */
            return true;
        }


        /*
         * Hot Java event callback for handling an end tag.
         *
         * @param tag The HTML tag being handled.
         * @param position the position of the tag.
         *
         */
        public void handleEndTag(HTML.Tag tag, int position) 
        {
            if (false == rewriter.enterEndTagEvent(tag))
                return;

            if (tag == HTML.Tag.HEAD)
            {
                emit = true;
                return;
            }

           if (false == isValidFragmentTag(tag))
                return;

           addToResult("</").addToResult(tag).addToResult(">");

            formatLine(tag);
            String appended = rewriter.exitEndTagEvent(tag);
            if (null != appended)
                result.write(appended);

        }


        /*
         * Hot Java event callback for handling errors.
         *
         * @param str The error message from Swing.
         * @param param A parameter passed to handler.
         *
         */
        public void handleError(java.lang.String str,int param) 
        {
            // ignored
        }

        /*
         * Hot Java event callback for HTML comments.
         *
         * @param values The character array of text comments.
         * @param param A parameter passed to handler.
         *
         */
        public void handleComment(char[] values,int param) 
        {
            // STRIP COMMENTS: addToResult(values);
            // this is questionable, we may need to turn this on for scripts inside comments
        }

        /*
         * Hot Java event callback for end of line strings.
         *
         * @param str The end-of-line string.
         *
         */
        public void handleEndOfLineString(java.lang.String str) 
        {
            addToResult(str);
        }


        /*
         * Prints new lines to make the output a little easier to read when debugging.
         *
         * @param tag The HTML tag being handled.         
         *
         */
        private void formatLine(HTML.Tag tag)
        {
            if (tag.isBlock() || 
                tag.breaksFlow() || 
                tag == HTML.Tag.FRAME ||
                tag == HTML.Tag.FRAMESET ||
                tag == HTML.Tag.SCRIPT)
            {
                result.write(lineSeparator);
            }
        }


        /*
         * Used to write tag and attribute objects to the output stream.
         * Returns a reference to itself so that these calls can be chained.
         *
         * @param txt Any text to be written out to stream with toString method.
         *            The object being written should implement its toString method.
         * @return A handle to the this, the callback, for chaining results.
         *
         */
        private Callback addToResult(Object txt)
        {
            // to allow for implementation using Stringbuffer or StringWriter
            // I don't know yet, which one is better in this case
            //if (ignoreLevel > 0 ) return this;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -