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

📄 diffprint.java

📁 简单介绍sis结合的一个例子 和一个集成的框架
💻 JAVA
字号:
// Decompiled by DJ v3.5.5.77 Copyright 2003 Atanas Neshkov  Date: 2003-6-23 11:15:32
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   DiffPrint.java

package cn.javabiz.framework.util;

import java.io.*;
import java.util.Vector;

// Referenced classes of package org.fat32.util:
//            Diff, UnaryPredicate

public class DiffPrint
{
    public static abstract class Base
    {

        public void print_script(Diff.change script)
        {
            for(Diff.change next = script; next != null;)
            {
                Diff.change t = next;
                Diff.change end = hunkfun(next);
                next = end.link;
                end.link = null;
                print_hunk(t);
                end.link = next;
            }

            outfile.flush();
        }

        protected Diff.change hunkfun(Diff.change hunk)
        {
            return hunk;
        }

        protected void analyze_hunk(Diff.change hunk)
        {
            int l0 = 0;
            int l1 = 0;
            int show_from = 0;
            int show_to = 0;
            boolean nontrivial = ignore == null;
            show_from = show_to = 0;
            int f0 = hunk.line0;
            int f1 = hunk.line1;
            for(Diff.change next = hunk; next != null; next = next.link)
            {
                l0 = (next.line0 + next.deleted) - 1;
                l1 = (next.line1 + next.inserted) - 1;
                show_from += next.deleted;
                show_to += next.inserted;
                for(int i = next.line0; i <= l0 && !nontrivial; i++)
                    if(!ignore.execute(file0[i]))
                        nontrivial = true;

                for(int i = next.line1; i <= l1 && !nontrivial; i++)
                    if(!ignore.execute(file1[i]))
                        nontrivial = true;

            }

            first0 = f0;
            last0 = l0;
            first1 = f1;
            last1 = l1;
            if(!nontrivial)
                show_from = show_to = 0;
            deletes = show_from;
            inserts = show_to;
        }

        protected abstract void print_hunk(Diff.change change);

        protected void print_1_line(String pre, Object linbuf)
        {
            outfile.println(pre + linbuf.toString());
        }

        protected void print_number_range(char sepchar, int a, int b)
        {
            if(++b > ++a)
                outfile.print("" + a + sepchar + b);
            else
                outfile.print(b);
        }

        public static char change_letter(int inserts, int deletes)
        {
            if(inserts == 0)
                return 'd';
            return deletes != 0 ? 'c' : 'a';
        }

        protected UnaryPredicate ignore;
        protected Object file0[];
        protected Object file1[];
        protected int first0;
        protected int last0;
        protected int first1;
        protected int last1;
        protected int deletes;
        protected int inserts;
        protected PrintWriter outfile;

        protected Base(Object a[], Object b[])
        {
            ignore = null;
            outfile = new PrintWriter(new OutputStreamWriter(System.out));
            file0 = a;
            file1 = b;
        }

        protected Base(Object a[], Object b[], Writer sw)
        {
            ignore = null;
            outfile = new PrintWriter(sw);
            file0 = a;
            file1 = b;
        }
    }

    public static class HtmlPrint extends Base
    {

        private String getColor()
        {
            return count++ % 2 != 0 ? color1 : color2;
        }

        protected void print_hunk(Diff.change hunk)
        {
            analyze_hunk(hunk);
            if(super.deletes == 0 && super.inserts == 0)
                return;
            super.outfile.print("<b><a href='javascript:gotoLine(");
            super.outfile.print(super.last0 + 1);
            super.outfile.print(")'>");
            print_number_range(',', super.first0, super.last0);
            super.outfile.print(Base.change_letter(super.inserts, super.deletes));
            print_number_range(',', super.first1, super.last1);
            super.outfile.println("</a></b>");
            if(super.deletes != 0)
            {
                super.outfile.println(html_00);
                for(int i = super.first0; i <= super.last0; i++)
                    print_1_line("&lt; ", super.file0[i]);

                super.outfile.println(html_end);
            }
            if(super.inserts != 0 && super.deletes != 0)
                super.outfile.println("---");
            if(super.inserts != 0)
            {
                super.outfile.println(html_01);
                for(int i = super.first1; i <= super.last1; i++)
                    print_1_line("&gt; ", super.file1[i]);

                super.outfile.println(html_end);
            }
        }

        String html_00;
        String html_01;
        String html_end;
        String color1;
        String color2;
        int count;

        public HtmlPrint(Object a[], Object b[], Writer sw)
        {
            super(a, b, sw);
            html_00 = "<font style='background-color:#80FFFF'>";
            html_01 = "<font style='background-color:#FFFF80'>";
            html_end = "</font>";
            color1 = "#efefef";
            color2 = "#ffffff";
            count = 0;
        }
    }

    public static class NormalPrint extends Base
    {

        protected void print_hunk(Diff.change hunk)
        {
            analyze_hunk(hunk);
            if(super.deletes == 0 && super.inserts == 0)
                return;
            print_number_range(',', super.first0, super.last0);
            super.outfile.print(Base.change_letter(super.inserts, super.deletes));
            print_number_range(',', super.first1, super.last1);
            super.outfile.println();
            if(super.deletes != 0)
            {
                for(int i = super.first0; i <= super.last0; i++)
                    print_1_line("< ", super.file0[i]);

            }
            if(super.inserts != 0 && super.deletes != 0)
                super.outfile.println("---");
            if(super.inserts != 0)
            {
                for(int i = super.first1; i <= super.last1; i++)
                    print_1_line("> ", super.file1[i]);

            }
        }

        public NormalPrint(Object a[], Object b[])
        {
            super(a, b);
        }
    }

    public static class EdPrint extends Base
    {

        protected void print_hunk(Diff.change hunk)
        {
            analyze_hunk(hunk);
            if(super.deletes == 0 && super.inserts == 0)
                return;
            print_number_range(',', super.first0, super.last0);
            super.outfile.println(Base.change_letter(super.inserts, super.deletes));
            if(super.inserts != 0)
            {
                boolean inserting = true;
                for(int i = super.first1; i <= super.last1; i++)
                {
                    if(!inserting)
                        super.outfile.println(((i - super.first1) + super.first0) + "a");
                    inserting = true;
                    if(".".equals(super.file1[i]))
                    {
                        super.outfile.println("..");
                        super.outfile.println(".");
                        super.outfile.println(((i - super.first1) + super.first0 + 1) + "s/^\\.\\././");
                        inserting = false;
                    } else
                    {
                        print_1_line("", super.file1[i]);
                    }
                }

                if(inserting)
                    super.outfile.println(".");
            }
        }

        public EdPrint(Object a[], Object b[])
        {
            super(a, b);
        }
    }


    static String[] slurp(String file)
        throws IOException
    {
        BufferedReader rdr = new BufferedReader(new FileReader(file));
        Vector s = new Vector();
        do
        {
            String line = rdr.readLine();
            if(line != null)
            {
                s.addElement(line);
            } else
            {
                String a[] = new String[s.size()];
                s.copyInto(a);
                return a;
            }
        } while(true);
    }

    public static void main(String argv[])
        throws IOException
    {
        String a[] = slurp(argv[argv.length - 2]);
        String b[] = slurp(argv[argv.length - 1]);
        Diff d = new Diff(a, b);
        boolean edstyle = "-e".equals(argv[0]);
        Diff.change script = d.diff_2(edstyle);
        if(script == null)
        {
            System.err.println("No differences");
        } else
        {
            Object obj;
            if(edstyle)
                obj = new EdPrint(a, b);
            else
                obj = new NormalPrint(a, b);
            ((Base) (obj)).print_script(script);
        }
    }

    public DiffPrint()
    {
    }
}

⌨️ 快捷键说明

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