1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
55
56
57
58 @Mojo(name="validate")
59 public class ValidateMojo
60 extends AbstractMavenReport
61 {
62
63
64
65
66 @Parameter(alias="taglib.src.dir", defaultValue="src/main/resources/META-INF")
67 private File srcDir;
68
69
70
71
72
73
74
75
76
77
78 public String getName( Locale locale )
79 {
80 return Messages.getString( locale, "Validate.name" );
81 }
82
83
84
85
86 public String getDescription( Locale locale )
87 {
88 return Messages.getString( locale, "Validate.description" );
89 }
90
91
92
93
94 public String getOutputName()
95 {
96 return "taglibvalidation";
97 }
98
99
100
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" ),
111 srcDir.getAbsolutePath() ) );
112 }
113
114 getLog()
115 .debug(
116 MessageFormat
117 .format( Messages.getString( "Taglib.validating" ), srcDir.getAbsolutePath() ) );
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 );
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" ),
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() ) );
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
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;
219 }
220 catch ( IOException e )
221 {
222 getLog().error( e.getMessage(), e );
223 }
224 return false;
225 }
226
227 }