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

📄 pprint.java

📁 windows 代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    private void printTag(Lexer lexer, Out fout, short mode, int indent, Node node)
    {
        char c;
        String p;

        addC('<', linelen++);

        if (node.type == Node.EndTag)
            addC('/', linelen++);

        p = node.element;
        for (int i = 0; i < p.length(); i++ )
            addC((int)Lexer.foldCase(p.charAt(i),
                                     this.configuration.UpperCaseTags,
                                     this.configuration.XmlTags),
                 linelen++);

        printAttrs(fout, indent, node, node.attributes);

        if ((this.configuration.XmlOut || lexer != null && lexer.isvoyager) &&
                (node.type == Node.StartEndTag || (node.tag.model & Dict.CM_EMPTY) != 0))
        {
            addC(' ', linelen++);   /* compatibility hack */
            addC('/', linelen++);
        }

        addC('>', linelen++);;

        if (node.type != Node.StartEndTag && !((mode & PREFORMATTED) != 0))
        {
            if (indent + linelen >= this.configuration.wraplen)
                wrapLine(fout, indent);

            if (indent + linelen < this.configuration.wraplen)
            {
                /*
                 wrap after start tag if is <br/> or if it's not
                 inline or it is an empty tag followed by </a>
                */
                if (afterSpace(node))
                {
                    if (!((mode & NOWRAP) != 0) &&
                        (!((node.tag.model & Dict.CM_INLINE) != 0) ||
                          (node.tag == TagTable.tagBr) ||
                          (((node.tag.model & Dict.CM_EMPTY) != 0) && 
                          node.next == null &&
                          node.parent.tag == TagTable.tagA)))
                    {
                        wraphere = linelen;
                    }
                }
            }
            else
                condFlushLine(fout, indent);
        }
    }

    private void printEndTag(Out fout, short mode, int indent, Node node)
    {
        char c;
        String p;

       /*
         Netscape ignores SGML standard by not ignoring a
         line break before </A> or </U> etc. To avoid rendering 
         this as an underlined space, I disable line wrapping
         before inline end tags by the #if 0 ... #endif
       */
if (false) {
        if (indent + linelen < this.configuration.wraplen && !((mode & NOWRAP) != 0))
            wraphere = linelen;
}

        addC('<', linelen++);
        addC('/', linelen++);

        p = node.element;
        for (int i = 0; i < p.length(); i++ )
            addC((int)Lexer.foldCase(p.charAt(i),
                                     this.configuration.UpperCaseTags,
                                     this.configuration.XmlTags),
                 linelen++);

        addC('>', linelen++);
    }

    private void printComment(Out fout, int indent, Node node)
    {
        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;

        addC('<', linelen++);
        addC('!', linelen++);
        addC('-', linelen++);
        addC('-', linelen++);
if (false) {
        if (linelen < this.configuration.wraplen)
            wraphere = linelen;
}
        printText(fout, COMMENT, indent,
                        node.textarray, node.start, node.end);
if (false) {
        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;
}
        // See Lexer.java: AQ 8Jul2000
        addC('-', linelen++);
        addC('-', linelen++);
        addC('>', linelen++);

        if (node.linebreak)
            flushLine(fout, indent);
    }

    private void printDocType(Out fout, int indent, Node node)
    {
        boolean q = this.configuration.QuoteMarks;

        this.configuration.QuoteMarks = false;

        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;

        condFlushLine(fout, indent);

        addC('<', linelen++);
        addC('!', linelen++);
        addC('D', linelen++);
        addC('O', linelen++);
        addC('C', linelen++);
        addC('T', linelen++);
        addC('Y', linelen++);
        addC('P', linelen++);
        addC('E', linelen++);
        addC(' ', linelen++);

        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;

        printText(fout, (short)0, indent,
                        node.textarray, node.start, node.end);

        if (linelen < this.configuration.wraplen)
            wraphere = linelen;

        addC('>', linelen++);
        this.configuration.QuoteMarks = q;
        condFlushLine(fout, indent);
    }

    private void printPI(Out fout, int indent, Node node)
    {
        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;

        addC('<', linelen++);
        addC('?', linelen++);

        /* set CDATA to pass < and > unescaped */
        printText(fout, CDATA, indent,
                    node.textarray, node.start, node.end);

        if (node.textarray[node.end - 1] != (byte)'?')
            addC('?', linelen++);

        addC('>', linelen++);
        condFlushLine(fout, indent);
    }

    /* note ASP and JSTE share <% ... %> syntax */
    private void printAsp(Out fout, int indent, Node node)
    {
        int savewraplen = this.configuration.wraplen;

        /* disable wrapping if so requested */

        if (!this.configuration.WrapAsp || !this.configuration.WrapJste)
            this.configuration.wraplen = 0xFFFFFF;  /* a very large number */
if (false) { //#if 0
        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;
} //#endif

        addC('<', linelen++);
        addC('%', linelen++);

        printText(fout, (this.configuration.WrapAsp ? CDATA : COMMENT), indent,
                    node.textarray, node.start, node.end);

        addC('%', linelen++);
        addC('>', linelen++);
        /* condFlushLine(fout, indent); */
        this.configuration.wraplen = savewraplen;
    }

    /* JSTE also supports <# ... #> syntax */
    private void printJste(Out fout, int indent, Node node)
    {
        int savewraplen = this.configuration.wraplen;

        /* disable wrapping if so requested */

        if (!this.configuration.WrapJste)
            this.configuration.wraplen = 0xFFFFFF;  /* a very large number */

        addC('<', linelen++);
        addC('#', linelen++);

        printText(fout, (this.configuration.WrapJste ? CDATA : COMMENT), indent,
                    node.textarray, node.start, node.end);

        addC('#', linelen++);
        addC('>', linelen++);
        /* condFlushLine(fout, indent); */
        this.configuration.wraplen = savewraplen;
    }

    /* PHP is based on XML processing instructions */
    private void printPhp(Out fout, int indent, Node node)
    {
        int savewraplen = this.configuration.wraplen;

        /* disable wrapping if so requested */

        if (!this.configuration.WrapPhp)
            this.configuration.wraplen = 0xFFFFFF;  /* a very large number */

if (false) { //#if 0
        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;
} //#endif
        addC('<', linelen++);
        addC('?', linelen++);

        printText(fout, (this.configuration.WrapPhp ? CDATA : COMMENT), indent,
                        node.textarray, node.start, node.end);

        addC('?', linelen++);
        addC('>', linelen++);
        /* PCondFlushLine(fout, indent); */
        this.configuration.wraplen = savewraplen;
    }

    private void printCDATA(Out fout, int indent, Node node)
    {
        int savewraplen = this.configuration.wraplen;

        condFlushLine(fout, indent);

        /* disable wrapping */

        this.configuration.wraplen = 0xFFFFFF;  /* a very large number */

        addC('<', linelen++);
        addC('!', linelen++);
        addC('[', linelen++);
        addC('C', linelen++);
        addC('D', linelen++);
        addC('A', linelen++);
        addC('T', linelen++);
        addC('A', linelen++);
        addC('[', linelen++);

        printText(fout, COMMENT, indent,
                        node.textarray, node.start, node.end);

        addC(']', linelen++);
        addC(']', linelen++);
        addC('>', linelen++);
        condFlushLine(fout, indent);
        this.configuration.wraplen = savewraplen;
    }

    private void printSection(Out fout, int indent, Node node)
    {
        int savewraplen = this.configuration.wraplen;

        /* disable wrapping if so requested */

        if (!this.configuration.WrapSection)
            this.configuration.wraplen = 0xFFFFFF;  /* a very large number */

if (false) { //#if 0
        if (indent + linelen < this.configuration.wraplen)
            wraphere = linelen;
} //#endif
        addC('<', linelen++);
        addC('!', linelen++);
        addC('[', linelen++);

        printText(fout, (this.configuration.WrapSection ? CDATA : COMMENT), indent,
                        node.textarray, node.start, node.end);

        addC(']', linelen++);
        addC('>', linelen++);
        /* PCondFlushLine(fout, indent); */
        this.configuration.wraplen = savewraplen;
    }

    private boolean shouldIndent(Node node)
    {
        if (!this.configuration.IndentContent)
            return false;

        if (this.configuration.SmartIndent)
        {
            if (node.content != null && ((node.tag.model & Dict.CM_NO_INDENT) != 0))
            {
                for (node = node.content; node != null; node = node.next)
                    if (node.tag != null && (node.tag.model & Dict.CM_BLOCK) != 0)
                        return true;

                return false;
            }

            if ((node.tag.model & Dict.CM_HEADING) != 0)
                return false;

            if (node.tag == TagTable.tagP)
                return false;

            if (node.tag == TagTable.tagTitle)
                return false;
        }

        if ((node.tag.model & (Dict.CM_FIELD | Dict.CM_OBJECT)) != 0)
            return true;

        if (node.tag == TagTable.tagMap)
            return true;

        return !((node.tag.model & Dict.CM_INLINE) != 0);
    }

    public void printTree(Out fout, short mode, int indent,
                          Lexer lexer, Node node)
    {
        Node content, last;

        if (node == null)
            return;

        if (node.type == Node.TextNode)
            printText(fout, mode, indent,
                        node.textarray, node.start, node.end);
        else if (node.type == Node.CommentTag)
        {
            printComment(fout, indent, node);
        }
        else if (node.type == Node.RootNode)
        {
            for (content = node.content;
                    content != null;
                    content = content.next)
               printTree(fout, mode, indent, lexer, content);
        }
        else if (node.type == Node.DocTypeTag)
            printDocType(fout, indent, node);
        else if (node.type == Node.ProcInsTag)
            printPI(fout, indent, node);
        else if (node.type == Node.CDATATag)
            printCDATA(fout, indent, node);
        else if (node.type == Node.SectionTag)
            printSection(fout, indent, node);
        else if (node.type == Node.AspTag)
            printAsp(fout, indent, node);
        else if (node.type == Node.JsteTag)
            printJste(fout, indent, node);
        else if (node.type == Node.PhpTag)
            printPhp(fout, indent, node);
        else if ((node.tag.model & Dict.CM_EMPTY) != 0 || node.type == Node.StartEndTag)
        {
            if (!((node.tag.model & Dict.CM_INLINE) != 0))
                condFlushLine(fout, indent);

            if (node.tag == TagTable.tagBr && node.prev != null &&
                node.prev.tag != TagTable.tagBr && this.configuration.BreakBeforeBR)
                flushLine(fout, indent);

            if (this.configuration.MakeClean && node.tag == TagTable.tagWbr)
                printString(fout, indent, " ");
            else
                printTag(lexer, fout, mode, indent, node);

            if (node.tag == TagTable.tagParam || node.tag == TagTable.tagArea)
                condFlushLine(fout, indent);
            else if (node.tag == TagTable.tagBr || node.tag == TagTable.tagHr)
                flushLine(fout, indent);
        }
        else /* some kind of container element */
        {
            if (node.tag != null && node.tag.parser == ParserImpl.getParsePre())
            {
                condFlushLine(fout, indent);

                indent = 0;
                condFlushLine(fout, indent);
                printTag(lexer, fout, mode, indent, node);
                flushLine(fout, indent);

                for (content = node.content;
                        content != null;
                        content = content.next)
                    printTree(fout, (short)(mode | PREFORMATTED | NOWRAP), indent, lexer, content);

                condFlushLine(fout, indent);
                printEndTag(fout, mode, indent, node);
                flushLine(fout, indent);

                if (this.configuration.IndentContent == false && node.next != null)
                    flushLine(fout, indent);
            }
            else if (node.tag == TagTable.tagStyle || node.tag == TagTable.tagScript)
            {
                condFlushLine(fout, indent);

                indent = 0;
                condFlushLine(fout, indent);
                printTag(lexer, fout, mode, indent, node);
                flushLine(fout, indent);

                for (content = node.content;
                        content != null;
                        content = content.next)
                    printTree(fout, (short)(mode | PREFORMATTED | NOWRAP |CDATA), indent, lexer, content);

                condFlushLine(fout, indent);
                printEndTag(fout, mode, indent, node);
                flushLine(fout, indent);

                if (this.configuration.IndentContent == false && node.next != null)
                    flushLine(fout, indent);
            }
            else if ((node.tag.model & Dict.CM_INLINE) != 0)
            {
                if (this.configuration.MakeClean)
                {
                    /* discards <font> and </font> tags */
                    if (node.tag == TagTable.tagFont)
                    {
                        for (content = node.content;
                                content != null;
                                content = content.next)
                            printTree(fout, mode, indent, lexer, content);
                        return;
                    }

                    /* replace <nobr>...</nobr> by &nbsp; or &#160; etc. */
                    if (node.tag == TagTable.tagNobr)
                    {
                        for (content = node.content;
                                content != null;
                                content = content.next)
                            printTree(fout, (short)(mode|NOWRAP), indent, lexer, content);
                        return;
                    }
                }

                /* otherwise a normal inline element */

⌨️ 快捷键说明

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