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.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
50
51
52
53 @Mojo(name="tagreference")
54 public class TagreferenceMojo extends AbstractMavenReport
55 {
56
57
58
59
60 @Parameter(alias="taglib.src.dir", defaultValue="src/main/resources/META-INF")
61 private File srcDir;
62
63
64
65
66 @Parameter(defaultValue="false")
67 private boolean parseHtml;
68
69
70
71
72 public String getOutputName()
73 {
74 return "tagreference";
75 }
76
77
78
79
80 public String getName(Locale locale)
81 {
82 return Messages.getString(locale, "Tagreference.name");
83 }
84
85
86
87
88 public String getDescription(Locale locale)
89 {
90 return Messages.getString(locale, "Tagreference.description");
91 }
92
93
94
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"),
103 srcDir.getAbsolutePath()));
104 }
105
106 getLog().debug(
107 MessageFormat.format(Messages.getString("Taglib.validating"), srcDir.getAbsolutePath()));
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"),
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()));
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
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 }