SonarQube Tutorial: XML-Plugin – Setup and usage

After the initial setup and analysis of some dummy Java projects I tried to configure my SonarQube server to perform some analysis on BPMN process models. As the serialization is in fact nothing else than XML code it should be feasible to use the SonarQube XML Plugin for this task. This post explains how to install and use the SonarQube XML Plugin. And how it can be used to perform schema validation on BPMN process models.

Installing the Plugin

SonarQube has the ability to administer and install Plugins using the web frontend, so the installation of the XML Plugin is rather simple and straightforward:

  • After starting the server, call the web frontend and login using an Administrator account (default is: username: admin, password: admin):
    sonarqube-login
  • Navigate to “Settings” → “Update Center”
  • Under “Available Plugins” you can find the XML Plugin, click on the title “Xml” and then on “install”:sonarqube-avail-plugins-xml
  • To finish the installation the server has to be restarted.
  • After the restart, you can find “XML Profiles” in the “Quality profiles” tab in the front-end. By default there should be 3 rules activated for the profile “Sonar way”:
    sonarqube-qualityprofiles-xml-default

Adding a XML/BPMN Project

In order to analyze an XML file (or BPMN file in my case) a sonar project must be created and added to the server, e.g., by executing the sonar-runner:

  • To test the plugin I have created a simple project consisting of a single folder “src” which contains a BPMN file “SimpleProcess.bpmn” and the following sonar-project.properties file in the root folder:
# Required metadata
sonar.projectKey=simple-bpmn-xml-project
sonar.projectName=Simple BPMN XML project
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=xml

# Encoding of the source files
sonar.sourceEncoding=UTF-8

Most of the file is identical to the sonar-project.properties files used in the previous post – however, as we like to test XML files the property sonar.language must be set to “xml“.

  • To add the project to your SonarQube server just run the sonar-runner as usual.
  • The project is now added to your dashboards. However, when digging into the details you will not see any results. This is due to the fact, that per default only files with the file extensions *.xml or *.xhtml will be checked by the XML Plugin. To add the *.bpmn extension you have to modify the project settings either in the frontend:
    sonarqube-proj-settings-xml

    • by refining the file extensions to be checked in the “project configuration”→ “settings” → “XML” (Note: You have to be logged in to see and modify the project configuration!)
    • or by adding this line to your sonar-project.properties: sonar.xml.fileExtensions=xml,xhtml,bpmn
  • After rerunning the sonar-runner now also all *.bpmn files should be checked.

Performing a Schema Validation using the XML Plugin

The three default rules activated after the installation of the XML Plugin are of course not enough to validate whether a BPMN process is correct or not. Quoting from the XML Plugin Documentation the plugin is able to perform a “Validation of XML files against a provided XML schema” which is a good starting point for BPMN validation as there exists a normative BPMN20.xsd XML schema definition. To enable schema validation for BPMN (and XML files in general) the following steps have to be performed:

  • The rule for performing Schema validations is deactivated by default. So, the first step is to activate and configure this rule:
    • Open the “Sonar way” XML-Quality Profile
    • Set the “Activation” filter to “Any” and Search for rules
    • Activate the checkbox for the rule “XML Schema Validation”
    • Click on the title “XML Schema Validation” to open the configuration details for the according rule:
      sonarqube-qualityprofiles-xml-schemaVal
      There are two defined parameters to set:

      • First, the file pattern defines which files should be checked. Some examples: myFile.xml, src/folder/folder/*.xml, **/*.xml – or in my case: *.bpmn.
      • Second, the relavant schema for the files selected by the file pattern have to be provided. The description states that when using the default value “autodetect” the “parser will try to load the schema based on the doctype or the namespace declaration in the document.” However, this did not work in my case when using the xsi:schemaLocation attribute to define the location of a schema definition. Though, providing an URL to the schema (as can be seen in the screenshot above), or defining a relative path works (Note: path should be defined relative to the project root).
  • To perform the schema validation again the sonar-runner has to be executed.

Well, okay… this should work fine – and does so in most use cases but it fails when a BPMN file is validated against the normative XSD as a SAXParseException is not really the result I expected:

sonarqube-xml-validation-saxparseexec

The problem in this case is that the BPMN20.xsd references other XML schema files using the <import> element – and this imports are not resolved automatically by the schema parser. To solve this issues all referenced XSDs have to be added to the list of schemas in the XML Schema Validation rule in SonarQube. Note, that the order of the XSD is important here: If A.xsd imports B.xsd which imports C.xsd, then the order in the rule configuration must be: C.xsd B.xsd A.xsd.

So, in the BPMN use case the following schemas are needed: src/xsd/DC.xsd src/xsd/DI.xsd src/xsd/BPMNDI.xsd src/xsd/Semantic.xsd src/xsd/BPMN20.xsd.

Unfortunately this does not solve all issues, as apart from the <import> element the BPMN20.xsd also uses an <include> element to include the file Semantic.xsd. This <include> basically “copies” the definitions of the included schema into the base file (which has to be defined in the same namespace). The <include> also is not resolved automatically – if the included file is added to the list of schemas, this leads to false warnings. Therefore, I “included” the definitions of Semantic.xsd  by copying the contents in the BPMN20.xsd.

The complete example project, including two BPMN files – one correct and one violating the XSD definitions – is available on github.com again.

Conclusion

It is possible to use the SonarQube XML Plugin to perform a XML schema validation. Despite working for standalone XSD definitions, the schema validation has problems if complex schema definitions using imports and includes are used. Yet, it is possible to adjust the configuration so that also these schema validations could be performed using SonarQube. Apart from the ability to perform schema validations the XML Plugin has another feature “Validation of rules in XML files” which will be described in the next blog post.

Advertisements

1 Comment

Filed under BPMN, Development, SonarQube

One response to “SonarQube Tutorial: XML-Plugin – Setup and usage

  1. Pingback: SonarQube – What is it? How to get started? Why do I use it? | Matthias Geiger's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s