
net.yetamine.pet4bnd.format.Format2Map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pet4bnd-maven-plugin Show documentation
Show all versions of pet4bnd-maven-plugin Show documentation
Package exports tracker for bnd.
The newest version!
/*
* Copyright 2016 Yetamine
*
* 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 net.yetamine.pet4bnd.format;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import net.yetamine.pet4bnd.model.Bundle;
import net.yetamine.pet4bnd.model.Persistable;
/**
* Formats a definition to a {@link Map}.
*/
public final class Format2Map implements Persistable {
/** Suffix for the XML-formatted output. */
private static final String SUFFIX_XML = ".xml";
/** Comment to include in the generated files. */
private static final String COMMENT = "Generated by the pet4bnd tool";
/** Content to format on demand. */
private final Map content;
/**
* Creates a new instance.
*
* @param definition
* the definition to format. It must not be {@code null}.
*/
public Format2Map(Bundle definition) {
final Map view = new LinkedHashMap<>();
definition.exports().values().forEach(p -> {
final String packageName = p.packageName();
view.put(packageName, p.version().resolution().toString());
p.attributes().ifPresent(a -> view.put(packageName + "#attributes", a));
});
view.put("$bundle", definition.version().resolution().toString());
content = Collections.unmodifiableMap(view);
}
/**
* Provides a read-only view on the content.
*
* @return the content
*/
public Map content() {
return content;
}
/**
* Returns a copy of the content as {@link Properties}.
*
* @return a copy of the content
*/
public Properties toProperties() {
final Properties result = new Properties();
result.putAll(content);
return result;
}
/**
* @see net.yetamine.pet4bnd.model.Persistable#persist(java.io.OutputStream)
*/
public void persist(OutputStream sink) throws IOException {
toProperties().store(sink, COMMENT);
}
/**
* @see net.yetamine.pet4bnd.model.Persistable#store(java.nio.file.Path)
*/
public void store(Path path) throws IOException {
final Path file = path.getFileName();
if (file == null) { // Might be null for some paths (e.g., "c:")
throw new IllegalArgumentException("Missing file name in the path: " + path);
}
try (OutputStream sink = Files.newOutputStream(path)) {
final Properties properties = toProperties();
final String fileName = file.toString();
if ((fileName.length() > SUFFIX_XML.length()) && fileName.endsWith(SUFFIX_XML)) {
properties.storeToXML(sink, COMMENT, StandardCharsets.UTF_8.toString());
return;
}
properties.store(sink, COMMENT);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy