View Javadoc
1   /*
2    * The MIT License
3    * Copyright © 2004-2014 Fabrizio Giustina
4    * Copyright © 2022-2022 Web-Legacy
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package net.sf.maventaglib;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.text.MessageFormat;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Locale;
32  
33  import javax.xml.parsers.DocumentBuilder;
34  
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugins.annotations.Mojo;
37  import org.apache.maven.plugins.annotations.Parameter;
38  import org.apache.maven.reporting.AbstractMavenReport;
39  import org.apache.maven.reporting.MavenReportException;
40  import org.codehaus.plexus.util.FileUtils;
41  import org.w3c.dom.Document;
42  
43  import net.sf.maventaglib.checker.Tld;
44  import net.sf.maventaglib.checker.TldParser;
45  import net.sf.maventaglib.util.XmlHelper;
46  
47  
48  /**
49   * Generates a tag reference xdoc that can be integrated in a maven generated site.
50   * @author Fabrizio Giustina
51   * @version $Id: TagreferenceMojo.java 217 2014-08-15 20:50:32Z fgiust $
52   */
53  @Mojo(name="tagreference")
54  public class TagreferenceMojo extends AbstractMavenReport
55  {
56  
57      /**
58       * Directory containing tld files. Subdirectories are also processed.
59       */
60      @Parameter(alias="taglib.src.dir", defaultValue="src/main/resources/META-INF")
61      private File srcDir;
62  
63      /**
64       * Whether to parse html in the description of tld info, tags and attributes. The default value is false.
65       */
66      @Parameter(defaultValue="false")
67      private boolean parseHtml;
68  
69      /**
70       * @see org.apache.maven.reporting.MavenReport#getOutputName()
71       */
72      public String getOutputName()
73      {
74          return "tagreference"; //$NON-NLS-1$
75      }
76  
77      /**
78       * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
79       */
80      public String getName(Locale locale)
81      {
82          return Messages.getString(locale, "Tagreference.name"); //$NON-NLS-1$
83      }
84  
85      /**
86       * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
87       */
88      public String getDescription(Locale locale)
89      {
90          return Messages.getString(locale, "Tagreference.description"); //$NON-NLS-1$
91      }
92  
93      /**
94       * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
95       */
96      @Override
97      protected void executeReport(Locale locale) throws MavenReportException
98      {
99          if (!srcDir.isDirectory())
100         {
101             throw new MavenReportException(MessageFormat.format(
102                 Messages.getString("Taglib.notadir"), //$NON-NLS-1$
103                     srcDir.getAbsolutePath()));
104         }
105 
106         getLog().debug(
107             MessageFormat.format(Messages.getString("Taglib.validating"), srcDir.getAbsolutePath())); //$NON-NLS-1$
108 
109         DocumentBuilder builder;
110 
111         try
112         {
113             builder = XmlHelper.getDocumentBuilder();
114         }
115         catch (MojoExecutionException e)
116         {
117             throw new MavenReportException(e.getMessage(), e);
118         }
119         List<File> tlds;
120         try
121         {
122             tlds = FileUtils.getFiles(srcDir, "**/*.tld", null);
123         }
124         catch (IOException e)
125         {
126             throw new MavenReportException(e.getMessage(), e);
127         }
128         List<Tld> tldList = new ArrayList<>();
129         for (File current : tlds)
130         {
131             Document tldDoc;
132             try
133             {
134                 tldDoc = builder.parse(current);
135             }
136             catch (Exception e)
137             {
138                 throw new MavenReportException(MessageFormat.format(Messages.getString("Taglib.errorwhileparsing"), //$NON-NLS-1$
139                     current.getAbsolutePath()), e);
140 
141             }
142 
143             Tld tld = TldParser.parse(tldDoc, current.getName());
144             tldList.add(tld);
145 
146         }
147 
148         if (tldList.size() == 0)
149         {
150             getLog().info(
151                 MessageFormat.format(Messages.getString("Taglib.notldfound"), srcDir.getAbsolutePath())); //$NON-NLS-1$
152             return;
153         }
154 
155         new TagreferenceRenderer(
156             getSink(),
157             locale,
158             tldList.toArray(new Tld[tldList.size()]),
159             parseHtml,
160             getLog()).render();
161 
162     }
163 
164     /**
165      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
166      */
167     @Override
168     public boolean canGenerateReport()
169     {
170         if (!srcDir.isDirectory())
171         {
172             return false;
173         }
174 
175         try
176         {
177             return FileUtils.getFiles(srcDir, "**/*.tld", null).size() > 0;
178         }
179         catch (IOException e)
180         {
181             getLog().error(e.getMessage(), e);
182         }
183         return false;
184 
185     }
186 
187 }