com.brambolt.util.jar.Manifests Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brambolt-rt Show documentation
Show all versions of brambolt-rt Show documentation
Small utilities and convenience wrappers.
/*
* Copyright 2017-2021 Brambolt.
*
* 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.brambolt.util.jar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import static java.util.Arrays.asList;
/**
* A trait to facilitate defining manifest operations.
*/
@java.lang.SuppressWarnings("unused")
public interface Manifests {
/** The manifest version attribute name. */
String MANIFEST_VERSION = "Manifest-Version";
/** The signature version attribute name. */
String SIGNATURE_VERSION = "Signature-Version";
/**
* Accessor for the manifest file path in the parameter file system.
* @param fs The file system that holds the manifest file.
* @return The path to the manifest file in the file system.
*/
static Path getPath(FileSystem fs) {
return fs.getPath("META-INF/MANIFEST.MF");
}
/**
* Reads the entire manifest file into a string, using the UTF-8 encoding.
* @param fs The file system to read the manifest from.
* @return A string holding the content of the manifest file.
* @throws IOException If unable to read the manifest file.
*/
static String readString(FileSystem fs) throws IOException {
return readString(fs, StandardCharsets.UTF_8);
}
/**
* Reads the entire manifest file into a string.
* @param fs The file system to read the manifest from.
* @param encoding The character set to read the manifest with.
* @return A string holding the content of the manifest file.
* @throws IOException If unable to read the manifest file.
*/
static String readString(FileSystem fs, Charset encoding) throws IOException {
return com.brambolt.nio.file.Files.readString(getPath(fs), encoding);
}
/**
* Reads the entire manifest file as a list of lines, using the UTF-8 encoding.
* @param fs The file system holding the manifest file.
* @return A list of strings holding the lines of the manifest file.
* @throws IOException If unable to read the manifest file.
*/
static List readAllLines(FileSystem fs) throws IOException {
return readAllLines(fs, StandardCharsets.UTF_8);
}
/**
* Reads the entire manifest file as a list of lines.
* @param fs The file system holding the manifest file.
* @param encoding The character set to read the manifest file with.
* @return A list of strings holding the lines of the manifest file.
* @throws IOException If unable to read the manifest file.
*/
static List readAllLines(FileSystem fs, Charset encoding) throws IOException {
return java.nio.file.Files.readAllLines(getPath(fs), encoding);
}
/**
* Applies a manifest conversion operation to the manifest in the
* parameter file system.
* @param fs The file system to locate the manifest file in.
* @throws IOException If unable to work with the manifest file.
* @see #apply(List)
*/
default void apply(FileSystem fs) throws IOException {
apply(getPath(fs));
}
/**
* Applies a manifest conversion operation to the manifest at the
* parameter path.
* @param manifestPath The path to the manifest file.
* @throws IOException If unable to work with the manifest file.
* @see #apply(List)
*/
default void apply(Path manifestPath) throws IOException {
Files.write(
manifestPath,
(String.join("\n",
apply(Files.readAllLines(manifestPath))) + "\n")
.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.TRUNCATE_EXISTING);
}
/**
* Applies a manifest conversion operation to the manifest content.
* @param content The manifest content to operate on.
* @return The converted manifest content.
* @see #apply(List)
*/
default String apply(String content) {
return rejoin(apply(split(content)));
}
/**
* Converts the parameter manifest content into a list of lines.
* @param content The content to convert.
* @return A list of lines parsed from the parameter content.
*/
default List split(String content) {
return asList(content.split("\n"));
}
/**
* Converts the parameter list of lines to a manifest content string.
* @param lines The list of lines to combine.
* @return Manifest string content created by concatenating the parameter lines.
*/
default String rejoin(List lines) {
return String.join("\n", lines) + "\n"; // Newline before EOF
}
/**
* Idempotent. Override this method to define specific manifest operations.
* @param lines The manifest lines to operate on.
* @return The converted manifest lines.
*/
default List apply(List lines) {
return lines;
}
/**
* Determines whether the parameter line defines the parameter attribute.
* @param line The line to check.
* @param attributeName The manifest attribute name to check for.
* @return True iff the line defines the attribute.
*/
static boolean isLineAttribute(String line, String attributeName) {
return line.trim().toLowerCase().startsWith(attributeName.toLowerCase());
}
/**
* Checks whether the parameter manifest lines define the parameter attribute.
* @param lines The lines to check.
* @param attributeName The attribute name to check for.
* @return True iff the lines define a value for the parameter attribute.
*/
static boolean hasAttribute(List lines, String attributeName) {
return lines.stream().anyMatch(line -> isLineAttribute(line, attributeName));
}
/**
* Checks whether the manifest file in the parameter file system defines
* the parameter attribute.
* @param fs The file system to locate the manifest file to check in.
* @param attributeName The attribute name to check for.
* @return True iff the manifest file in the file system defines the attribute.
* @throws IOException If unable to read the manifest file from the file system.
*/
static boolean hasAttribute(FileSystem fs, String attributeName) throws IOException {
return hasAttribute(readAllLines(fs), attributeName);
}
/**
* Indicates whether the parameter line defines the manifest version attribute.
* @param line The line to check.
* @return True iff the line holds the manifest version attribute.
*/
static boolean isManifestVersion(String line) {
return isLineAttribute(line, MANIFEST_VERSION);
}
/**
* Indicates whether the parameter line defines the signature version attribute.
* @param line The line to check.
* @return True iff the line defines the signature version attribute.
*/
static boolean isSignatureVersion(String line) {
return isLineAttribute(line, SIGNATURE_VERSION);
}
}