Content of file XsltMacroFilter.java.html

<?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="en"><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>XsltMacroFilter.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">NorthernWind :: Frontend :: Components :: HTML Template</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.impl.filter</a> &gt; <span class="el_source">XsltMacroFilter.java</span></div><h1>XsltMacroFilter.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
 * %%
 * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed 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.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.northernwind.core.impl.filter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.Optional;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import it.tidalwave.northernwind.core.model.Resource;
import it.tidalwave.northernwind.core.model.ResourceFile;
import it.tidalwave.northernwind.core.model.SiteProvider;
import it.tidalwave.northernwind.core.model.Template.Aggregate;
import it.tidalwave.northernwind.core.impl.model.Filter;
import lombok.extern.slf4j.Slf4j;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
import static it.tidalwave.northernwind.core.model.Resource._Resource_;
import static it.tidalwave.northernwind.core.model.Template.Aggregates.toAggregates;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L72">@Configurable @Order(HIGHEST_PRECEDENCE) @Slf4j</span>
<span class="pc bpc" id="L73" title="9 of 18 branches missed.">public class XsltMacroFilter implements Filter</span>
  {
    private static final String XSLT_TEMPLATES_PATH = &quot;/XsltTemplates/.*&quot;;

    private static final String DOCTYPE_HTML = &quot;&lt;!DOCTYPE html&gt;&quot;;

    @Inject
    private ApplicationContext context;

    @Inject
    private DocumentBuilderFactory factory;

    @Inject
    private TransformerFactory transformerFactory;

    @Inject
    private Provider&lt;SiteProvider&gt; siteProvider;

<span class="pc bpc" id="L91" title="2 of 4 branches missed.">    private String xslt = &quot;&quot;;</span>

    private volatile boolean initialized;

    private Method serializerMethod;

    /*******************************************************************************************************************
     *
     ******************************************************************************************************************/
    // FIXME: this should be shared between instances
this should be shared between instances
private void initialize() { <span class="fc" id="L103"> log.info(&quot;Retrieving XSLT templates&quot;);</span> <span class="fc" id="L104"> final var site = siteProvider.get().getSite();</span> <span class="fc" id="L105"> final var macros = site.find(_Resource_).withRelativePath(XSLT_TEMPLATES_PATH)</span> <span class="fc" id="L106"> .stream()</span> <span class="fc" id="L107"> .map(Resource::getFile)</span> <span class="fc" id="L108"> .map(f -&gt; Aggregate.of(&quot;body&quot;, asText(f)).with(&quot;name&quot;, f.getPath()))</span> <span class="fc" id="L109"> .collect(toAggregates(&quot;macros&quot;));</span> <span class="fc" id="L110"> xslt = site.getTemplate(getClass(), Optional.empty(), &quot;XsltTemplate.xslt&quot;).render(macros);</span> <span class="fc" id="L111"> log.trace(&quot;&gt;&gt;&gt;&gt; xslt: {}&quot;, xslt);</span> try { <span class="fc" id="L115"> final var clazz = Thread.currentThread().getContextClassLoader().loadClass(</span> &quot;it.tidalwave.northernwind.core.impl.util.XhtmlMarkupSerializerDecoupler&quot;); <span class="fc" id="L117"> serializerMethod = clazz.getMethod(&quot;serialize&quot;, Node.class, StringWriter.class);</span> } <span class="nc" id="L119"> catch (ClassNotFoundException | NoSuchMethodException e)</span> { <span class="nc" id="L121"> throw new RuntimeException(e);</span> <span class="fc" id="L122"> }</span> <span class="fc" id="L123"> }</span> /******************************************************************************************************************* * ******************************************************************************************************************/ @Override @Nonnull public String filter (@Nonnull final String text, @Nonnull final String mimeType) { <span class="fc bfc" id="L131" title="All 2 branches covered."> if (!&quot;application/xhtml+xml&quot;.equals(mimeType))</span> { <span class="fc" id="L133"> log.debug(&quot;Cannot filter resources not in XHTML: {}&quot;, mimeType);</span> <span class="fc" id="L134"> return text;</span> } // FIXME: buggy and cumbersome <span class="pc bpc" id="L138" title="1 of 2 branches missed."> if (!initialized)</span> { <span class="fc" id="L140"> synchronized (this)</span> { <span class="pc bpc" id="L142" title="1 of 2 branches missed."> if (!initialized)</span> { <span class="fc" id="L144"> initialize();</span> <span class="fc" id="L145"> initialized = true;</span> } <span class="fc" id="L147"> }</span> } try { <span class="fc" id="L152"> final var result = new DOMResult();</span> <span class="fc" id="L153"> final var transformer = createTransformer();</span> // Fix for NW-100 <span class="fc" id="L155"> transformer.transform(new DOMSource(stringToNode(text.replace(&quot;xml:lang&quot;, &quot;xml_lang&quot;))), result);</span> <span class="fc" id="L157"> final var stringWriter = new StringWriter();</span> <span class="fc bfc" id="L159" title="All 2 branches covered."> if (text.startsWith(DOCTYPE_HTML))</span> { <span class="fc" id="L161"> stringWriter.append(DOCTYPE_HTML).append(&quot;\n&quot;);</span> } // Fix for NW-96 // This must be accessed by reflection because the JDK 11+ compiler with --source 11 refuses to compile // stuff that depends on com.sun.* classes. <span class="fc" id="L167"> serializerMethod.invoke(null, result.getNode(), stringWriter);</span> <span class="fc" id="L168"> return stringWriter.toString().replace(&quot;xml_lang&quot;, &quot;xml:lang&quot;).replace(&quot; xmlns=\&quot;\&quot;&quot;, &quot;&quot;); // FIXME:</span> } <span class="nc" id="L170"> catch (SAXParseException e)</span> { <span class="nc" id="L172"> log.error(&quot;XML parse error: {} at l{}:c{}&quot;, e.getMessage(), e.getLineNumber(), e.getColumnNumber());</span> <span class="nc" id="L173"> log.error(text);</span> <span class="nc" id="L174"> throw new RuntimeException(e);</span> } <span class="nc" id="L176"> catch (TransformerException e)</span> { <span class="nc" id="L178"> log.error(&quot;XSL error: {} at {}&quot;, e, e.getLocationAsString());</span> <span class="nc" id="L179"> log.error(xslt);</span> <span class="nc" id="L180"> throw new RuntimeException(e);</span> } <span class="nc" id="L182"> catch (IOException | SAXException | ParserConfigurationException | IllegalAccessException | InvocationTargetException e)</span> { <span class="nc" id="L184"> throw new RuntimeException(e);</span> } } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private Transformer createTransformer() throws TransformerConfigurationException { <span class="fc" id="L195"> final Source transformation = new StreamSource(new StringReader(xslt));</span> <span class="fc" id="L196"> final var transformer = transformerFactory.newTransformer(transformation);</span> try { <span class="nc" id="L200"> final var uriResolver = context.getBean(URIResolver.class);</span> <span class="nc" id="L201"> log.trace(&quot;Using URIResolver: {}&quot;, uriResolver.getClass());</span> <span class="nc" id="L202"> transformer.setURIResolver(uriResolver);</span> } <span class="fc" id="L204"> catch (NoSuchBeanDefinitionException e)</span> { // ok, not installed <span class="nc" id="L207"> }</span> <span class="fc" id="L209"> return transformer;</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private static String asText (@Nonnull final ResourceFile file) { try { <span class="fc" id="L220"> log.info(&quot;&gt;&gt;&gt;&gt; {}&quot;, file.getPath().asString());</span> <span class="fc" id="L221"> return file.asText(&quot;UTF-8&quot;);</span> } <span class="nc" id="L223"> catch (IOException e)</span> { <span class="nc" id="L225"> throw new RuntimeException(e);</span> } } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private Node stringToNode (@Nonnull final String string) throws IOException, SAXException, ParserConfigurationException { <span class="fc" id="L236"> factory.setValidating(false);</span> <span class="fc" id="L237"> final var builder = factory.newDocumentBuilder();</span> <span class="fc" id="L238"> final var source = new InputSource(new StringReader(string));</span> <span class="fc" id="L239"> return builder.parse(source);</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>