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

📄 tidy.java

📁 windows 代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    /**
     * Command line interface to parser and pretty printer.
     */

    public static void main(String[] argv)
    {
        int totalerrors = 0;
        int totalwarnings = 0;
        String file;
        InputStream in;
        String prog = "Tidy";
        Node  document;
        Node  doctype;
        Lexer lexer;
        String s;
        Out out = new OutImpl();   /* normal output stream */
        PPrint pprint;
        int argc = argv.length + 1;
        int argIndex = 0;
        Tidy tidy;
        Configuration configuration;
        String arg;
        String current_errorfile = "stderr";

        tidy = new Tidy();
        configuration = tidy.getConfiguration();

        /* read command line */

        while (argc > 0)
        {
            if (argc > 1 && argv[argIndex].startsWith("-"))
            {
                /* support -foo and --foo */
                arg = argv[argIndex].substring(1);

                if (arg.length() > 0 && arg.charAt(0) == '-')
                    arg = arg.substring(1);

                if (arg.equals("xml"))
                    configuration.XmlTags = true;
                else if (arg.equals("asxml") || arg.equals("asxhtml"))
                    configuration.xHTML = true;
                else if (arg.equals("indent"))
                {
                    configuration.IndentContent = true;
                    configuration.SmartIndent = true;
                }
                else if (arg.equals("omit"))
                    configuration.HideEndTags = true;
                else if (arg.equals("upper"))
                    configuration.UpperCaseTags = true;
                else if (arg.equals("clean"))
                    configuration.MakeClean = true;
                else if (arg.equals("raw"))
                    configuration.CharEncoding = Configuration.RAW;
                else if (arg.equals("ascii"))
                    configuration.CharEncoding = Configuration.ASCII;
                else if (arg.equals("latin1"))
                    configuration.CharEncoding = Configuration.LATIN1;
                else if (arg.equals("utf8"))
                    configuration.CharEncoding = Configuration.UTF8;
                else if (arg.equals("iso2022"))
                    configuration.CharEncoding = Configuration.ISO2022;
                else if (arg.equals("mac"))
                    configuration.CharEncoding = Configuration.MACROMAN;
                else if (arg.equals("numeric"))
                    configuration.NumEntities = true;
                else if (arg.equals("modify"))
                    configuration.writeback = true;
                else if (arg.equals("change"))  /* obsolete */
                    configuration.writeback = true;
                else if (arg.equals("update"))  /* obsolete */
                    configuration.writeback = true;
                else if (arg.equals("errors"))
                    configuration.OnlyErrors = true;
                else if (arg.equals("quiet"))
                    configuration.Quiet = true;
                else if (arg.equals("slides"))
                    configuration.BurstSlides = true;
                else if (arg.equals("help") ||
                         argv[argIndex].charAt(1) == '?'||
                         argv[argIndex].charAt(1) == 'h')
                {
                    Report.helpText(new PrintWriter(System.out, true), prog);
                    System.exit(1);
                }
                else if (arg.equals("config"))
                {
                    if (argc >= 3)
                    {
                        configuration.parseFile(argv[argIndex + 1]);
                        --argc;
                        ++argIndex;
                    }
                }
                else if (argv[argIndex].equals("-file") ||
                         argv[argIndex].equals("--file") ||
                            argv[argIndex].equals("-f"))
                {
                    if (argc >= 3)
                    {
                        configuration.errfile =
                            new String(argv[argIndex + 1]);
                        --argc;
                        ++argIndex;
                    }
                }
                else if (argv[argIndex].equals("-wrap") ||
                         argv[argIndex].equals("--wrap") ||
                            argv[argIndex].equals("-w"))
                {
                    if (argc >= 3)
                    {
                        configuration.wraplen =
                            Integer.parseInt(argv[argIndex + 1]);
                        --argc;
                        ++argIndex;
                    }
                }
                else if (argv[argIndex].equals("-version") ||
                         argv[argIndex].equals("--version") ||
                            argv[argIndex].equals("-v"))
                {
                    Report.showVersion(tidy.getErrout());
                    System.exit(0);
                }
                else
                {
                    s = argv[argIndex];

                    for (int i = 1; i < s.length(); i++)
                    {
                        if (s.charAt(i) == 'i')
                        {
                            configuration.IndentContent = true;
                            configuration.SmartIndent = true;
                        }
                        else if (s.charAt(i) == 'o')
                            configuration.HideEndTags = true;
                        else if (s.charAt(i) == 'u')
                            configuration.UpperCaseTags = true;
                        else if (s.charAt(i) == 'c')
                            configuration.MakeClean = true;
                        else if (s.charAt(i) == 'n')
                            configuration.NumEntities = true;
                        else if (s.charAt(i) == 'm')
                            configuration.writeback = true;
                        else if (s.charAt(i) == 'e')
                            configuration.OnlyErrors = true;
                        else if (s.charAt(i) == 'q')
                            configuration.Quiet = true;
                        else
                            Report.unknownOption(tidy.getErrout(), s.charAt(i));
                    }
                }

                --argc;
                ++argIndex;
                continue;
            }

            /* ensure config is self-consistent */
            configuration.adjust();

            /* user specified error file */
            if (configuration.errfile != null)
            {
                /* is it same as the currently opened file? */
                if (!configuration.errfile.equals(current_errorfile))
                {
                    /* no so close previous error file */

                    if (tidy.getErrout() != tidy.getStderr())
                        tidy.getErrout().close();

                    /* and try to open the new error file */
                    try
                    {
                        tidy.setErrout(
                            new PrintWriter(
                                new FileWriter(configuration.errfile), true));
                        current_errorfile = configuration.errfile;
                    }
                    catch (IOException e)
                    {
                        /* can't be opened so fall back to stderr */
                        current_errorfile = "stderr";
                        tidy.setErrout(tidy.getStderr());
                    }
                }
            }

            if (argc > 1)
            {
                file = argv[argIndex];
                try
                {
                    in = new FileInputStream(file);
                }
                catch (FileNotFoundException e)
                {
                    in = null;
                }
                catch (IOException e)
                {
                    in = null;
                }
            }
            else
            {
                file = "stdin";
                in = System.in;
            }

            if (in != null)
            {
                lexer = new Lexer(new StreamInImpl(in,
                                                   configuration.CharEncoding,
                                                   configuration.tabsize),
                                  configuration);
                lexer.errout = tidy.getErrout();

                /*
                  store pointer to lexer in input stream
                  to allow character encoding errors to be
                  reported
                */
                lexer.in.lexer = lexer;

                /* Tidy doesn't alter the doctype for generic XML docs */
                if (configuration.XmlTags)
                    document = ParserImpl.parseXMLDocument(lexer);
                else
                {
                    lexer.warnings = 0;
                    if (!configuration.Quiet)
                        Report.helloMessage(tidy.getErrout(), Report.RELEASE_DATE, file);

                    document = ParserImpl.parseDocument(lexer);

                    if (!document.checkNodeIntegrity())
                    {
                        Report.badTree(tidy.getErrout());
                        System.exit(1);
                    }

                    /* simplifies <b><b> ... </b> ...</b> etc. */
                    Clean.nestedEmphasis(document);

                    /* cleans up <dir>indented text</dir> etc. */
                    Clean.list2BQ(document);
                    Clean.bQ2Div(document);

                    /* replaces i by em and b by strong */
                    if (configuration.LogicalEmphasis)
                        Clean.emFromI(document);

                    if (configuration.Word2000 && Clean.isWord2000(document))
                    {
                        /* prune Word2000's <![if ...]> ... <![endif]> */
                        Clean.dropSections(lexer, document);

                        /* drop style & class attributes and empty p, span elements */
                        Clean.cleanWord2000(lexer, document);
                    }

                    /* replaces presentational markup by style rules */
                    if (configuration.MakeClean || configuration.DropFontTags)
                        (new Clean()).cleanTree(lexer, document);

                    if (!document.checkNodeIntegrity())
                    {
                        Report.badTree(tidy.getErrout());
                        System.exit(1);
                    }
                    doctype = document.findDocType();
                    if (document.content != null)
                    {
                        if (configuration.xHTML)
                            lexer.setXHTMLDocType(document);
                        else
                            lexer.fixDocType(document);

                        if (configuration.TidyMark)
                            lexer.addGenerator(document);
                    }

                    /* ensure presence of initial <?XML version="1.0"?> */
                    if (configuration.XmlOut && configuration.XmlPi)
                        lexer.fixXMLPI(document);

                    totalwarnings += lexer.warnings;
                    totalerrors += lexer.errors;

                    if(!configuration.Quiet && document.content != null)
                    {
                        Report.reportVersion(tidy.getErrout(), lexer, file, doctype);
                        Report.reportNumWarnings(tidy.getErrout(), lexer);
                    }
                }

                if (in != System.in)
                {
                    try
                    {
                        in.close();
                    }
                    catch (IOException e ) {}
                }

                if (lexer.errors > 0)
                    Report.needsAuthorIntervention(tidy.getErrout());

                out.state = StreamIn.FSM_ASCII;
                out.encoding = configuration.CharEncoding;

                if (!configuration.OnlyErrors && lexer.errors == 0)
                {
                    pprint = new PPrint(configuration);

                    if (configuration.BurstSlides)
                    {
                        Node body;

                        body = null;
                        /*
                           remove doctype to avoid potential clash with
                           markup introduced when bursting into slides
                        */
                        /* discard the document type */
                        doctype = document.findDocType();

                        if (doctype != null)
                            Node.discardElement(doctype);

                        /* slides use transitional features */
                        lexer.versions |= Dict.VERS_HTML40_LOOSE;

                        /* and patch up doctype to match */
                        if (configuration.xHTML)
                            lexer.setXHTMLDocType(document);
                        else
                            lexer.fixDocType(document);


                        /* find the body element which may be implicit */
                        body = Node.findBody(document);

                        if (body != null)
                        {
                            Report.reportNumberOfSlides(tidy.getErrout(), PPrint.countSlides(body));
                            pprint.createSlides(lexer, document);
                        }
                        else
                            Report.missingBody(tidy.getErrout());
                    }
                    else if (configuration.writeback)
                    {
                        try
                        {
                            out.out = new FileOutputStream(file);

                            if (configuration.XmlTags)
                                pprint.printXMLTree(out, (short)0, 0, lexer, document);
                            else
                                pprint.printTree(out, (short)0, 0, lexer, document);

                            pprint.flushLine(out, 0);
                            out.out.close();
                        }
                        catch (IOException e)
                        {
                            tidy.getErrout().println(file + e.toString());
                        }
                    }
                    else
                    {
                        out.out = System.out;

                        if (configuration.XmlTags)
                            pprint.printXMLTree(out, (short)0, 0, lexer, document);
                        else
                            pprint.printTree(out, (short)0, 0, lexer, document);

                        pprint.flushLine(out, 0);
                    }

                }

                Report.errorSummary(lexer);
            }
            else
                Report.unknownFile(tidy.getErrout(), prog, file);

            --argc;
            ++argIndex;

            if (argc <= 1)
                break;
        }

        if (totalerrors + totalwarnings > 0)
            Report.generalInfo(tidy.getErrout());

        if (tidy.getErrout() != tidy.getStderr())
            tidy.getErrout().close();

        /* return status can be used by scripts */

        if (totalerrors > 0)
            System.exit(2);

        if (totalwarnings > 0)
            System.exit(1);

        /* 0 signifies all is ok */
        System.exit(0);
    }
}

⌨️ 快捷键说明

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