KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > MetricsConfigurationHandler


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import java.io.*;
36 import java.net.*;
37 import java.util.*;
38
39 import org.apache.log4j.*;
40
41 import org.xml.sax.*;
42 import org.xml.sax.helpers.*;
43
44 public class MetricsConfigurationHandler extends DefaultHandler {
45     private static final int PROJECT = 0;
46     private static final int GROUP = 1;
47     private static final int CLASS = 2;
48     private static final int METHOD = 3;
49     
50     private MetricsConfiguration configuration;
51     private int section;
52     private MeasurementDescriptor descriptor;
53     private String JavaDoc name;
54     private String JavaDoc pattern;
55     
56     private StringBuffer JavaDoc currentName = new StringBuffer JavaDoc();
57
58     public MetricsConfigurationHandler() {
59         this(new MetricsConfiguration());
60     }
61
62     public MetricsConfigurationHandler(MetricsConfiguration configuration) {
63         this.configuration = configuration;
64     }
65
66     public MetricsConfiguration getMetricsConfiguration() {
67         return configuration;
68     }
69     
70     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
71         Logger.getLogger(getClass()).debug("startElement qName = " + qName);
72
73         for (int i=0; i<atts.getLength(); i++) {
74             Logger.getLogger(getClass()).debug(" " + atts.getQName(i) + ": " + atts.getValue(i));
75         }
76
77         currentName.delete(0, currentName.length());
78
79         if (qName.equals("project-measurements")) {
80             section = PROJECT;
81         } else if (qName.equals("group-measurements")) {
82             section = GROUP;
83         } else if (qName.equals("class-measurements")) {
84             section = CLASS;
85         } else if (qName.equals("method-measurements")) {
86             section = METHOD;
87         } else if (qName.equals("measurement")) {
88             descriptor = new MeasurementDescriptor();
89
90             if (atts.getValue("visible") != null) {
91                 descriptor.setVisible("true".equalsIgnoreCase(atts.getValue("visible")) ||
92                                    "yes".equalsIgnoreCase(atts.getValue("visible")) ||
93                                    "on".equalsIgnoreCase(atts.getValue("visible")));
94             }
95
96             if (atts.getValue("cached") != null) {
97                 descriptor.setCached("true".equalsIgnoreCase(atts.getValue("cached")) ||
98                                    "yes".equalsIgnoreCase(atts.getValue("cached")) ||
99                                    "on".equalsIgnoreCase(atts.getValue("cached")));
100             }
101             
102             switch (section) {
103                 case PROJECT:
104                     configuration.addProjectMeasurement(descriptor);
105                     break;
106                 case GROUP:
107                     configuration.addGroupMeasurement(descriptor);
108                     break;
109                 case CLASS:
110                     configuration.addClassMeasurement(descriptor);
111                     break;
112                 case METHOD:
113                     configuration.addMethodMeasurement(descriptor);
114                     break;
115             }
116         }
117     }
118
119     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
120         if (qName.equals("short-name")) {
121             descriptor.setShortName(currentName.toString().trim());
122         } else if (qName.equals("long-name")) {
123             descriptor.setLongName(currentName.toString().trim());
124         } else if (qName.equals("class")) {
125             try {
126                 descriptor.getClassForByName(currentName.toString().trim());
127             } catch (ClassNotFoundException JavaDoc ex) {
128                 throw new SAXException("Class not found: " + currentName.toString().trim());
129             }
130         } else if (qName.equals("init")) {
131             descriptor.setInitText(currentName.toString().trim());
132         } else if (qName.equals("lower-threshold")) {
133             descriptor.setLowerThreshold(currentName.toString().trim());
134         } else if (qName.equals("upper-threshold")) {
135             descriptor.setUpperThreshold(currentName.toString().trim());
136         } else if (qName.equals("name")) {
137             name = currentName.toString().trim();
138         } else if (qName.equals("pattern")) {
139             pattern = currentName.toString().trim();
140         } else if (qName.equals("group-definition")) {
141             configuration.addGroupDefinition(name, pattern);
142         }
143         
144         Logger.getLogger(getClass()).debug("endElement qName = " + qName + " (\"" + currentName.toString().trim() + "\")");
145     }
146
147     public void characters(char[] ch, int start, int length) throws SAXException {
148         currentName.append(ch, start, length);
149         Logger.getLogger(getClass()).debug("characters: \"" + new String JavaDoc(ch, start, length) + "\"");
150     }
151 }
152
Popular Tags