View Javadoc
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      @Override
66      public void contextualize(final Context context) throws ContextException {
67          container = (PlexusContainer) context.get(PlexusConstants.PLEXUS_KEY);
68      }
69  
70      /**
71       * {@inheritDoc}
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 }