Class |
Line # |
Actions |
|||||
---|---|---|---|---|---|---|---|
PartialTemplateMacro | 50 | 24 | 0% | 11 | 34 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 Christophe Friederich | |
47 | * @since 2.4 | |
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 | * in case of Exception during snippet download error will ignored and empty content returned. | |
62 | */ | |
63 | private final boolean ignoreDownloadError = true; | |
64 | ||
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 | */ | |
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 | ||
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 | ||
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 | } |