001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import org.apache.log4j.*;
036: import org.xml.sax.*;
037: import org.xml.sax.helpers.*;
038:
039: public class MetricsConfigurationHandler extends DefaultHandler {
040: private static final int PROJECT = 0;
041: private static final int GROUP = 1;
042: private static final int CLASS = 2;
043: private static final int METHOD = 3;
044:
045: private MetricsConfiguration configuration;
046: private int section;
047: private MeasurementDescriptor descriptor;
048: private String name;
049: private String pattern;
050:
051: private StringBuffer currentName = new StringBuffer();
052:
053: public MetricsConfigurationHandler() {
054: this (new MetricsConfiguration());
055: }
056:
057: public MetricsConfigurationHandler(
058: MetricsConfiguration configuration) {
059: this .configuration = configuration;
060: }
061:
062: public MetricsConfiguration getMetricsConfiguration() {
063: return configuration;
064: }
065:
066: public void startElement(String namespaceURI, String localName,
067: String qName, Attributes atts) throws SAXException {
068: Logger.getLogger(getClass()).debug(
069: "startElement qName = " + qName);
070:
071: for (int i = 0; i < atts.getLength(); i++) {
072: Logger.getLogger(getClass())
073: .debug(
074: " " + atts.getQName(i) + ": "
075: + atts.getValue(i));
076: }
077:
078: currentName.delete(0, currentName.length());
079:
080: if (qName.equals("project-measurements")) {
081: section = PROJECT;
082: } else if (qName.equals("group-measurements")) {
083: section = GROUP;
084: } else if (qName.equals("class-measurements")) {
085: section = CLASS;
086: } else if (qName.equals("method-measurements")) {
087: section = METHOD;
088: } else if (qName.equals("measurement")) {
089: descriptor = new MeasurementDescriptor();
090:
091: if (atts.getValue("visible") != null) {
092: descriptor.setVisible("true".equalsIgnoreCase(atts
093: .getValue("visible"))
094: || "yes".equalsIgnoreCase(atts
095: .getValue("visible"))
096: || "on".equalsIgnoreCase(atts
097: .getValue("visible")));
098: }
099:
100: if (atts.getValue("cached") != null) {
101: descriptor.setCached("true".equalsIgnoreCase(atts
102: .getValue("cached"))
103: || "yes".equalsIgnoreCase(atts
104: .getValue("cached"))
105: || "on".equalsIgnoreCase(atts
106: .getValue("cached")));
107: }
108:
109: switch (section) {
110: case PROJECT:
111: configuration.addProjectMeasurement(descriptor);
112: break;
113: case GROUP:
114: configuration.addGroupMeasurement(descriptor);
115: break;
116: case CLASS:
117: configuration.addClassMeasurement(descriptor);
118: break;
119: case METHOD:
120: configuration.addMethodMeasurement(descriptor);
121: break;
122: }
123: }
124: }
125:
126: public void endElement(String namespaceURI, String localName,
127: String qName) throws SAXException {
128: if (qName.equals("short-name")) {
129: descriptor.setShortName(currentName.toString().trim());
130: } else if (qName.equals("long-name")) {
131: descriptor.setLongName(currentName.toString().trim());
132: } else if (qName.equals("class")) {
133: try {
134: descriptor.getClassForByName(currentName.toString()
135: .trim());
136: } catch (ClassNotFoundException ex) {
137: throw new SAXException("Class not found: "
138: + currentName.toString().trim());
139: }
140: } else if (qName.equals("init")) {
141: descriptor.setInitText(currentName.toString().trim());
142: } else if (qName.equals("lower-threshold")) {
143: descriptor.setLowerThreshold(currentName.toString().trim());
144: } else if (qName.equals("upper-threshold")) {
145: descriptor.setUpperThreshold(currentName.toString().trim());
146: } else if (qName.equals("name")) {
147: name = currentName.toString().trim();
148: } else if (qName.equals("pattern")) {
149: pattern = currentName.toString().trim();
150: } else if (qName.equals("group-definition")) {
151: configuration.addGroupDefinition(name, pattern);
152: }
153:
154: Logger.getLogger(getClass()).debug(
155: "endElement qName = " + qName + " (\""
156: + currentName.toString().trim() + "\")");
157: }
158:
159: public void characters(char[] ch, int start, int length)
160: throws SAXException {
161: currentName.append(ch, start, length);
162: Logger.getLogger(getClass())
163: .debug(
164: "characters: \""
165: + new String(ch, start, length) + "\"");
166: }
167: }
|