package csdl.jblanket.report.element;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
/**
* Handles the content of an XML file as it is encountered by the SAX parser for the report.
* <p>
* NOTE: Method descriptions taken from org.xml.sax.ContentHandler.
*
* @author Joy M. Agustin
* @version $Id: ElementContentHandler.java,v 1.2 2004/11/07 08:53:26 timshadel Exp $
*/
public class ElementContentHandler implements ContentHandler {
/** Locates any content handler event in the XML source document */
private Locator locator;
/** Container for names spaces */
private Map namespaceMappings;
/** Set of all MethodSets */
private MethodSetsElement methodSets;
/** Current MethodElement being processed */
private MethodElement currentMethodElement;
/** Category of methods being processed */
private String methodCategory;
private String timeStamp;
/**
* Constructs a new ElementContentHandler object.
*
* @param methodSets the set of MethodSets being processed.
* @param methodCategory the category of methods being processed.
*/
public ElementContentHandler(MethodSetsElement methodSets, String methodCategory) {
this.namespaceMappings = new HashMap();
this.methodSets = methodSets;
this.methodCategory = methodCategory;
}
/**
* Processes notification of character data -- not implemented.
*
* @param chars the characters making up the data within the element.
* @param start the starting index of characters.
* @param length the number of characters in <code>chars</code>.
* @throws SAXException if any SAX error occurs.
*/
public void characters(char[] chars, int start, int length) throws SAXException {
}
/**
* Processes notification of the end of a document -- not implemented.
*
* @throws SAXException if any SAX error occurs.
*/
public void endDocument() throws SAXException {
}
/**
* Processes notification of the end of an element.
*
* @param namespaceURI namespace URI of current element in context wrt document's complete set of
* namespaces.
* @param localName local, unprefixed name of current element.
* @param qName unmodified, unchanged name of current element.
* @throws SAXException if any SAX error occurs.
*/
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (!localName.equals("Method")) {
return;
}
// get MethodSetElement from methodSetMap
String className = this.currentMethodElement.getClassName();
MethodSetElement methodSet = (MethodSetElement) this.methodSets.get(className);
if (methodSet == null) {
String packageName = className.substring(0, className.lastIndexOf("."));
methodSet = new MethodSetElement(className.substring(0, className.lastIndexOf(".")),
className.substring(className.lastIndexOf(".") + 1));
this.methodSets.put(className, methodSet);
}
if ("tested".equals(this.methodCategory)) {
methodSet.addTestMethod(this.currentMethodElement);
}
else if ("untested".equals(this.methodCategory)) {
methodSet.addUntestMethod(this.currentMethodElement);
}
else if ("untestable".equals(this.methodCategory)) {
methodSet.addUntestableMethod(this.currentMethodElement);
}
else if ("oneline".equals(this.methodCategory)) {
methodSet.addOneLineMethod(this.currentMethodElement);
}
else if ("constructor".equals(this.methodCategory)) {
methodSet.addConstructorMethod(this.currentMethodElement);
}
else if ("excludedIndividual".equals(this.methodCategory)) {
methodSet.addExcludedIndividualMethod(this.currentMethodElement);
}
// get time stamp
methodSet.setTimeStamp(this.timeStamp);
}
/**
* Processes end of the scope of a prefix-URI mapping.
*
* @param prefix the prefix that was being mapped.
* @throws SAXException if any SAX error occurs.
*/
public void endPrefixMapping(String prefix) throws SAXException {
for (Iterator i = this.namespaceMappings.keySet().iterator(); i.hasNext(); ) {
String uri = (String) i.next();
String thisPrefix = (String) this.namespaceMappings.get(uri);
if (prefix.equals(thisPrefix)) {
this.namespaceMappings.remove(uri);
break;
}
}
}
/**
* Processes notification of ignorable whitespace in element content -- not implemented.
*
* @param chars the characters making up the data within the element.
* @param start the starting index of characters.
* @param length the number of characters in <code>chars</code>.
* @throws SAXException if any SAX error occurs.
*/
public void ignorableWhitespace(char[] chars, int start, int length) throws SAXException {
}
/**
* Processes notification of a processing instruction -- not implemented.
*
* @param target the processing instruction target.
* @param data the processing instruction data, or null if none was supplied.
* @throws SAXException if any SAX error occurs.
*/
public void processingInstruction(String target, String data) throws SAXException {
}
/**
* Processes an object for locating the origin of SAX document events.
*
* @param locator an object that can return the location of any SAX document event.
*/
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
/**
* Processes notification of a skipped entity -- not implemented.
*
* @param name the name of the skipped entry.
* @throws SAXException if any SAX error occurs.
*/
public void skippedEntity(String name) throws SAXException {
}
/**
* Processes notification of the beginning of a document - not implememented.
*
* @throws SAXException if any SAX error occurs.
*/
public void startDocument() throws SAXException {
}
/**
* Processes notification of the beginning of an element.
*
* @param namespaceURI namespace URI of current element in context wrt document's complete set of
* namespaces.
* @param localName local, unprefixed name of current element.
* @param qName unmodified, unchanged name of current element.
* @param atts reference to all of the attributes within current element.
* @throws SAXException if any SAX error occurs.
*/
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
throws SAXException {
if (localName.equals("MethodSet")) {
this.timeStamp = atts.getValue("timestamp");
}
else if (localName.equals("Method")) {
// get class name
String className = atts.getValue("class");
String methodName = atts.getValue("method");
MethodElement method = new MethodElement(className, methodName);
//this.methodSetsMap.put(className, method);
this.currentMethodElement = method;
}
else if (localName.equals("Parameter")) {
ParameterElement parameter = new ParameterElement(atts.getValue("type"));
this.currentMethodElement.addParameter(parameter);
}
}
/**
* Processes begin of the scope of a prefix-URI Namespace mapping.
*
* @param prefix the namespace prefix being declared.
* @param uri the namespace URI the prefix is mapped to.
* @throws SAXException if any SAX error occurs.
*/
public void startPrefixMapping(String prefix, String uri) throws SAXException {
this.namespaceMappings.put(uri, prefix);
}
}
|