-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* Copyright (C) 2008 Mycila ([email protected]) | ||
* | ||
* Licensed 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.mycila.maven.plugin.license; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import com.mycila.maven.plugin.license.util.FileUtils; | ||
import com.mycila.maven.plugin.license.util.resource.ResourceFinder; | ||
|
||
/** | ||
* Provides an access to the license template text. | ||
* | ||
* @author <a href="https://github.com/ppalaga">Peter Palaga</a> | ||
*/ | ||
public abstract class HeaderSource { | ||
|
||
/** | ||
* A {@link HeaderSource} built from a lincense header template literal. | ||
*/ | ||
public static class InlineHeaderSource extends HeaderSource { | ||
public InlineHeaderSource(String content) { | ||
super(content, true); | ||
} | ||
|
||
/** | ||
* @return always {@code false} because this {@link InlineHeaderSource} was not loaded from an {@link URL} | ||
*/ | ||
@Override | ||
public boolean isFromLocation(URL location) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "inline: " + content; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A {@link HeaderSource} loaded from a {@link URL}. | ||
*/ | ||
public static class PathHeaderSource extends HeaderSource { | ||
private final URL headerUrl; | ||
|
||
public PathHeaderSource(URL headerUrl, String encoding) throws IOException { | ||
super(FileUtils.read(headerUrl, encoding), false); | ||
this.headerUrl = headerUrl; | ||
} | ||
|
||
@Override | ||
public boolean isFromLocation(URL location) { | ||
return this.headerUrl.equals(location); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return headerUrl + ": " + content; | ||
} | ||
|
||
} | ||
|
||
public static HeaderSource of(String headerPath, String encoding, ResourceFinder finder) { | ||
return of(null, encoding, finder); | ||
} | ||
|
||
/** | ||
* Checking the params left to right, returns the first available {@link HeaderSource} that can be created. If | ||
* {@code inlineHeader} is not {@code null} returns a new {@link InlineHeaderSource}. Otherwise attempts to create a | ||
* new {@link PathHeaderSource} out of {@code headerPath} and {@code encoding}. | ||
* | ||
* @param inlineHeader the text of a lincense header template | ||
* @param headerPath a path resolvable by the {@code finder} | ||
* @param encoding the encoding to use when readinf {@code headerPath} | ||
* @param finder the {@link ResourceFinder} to use to resolve {@code headerPath} | ||
* @return a new {@link HeaderSource} | ||
*/ | ||
public static HeaderSource of(String inlineHeader, String headerPath, String encoding, ResourceFinder finder) { | ||
if (inlineHeader != null && !inlineHeader.isEmpty()) { | ||
return new InlineHeaderSource(inlineHeader); | ||
} else if (headerPath == null) { | ||
throw new IllegalArgumentException("Either inlineHeader or header path need to be specified"); | ||
} else { | ||
try { | ||
final URL headerUrl = finder.findResource(headerPath); | ||
return new PathHeaderSource(headerUrl, encoding); | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException( | ||
"Cannot read header document " + headerPath + ". Cause: " + e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
protected final String content; | ||
private final boolean inline; | ||
|
||
public HeaderSource(String content, boolean inline) { | ||
super(); | ||
this.content = content; | ||
this.inline = inline; | ||
} | ||
|
||
/** | ||
* @return the text of the license template | ||
*/ | ||
public String getContent() { | ||
return content; | ||
} | ||
|
||
/** | ||
* @return {@code true} if this {@link HeaderSource} was created from a string rather by loading the bits from an | ||
* URL; {@code false} otherwise | ||
*/ | ||
public boolean isInline() { | ||
return inline; | ||
} | ||
|
||
/** | ||
* Retuns {@code true} if this {@link HeaderSource} was loaded from the URL given in the {@code location} parameter | ||
* or {@code false} otherwise. | ||
* | ||
* @param location | ||
* the URL to tell if this {@link HeaderSource} was loaded from it | ||
* @return {@code true} if this {@link HeaderSource} was loaded from the URL given in the {@code location} parameter | ||
* or {@code false} otherwise | ||
*/ | ||
public abstract boolean isFromLocation(URL location); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,19 +15,18 @@ | |
*/ | ||
package com.mycila.maven.plugin.license.header; | ||
|
||
import com.mycila.maven.plugin.license.HeaderSection; | ||
import com.mycila.maven.plugin.license.document.Document; | ||
import com.mycila.maven.plugin.license.util.StringUtils; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
|
||
import static com.mycila.maven.plugin.license.util.FileUtils.read; | ||
import com.mycila.maven.plugin.license.HeaderSection; | ||
import com.mycila.maven.plugin.license.HeaderSource; | ||
import com.mycila.maven.plugin.license.document.Document; | ||
import com.mycila.maven.plugin.license.util.StringUtils; | ||
|
||
import static com.mycila.maven.plugin.license.util.FileUtils.readFirstLines; | ||
import static com.mycila.maven.plugin.license.util.FileUtils.remove; | ||
|
||
|
@@ -38,13 +37,12 @@ | |
* @author Mathieu Carbou ([email protected]) | ||
*/ | ||
public final class Header { | ||
private final URL location; | ||
private final HeaderSource location; | ||
private final String headerContent; | ||
private final String headerContentOneLine; | ||
private String[] lines; | ||
private final HeaderSection[] sections; | ||
private final int maxLength; | ||
private final boolean inline; | ||
|
||
/** | ||
* Constructs a <code>Header</code> object pointing to a license template file. In case of the template contains | ||
|
@@ -55,15 +53,11 @@ public final class Header { | |
* @throws IllegalArgumentException If the header file location is null or if an error occurred while reading the | ||
* file content. | ||
*/ | ||
public Header(URL location, String encoding, HeaderSection[] sections, String headerText) { | ||
if (location == null && headerText == null) { | ||
throw new IllegalArgumentException("Cannot read license template header file with a null location"); | ||
} | ||
public Header(HeaderSource location, HeaderSection[] sections) { | ||
this.location = location; | ||
this.inline = location == null; | ||
this.sections = sections; | ||
try { | ||
this.headerContent = location == null ? headerText : read(location, encoding); | ||
this.headerContent = location.getContent(); | ||
lines = headerContent.replace("\r", "").split("\n"); | ||
headerContentOneLine = remove(headerContent, " ", "\t", "\r", "\n"); | ||
} catch (Exception e) { | ||
|
@@ -101,15 +95,11 @@ public int getMaxLineLength() { | |
* | ||
* @return The URL location. | ||
*/ | ||
public URL getLocation() { | ||
public HeaderSource getLocation() { | ||
return location; | ||
} | ||
|
||
public boolean isInline() { | ||
return inline; | ||
} | ||
|
||
public String eol(boolean unix) { | ||
public String eol(boolean unix) { | ||
return unix ? "\n" : "\r\n"; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.