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());
}
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.