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