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

📄 xhtmlelementtowikitranslator.java

📁 wiki建站资源 java编写的 很好用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ecyrd.jspwiki.htmltowiki;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.xpath.XPath;

/**
 * Converting XHtml to Wiki Markup
 * 
 * @author Sebastian Baltes (sbaltes@gmx.com)
 */
public class XHtmlElementToWikiTranslator
{

    private XHtmlToWikiConfig config;

    private WhitespaceTrimWriter outTimmer;

    private PrintWriter out;

    private LiStack li = new LiStack();

    private PreStack pre = new PreStack();

    public XHtmlElementToWikiTranslator( Element base ) throws IOException, JDOMException
    {
        this( base, new XHtmlToWikiConfig() );
    }

    public XHtmlElementToWikiTranslator( Element base, XHtmlToWikiConfig config ) throws IOException, JDOMException
    {
        this.config = config;
        outTimmer = new WhitespaceTrimWriter();
        out = new PrintWriter( outTimmer );
        print( base );
    }

    public String getWikiString()
    {
        return outTimmer.toString();
    }

    private void print( String s )
    {
        s = StringEscapeUtils.unescapeHtml( s );
        out.print( s );
    }

    private void print( Object element ) throws IOException, JDOMException
    {
        if( element instanceof Text )
        {
            Text t = (Text)element;
            String s = t.getText();
            if( pre.isPreMode() )
            {
                out.print( s );
            }
            else
            {
                s = s.replaceAll( "\\s+", " " );
                if( !s.equals( " " ) )
                {
                    out.print( s );
                }
            }
        }
        else if( element instanceof Element )
        {
            Element base = (Element)element;
            if( "imageplugin".equals( base.getAttributeValue( "class" ) ) )
            {
                printImage( base );
            }
            else
            {
                boolean bold = false;
                boolean italic = false;
                boolean monospace = false;
                String cssSpecial = null;
                Map styleProps = getStylePropertiesLowerCase( base );
                if( styleProps != null )
                {
                    String weight = (String)styleProps.remove( "font-weight" );
                    String style = (String)styleProps.remove( "font-style" );
                    String font = (String)styleProps.remove( "font-family" );
                    String center = (String)styleProps.get( "text-align" );
                    if( "center".equals( center ) )
                    {
                        styleProps.put( "display", "block" );
                    }
                    italic = "oblique".equals( style ) || "italic".equals( style );
                    bold = "bold".equals( weight ) || "bolder".equals( weight );
                    monospace = font != null && (font.indexOf( "mono" ) >= 0 || font.indexOf( "courier" ) >= 0);
                    if( !styleProps.isEmpty() )
                    {
                        cssSpecial = propsToStyleString( styleProps );
                    }
                }
                if( bold )
                {
                    out.print( "__" );
                }
                if( italic )
                {
                    out.print( "''" );
                }
                if( monospace )
                {
                    out.print( "{{{" );
                    pre.push();
                }
                if( cssSpecial != null )
                {
                    out.print( "%%(" + cssSpecial + " )" );
                }
                printChildren( base );
                if( cssSpecial != null )
                {
                    out.print( "%%" );
                }
                if( monospace )
                {
                    pre.pop();
                    out.print( "}}}" );
                }
                if( italic )
                {
                    out.print( "''" );
                }
                if( bold )
                {
                    out.print( "__" );
                }
            }
        }
    }

    private void printChildren( Element base ) throws IOException, JDOMException
    {
        for( Iterator i = base.getContent().iterator(); i.hasNext(); )
        {
            Object c = i.next();
            if( c instanceof Element )
            {
                Element e = (Element)c;
                String n = e.getName().toLowerCase();
                if( n.equals( "h1" ) )
                {
                    out.print( "!!!" );
                    print( e );
                    out.println();
                }
                else if( n.equals( "h2" ) )
                {
                    out.print( "!!" );
                    print( e );
                    out.println();
                }
                else if( n.equals( "h3" ) )
                {
                    out.print( "!" );
                    print( e );
                    out.println();
                }
                else if( n.equals( "h4" ) )
                {
                    out.print( "!" );
                    print( e );
                    out.println();
                }
                else if( n.equals( "p" ) )
                {
                    out.println();
                    out.println();
                    print( e );
                    out.println();
                    out.println();
                }
                else if( n.equals( "br" ) )
                {
                    if( pre.isPreMode() )
                    {
                        out.println();
                    }
                    else
                    {
                        out.print( " \\\\" );
                    }
                    print( e );
                }
                else if( n.equals( "hr" ) )
                {
                    out.println();
                    print( "----" );
                    print( e );
                    out.println();
                }
                else if( n.equals( "table" ) )
                {
                    if( !outTimmer.isCurrentlyOnLineBegin() )
                    {
                        out.println();
                    }
                    print( e );
                }
                else if( n.equals( "tr" ) )
                {
                    print( e );
                    out.println();
                }
                else if( n.equals( "td" ) )
                {
                    out.print( "| " );
                    print( e );
                    if( !pre.isPreMode() )
                    {
                        print( " " );
                    }
                }
                else if( n.equals( "th" ) )
                {
                    out.print( "|| " );
                    print( e );
                    if( !pre.isPreMode() )
                    {
                        print( " " );
                    }
                }
                else if( n.equals( "a" ) )
                {
                    if( !isIgnorableWikiMarkupLink( e ) )
                    {
                        if( e.getChild( "IMG" ) != null )
                        {
                            printImage( e );
                        }
                        else
                        {
                            String ref = e.getAttributeValue( "href" );
                            if( ref == null )
                            {
                                print( e );
                            }
                            else
                            {
                                ref = trimLink( ref );
                                if( ref != null )
                                {
                                    if( ref.startsWith( "#" ) )

⌨️ 快捷键说明

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