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.net.MalformedURLException;
29  import java.net.URL;
30  import java.net.URLClassLoader;
31  import java.security.AccessController;
32  import java.security.PrivilegedAction;
33  import java.text.MessageFormat;
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.Locale;
37  
38  import javax.xml.parsers.DocumentBuilder;
39  
40  import net.sf.maventaglib.checker.Tld;
41  import net.sf.maventaglib.checker.TldParser;
42  import net.sf.maventaglib.util.XmlHelper;
43  
44  import org.apache.maven.artifact.DependencyResolutionRequiredException;
45  import org.apache.maven.plugin.MojoExecutionException;
46  import org.apache.maven.plugins.annotations.Mojo;
47  import org.apache.maven.plugins.annotations.Parameter;
48  import org.apache.maven.reporting.AbstractMavenReport;
49  import org.apache.maven.reporting.MavenReportException;
50  import org.codehaus.plexus.util.FileUtils;
51  import org.w3c.dom.Document;
52  
53  /**
54   * Generates a tag library validation report.
55   * @author Fabrizio Giustina
56   * @version $Id: ValidateMojo.java 217 2014-08-15 20:50:32Z fgiust $
57   */
58  @Mojo(name="validate")
59  public class ValidateMojo
60      extends AbstractMavenReport
61  {
62  
63      /**
64       * Directory containing tld files. Subdirectories are also processed.
65       */
66      @Parameter(alias="taglib.src.dir", defaultValue="src/main/resources/META-INF")
67      private File srcDir;
68  
69      /*
70       * The directory containing generated test classes of the project being tested.
71       */
72      //@Parameter(property="project.build.outputDirectory", required=true)
73      //private File buildOutputDirectory;
74  
75      /**
76       * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
77       */
78      public String getName( Locale locale )
79      {
80          return Messages.getString( locale, "Validate.name" ); //$NON-NLS-1$
81      }
82  
83      /**
84       * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
85       */
86      public String getDescription( Locale locale )
87      {
88          return Messages.getString( locale, "Validate.description" ); //$NON-NLS-1$
89      }
90  
91      /**
92       * @see org.apache.maven.reporting.MavenReport#getOutputName()
93       */
94      public String getOutputName()
95      {
96          return "taglibvalidation"; //$NON-NLS-1$
97      }
98  
99      /**
100      * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
101      */
102     @Override
103     protected void executeReport( Locale locale )
104         throws MavenReportException
105     {
106 
107         if ( !srcDir.isDirectory() )
108         {
109             throw new MavenReportException( MessageFormat
110                 .format( Messages.getString( "Taglib.notadir" ), //$NON-NLS-1$
111                     srcDir.getAbsolutePath() ) );
112         }
113 
114         getLog()
115             .debug(
116                     MessageFormat
117                         .format( Messages.getString( "Taglib.validating" ), srcDir.getAbsolutePath() ) ); //$NON-NLS-1$
118 
119         DocumentBuilder builder;
120 
121         try
122         {
123             builder = XmlHelper.getDocumentBuilder();
124         }
125         catch ( MojoExecutionException e )
126         {
127             throw new MavenReportException( e.getMessage(), e );
128         }
129 
130         List<File> tlds;
131         try
132         {
133             tlds = FileUtils.getFiles( srcDir, "**/*.tld", null ); //$NON-NLS-1$
134         }
135         catch ( IOException e )
136         {
137             throw new MavenReportException( e.getMessage(), e );
138         }
139 
140         List<Tld> tldList = new ArrayList<>();
141         for ( File current : tlds )
142         {
143             Document tldDoc;
144             try
145             {
146                 tldDoc = builder.parse( current );
147             }
148             catch ( Exception e )
149             {
150                 throw new MavenReportException( MessageFormat.format( Messages.getString( "Taglib.errorwhileparsing" ), //$NON-NLS-1$
151                                                                       current.getAbsolutePath() ), e );
152             }
153 
154             Tld tld = TldParser.parse( tldDoc, current.getName() );
155             tldList.add( tld );
156 
157         }
158         if ( tldList.size() == 0 )
159         {
160             getLog()
161                 .info(
162                        MessageFormat
163                            .format(
164                                     Messages.getString( "Taglib.notldfound" ), srcDir.getAbsolutePath() ) ); //$NON-NLS-1$
165             return;
166         }
167 
168         List<String> classPathStrings;
169         try
170         {
171             classPathStrings = this.project.getCompileClasspathElements();
172         }
173         catch ( DependencyResolutionRequiredException e )
174         {
175             throw new MavenReportException( e.getMessage(), e );
176         }
177 
178         List<URL> URLs = new ArrayList<>( classPathStrings.size() );
179 
180         for ( String classPathString : classPathStrings )
181         {
182             try
183             {
184                 URLs.add( new File( classPathString ).toURI().toURL() );
185             }
186             catch ( MalformedURLException e )
187             {
188                 throw new MavenReportException( e.getMessage(), e );
189             }
190         }
191 
192         URLClassLoader projectClassLoader = AccessController.doPrivileged(
193                 (PrivilegedAction<URLClassLoader>) () ->
194                         new URLClassLoader( URLs.toArray( new URL[0] ), null )
195         );
196 
197         ValidateRenderer r = new ValidateRenderer( getSink(), locale,
198                                                    tldList.toArray( new Tld[tldList.size()] ), getLog(),
199                                                    projectClassLoader );
200 
201         r.render();
202 
203     }
204 
205     /**
206      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
207      */
208     @Override
209     public boolean canGenerateReport()
210     {
211         if ( !srcDir.isDirectory() )
212         {
213             return false;
214         }
215 
216         try
217         {
218             return FileUtils.getFiles( srcDir, "**/*.tld", null ).size() > 0; //$NON-NLS-1$
219         }
220         catch ( IOException e )
221         {
222             getLog().error( e.getMessage(), e );
223         }
224         return false;
225     }
226 
227 }