www.sdmetrics.com

Package sdmetrics.model

This package implements the UML model of SDMetrics, and provides the XMI import.

See:
          Description

Interface Summary
XMIReader.ProgressMessageHandler Interface with a callback method for the XMI reader to report progress to.
 

Class Summary
ElementPool Class to store the model elements of the UML design to be analyzed.
MetaModel Class to read the SDMetrics UML metamodel from the metamodel definition file, and store the model for retrieval.
ModelElement Represent a model element in a UML design.
VersionChecker Class to check the version attribute of an SDMetrics project file.
XMIReader Reads the XMI source file, processing it as specified by the XMI transformation file.
XMITransformations Reads the XMI transformation file and stores its contents, and provide access to the XMI transformations defined in the file.
 

Package sdmetrics.model Description

This package implements the UML model of SDMetrics, and provides the XMI import.

Tutorial - how to read an XMI file

The following code snippets show how to parse an XMI file and access the model information.

  1. Required imports:
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.XMLReader;
    import sdmetrics.model.*;
    
  2. Initialize your SAX parser
    XMLReader parser;
    // Code to init your SAX parser, which will look something like this:
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(false);
    SAXParser saxParser = spf.newSAXParser();
    parser = saxParser.getXMLReader();
    parser.setErrorHandler(your_error_handler_object);
    
  3. Read the Meta Model
    MetaModel mm = new MetaModel();
    parser.setContentHandler(mm);
    parser.parse("the_metamodel_definition_file.xml");
    
  4. Read the XMI transformation file
    XMITransformations trans=new XMITransformations(mm);
    parser.setContentHandler(trans);
    parser.parse("the_xmi_transformation_file.xml");
    
  5. Read the XMI file with the UML model
    // create the element pool to store the model elements
    ElementPool pool=new ElementPool(mm);
    // create XMI reader to parse the XMI file and populate the element pool
    XMIReader xmir = new XMIReader(mm,trans,pool);
    // optionally, register a message handler to intercept the XMIReader's progress messages
    xmir.setProgressMessageHandler(new XMIReader.ProgressMessageHandler() {
    	public void reportXMIProgress(String msg) { System.out.println(msg); }
    });
    // and parse the file
    parser.setContentHandler(xmir);
    parser.parse("the_XMI_file.xmi");
    
  6. Optionally, specify element filters to get rid of standard libraries or 3rd party APIs
    String[] filters={"#.java","#.javax","#.org.xml"};
    pool.setFilter(filters,true,true,true);
    
    
  7. Access the UML model
    The following example writes all model elements and their attribute values to the console.
    // iterate over all model element types in the meta model
    for(int type=0; type<mm.getNumberOfTypes(); type++) {
    	System.out.println("Elements of type: "+mm.getTypeName(type));
    	java.util.ArrayList elements=pool.getAcceptedElements(type);
    	// iterate over all model elements of the current type that
    	// also passed the element filter
    	for(int el=0; el<elements.size(); el++) {
    		ModelElement me = (ModelElement) elements.get(el);
    		System.out.println("Element name: "+me.getName()+" ");
    		// write out the value of each attribute of the current model element
    		for (int attr = 0; attr < mm.getAttributeCount(type); attr++) {
    			System.out.print("  Attribute: " + mm.getAttributeName(type, attr));
    			if (mm.isSetAttribute(type, attr))
    				System.out.println(" Set Value: " + me.getSetAttribute(attr));
    			else if (mm.isRefAttribute(type, attr)) {
    				System.out.print(" References: ");
    				ModelElement referenced = me.getRefAttribute(attr);
    				System.out.println(
    					(referenced == null) ? "nothing" : referenced.getFullName());
    			}
    			else
    				System.out.println(" Value: " + me.getXMLAttribute(attr));
    		}
    	}
    }
    


www.sdmetrics.com