001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.license;
018:
019: import java.io.File;
020: import java.util.regex.Matcher;
021: import java.util.regex.Pattern;
022:
023: import junit.framework.Assert;
024:
025: import org.apache.wicket.util.string.Strings;
026:
027: class JavaLicenseHeaderHandler extends AbstractLicenseHeaderHandler {
028: private final Pattern javaHeaderPattern = Pattern.compile(
029: "^(.*?)package.*$", Pattern.MULTILINE | Pattern.DOTALL);
030:
031: /**
032: * Construct.
033: *
034: * @param ignoreFiles
035: */
036: public JavaLicenseHeaderHandler(String[] ignoreFiles) {
037: super (ignoreFiles);
038: }
039:
040: public boolean addLicenseHeader(File file) {
041: boolean added = false;
042:
043: try {
044: String fileContent = new org.apache.wicket.util.file.File(
045: file).readString();
046:
047: Matcher mat = javaHeaderPattern.matcher(fileContent);
048: if (mat.matches()) {
049: String header = mat.group(1);
050: if (header.equals(getLicenseHeader()) == false) {
051: String newContent = Strings.replaceAll(fileContent,
052: header, "").toString();
053: newContent = getLicenseHeader().trim()
054: + LINE_ENDING + newContent;
055: new org.apache.wicket.util.file.File(file)
056: .write(newContent);
057:
058: added = true;
059: }
060: } else {
061: Assert.fail();
062: }
063: } catch (Exception e) {
064: e.printStackTrace();
065: Assert.fail(e.getMessage());
066: }
067:
068: return added;
069: }
070:
071: public boolean checkLicenseHeader(File file) {
072: String header = extractLicenseHeader(file, 0, 16);
073:
074: return getLicenseHeader().equals(header);
075: }
076:
077: public String[] getSuffixes() {
078: return new String[] { "java" };
079: }
080:
081: protected String getLicenseHeaderFilename() {
082: return "javaLicense.txt";
083: }
084:
085: public String getLicenseType(File file) {
086: String licenseType = null;
087:
088: String header = extractLicenseHeader(file, 0, 20);
089:
090: // Check for some of the known license types:
091: if (header.indexOf("Apache License, Version 2.0") != -1) {
092: licenseType = "ASL2";
093: } else if (header
094: .indexOf("The Apache Software License, Version 1.1") != -1) {
095: licenseType = "ASL1.1";
096: }
097:
098: return licenseType;
099: }
100:
101: }
|