给JAVA源代码文件统一地添加licence信息头
来源:互联网 发布:绵阳网络装修公司 编辑:程序博客网 时间:2024/04/28 15:59
/**
* 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="/**\n" +
" * Licensed to the Apache Software Foundation (ASF) under one or more\n" +
" * contributor license agreements. See the NOTICE file distributed with\n" +
" * this work for additional information regarding copyright ownership.\n" +
" * The ASF licenses this file to You under the Apache License, Version 2.0\n" +
" * (the \"License\"); you may not use this file except in compliance with\n" +
" * the License. You may obtain a copy of the License at\n" +
" *\n" +
" * http://www.apache.org/licenses/LICENSE-2.0\n" +
" *\n" +
" * Unless required by applicable law or agreed to in writing, software\n" +
" * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" * See the License for the specific language governing permissions and\n" +
" * limitations under the License.\n" +
" */";
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("\n\n");
writer.write(text);
}
}
catch (Exception ex) {
fail.add(path.getAbsolutePath());
}
}
}
}
}
}
* 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="/**\n" +
" * Licensed to the Apache Software Foundation (ASF) under one or more\n" +
" * contributor license agreements. See the NOTICE file distributed with\n" +
" * this work for additional information regarding copyright ownership.\n" +
" * The ASF licenses this file to You under the Apache License, Version 2.0\n" +
" * (the \"License\"); you may not use this file except in compliance with\n" +
" * the License. You may obtain a copy of the License at\n" +
" *\n" +
" * http://www.apache.org/licenses/LICENSE-2.0\n" +
" *\n" +
" * Unless required by applicable law or agreed to in writing, software\n" +
" * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" * See the License for the specific language governing permissions and\n" +
" * limitations under the License.\n" +
" */";
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("\n\n");
writer.write(text);
}
}
catch (Exception ex) {
fail.add(path.getAbsolutePath());
}
}
}
}
}
}
- 给JAVA源代码文件统一地添加licence信息头
- java给文件内容添加头
- 给PullToRefresh添加头信息
- servlet 文件下载头信息的添加
- Android 给Retrofit2和OkHttp3添加统一的请求头Header
- 修复Dex文件头--Java源代码
- 给java文件批量添加License信息
- Java 获取和判断文件头信息
- Java 获取和判断文件头信息
- myeclipse 自动添加头时间,作者,文件注释等信息
- notepad++编辑器打开一个文件时,给文件添加信息
- 给UICollectionView添加头视图
- 给webView添加头视图
- 用Java分析C源代码中头文件使用频率
- Linux_使用java给文件添加权限
- Java下载网络文件时获取响应头文件信息
- MIME头文件信息
- caf文件头信息
- JavaScript面向对象简易实现
- Android WIFI 架构和控制流程
- 文件管理 mv、cp、cat、touch、more、less、rm、chown 、chgrp、chmod、ln、find、cmp、cut、whereis、which
- 使用VS2010命令行编译C#文件
- C# 获取当前路径方法
- 给JAVA源代码文件统一地添加licence信息头
- Htmlayout对话框程序输入法切换问题解决方案
- ScrollView 和 ListView 与软键盘弹出导致屏幕尺寸发生改变
- win32创建控件和提示文本
- 【转载】【程序员练级】提高英语阅读水平经验分享上篇
- MYSQL5.6新功能发布(一)
- 美国最常见英文名字排行榜(500名)
- Unity3D教程:优化Draw Call
- C语言学习之用二维数组实现矩阵转置