Search This Blog

Friday, December 11, 2015

XML Basics

XML is the abbreviation for EXtensible Markup Language. It's used to store and transport data. It's both human-readable and meachine -readable format.

There is no pre-defined tags (like
in HTML) in XML. Here, the developer must define the tags and document structure.


XML documents are extensible meaning adding/removing tags to the XML document will not impact the application that process the document.

XML Tree Structure: XML docs are formed as ELEMENT TREES. They have root element and child elements.

<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>


XML doc components:
1. XML Prolog (Optional) . It contains two parts.
             1. XML Declaration(Example: <?xml version="1.0" encoding="UTF-8"?>)
             2.DTD Declaration
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. root element (Mandatory)  (Document)
3. child elements
4. xml data

XML Namespaces: 
If you use same element name twice with different child elements, there will be conflict with element name. XML Namespace is used to avoid the conflict. use a prefix and define xmlns attribute with unique URL(any dummy URL) value.

Example 1:
<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.venkat.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>

Example 2:
The namespace can also be declared at root element level:

<root
xmlns:h=
"http://www.w3.org/TR/html4/"
xmlns:f=
"http://www.w3schools.com/furniture">

<h:table>....

..............

Example 3(Default namesapce):

<table xmlns="http://www.w3.org/TR/html4/">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

<table xmlns="http://venkat.com/furniture">
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>


XSD:
 It's the equivalent of DTD. It's the .xsd file that is defined for xml file. In xml file, xsd is referenced as shown below:

<?xml version="1.0"?>

<note xmlns="http://www.w3schools.com/venkat"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.w3schools.com/venkat note.xsd">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Explanation:
xmlns -  default namespace
schemaLocation - (defalut namespace) (path of .xsd file)

Valid vs Well-formed documents:
If the XML document follows the syntax, it's called well-formed document.
If the XMl document is validated against DTD or XML Schema(xsd), then it;s called both 'well-formed' and 'valid' document.

XPath:  It is a language for finding information in an XML doc.

XML Viewer: You can use any browser to view XML file.

How do you create xml from xsd using JAXB:
Sometimes, you will be getting only .xsd file and test data. You need to generate the xml file from the xsd file and test data.

1. Generate java classes from the xsd file using xjc utility
2. write a java classto set data to the generated java classes and to marshall the java objects to xml file using JAXB classes.

Static Code Analysis using SonarQube

SonarQube is a open source Quality Management Platform. Download SonarQube from http://www.sonarqube.org/downloads/ (Downloaded Zip file contains Web Server that comes with Embedded Database).

The best method to run the SonarQube platform is to set up the VM (preferabley Windows Server). Then install the SonarQube in the VM and run it there. Ensure that Firewall settings of the VM allows inbound connections.

Install SonarQube:
Unzip the downloaded .zip file and place them in your drive. If you want to configure different port, please edit sonar.properties file under /conf folder.

Run SonarQube:
Navigate to /SonarQube-5.1/bin/windows-x86-64/ folder and run startsonar.bat file. (It runs the WebServer at port number 9000).
Navigate to URL: http://localhost:9000

Analyze the code:
Install SonarQube Scanner:
SonarQube comes with separate tool for analyzing the source code. Download SonarQube Scanner (command line tool) from http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+SonarQube+Scanner

Unzip the zip file and place them in your drive.

Configure SonarQube Scanner:

Open /conf/sonar-runner.properties
Edit
#----- Default SonarQube server
sonar.host.url=http://vm-name:9000

Add the /bin directory to your path variable.

Open command prompt
type >sonar-runner -version

Run SonarQube Scanner:
Create a configuration file (sonar-project.properties) in the root directory of the java project. Specify project details, source code location (src folder path) and module names if the application has multiple modules (like web, service, common, etc).

Open command prompt. Navigate to the root directory of the java project. run >sonar-runner
This will run sonar analysis and push the analysis data to the sonarqube server's embedded database.

Navigate to the SonarQube URL and see the results in dashboard.

Note: If you want to use your own database such as oracle, MySQL, etc, please refer SonarQube Documentation for configuration steps.
If you want to run sonarqube analysis as part of Continuous Integration using Jenkins server, you can configure it in Jenkins server job.

Unit Tests Suceess report in SonarQube: SonarQube Scanner(Runner) can not execute the Unit test cases (Junit java classes), it can only analyze the testing reports. You have to run the Junit Test cases with maven or your preferred build tool (Note that running JUnit in Eclipse will not generate report) to generate the report. Then provide the report path in the sonar-project.properties. Then run the sonar analysis using SonarQube scanner. SonarQube runner uses the Java Plugin installed in SonarQube server to analyze the Junit report.

Sample configuration in Sonar-project.properties;

sonar.tests=src/test/java
#Tells SonarQube where the unit tests execution reports(Maven generated report) are 
sonar.junit.reportsPath=target/surefire-reports

Unit Test Code Coverage report in SonarQube: To collect the Unit Test Coverage, you need to launch JaCoCo tool along with your Maven build that runs Junit testcases.  Use this command: mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true
Once you run the maven build with the above command, jacoco.exec file will be generated at folder /target/jacoco.exec

Now run the Sonar Runner with the following configuration in sonar-project.properties:

#Location of class files are required for java plugin(in SonarQube Server) to generate report out of jacoco.exec file
sonar.binaries=target/classes
#Tells SonarQube where the jacoco coverage data file(Maven generated file) are 
sonar.jacoco.reportsPath=target/jacoco.exec

Sample report at Sonar Dashboard: