1. Project Clover database mar. janv. 20 2026 12:32:22 CET
  2. Package org.devacfr.maven.skins.reflow.snippet

File PartialTemplateMacro.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart1.png
94% of files have more coverage

Code metrics

10
24
4
1
117
75
11
0,46
6
4
2,75

Classes

Class Line # Actions
PartialTemplateMacro 46 24 0% 11 34
0.1052631610,5%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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  0 toggle @Override
61    public void contextualize(final Context context) throws ContextException {
62  0 container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY);
63    }
64   
65    /**
66    * {@inheritDoc}
67    */
 
68  0 toggle @Override
69    public void execute(final Sink sink, final MacroRequest request) throws MacroExecutionException {
70  0 final String filePath = (String) request.getParameter("file");
71  0 String encoding = (String) request.getParameter("encoding");
72   
73  0 File file;
74   
75  0 if (Strings.isNullOrEmpty(encoding)) {
76  0 encoding = Charsets.UTF_8.name();
77    }
78   
79  0 if (!Strings.isNullOrEmpty(filePath)) {
80  0 file = new File(filePath);
81   
82  0 if (!file.isAbsolute()) {
83  0 file = new File(request.getBasedir(), filePath);
84    }
85    } else {
86  0 throw new IllegalArgumentException("The 'file' param has to be given.");
87    }
88   
89  0 try {
90  0 sink.rawText(getSnippet(file, encoding));
91    } catch (final IOException e) {
92  0 throw new MacroExecutionException("Error reading snippet", e);
93    }
94    }
95   
 
96  0 toggle private String getSnippet(final File file, final String encoding) throws IOException {
97  0 try {
98  0 return convertSnippet(Files.asByteSource(file).asCharSource(Charset.forName(encoding)));
99   
100    } catch (final IOException e) {
101  0 if (ignoreDownloadError) {
102  0 if (LOGGER.isDebugEnabled()) {
103  0 LOGGER.debug("IOException which reading " + file + ": " + e);
104    }
105  0 return "Error during retrieving content skip as ignoreDownloadError activated.";
106    } else {
107  0 throw e;
108    }
109    }
110    }
111   
 
112  2 toggle String convertSnippet(final CharSource source) throws IOException {
113  2 final String src = source.read();
114  2 final Document doc = JsoupUtils.createHtmlDocument(src);
115  2 return new ComponentResolver().normalize(doc).html();
116    }
117    }