blob: 1b742f28a206d5d7386e96f05d3e313d8574e2df [file]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>Exporter.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Creadur RAT::Tools</a> &gt; <a href="index.source.html" class="el_package">org.apache.rat.documentation</a> &gt; <span class="el_source">Exporter.java</span></div><h1>Exporter.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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 org.apache.rat.documentation;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.rat.api.Document;
import org.apache.rat.api.RatException;
import org.apache.rat.document.DocumentName;
import org.apache.rat.document.DocumentNameMatcher;
import org.apache.rat.document.FileDocument;
import org.apache.rat.documentation.velocity.RatTool;
import org.apache.rat.report.RatReport;
import org.apache.rat.walker.DirectoryWalker;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
import org.apache.velocity.tools.generic.EscapeTool;
/**
* Uses Apache Velocity to write a document containing RAT configuration information.
*
* @see &lt;a href=&quot;https://velocity.apache.org/&quot;&gt;Apache Velocity&lt;/a&gt;
*/
<span class="nc" id="L51">public class Exporter {</span>
@Override
public String toString() {
<span class="nc" id="L55"> return &quot;Documentation exporter&quot;;</span>
}
/**
* Executes the generation of documentation from a configuration definition.
* Arguments are
* &lt;ol&gt;
* &lt;li&gt;Template directory - the top level directory in a tree of Velocity templates. The process scans the directory
* tree looking for files ending in {@code .vm} and processes them.&lt;/li&gt;
* &lt;li&gt;Output directory - The top level directory to writhe the processed files to. The process removes the {@code .vm}
* from the input file name and writes the resulting file to an equivalent directory entry in the output directory.&lt;/li&gt;
* &lt;/ol&gt;
*
* @param args the arguments
* @throws RatException on RAT processing error.
*/
public static void main(final String[] args) throws
RatException {
<span class="nc" id="L73"> String templateDir = args[0];</span>
<span class="nc" id="L74"> String outputDir = args[1];</span>
// create a DirectoryWalker to walk the template tree.
<span class="nc" id="L77"> File sourceDir = new File(templateDir);</span>
<span class="nc" id="L78"> DocumentName sourceName = DocumentName.builder(sourceDir).build();</span>
<span class="nc" id="L79"> FileFilter fileFilter = new OrFileFilter(new SuffixFileFilter(&quot;.vm&quot;), DirectoryFileFilter.INSTANCE);</span>
<span class="nc" id="L80"> Document document = new FileDocument(sourceName, sourceDir, new DocumentNameMatcher(fileFilter));</span>
<span class="nc" id="L81"> DirectoryWalker walker = new DirectoryWalker(document);</span>
// create a rewriter that writes to the target directory.
<span class="nc" id="L84"> DocumentName targetDir = DocumentName.builder(new File(outputDir)).build();</span>
// have the walker process clean and then the rewrite.
<span class="nc" id="L87"> walker.run(new Cleaner(targetDir));</span>
<span class="nc" id="L88"> walker.run(new Rewriter(targetDir));</span>
<span class="nc" id="L89"> }</span>
/**
* A RatReport implementation that processes the {@code .vm} files in the template tree and writes the
* results to the output tree.
*/
private static class Rewriter implements RatReport {
/**
* The base directory we are targeting for output
*/
private final DocumentName targetDir;
/**
* The name of the root dir we are reading from
*/
private final DocumentName rootDir;
/**
* The configured velocity engine
*/
private final VelocityEngine velocityEngine;
/**
* The context for Velocity
*/
private final VelocityContext context;
/**
* Create a rewriter.
*
* @param targetDir the root of the output directory tree.
*/
<span class="nc" id="L118"> Rewriter(final DocumentName targetDir) {</span>
<span class="nc" id="L119"> this.targetDir = targetDir;</span>
<span class="nc" id="L120"> this.rootDir = DocumentName.builder(new File(&quot;.&quot;)).build();</span>
<span class="nc" id="L121"> velocityEngine = new VelocityEngine();</span>
<span class="nc" id="L122"> velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, &quot;file&quot;);</span>
<span class="nc" id="L123"> velocityEngine.setProperty(&quot;file.resource.loader.class&quot;, FileResourceLoader.class.getName());</span>
<span class="nc" id="L124"> velocityEngine.init();</span>
<span class="nc" id="L125"> context = new VelocityContext();</span>
<span class="nc" id="L126"> context.put(&quot;esc&quot;, new EscapeTool());</span>
<span class="nc" id="L127"> context.put(&quot;rat&quot;, new RatTool());</span>
<span class="nc" id="L128"> }</span>
/**
* Processes the input document and creates an output document at an equivalent place in the output tree.
*
* @param document the input document.
*/
@Override
public void report(final Document document) {
<span class="nc" id="L137"> String localized = document.getName().localized();</span>
<span class="nc" id="L138"> DocumentName outputFile = targetDir.resolve(localized.substring(0, localized.length() - 3));</span>
<span class="nc" id="L139"> DocumentName relativeFile = DocumentName.builder(document.getName()).setBaseName(rootDir).build();</span>
try {
<span class="nc" id="L141"> final File file = outputFile.asFile();</span>
<span class="nc bnc" id="L142" title="All 4 branches missed."> if (!file.getParentFile().exists() &amp;&amp; !file.getParentFile().mkdirs()) {</span>
<span class="nc" id="L143"> throw new IOException(&quot;Unable to create directory: &quot; + file.getParentFile());</span>
}
<span class="nc" id="L145"> final Template template = velocityEngine.getTemplate(relativeFile.localized());</span>
<span class="nc" id="L147"> try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {</span>
<span class="nc" id="L148"> template.merge(context, writer);</span>
}
<span class="nc" id="L150"> } catch (IOException e) {</span>
<span class="nc" id="L151"> throw new RuntimeException(e);</span>
<span class="nc" id="L152"> }</span>
<span class="nc" id="L153"> }</span>
}
/**
* A RatReport implementation that processes the {@code .vm} files in the template tree and writes the
* results to the output tree.
*
* @param targetDir The base directory we are targeting for output
*/
private record Cleaner(DocumentName targetDir) implements RatReport {
/**
* Create a rewriter.
*
* @param targetDir the root of the output directory tree.
*/
<span class="nc" id="L168"> private Cleaner {</span>
<span class="nc" id="L169"> }</span>
/**
* Processes the input document and creates an output document at an equivalent place in the output tree.
*
* @param document the input document.
*/
@Override
public void report(final Document document) {
<span class="nc" id="L178"> String localized = document.getName().localized();</span>
<span class="nc" id="L179"> DocumentName outputFile = targetDir.resolve(localized.substring(0, localized.length() - 3));</span>
try {
<span class="nc" id="L181"> final File file = outputFile.asFile();</span>
<span class="nc bnc" id="L182" title="All 2 branches missed."> if (file.exists()) {</span>
<span class="nc" id="L183"> Files.delete(file.toPath());</span>
}
<span class="nc" id="L185"> } catch (IOException e) {</span>
<span class="nc" id="L186"> throw new RuntimeException(e);</span>
<span class="nc" id="L187"> }</span>
<span class="nc" id="L188"> }</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.14.202510111229</span></div></body></html>