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