001: package csdl.jblanket.report.element;
002:
003: import java.util.HashMap;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import org.xml.sax.Attributes;
008: import org.xml.sax.ContentHandler;
009: import org.xml.sax.Locator;
010: import org.xml.sax.SAXException;
011:
012: /**
013: * Handles the content of an XML file as it is encountered by the SAX parser for the report.
014: * <p>
015: * NOTE: Method descriptions taken from org.xml.sax.ContentHandler.
016: *
017: * @author Joy M. Agustin
018: * @version $Id: ElementContentHandler.java,v 1.2 2004/11/07 08:53:26 timshadel Exp $
019: */
020: public class ElementContentHandler implements ContentHandler {
021:
022: /** Locates any content handler event in the XML source document */
023: private Locator locator;
024: /** Container for names spaces */
025: private Map namespaceMappings;
026:
027: /** Set of all MethodSets */
028: private MethodSetsElement methodSets;
029: /** Current MethodElement being processed */
030: private MethodElement currentMethodElement;
031:
032: /** Category of methods being processed */
033: private String methodCategory;
034:
035: private String timeStamp;
036:
037: /**
038: * Constructs a new ElementContentHandler object.
039: *
040: * @param methodSets the set of MethodSets being processed.
041: * @param methodCategory the category of methods being processed.
042: */
043: public ElementContentHandler(MethodSetsElement methodSets,
044: String methodCategory) {
045:
046: this .namespaceMappings = new HashMap();
047:
048: this .methodSets = methodSets;
049:
050: this .methodCategory = methodCategory;
051: }
052:
053: /**
054: * Processes notification of character data -- not implemented.
055: *
056: * @param chars the characters making up the data within the element.
057: * @param start the starting index of characters.
058: * @param length the number of characters in <code>chars</code>.
059: * @throws SAXException if any SAX error occurs.
060: */
061: public void characters(char[] chars, int start, int length)
062: throws SAXException {
063: }
064:
065: /**
066: * Processes notification of the end of a document -- not implemented.
067: *
068: * @throws SAXException if any SAX error occurs.
069: */
070: public void endDocument() throws SAXException {
071: }
072:
073: /**
074: * Processes notification of the end of an element.
075: *
076: * @param namespaceURI namespace URI of current element in context wrt document's complete set of
077: * namespaces.
078: * @param localName local, unprefixed name of current element.
079: * @param qName unmodified, unchanged name of current element.
080: * @throws SAXException if any SAX error occurs.
081: */
082: public void endElement(String namespaceURI, String localName,
083: String qName) throws SAXException {
084:
085: if (!localName.equals("Method")) {
086: return;
087: }
088:
089: // get MethodSetElement from methodSetMap
090: String className = this .currentMethodElement.getClassName();
091: MethodSetElement methodSet = (MethodSetElement) this .methodSets
092: .get(className);
093: if (methodSet == null) {
094: String packageName = className.substring(0, className
095: .lastIndexOf("."));
096: methodSet = new MethodSetElement(className.substring(0,
097: className.lastIndexOf(".")), className
098: .substring(className.lastIndexOf(".") + 1));
099: this .methodSets.put(className, methodSet);
100: }
101:
102: if ("tested".equals(this .methodCategory)) {
103: methodSet.addTestMethod(this .currentMethodElement);
104: } else if ("untested".equals(this .methodCategory)) {
105: methodSet.addUntestMethod(this .currentMethodElement);
106: } else if ("untestable".equals(this .methodCategory)) {
107: methodSet.addUntestableMethod(this .currentMethodElement);
108: } else if ("oneline".equals(this .methodCategory)) {
109: methodSet.addOneLineMethod(this .currentMethodElement);
110: } else if ("constructor".equals(this .methodCategory)) {
111: methodSet.addConstructorMethod(this .currentMethodElement);
112: } else if ("excludedIndividual".equals(this .methodCategory)) {
113: methodSet
114: .addExcludedIndividualMethod(this .currentMethodElement);
115: }
116:
117: // get time stamp
118: methodSet.setTimeStamp(this .timeStamp);
119: }
120:
121: /**
122: * Processes end of the scope of a prefix-URI mapping.
123: *
124: * @param prefix the prefix that was being mapped.
125: * @throws SAXException if any SAX error occurs.
126: */
127: public void endPrefixMapping(String prefix) throws SAXException {
128:
129: for (Iterator i = this .namespaceMappings.keySet().iterator(); i
130: .hasNext();) {
131: String uri = (String) i.next();
132: String this Prefix = (String) this .namespaceMappings
133: .get(uri);
134: if (prefix.equals(this Prefix)) {
135: this .namespaceMappings.remove(uri);
136: break;
137: }
138: }
139: }
140:
141: /**
142: * Processes notification of ignorable whitespace in element content -- not implemented.
143: *
144: * @param chars the characters making up the data within the element.
145: * @param start the starting index of characters.
146: * @param length the number of characters in <code>chars</code>.
147:
148: * @throws SAXException if any SAX error occurs.
149: */
150: public void ignorableWhitespace(char[] chars, int start, int length)
151: throws SAXException {
152: }
153:
154: /**
155: * Processes notification of a processing instruction -- not implemented.
156: *
157: * @param target the processing instruction target.
158: * @param data the processing instruction data, or null if none was supplied.
159: * @throws SAXException if any SAX error occurs.
160: */
161: public void processingInstruction(String target, String data)
162: throws SAXException {
163: }
164:
165: /**
166: * Processes an object for locating the origin of SAX document events.
167: *
168: * @param locator an object that can return the location of any SAX document event.
169: */
170: public void setDocumentLocator(Locator locator) {
171: this .locator = locator;
172: }
173:
174: /**
175: * Processes notification of a skipped entity -- not implemented.
176: *
177: * @param name the name of the skipped entry.
178: * @throws SAXException if any SAX error occurs.
179: */
180: public void skippedEntity(String name) throws SAXException {
181: }
182:
183: /**
184: * Processes notification of the beginning of a document - not implememented.
185: *
186: * @throws SAXException if any SAX error occurs.
187: */
188: public void startDocument() throws SAXException {
189: }
190:
191: /**
192: * Processes notification of the beginning of an element.
193: *
194: * @param namespaceURI namespace URI of current element in context wrt document's complete set of
195: * namespaces.
196: * @param localName local, unprefixed name of current element.
197: * @param qName unmodified, unchanged name of current element.
198: * @param atts reference to all of the attributes within current element.
199: * @throws SAXException if any SAX error occurs.
200: */
201: public void startElement(String namespaceURI, String localName,
202: String qName, Attributes atts) throws SAXException {
203:
204: if (localName.equals("MethodSet")) {
205:
206: this .timeStamp = atts.getValue("timestamp");
207: } else if (localName.equals("Method")) {
208:
209: // get class name
210: String className = atts.getValue("class");
211: String methodName = atts.getValue("method");
212: MethodElement method = new MethodElement(className,
213: methodName);
214:
215: //this.methodSetsMap.put(className, method);
216: this .currentMethodElement = method;
217: } else if (localName.equals("Parameter")) {
218:
219: ParameterElement parameter = new ParameterElement(atts
220: .getValue("type"));
221: this .currentMethodElement.addParameter(parameter);
222: }
223:
224: }
225:
226: /**
227: * Processes begin of the scope of a prefix-URI Namespace mapping.
228: *
229: * @param prefix the namespace prefix being declared.
230: * @param uri the namespace URI the prefix is mapped to.
231: * @throws SAXException if any SAX error occurs.
232: */
233: public void startPrefixMapping(String prefix, String uri)
234: throws SAXException {
235: this.namespaceMappings.put(uri, prefix);
236: }
237: }
|