原文件来自
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="/**\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工具,包含生成、验证功能,可直接使用
C#3种软件licence开发源代码
支持word水印,包含licence文件和jar包,支持doc和docx 支持word水印
在KeyGenerater类中使用junit测试generater()方法即可测试demo
Java card licence price
aspose-words添加word水印的jar包,licence文件,实例代码。拿过来基本可以直接用。水印呈45度角,居中
docx4J 文件源代码 docx4J source files docx4j is an open source Java library for manipulating OpenXML WordprocessingML documents, released under the Apache software licence. docx is the default file ...
web系统Licence验证 保证java web ,可以现在IP,mac,自定义参数,License生成器 (JAVA源码+界面) 其中包括license授权机制的原理和制作license的具体步骤 增加了mac 地址验证
java中使用公钥加密私钥解密原理. KeyGenerater类: public class KeyGenerater { private byte[] priKey; private byte[] pubKey; public void generater() { try { KeyPairGenerator keygen = KeyPairGenerator ...
可以用到2028年
[资料] 可用到2038年的Polar SI9000 破解Licence文件\r\n\r\n网络上流行的Polar SI9000 V7.1.0的破解Licence文件的到期时间是2013-1-1,马上就要到期,急于寻找解决办法,于是在网上疯找,功夫不负有心人,终于找到...
src目录用于存放工程的包及java源码文件。 下面是src目录的子目录: src ├ cn.eoe.app --存放程序全局性类的包 ├ cn.eoe.app.adapter --存放适配器的实现类的包 ├ cn.eoe.app.adapter.base --存放适配器基类的...
MATLAB 2017a的licence文件,matlab2017a版本证书文件
xilinx ise13.1 licence 文件
altium desiger 13的licence文件和keygen工具
ti 德州仪器 ccs6.0 licence 破解文件,没有该文件可以使用ccs,但是有组件限制和调试限制,使用license可以正常调试和开发
vivado2017破解文件,测试win7 64位可用
MATLAB 2015b的crack,里面包含有licence文件,亲测可用。我的matlab过期之后在安装目录里怎么也找不到licence,所以下了这个文件,直接解压到根目录覆盖,然后打开matlab选择根目录底下的lic文件就可以继续使用了
tribon licence文件安装tribon 许可文件
Java屏幕保护程序,全开源 文件包:(分享) ./SaverX_Y.bat 和SuperSaver.bat 为启动屏保的批处理文件,X代表X号屏保,Y代表模式(拖影/无拖影)。双击执行。 ./src/ 为源代码,因为很不幸的IDE不支持中文...