JAVA 소스 코드 파일 licence 정보 헤더 추가

7654 단어 자바
원본 파일
http://yangshangchuan.iteye.com/blog/1841150

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.apdplat.platform.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
 *  JAVA          licence   
 *     package、import、     、   public class
 *    Java7    ,  
 * @author ysc
 */
public class AddLicenceForJavaFile {
    private static int count = 0;
    private static List<String> fail = new ArrayList<>();
    private static List<String> wrong = new ArrayList<>();

    public static void main(String[] args) {
        String licence="/**
" + " * Licensed to the Apache Software Foundation (ASF) under one or more
" + " * contributor license agreements. See the NOTICE file distributed with
" + " * this work for additional information regarding copyright ownership.
" + " * The ASF licenses this file to You under the Apache License, Version 2.0
" + " * (the \"License\"); you may not use this file except in compliance with
" + " * the License. You may obtain a copy of the License at
" + " *
" + " * http://www.apache.org/licenses/LICENSE-2.0
" + " *
" + " * Unless required by applicable law or agreed to in writing, software
" + " * distributed under the License is distributed on an \"AS IS\" BASIS,
" + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
" + " * See the License for the specific language governing permissions and
" + " * limitations under the License.
" + " */"; addLicenceForJavaFile(new File("D:\\Workspaces\\NetBeansProjects\\APDPlat"),licence); System.out.println(" "+count+" Java licence "); if(fail.size()>0){ System.out.println(" "+fail.size()); for(String f : fail){ System.out.println(" "+f); } } if(wrong.size()>0){ System.out.println("JAVA "+wrong.size()); for(String w : wrong){ System.out.println(" "+w); } } } /** * JAVA licence * @param path * @param licence ( netbeans ,IDE , ) */ private static void addLicenceForJavaFile(File path, String licence) { if (path != null && path.exists()) { // if (path.isDirectory()) { String[] children = path.list(); for (int i = 0; i < children.length; i++) { File child = new File(path.getPath() + System.getProperty("file.separator") + children[i]); // addLicenceForJavaFile(child, licence); } } else { // java if (path.getName().toLowerCase().endsWith(".java")) { System.out.println(path.getAbsolutePath()); count++; try { byte[] content; try (RandomAccessFile f = new RandomAccessFile(path, "rw")) { content = new byte[ (int) f.length()]; f.readFully(content); } String text = new String(content); text = text.trim(); while (text.startsWith("/n")) { text = text.substring(1); } // licence, int pos = text.indexOf(licence); if(pos!=-1){ return; } // package , package if (text.indexOf("package") != -1) { text = text.substring(text.indexOf("package")); } // package , import , import else if (text.indexOf("package") == -1 && text.indexOf("import") != -1) { text = text.substring(text.indexOf("import")); } // package import , , else if (text.indexOf("package") == -1 && text.indexOf("import") == -1 && text.indexOf("/**") != -1 && text.indexOf("public class") != -1 && text.indexOf("/**")<text.indexOf("public class") ) { text = text.substring(text.indexOf("/**")); } // package import , public class else if (text.indexOf("package") == -1 && text.indexOf("import") == -1 && text.indexOf("public class") != -1 && ( text.indexOf("/**")>text.indexOf("public class") || text.indexOf("/**")==-1 )) { text = text.substring(text.indexOf("public class")); }else{ wrong.add(path.getAbsolutePath()); return; } try (FileWriter writer = new FileWriter(path)) { writer.write(licence); writer.write("

"); writer.write(text); } } catch (Exception ex) { fail.add(path.getAbsolutePath()); } } } } } }

좋은 웹페이지 즐겨찾기