1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
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
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
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 }