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.HashSet;
30  import java.util.List;
31  import java.util.Locale;
32  import java.util.Set;
33  
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.reporting.AbstractMavenReport;
38  import org.apache.maven.reporting.MavenReport;
39  import org.apache.maven.reporting.MavenReportException;
40  import org.codehaus.plexus.util.FileUtils;
41  
42  import com.sun.tlddoc.TLDDocGenerator;
43  
44  
45  /**
46   * Generates taglibdoc documentation.
47   * @author Fabrizio Giustina
48   * @version $Id: TaglibdocMojo.java 217 2014-08-15 20:50:32Z fgiust $
49   */
50  @Mojo(name="taglibdoc")
51  public class TaglibdocMojo extends AbstractMavenReport implements MavenReport
52  {
53  
54      /**
55       * title for tlddoc generated documentation.
56       */
57      @Parameter(defaultValue="${project.name} Tag library documentation")
58      private String title;
59  
60      /**
61       * TldDoc output dir.
62       */
63      @Parameter(defaultValue="${project.reporting.outputDirectory}/tlddoc")
64      private File tldDocDir;
65  
66      /**
67       * Directory containing tld or tag files. Subdirectories are also processed, unless the "dontRecurseIntoSubdirs"
68       * parameter is set.
69       */
70      @Parameter(alias="taglib.src.dir", defaultValue="src/main/resources/META-INF")
71      private File srcDir;
72  
73      /**
74       * If set, only file contained directly in the specified directory are used.
75       */
76      @Parameter
77      private boolean dontRecurseIntoSubdirs;
78  
79      /**
80       * Directory containing custom xsl files (equivalent to the "-xslt" parameter to tlddoc).
81       */
82      @Parameter
83      private File xsltDir;
84  
85      /**
86       * @see AbstractMavenReport#execute()
87       */
88      @Override
89      public void execute() throws MojoExecutionException
90      {
91          getLog().debug(MessageFormat.format(Messages.getString("Taglib.generating.tlddoc"), //$NON-NLS-1$
92              srcDir.getAbsolutePath() ));
93          TLDDocGenerator generator = new TLDDocGenerator();
94          generator.setOutputDirectory(tldDocDir);
95          generator.setQuiet(true);
96          generator.setWindowTitle(this.title);
97          if (xsltDir != null)
98          {
99              generator.setXSLTDirectory(xsltDir);
100         }
101 
102         String searchprefix = dontRecurseIntoSubdirs ? "" : "**/";
103 
104         if (!srcDir.isDirectory())
105         {
106             throw new MojoExecutionException(MessageFormat.format(
107                 Messages.getString("Taglib.notadir"), //$NON-NLS-1$
108                     srcDir.getAbsolutePath() ));
109         }
110 
111         try
112         {
113             // handle tlds
114             List<File> tlds = FileUtils.getFiles(srcDir, searchprefix + "*.tld", null); //$NON-NLS-1$
115             for (File tld : tlds)
116             {
117                 generator.addTLD(tld);
118             }
119 
120             // handle tag files. Add any directory containing .tag or .tagx files
121             List<File> tags = FileUtils.getFiles(srcDir, searchprefix + "*.tag", null); //$NON-NLS-1$
122             tags.addAll(FileUtils.getFiles(srcDir, searchprefix + "*.tagx", null)); //$NON-NLS-1$
123 
124             if (!tags.isEmpty())
125             {
126                 Set<File> directories = new HashSet<>();
127                 for (File tag : tags)
128                 {
129                     directories.add(tag.getParentFile());
130                 }
131                 for (File directory : directories)
132                 {
133                     generator.addTagDir(directory);
134                 }
135             }
136 
137         }
138         catch (IOException e)
139         {
140             throw new MojoExecutionException(e.getMessage(), e);
141         }
142 
143         try
144         {
145             generator.generate();
146         }
147         catch (Throwable e)
148         {
149             getLog().error(MessageFormat.format(Messages.getString("Taglib.exception"), //$NON-NLS-1$
150                 e.getClass(), e.getMessage() ), e);
151         }
152     }
153 
154     /**
155      * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
156      */
157     @Override
158     protected void executeReport(Locale locale) throws MavenReportException
159     {
160         try
161         {
162             execute();
163         }
164         catch (MojoExecutionException e)
165         {
166             throw new MavenReportException(e.getMessage(), e);
167         }
168 
169     }
170 
171     /**
172      * @see org.apache.maven.reporting.MavenReport#getOutputName()
173      */
174     public String getOutputName()
175     {
176         return "tlddoc/index"; //$NON-NLS-1$
177     }
178 
179     /**
180      * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
181      */
182     public String getName(Locale locale)
183     {
184         return Messages.getString(locale, "Taglibdoc.name"); //$NON-NLS-1$
185     }
186 
187     /**
188      * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
189      */
190     public String getDescription(Locale locale)
191     {
192         return Messages.getString(locale, "Taglibdoc.description"); //$NON-NLS-1$
193     }
194 
195     /**
196      * @see org.apache.maven.reporting.MavenReport#isExternalReport()
197      */
198     @Override
199     public boolean isExternalReport()
200     {
201         return true;
202     }
203 
204     /**
205      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
206      */
207     @Override
208     public boolean canGenerateReport()
209     {
210 
211         if (!srcDir.isDirectory())
212         {
213             return false;
214         }
215 
216         try
217         {
218             boolean hasTldFiles = FileUtils.getFiles(srcDir, "**/*.tld", null).size() > 0;//$NON-NLS-1$
219             boolean hasTagFiles = FileUtils.getFiles(srcDir, "**/*.tag", null).size() > 0;//$NON-NLS-1$
220             return hasTldFiles || hasTagFiles;
221         }
222         catch (IOException e)
223         {
224             getLog().error(e.getMessage(), e);
225         }
226         return false;
227 
228     }
229 
230 }