Pages

Sunday, September 22, 2013

XQStyle - An XQuery analysis tool

What is XQuery ?

- XQuery is a query and functional programming language.

- XQuery is designed to query and transform collections of structured and unstructured data,      usually in the form of XML (not just XML files, but anything that can   appear as XML).

- XQuery is a W3C Recommendation.

- XQuery is compatible with several W3C standards, such as XML, Namespaces, XSLT, XPath, and    XML Schema.

- XQuery is built on XPath expressions.

*****************************************************************************

Why Xquery ?

- Now a days XML is most widely used data transportation and data storage medium, XQuery is    an official W3C recommendation to deal with the XML data.

- XML use is widespread across modern information systems in all industry, government, and    academic sectors.

- XQuery is used to extract, create and manipulate the XML data.

- After the evolution of MarkLogic, exists, BaseX, XMLdb etc. XML databases XQuery is becoming the primary language to deal with XML content.
**********************************************************************

Why an analysis tool is needed? 

- A good quality code is a code which is well documented, easy to understand, well structured and well optimized.
- Analyzing code without executing it. Generally used to find bugs or ensure conformance to coding guidelines.

****************************************************************************************

1-  XQStyle Overview

Problem:

XQuery is designed to query XML data - not just XML files, but anything that can appear as XML, including databases. Xquery is a recommendation by W3C.
W3C recommends coding conventions and coding standards for Xquery that should be strictly followed to deliver a good quality code.

At present we don’t have any specific tool/plugin for xquery code analysis which performs automated code analysis or code review.
We have to do it manually and that also requires extra hours and extra effort to ensure and deliver a quality code.

Approach Finalization:

The idea for XQStyle tool came from an existing tool PMD (A java code analysis tool).
After researching and analysing a lot on the working and features of PMD, I decided to develop  the XQStyle tool in Java, as java has very good capability for file handling, IO operations, string handling and xml document parsing.

Solution:
"XQStyle - An XQuery code analysis tool"

Features:

v  XQStyle is very easy to use, it can be easily configured on developer machine.
v  XQStyle supports analysis of XQuery versions such as 0.9, 0.9-ml, 1.0 and 1.0-ml .
v  XQStyle can be easily integrated with build tools such as HUDSON/JENKINS (CI Tools).

v  XQStyle takes the customized rule set configuration in the form of XML same as PMD.

v  If developer will not provide the custom rule set configuration, XQStyle will analyse the code using its built-in rulesets.

v  XQStyle has basic all, design & code optimization rules recommended by W3C for XQuery code analysis. 

v  XQStyle rules sets can be easily customized by developers as per their specific needs.

v  Developers can categorise rules and set the prioritise of the rules as per their specific needs.

v  Developers can also pass external parameters specific to configured rules which requires external inputs. For eaxmple, Code Size or Function Length.

v  Developers can easily enable or disable rules without removing the rule from the rule set xml.

v  XQStyle is capable of analysing the code for a single xquery file as well as on directory input by recursively traversing the file system for the given directory.

v  It generates code analysis reports in two formats XML & HTML based on the configured renderer. XMLRenderer generates the report in xml format and HTMLRenderer generates the report in html format.

v  Violations in the report can be easily filtered based on “High”, “Normal” & “Low” priorities.

v  XQStyle can be easily integrated with XQuery code repository using Ant scripts/Batch files.


2       Using XQStyle 

2.1         System Requirements

v  Should have Jre 5 or later version installed. 
  Dependency library: commons-lang3-3.1.jar or later (Download apache commons library from https://mvnrepository.com/artifact/org.apache.commons/commons-lang3)
v  The path where the tool will be installed must have enough space and must have admin privilege.
v  Apache ant 1.8 (Optional, required only when ant script is used to generate reports).

2.2       Using on developer machine via windows bat file.

         To use this tool on developer machine you need to configure it using following steps.

v  Extract the XQStyle.zip file in C:\XQStyle or C:\XQStyle drive which contains xqstyle.jar,its dependencies and a bat file ("runxqstyle.bat") .
v  Click on the bat file “runxqstyle.bat” to run the tool.
v  Provide the xquery file name with full qualified path or directory path.
For e.g. : “C:\Users\Abhinav\Desktop\XqueryRepository

v  Provide the custom xqstyle ruleset xml file with full qualified path [its Optional, Inbuilt rule sets will be used if not provided].
For e.g. : “C:\Users\Abhinav\Desktop\xqstyleruleset.xml

v  Provide the output directory will full full qualified path, where reports will be saved.
For e.g. : “C:\Users\Abhinav\Desktop\report



v  Go to output directory of reports, you will be able to see reports generated by XQStyle.
v  Click on the “index.html”, it contains report links for every xquery file and also package name where xquery file kept on.



v  Click on each link to view the report specific to each XQuery file. You can filter violations based on High,Low and Normal priorities using check boxes.



v  Unmark “High Priority” checkbox if you want to filter violations on Low and Normal Priority.




v  Unmark “Normal Priority” checkbox if you want to filter violations on Low and High Priority.



v  Unmark “Low Priority” checkbox if you want to filter violations on Normal and High Priority.



2.3         Using on continuous integration tool (Jenkins/Hudson)

        v  Create a job on HUDSON/JENKINS specific to your project if not already created.

        v  Create a ant build script xml and add the below given script in the "xqstyle-ant-script.xml".  
   

<project name="XQStyle" basedir="." default="about">
                               
  <property name="lib.dir" value="${basedir}/xqstyle-lib"/>
  <property name="app.dir" value="${basedir}/src"/>
 
   <target name="about">
                 <echo>XQStyle xquery code analyser script.</echo>
   </target>
                               
    <!--
       =============================================================
                    XQStyle CONFIGURATION START
       =============================================================
    -->
       <target name="xqstyle-init">
              <echo>******Cleaning the existing xqstyle-reports******</echo>
              <delete dir="${basedir}\xqstyle-reports" />
              <echo>******Creating the required directories for xqstyle-reports******</echo>
              <mkdir dir="${basedir}\xqstyle-reports" />
              <touch file="${basedir}\xqstyle-reports\index.html"/>
 <!-- Pass the source directory or file as a parameter. e.g. -Dsource=c:\test\xquery -->
      <property name="source" value=""/>
  <!-- Pass the source directory or file as a parameter. e.g. -Dsource=c:\test\xqstyleruleset.xml. 
       If this parameter is empty then built-in ruleset will be used -->

              <property name="ruleset" value=""/>
       </target>
            
       <target name="run-xqstyle" depends="xqstyle-init">
              <echo>******Processing xquery analysis******</echo>
              <java classname="com.xqstyle.renderers.XQStyleHtmlRenderer">
                     <arg value="${source}"/>
                     <arg value="${ruleset}"/>
                     <arg value="${basedir}\xqstyle-reports"/>
                     <classpath>
                           <pathelement location="${lib.dir}\xqstyle.jar"/>
                           <pathelement location="${lib.dir}\commons-lang3-3.1.jar"/>
                     </classpath>
              </java>
       </target>
   <!--
      =============================================================
                XQStyle CONFIGURATION END
      =============================================================
   -->
</project>


v  Install “HTML Publisher Plugin” in Hudson/Jenkins via manage plugins.




v  Go to “Configure” job for your project.
v  Under “Build” section configure targets as “run-xqstyle” and Build file as “./xqstyle-ant-script.xml”.



v  Go to “Post Build Actions/Taks” section.
v  Select “Publish HTML reports” chekbox and click on “add” button.
v  Provide the location or path of reports directory created by ant acript.

         



v Save the configuration changes.


v  Click on “Build Now” to build the project.
Console output:



v  Go to the workspace i.e. “XQStyle-Poc”.
    
                           


v  Click on HTML Reports to view the generated report by XQStyle.

v  Reports can be downloaded in zip format by clicking on “ZIP” link highlighted in red in the index report.
v  User can directly navigate back to work-space by clicking link highlighted in red in the index report.
v  Reports can be viewed same as described in first section 2.2.

2.4    GUI

o   Open 'XQStyle.exe'


o   Select the Input directory
o   Select the target directory
o   Select ruleset xml (Optional)





o   Click on “Run XQStyle” to start analysis



o   See the log file.


o   See the report generated in “Output Directory” provided.

o   Open the “index.html” file.


o   Reports can be viewed same as described in first section 2.2


Developed By: Abhinav Kumar Mishra

Copy of this tool is available here under Apache 2.0 license.

8 comments:

  1. Hi Abhinav,

    Great Job,

    Is there any setup from where we can use this tool?

    ReplyDelete
    Replies
    1. Thanks Vishnu :)

      Yeah I have the setup, but i have not uploaded it on the internet.

      Delete
    2. You can find the executables here: https://github.com/abhinavmishra14/XQSTyle

      Delete
  2. Is it possible to share the setup ?

    ReplyDelete
    Replies
    1. You can find the executables here: https://github.com/abhinavmishra14/XQSTyle

      Delete
  3. Thanks Abhinav,
    kindly help with the below.

    How about scanning multiple directories at
    one shot, C:\XQCode\*\*.* and C:\XQCode\*, but it didnt take that.

    Got the below error when input path is given as C:\XQCode\*\*.*
    [XQStyleHtmlRenderer:] IOException in main() : C:\Reviewer\XQStyle\report\*.*_xqstylereport_20190911183840.html (The filename, directory name, or volu
    me label syntax is incorrect)
    java.io.FileNotFoundException: C:\Reviewer\XQStyle\report\*.*_xqstylereport_20190911183840.html (The filename, directory name, or volume label syntax
    is incorrect)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(Unknown Source)
    at java.io.FileOutputStream.(Unknown Source)
    at java.io.FileOutputStream.(Unknown Source)
    at java.io.FileWriter.(Unknown Source)
    at com.xqstyle.processor.impl.a.a(Unknown Source)
    at com.xqstyle.processor.impl.a.a(Unknown Source)
    at com.xqstyle.report.renderers.XQStyleHtmlRenderer.main(Unknown Source)

    ReplyDelete
  4. It is working now for me. Thanks. CAn we integrate this with Sonar and publish the report to sonar server.

    ReplyDelete
  5. Hi Abhinav, is it posible to share the java source. please let me know.

    ReplyDelete