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

📄 xcustomhtmlfactory.java

📁 XBrowser是一个完全免费并且开源的Web浏览器
💻 JAVA
字号:
/****************************************************************
*              XBrowser  -  eXtended web Browser                *
*                                                               *
*           Copyright (c) 2000-2001  Armond Avanes              *
*     Refer to ReadMe & License files for more information      *
*                                                               *
*                                                               *
*                 By: Uladzimir V. Kavalchuk                    *
*               Uladzimir_Kavalchuk@ep.minsk.by                 *
*               http://xbrowser.sourceforge.net/                *
*****************************************************************/
package xbrowser.renderer.custom;

import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

import xbrowser.renderer.custom.js.*;

public class XCustomHTMLFactory  {

    protected void addScript( String text, Document doc) {
	   XIScriptConsumer sc = getConsumer( doc);
		if( sc != null)
            sc.addScript( text, startScriptTag);
    }

	protected void addImage( XJSImage img, Document doc) {
	    XDOM dom = getDOMSink(doc);
		if( dom != null)
			dom.addObject( img, "", XDOM.IMAGES);
	}

	protected void setEndScriptTag( Element startScriptTag, Element elem) {
	    XIScriptConsumer sc = getConsumer( elem.getDocument());
        if( sc != null)
			sc.setEndScriptTag( startScriptTag, elem);
	}

	protected void endParsing( Document doc) {
	    XIScriptConsumer sc = getConsumer( doc);
        if( sc != null)
			sc.endParsing();
	}

    public View create( Element elem, HTML.Tag kind) {

        out( "Elem: " + elem.getName() + ", kind: " + kind);
        if( kind == HTML.Tag.SCRIPT) {

            out( "@" + elem.getStartOffset() + ( ! insideScriptTag ? "<SCRIPT>: " : "</SCRIPT>"));
		    if( ! insideScriptTag) {
				startScriptTag = elem;
				out( "<SCRIPT>? " + startScriptTag);
			} else { // tell about </SCRIPT>
				 out( "</SCRIPT>? " + elem);
			     setEndScriptTag( startScriptTag, elem);
				 startScriptTag = null;
			}
            insideScriptTag = !insideScriptTag;
        }
        else if( insideScriptTag && kind == HTML.Tag.COMMENT) {
            out( "<COMMENT>: ");
            out( dumpComment( elem));
        }
        else if( insideScriptTag && kind == HTML.Tag.CONTENT)
            out( dumpScriptContents( elem));
        else if( kind == HTML.Tag.IMG) {
            View myView = null; // new BetterImageView( elem);

			XJSImage img = new XJSImage( elem);
			Document doc = elem.getDocument();
			addImage( img, doc);
			String imgSrc = img.getSrc();
			if( imgSrc != null && ! imgSrc.toLowerCase().startsWith( "file"))
			try {
				URL url = new URL( ((HTMLDocument)doc).getBase(), imgSrc);
				if( url.getProtocol().equalsIgnoreCase("http") &&
					  ! XRendererSupport.getContext().getCacheManager().isCached( url.toString())) {
					// Save in background thread
					File imgCacheFile = XRendererSupport.getContext().getCacheManager().getFileToCache( url.toString());
					if( doc instanceof XHTMLDocument) {
					    XHTMLDocument hdoc = (XHTMLDocument)doc;
						Thread saveThread = new XRendererSupport.SavingThread( url, imgCacheFile, hdoc.getSaveQueue());
						hdoc.addSaveThread( saveThread);

						saveThread.start();
					}
				}
			} catch( MalformedURLException mfe) {
			    XRendererSupport.getContext().getLogger().error( this, "Bad URL in image: " + imgSrc);
			} catch( IOException iox) {
			    XRendererSupport.getContext().getLogger().error( this, iox);
			}

            return myView;
        }
        else if( kind == HTML.Tag.BODY) {
		    out("<BODY> encountered!");
			AttributeSet attrs = elem.getAttributes();

			extractProperty( elem, attrs, "FGCOLOR", "BLACK");
			extractProperty( elem, attrs, "BGCOLOR", "WHITE");
			extractProperty( elem, attrs, "LINKCOLOR", "BLUE");
			extractProperty( elem, attrs, "ALINKCOLOR", "BLUE");
			extractProperty( elem, attrs, "VLINKCOLOR", "#800080");

			insideScriptTag = false;
			startScriptTag = null;
		}
		else if( (kind == HTML.Tag.INPUT) ||
				 (kind == HTML.Tag.SELECT) ||
				 (kind == HTML.Tag.TEXTAREA)
			   )
			return new XFormView(elem);

        return null;
    } // create

	private void extractProperty( Element elem, AttributeSet attrs, String name, String defValue) {
	    Object a = attrs.getAttribute( name);
		String value = ( a == null) ? defValue : a.toString();
	    XDOM dom = getDOMSink( elem.getDocument());
		if( dom != null)
			dom.setDocProperty( name, value);
	}

    private String dumpComment( Element elem) {
        AttributeSet as = elem.getAttributes();
        if (as != null) {
            Object comment = as.getAttribute(HTML.Attribute.COMMENT);
            if (comment instanceof String) {
                String script = (String)comment;
                addScript( script, elem.getDocument());
                return "script comment {" + script + "}";
            }
        }
        return "...No script in comment...";
    }

    private String dumpScriptContents( Element elem) {
        String result = "script contents: {";
        int start = elem.getStartOffset();
        int end = elem.getEndOffset();
        Document doc = elem.getDocument();
        try {
            String text = doc.getText( start, end-start);
            addScript( text, doc);
            result += text;
        } catch( BadLocationException e) {
            XRendererSupport.getContext().getLogger().error( this, "BadLoc(" + start + ", " + end);
        }
        return result + "}";
    }

    private void out( String s) {
		//XRendererSupport.getContext().getLogger().message(this, s);
    }

	private XIScriptConsumer getConsumer( Document doc) {
        if( doc instanceof XIScriptConsumer)
			return (XIScriptConsumer)doc;
		XRendererSupport.getContext().getLogger().error( this, "Document is not XIScriptConsumer!");
		return null;
	}

	private XDOM getDOMSink( Document doc) {
        XIScriptConsumer sc = getConsumer( doc);
		if( sc != null) {
			XDOM dom = sc.getSink();
			if( dom == null)
				XRendererSupport.getContext().getLogger().error( this, "Document's sink is null!");
			return dom;
        }
		return null;
	}

    // public static abstract class BetterImageView extends View { } // ImageView {

// Attributes:
    private boolean insideScriptTag = false;
	private Element startScriptTag = null;
    // int startScript = -1;
}

⌨️ 快捷键说明

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