www.sdmetrics.com

sdmetrics.app
Class XMLPeeker

java.lang.Object
  |
  +--org.xml.sax.helpers.DefaultHandler
        |
        +--sdmetrics.app.XMLPeeker
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public class XMLPeeker
extends org.xml.sax.helpers.DefaultHandler

Peeks into an XML file to determine if it is an SDMetrics Project File, and if so, what type of file. For XMI files, the XMI header information (XMI version, XMI exporter name, XMI exporter version) is also retrieved.

To use this class, call peekIntoFile(java.lang.String) with the name of the file to check. Afterwards, the type of the file can be queried using the queries isXMIFile(), isMetaModelFile() etc.


Constructor Summary
XMLPeeker(sdmetrics.util.MyXMLParser parser)
          Initialize object, letting it use an existing XML parser.
 
Method Summary
 void characters(char[] ch, int start, int length)
          SAX parser handler: retrieve XMI exporter name and version from PCDATA.
 void endDocument()
          SAX parser handler: end of the document.
 void endElement(java.lang.String uri, java.lang.String local, java.lang.String raw)
          SAX parser handler: detect end of XMI documentation.
 java.lang.String getErrorMsg()
          Get an error message describing what error occurred parsing the XML file.
 java.lang.String getTopLevelXMLElementName()
          Get the name of the top-level XML element of the XML file.
 java.lang.String getXMIExporter()
          If the XML file checked was an XMI file, get the XMI exporter that created the file.
 java.lang.String getXMIExporterVersion()
          If the XML file checked was an XMI file, get the version number of the XMI exporter that created the file.
 java.lang.String getXMIVersion()
          If the XML file checked was an XMI file, get the XMI version of the file.
 boolean isMetaModelFile()
          Check if the XML file is a metamodel definition file.
 boolean isMetricDefinitionFile()
          Check if the XML file is a metric definition file.
 boolean isXMIFile()
          Check if the XML file is an XMI file.
 boolean isXMITransformationFile()
          Check if the XML file is an XMI transformation file.
 boolean peekIntoFile(java.lang.String file)
          Parse the head of an XML file to determine its type (XMI, metamodel definition, etc).
 void startElement(java.lang.String uri, java.lang.String local, java.lang.String raw, org.xml.sax.Attributes attrs)
          SAX parser handler: Scan XML elements for XMI header information and collect the relevant information for later retrieval.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLPeeker

public XMLPeeker(sdmetrics.util.MyXMLParser parser)
Initialize object, letting it use an existing XML parser.

Parameters:
parser - XML parser to use. If this is null, the XML peeker will use an XML parser of its own.
Method Detail

peekIntoFile

public boolean peekIntoFile(java.lang.String file)
Parse the head of an XML file to determine its type (XMI, metamodel definition, etc).

Parameters:
file - URL of the file to parse.
Returns:
true if the file type was recognized, false if the file type could not be recognized or an IO error occurred.

getErrorMsg

public java.lang.String getErrorMsg()
Get an error message describing what error occurred parsing the XML file.

Returns:
String describing the error.

isXMIFile

public boolean isXMIFile()
Check if the XML file is an XMI file.

Returns:
true if file is an XMI file, else false.

isMetaModelFile

public boolean isMetaModelFile()
Check if the XML file is a metamodel definition file.

Returns:
true if file is a metamodel definition file, else false.

isXMITransformationFile

public boolean isXMITransformationFile()
Check if the XML file is an XMI transformation file.

Returns:
true if file is an XMI transformation file, else false.

isMetricDefinitionFile

public boolean isMetricDefinitionFile()
Check if the XML file is a metric definition file.

Returns:
true if file is a metric definition file, else false.

getTopLevelXMLElementName

public java.lang.String getTopLevelXMLElementName()
Get the name of the top-level XML element of the XML file.

Returns:
Name of the root XML element.

getXMIVersion

public java.lang.String getXMIVersion()
If the XML file checked was an XMI file, get the XMI version of the file.

Returns:
String with the XMI version of the XMI file.

getXMIExporter

public java.lang.String getXMIExporter()
If the XML file checked was an XMI file, get the XMI exporter that created the file.

Returns:
String with the XMI exporter name for the XMI file.

getXMIExporterVersion

public java.lang.String getXMIExporterVersion()
If the XML file checked was an XMI file, get the version number of the XMI exporter that created the file.

Returns:
String with the XMI exporter version number for the XMI file.

startElement

public void startElement(java.lang.String uri,
                         java.lang.String local,
                         java.lang.String raw,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
SAX parser handler: Scan XML elements for XMI header information and collect the relevant information for later retrieval.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String local,
                       java.lang.String raw)
SAX parser handler: detect end of XMI documentation.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler

characters

public void characters(char[] ch,
                       int start,
                       int length)
SAX parser handler: retrieve XMI exporter name and version from PCDATA.

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
SAX parser handler: end of the document. Throws SAXException signalling "correct" abortion.

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXException

www.sdmetrics.com