1 package org.apache.maven.plugin.changes;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Goal which validate the <code>changes.xml</code> file.
37 *
38 * @goal changes-validate
39 *
40 * @author <a href="mailto:olamy@apache.org">olamy</a>
41 * @version $Id: ChangesValidatorMojo.html 816598 2012-05-08 12:46:49Z hboutemy $
42 * @since 2.1
43 * @threadSafe
44 */
45 public class ChangesValidatorMojo
46 extends AbstractMojo
47 {
48
49 /**
50 * @component role="org.apache.maven.plugin.changes.schema.ChangesSchemaValidator" roleHint="default"
51 */
52 private ChangesSchemaValidator changesSchemaValidator;
53
54 /**
55 * The changes xsd version.
56 *
57 * @parameter expression="${changes.xsdVersion}" default-value="1.0.0"
58 */
59 private String changesXsdVersion;
60
61 /**
62 * Mojo failure if validation failed. If not and validation failed only a warning will be logged.
63 *
64 * @parameter expression="${changes.validate.failed}" default-value="false"
65 */
66 private boolean failOnError;
67
68 /**
69 * The path of the <code>changes.xml</code> file that will be converted into an HTML report.
70 *
71 * @parameter expression="${changes.xmlPath}" default-value="src/changes/changes.xml"
72 */
73 private File xmlPath;
74
75 /**
76 * @see org.apache.maven.plugin.Mojo#execute()
77 */
78 public void execute()
79 throws MojoExecutionException, MojoFailureException
80 {
81
82 if ( !xmlPath.exists() )
83 {
84 getLog().warn( "changes.xml file " + xmlPath.getAbsolutePath() + " does not exist." );
85 return;
86 }
87
88 try
89 {
90 XmlValidationHandler xmlValidationHandler = changesSchemaValidator
91 .validateXmlWithSchema( xmlPath, changesXsdVersion, failOnError );
92 boolean hasErrors = !xmlValidationHandler.getErrors().isEmpty();
93 if ( hasErrors )
94 {
95 logSchemaValidation( xmlValidationHandler.getErrors() );
96 if ( failOnError )
97 {
98 throw new MojoExecutionException( "changes.xml file " + xmlPath.getAbsolutePath()
99 + " is not valid, see previous errors." );
100 }
101 else
102 {
103 getLog().info( " skip previous validation errors due to failOnError=false." );
104 }
105 }
106 }
107 catch ( SchemaValidatorException e )
108 {
109 if ( failOnError )
110 {
111 throw new MojoExecutionException( "failed to validate changes.xml file " + xmlPath.getAbsolutePath()
112 + ": " + e.getMessage(), e );
113 }
114 }
115 }
116
117 private void logSchemaValidation( List /*SAXException*/errors )
118 {
119 getLog().warn( "failed to validate changes.xml file " + xmlPath.getAbsolutePath() );
120 getLog().warn( "validation errors: " );
121 for ( Iterator iterator = errors.iterator(); iterator.hasNext(); )
122 {
123 SAXException error = (SAXException) iterator.next();
124 getLog().warn( error.getMessage() );
125 }
126 }
127
128 }