Features
Any XML file can be validated with this plugin.The plugin provides static code analysis of XML files within Sonar. Typical use cases are validation of files for coding or usability standards.
The following functionality is provided:
- XML validation
- XML schema validatation
- Configurable rules using XPath
- sizing (files, lines of code)
The XML validation is available out-of-the box. The configurable XPath rules requires some knowledge of XPath.
The plugin scans the following files by default: .xml, .xhtml. The file extensions are configurable.
The plugin imports the XML files in Sonar, and performs the validations. The checks are configurable in the Sonar rules repository.
Use Cases
Typical use cases:
- validation of XHTML files
By using the XHTML schemas as provided by the W3C (strict, transitional, and others) quickly validate the markup of a set of XHTML files.
- validation of XML files
Validate a set of XML files for conformance to the provided XML schema.
- Validate rules in XML files
e.g,. validate the maven pom files for conformance to certain coding standards.
Configuration
The following properties of the plugin are configurable:
property |
description |
default value |
|---|---|---|
sonar.xml.sourceDirectory |
folder that will be scanned (relative to project root) |
|
sonar.xml.fileExtensions |
file extensions that will be scanned |
xml, xhtml |
sonar.xml.includeFileFilter |
additional filter for the files to be scanned |
|
sonar.xml.schemas |
namespace of schema to use for validation |
|
The properties may be set in sonar, the maven pom or the maven commmandline.
Usage & Installation
Installation
- Copy the jar into /extensions/plugins/ directory
- Restart Sonar Web server
Analysis
Create a maven pom for your project. Set the following properties:
- sonar.language: xml
- sonar.xml.sourceDirectory: [folder of the xml files]
- sonar.dynamicAnalysis: false
Sample pom file:
Quick analysis
For an existing maven project, you might start an analysis by the following command:
You may set the property sonar.branch to make sure the analysis will be reported in sonar under another project name, e.g. -Dsonar.branch=Xml.
XPath Rules
content to be provided
Schema Validation
Examples: Maven POM Validation
POM Validation per project
Recursively check the pom file in a project and its subprojects.
sourceDirectory is set to . means all files in the project are scanned.
includeFileFilter specifies to scan only pom.xml files (relative path to project root). So only the pom file in the current project will be scanned.
schemas is set to maven namespace so all pom files will be validated with the maven xml schema.
POM Validation (quick)
Quickly validate all pom files in a project and its subprojects.
-N is specified so only the root project will be analyzed by maven.
sourceDirectory is set to . means all files in the project are scanned.
includeFileFilter specifies to scan all pom.xml files. With this filter, also pom files in child modules will be scanned.
schemas is set to maven namespace so all pom files will be validated with the maven xml schema.
Plugin Architecture
The plugin uses the Xerces library for parsing and validation.
http://xerces.apache.org/xerces-j/

