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 (analysisTransform.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="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:uml="http://schema.omg.org/spec/UML/2.1.2" xmi:version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns="http://www.w3.org/2000/svg">
<xsl:output media-type="image/svg, text/xml" method="xml" indent="yes"/>
<xsl:template match="//*">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
. . .
<!-- 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']">
. . .
</xsl:for-each>
. . .
</svg>
</xsl:template>
</xsl:stylesheet>
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 =
"WEB-INF/wsdl/localhost_8080/AnalysisService/AnalysisWSService.wsdl")
private AnalysisWSService service;
private byte[] xmiDocument;
. . .
public String button1_action()
{
SessionBean1 sb1 = (SessionBean1) getBean("SessionBean1");
xmiDocument = fileUpload1.getUploadedFile().getBytes();
sb1.setXmiContent(xmiDocument);
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);
sb1.setContent(result);
return "showSVG";
}
}
Listing Five: Excerpt of Java code associated with the main page (Page1.jsp).
Conclusion
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.


