Channels ▼

Open Source

A Web Service for Calculating the Metrics of UML Class Diagrams

XSL Transformations

As seen in Figure 2, the Web service uses two XSL archives (analysisTransform.xsl and XMItoSVG.xsl) stored in the Web server. XSLT is a W3C language for transforming XML documents into other XML documents. The transformation is achieved by associating patterns with templates. A pattern is matched against elements in the source document. In Listing Four, an excerpt of XMItoSVG.xsl is shown. This transformation is used by the Web method GeneratesSVG, running the transformation of the received XMI file with a class diagram into SVG (Scalable Vector Graphics) format. The other XSL transformation (analysis­Transform.xsl) has been created for formatting the results of the Web method ProcessXMI as structured HTML output with the values of the calculated metrics.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xsl:stylesheet xmlns:xsl="" version="2.0"
                xmlns:uml="" xmi:version="2.1"

 <xsl:output media-type="image/svg, text/xml" method="xml" indent="yes"/>

 <xsl:template match="//*">

  <svg width="100%" height="100%" version="1.1" xmlns="">
   . . .

   <!-- this for-each is for drawing a SVG rectangle corresponding to a class in XMI file -->
   <xsl:for-each select="//packagedElement[@xmi:type='uml:Class']">
    . . .
   . . .

Listing Four: Excerpt of code of transformation XMItoSVG.xsl.

Testing the Web service

A simple Web application in Java with three JSPs is enough to illustrate the use of the service (see Figure 4).

Figure 4: Java Server pages of the Web application for testing Web service.

The main page is Page1.jsp, where the user can upload an XMI file that includes a UML class diagram (for example, in Figure 5, the user has selected a file Example.xmi, that contents the class diagram represented in Figure 1, with classes Person, Driver, and Car.

Figure 5: Main page (Page1.jsp) of the Web application for testing Web service.

If the user click the "View Metrics" button, the page MetricsResults.jsp is loaded. This JSP invokes the Web method ProcessXMI, included in the Web service, sending the Example.xmi file, and getting the table with the metrics shown in Figure 6 as a result.

Figure 6: Web page with metrics results (MetricsResults.jsp).

With both Page1.jsp and MetricsResults.jsp, the user can click on a button to view the class diagram directly in the Web browser. This is done via the Web method generateSVG. Listing Five shows the Java source code associated to the two buttons of Page1.jsp. The first button iis for viewing the metric, the second button is for viewing the SVG representation of the class.

public class Page1 extends AbstractPageBean {

@WebServiceRef(wsdlLocation = 
  private AnalysisWSService service;
  private byte[] xmiDocument;
  . . .
  public String button1_action() 
    SessionBean1 sb1 = (SessionBean1) getBean("SessionBean1");
    xmiDocument = fileUpload1.getUploadedFile().getBytes();
    return "showMetrics";

  public String button2_action() 
    SessionBean1 sb1 = (SessionBean1) getBean("SessionBean1");
    webservices.AnalysisWS port = service.getAnalysisWSPort();
    xmiDocument = fileUpload1.getUploadedFile().getBytes();
    String result = port.generateSVG(xmiDocument);
    return "showSVG";

Listing Five: Excerpt of Java code associated with the main page (Page1.jsp).


This project is open source. A running instance of the Web service is available as well. Most current CASE tools enable importing/exporting UML models in XMI format, so this service is a good complement to similar tools.

José and Luis are associate professors at University of Alcalá in Spain. Luis is also the vice-president of ATI), the main Spanish IT professionals association.

This article also appears in the Dr. Dobb's June 2012 Special Supplement.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.