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