java文件的缩进排版

java文件的缩进排版 
 
 
//以下是我做的java 文件的缩进排版,算法有缺点.但能用,
//不管怎么说,排java文件时比UltralEdit的排版强。

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import lang9.Str9;
import lang9.out;
import util9.Load9;

class DentJavaCode
{

    public static boolean dent(File file, File file1)
    {
        String as[] = new String[1];
        if(file == null || !file.exists())
        {
            return false;
        } else
        {
            as = Load9.copy(file, as);
            ArrayList arraylist = new ArrayList();
            dent(as, arraylist, 0);
            as = (String[])arraylist.toArray(as);
            out.b("s.len=" + as.length);
            Load9.copy(as, file1);
            return true;
        }
    }

    public static int dent(ArrayList arraylist, ArrayList arraylist1, int i)
    {
        if(arraylist == null && arraylist.size() == 0)
        {
            System.out.println("dent frm list1,but list1 isnull");
            return i;
        }
        if(arraylist1 == null)
        {
            System.out.println("dent to list2,but list2 is null");
            return i;
        } else
        {
            String as[] = new String[1];
            as = (String[])arraylist.toArray(as);
            return dent(as, arraylist1, i);
        }
    }

    public static String[] dent(String as[], String as1[], int i)
    {
        ArrayList arraylist = new ArrayList();
        dent(as, arraylist, i);
        if(as1 == null)
            as1 = new String[1];
        return (String[])arraylist.toArray(as1);
    }

    public static int dent(String as[], ArrayList arraylist, int i)
    {
        int j = as.length;
        boolean flag = false;
        if(as == null || as.length == 0)
        {
            System.out.println("dent from s[],but s[] is null");
            return i;
        }
        if(arraylist == null)
        {
            System.out.println("dent to list,but list is null");
            return i;
        }
        for(int l = 0; l < as.length; l++)
        {
            if(as[l] == null)
                continue;
            as[l] = as[l].trim();
            int k = as[l].length();
            if(k == 0)
                continue;
            int i1 = Str9.indexOf_outQuotes(special, as[l]);
            if(i1 < 0)
            {
                if(flag)
                    arraylist.add(space(2) + as[l]);
                else
                    arraylist.add(space(i) + as[l]);
                continue;
            }
            String s = as[l].substring(i1);
            if(s.indexOf("{") == 0)
                s = "{";
            else
            if(s.indexOf("}") == 0)
                s = "}";
            else
            if(s.indexOf("//") == 0)
                s = "//";
            else
            if(s.indexOf("*/") == 0)
                s = "*/";
            else
            if(s.indexOf("/*") == 0)
                s = "/*";
            if(s.equals("//"))
            {
                if(i1 > 0)
                {
                    arraylist.add(space(i) + as[l].substring(0, i1));
                    arraylist.add(space(i) + as[l].substring(i1));
                } else
                {
                    arraylist.add(space(i) + as[l]);
                }
                continue;
            }
            if(s.equals("*/"))
            {
                flag = false;
                if(i1 > 0)
                    arraylist.add(space(2) + as[l].substring(0, i1));
                arraylist.add("*/");
                i1 += 2;
                if(k > i1)
                {
                    as[l] = as[l].substring(i1);
                    l--;
                }
                continue;
            }
            if(s.equals("/*"))
            {
                flag = true;
                if(i1 > 0)
                {
                    arraylist.add(space(i) + as[l].substring(0, i1));
                    as[l] = as[l].substring(i1);
                }
                if((i1 = Str9.indexOf_outQuotes("*/", as[l], i1 + 2)) != -1)
                {
                    flag = false;
                    i1 += 2;
                    arraylist.add(as[l].substring(0, i1));
                    if(as[l].length() > i1)
                    {
                        as[l] = as[l].substring(i1);
                        l--;
                    }
                    continue;
                }
                if(as[l].length() <= 0)
                    continue;
                arraylist.add("/*");
                as[l] = as[l].substring(2);
                if(as[l].length() > 0)
                    arraylist.add(space(2) + as[l]);
                continue;
            }
            if(s.equals("{"))
            {
                i1++;
                if(as[l].indexOf("//") == i1)
                {
                    arraylist.add(space(i) + as[l]);
                    i += increment;
                    continue;
                }
                arraylist.add(space(i) + as[l].substring(0, i1));
                i += increment;
                if(k > i1)
                {
                    as[l] = as[l].substring(i1);
                    l--;
                }
                continue;
            }
            if(s.equals("}"))
            {
                arraylist.add(space(i) + as[l].substring(0, i1));
                i -= increment;
                i1++;
                if(k > i1 && as[l].charAt(i1) == ';')
                {
                    arraylist.add(space(i) + "};");
                    i1++;
                    if(k > i1)
                    {
                        as[l] = as[l].substring(i1);
                        l--;
                    }
                    continue;
                }
                if(as[l].indexOf("}//") == 0)
                {
                    System.out.println("2s[i]=" + as[l] + ",indent=" + i);
                    arraylist.add(space(i) + as[l]);
                    continue;
                }
                arraylist.add(space(i) + "}");
                if(k > i1)
                {
                    as[l] = as[l].substring(i1);
                    l--;
                }
            } else
            {
                arraylist.add(space(i) + as[l]);
            }
        }

        return i;
    }

    static String space(int i)
    {
        return Str9.repeat(' ', i);
    }

    public DentJavaCode(File file)
    {
        String as[] = null;
        as = Load9.copy(file, as);
        ArrayList arraylist = new ArrayList();
        dent(as, arraylist, 0);
        String s = file.getName();
        int i = 0;
        boolean flag = false;
        File file1;
        file1 = file1 = new File(s + "dent0");
        i = 1;
        do
        {
            if(i >= 5)
                break;
            file1 = new File(s + "dent" + i);
            if(!file1.exists())
                break;
            i++;
        } while(true);
        if(file1.exists())
            file1.delete();
        if(file.renameTo(file1))
        {
            String as1[] = (String[])arraylist.toArray(as);
            Load9.copy(as1, file);
            System.out.println("ok");
        } else
        {
            System.out.println("rename:false");
        }
    }

    public static void main(String args[])
    {
        String args1[] = new String[1];
        File file = new File(args[0]);
        args1 = Load9.copy(file, args1);
        ArrayList arraylist = new ArrayList();
        dent(args1, arraylist, 0);
        File file1 = new File(args[0] + "dent0");
        int i = 1;
        do
        {
            if(i >= 5)
                break;
            file1 = new File(args[0] + "dent" + i);
            if(!file1.exists())
                break;
            i++;
        } while(true);
        if(file1.exists())
            file1.delete();
        String args2[] = (String[])arraylist.toArray(args1);
        if(file.renameTo(file1))
        {
            Load9.copy(args2, file);
            System.out.println("ok");
        } else
        {
            System.out.println("rename:false");
        }
    }

    static int increment = 3;
    public static String special[] = {
        "{", "}", "//", "/*", "*/"
    };

}
 

你可能感兴趣的:(Java)