View Javadoc
1   /*
2   * Copyright 2012-2025 Christophe Friederich
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   * http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package org.devacfr.maven.skins.reflow.snippet;
17  
18  import com.google.common.base.Charsets;
19  import com.google.common.base.Strings;
20  import com.google.common.io.CharSource;
21  import com.google.common.io.Files;
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.charset.Charset;
25  import org.apache.maven.doxia.macro.AbstractMacro;
26  import org.apache.maven.doxia.macro.Macro;
27  import org.apache.maven.doxia.macro.MacroExecutionException;
28  import org.apache.maven.doxia.macro.MacroRequest;
29  import org.apache.maven.doxia.sink.Sink;
30  import org.codehaus.plexus.PlexusConstants;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.context.Context;
34  import org.codehaus.plexus.context.ContextException;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36  import org.devacfr.maven.skins.reflow.JsoupUtils;
37  import org.jsoup.nodes.Document;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  /**
42   * @author Christophe Friederich
43   * @since 2.4
44   */
45  @Component(role = Macro.class, hint = "partial")
46  public class PartialTemplateMacro extends AbstractMacro implements Contextualizable {
47  
48    /** */
49    private static final Logger LOGGER = LoggerFactory.getLogger(PartialTemplateMacro.class);
50  
51    /** */
52    @SuppressWarnings("unused")
53    private PlexusContainer container;
54  
55    /**
56     * in case of Exception during snippet download error will ignored and empty content returned.
57     */
58    private final boolean ignoreDownloadError = true;
59  
60    @Override
61    public void contextualize(final Context context) throws ContextException {
62      container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY);
63    }
64  
65    /**
66     * {@inheritDoc}
67     */
68    @Override
69    public void execute(final Sink sink, final MacroRequest request) throws MacroExecutionException {
70      final String filePath = (String) request.getParameter("file");
71      String encoding = (String) request.getParameter("encoding");
72  
73      File file;
74  
75      if (Strings.isNullOrEmpty(encoding)) {
76        encoding = Charsets.UTF_8.name();
77      }
78  
79      if (!Strings.isNullOrEmpty(filePath)) {
80        file = new File(filePath);
81  
82        if (!file.isAbsolute()) {
83          file = new File(request.getBasedir(), filePath);
84        }
85      } else {
86        throw new IllegalArgumentException("The 'file' param has to be given.");
87      }
88  
89      try {
90        sink.rawText(getSnippet(file, encoding));
91      } catch (final IOException e) {
92        throw new MacroExecutionException("Error reading snippet", e);
93      }
94    }
95  
96    private String getSnippet(final File file, final String encoding) throws IOException {
97      try {
98        return convertSnippet(Files.asByteSource(file).asCharSource(Charset.forName(encoding)));
99  
100     } catch (final IOException e) {
101       if (ignoreDownloadError) {
102         if (LOGGER.isDebugEnabled()) {
103           LOGGER.debug("IOException which reading " + file + ": " + e);
104         }
105         return "Error during retrieving content skip as ignoreDownloadError activated.";
106       } else {
107         throw e;
108       }
109     }
110   }
111 
112   String convertSnippet(final CharSource source) throws IOException {
113     final String src = source.read();
114     final Document doc = JsoupUtils.createHtmlDocument(src);
115     return new ComponentResolver().normalize(doc).html();
116   }
117 }