1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package org.devacfr.maven.skins.reflow.snippet; |
20 |
|
|
21 |
|
import java.io.File; |
22 |
|
import java.io.IOException; |
23 |
|
import java.nio.charset.Charset; |
24 |
|
|
25 |
|
import com.google.common.base.Charsets; |
26 |
|
import com.google.common.base.Strings; |
27 |
|
import com.google.common.io.CharSource; |
28 |
|
import com.google.common.io.Files; |
29 |
|
import org.apache.maven.doxia.macro.AbstractMacro; |
30 |
|
import org.apache.maven.doxia.macro.Macro; |
31 |
|
import org.apache.maven.doxia.macro.MacroExecutionException; |
32 |
|
import org.apache.maven.doxia.macro.MacroRequest; |
33 |
|
import org.apache.maven.doxia.sink.Sink; |
34 |
|
import org.codehaus.plexus.PlexusConstants; |
35 |
|
import org.codehaus.plexus.PlexusContainer; |
36 |
|
import org.codehaus.plexus.component.annotations.Component; |
37 |
|
import org.codehaus.plexus.context.Context; |
38 |
|
import org.codehaus.plexus.context.ContextException; |
39 |
|
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
40 |
|
import org.jsoup.Jsoup; |
41 |
|
import org.jsoup.nodes.Document; |
42 |
|
import org.slf4j.Logger; |
43 |
|
import org.slf4j.LoggerFactory; |
44 |
|
|
45 |
|
|
46 |
|
@author |
47 |
|
@since |
48 |
|
|
49 |
|
@Component(role = Macro.class, hint = "partial") |
|
|
| 10,5% |
Uncovered Elements: 34 (38) |
Complexity: 11 |
Complexity Density: 0,46 |
|
50 |
|
public class PartialTemplateMacro extends AbstractMacro implements Contextualizable { |
51 |
|
|
52 |
|
|
53 |
|
@SuppressWarnings("unused") |
54 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(PartialTemplateMacro.class); |
55 |
|
|
56 |
|
|
57 |
|
@SuppressWarnings("unused") |
58 |
|
private PlexusContainer container; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private final boolean ignoreDownloadError = true; |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
0 |
@Override... |
66 |
|
public void contextualize(final Context context) throws ContextException { |
67 |
0 |
container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
@inheritDoc |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0,38 |
|
73 |
0 |
@Override... |
74 |
|
public void execute(final Sink sink, final MacroRequest request) throws MacroExecutionException { |
75 |
0 |
final String filePath = (String) request.getParameter("file"); |
76 |
0 |
String encoding = (String) request.getParameter("encoding"); |
77 |
|
|
78 |
0 |
File file; |
79 |
|
|
80 |
0 |
if (Strings.isNullOrEmpty(encoding)) { |
81 |
0 |
encoding = Charsets.UTF_8.name(); |
82 |
|
} |
83 |
|
|
84 |
0 |
if (!Strings.isNullOrEmpty(filePath)) { |
85 |
0 |
file = new File(filePath); |
86 |
|
|
87 |
0 |
if (!file.isAbsolute()) { |
88 |
0 |
file = new File(request.getBasedir(), filePath); |
89 |
|
} |
90 |
|
} else { |
91 |
0 |
throw new IllegalArgumentException("The 'file' param has to be given."); |
92 |
|
} |
93 |
|
|
94 |
0 |
try { |
95 |
0 |
sink.rawText(getSnippet(file, encoding)); |
96 |
|
} catch (final IOException e) { |
97 |
0 |
throw new MacroExecutionException("Error reading snippet", e); |
98 |
|
} |
99 |
|
} |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0,57 |
|
101 |
0 |
private String getSnippet(final File file, final String encoding) throws IOException {... |
102 |
0 |
try { |
103 |
0 |
return convertSnippet(Files.asByteSource(file).asCharSource(Charset.forName(encoding))); |
104 |
|
|
105 |
|
} catch (final IOException e) { |
106 |
0 |
if (ignoreDownloadError) { |
107 |
0 |
if (LOGGER.isDebugEnabled()) { |
108 |
0 |
LOGGER.debug("IOException which reading " + file + ": " + e); |
109 |
|
} |
110 |
0 |
return "Error during retrieving content skip as ignoreDownloadError activated."; |
111 |
|
} else { |
112 |
0 |
throw e; |
113 |
|
} |
114 |
|
} |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
117 |
2 |
String convertSnippet(final CharSource source) throws IOException {... |
118 |
2 |
final String src = source.read(); |
119 |
2 |
final Document doc = Jsoup.parse(src); |
120 |
2 |
return new ComponentResolver().normalize(doc).html(); |
121 |
|
} |
122 |
|
} |