/* * The contents of this file are subject to the terms of the Common Development and * Distribution License (the License). You may not use this file except in compliance with the * License. * * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the * specific language governing permission and limitations under the License. * * When distributing Covered Software, include this CDDL Header Notice in each file and include * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL * Header, with the fields enclosed by brackets [] replaced by your own identifying * information: "Portions Copyright [year] [name of copyright owner]". * * Copyright 2015-2016 ForgeRock AS. * Portions Copyright 2026 3A Systems, LLC. */ package org.forgerock.opendj.maven.doc; import static org.forgerock.util.Utils.*; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateExceptionHandler; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; /** * Provides utility methods for generating documentation. */ public final class Utils { /** Line separator. */ static final String EOL = System.getProperty("line.separator"); /** * Creates a directory unless it already exists. * @param directory The directory to create. * @throws IOException Failed to create directory. */ static void createDirectory(final String directory) throws IOException { File dir = new File(directory); if (!dir.exists()) { if (!dir.mkdirs()) { throw new IOException("Failed to create directory: " + directory); } } } /** * Returns the path to the current Java executable. * @return The path to the current Java executable. */ static String getJavaCommand() { return System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; } /** * Copies the content of the original file to the copy. * @param original The original file. * @param copy The copy. * @throws IOException Failed to make the copy. */ static void copyFile(File original, File copy) throws IOException { try (InputStream input = new FileInputStream(original)) { copyInputStreamToFile(input, copy); } } /** * Copies the content of the original input stream to the copy. *
* The original input stream is closed on return, whether the copy succeeded or not.
* @param original The original input stream.
* @param copy The copy.
* @throws IOException Failed to make the copy.
*/
static void copyInputStreamToFile(InputStream original, File copy) throws IOException {
if (original == null) {
throw new IOException("Could not read input to copy.");
}
try {
createFile(copy);
try (OutputStream outputStream = new FileOutputStream(copy)) {
int bytesRead;
byte[] buffer = new byte[4096];
while ((bytesRead = original.read(buffer)) > 0) {
outputStream.write(buffer, 0, bytesRead);
}
}
} finally {
closeSilently(original);
}
}
/**
* Writes a string to a file.
* @param string The string to write
* @param file The file to write to
* @throws IOException The file did not exist, or could not be created for writing.
*/
static void writeStringToFile(final String string, final File file) throws IOException {
createFile(file);
try (PrintWriter printWriter = new PrintWriter(file)) {
printWriter.print(string);
}
}
/**
* Creates a file including parent directories if it does not yet exist.
* @param file The file to create
* @throws IOException Failed to create the file
*/
private static void createFile(File file) throws IOException {
if (!file.exists()) {
createDirectory(file.getParent());
if (!file.createNewFile()) {
throw new IOException("Failed to create " + file.getPath());
}
}
}
/** FreeMarker template configuration. */
static Configuration configuration;
/**
* Returns a FreeMarker configuration for applying templates.
* @return A FreeMarker configuration for applying templates.
*/
static Configuration getConfiguration() {
if (configuration == null) {
configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setClassForTemplateLoading(Utils.class, "/templates");
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
}
return configuration;
}
/**
* Returns the String result from applying a FreeMarker template.
* @param template The name of a template file found in {@code resources/templates/}.
* @param map The map holding the data to use in the template.
* @return The String result from applying a FreeMarker template.
*/
static String applyTemplate(final String template, final Map