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