1 | |
package org.apache.maven.plugin.changes; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.apache.maven.plugin.AbstractMojo; |
27 | |
import org.apache.maven.plugin.MojoExecutionException; |
28 | |
import org.apache.maven.plugin.MojoFailureException; |
29 | |
import org.apache.maven.plugin.changes.schema.ChangesSchemaValidator; |
30 | |
import org.apache.maven.plugin.changes.schema.SchemaValidatorException; |
31 | |
import org.apache.maven.plugin.changes.schema.XmlValidationHandler; |
32 | |
import org.xml.sax.SAXException; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 6 | public class ChangesValidatorMojo |
46 | |
extends AbstractMojo |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
private ChangesSchemaValidator changesSchemaValidator; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private String changesXsdVersion; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private boolean failOnError; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private File xmlPath; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void execute() |
79 | |
throws MojoExecutionException, MojoFailureException |
80 | |
{ |
81 | |
|
82 | 6 | if ( !xmlPath.exists() ) |
83 | |
{ |
84 | 0 | getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." ); |
85 | 0 | return; |
86 | |
} |
87 | |
|
88 | |
try |
89 | |
{ |
90 | 6 | XmlValidationHandler xmlValidationHandler = changesSchemaValidator |
91 | |
.validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError ); |
92 | 4 | boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty(); |
93 | 4 | if ( hasErrors ) |
94 | |
{ |
95 | 2 | logSchemaValidation( xmlValidationHandler.getErrors() ); |
96 | 2 | if ( failOnError ) |
97 | |
{ |
98 | 0 | throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath() |
99 | |
+ " is not valid, see previous errors." ); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 2 | getLog().info( " skip previous validation errors due to failOnError=false." ); |
104 | |
} |
105 | |
} |
106 | |
} |
107 | 2 | catch ( SchemaValidatorException e ) |
108 | |
{ |
109 | 2 | if ( failOnError ) |
110 | |
{ |
111 | 2 | throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() |
112 | |
+ ": " + e.getMessage(), e ); |
113 | |
} |
114 | 4 | } |
115 | 4 | } |
116 | |
|
117 | |
private void logSchemaValidation( List errors ) |
118 | |
{ |
119 | 2 | getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() ); |
120 | 2 | getLog().warn( "validation errors: " ); |
121 | 2 | for ( Iterator iterator = errors.iterator(); iterator.hasNext(); ) |
122 | |
{ |
123 | 2 | SAXException error = (SAXException) iterator.next(); |
124 | 2 | getLog().warn( error.getMessage() ); |
125 | 2 | } |
126 | 2 | } |
127 | |
|
128 | |
} |