1 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 name; 54 private String pattern; 55 56 private StringBuffer currentName = new StringBuffer (); 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 namespaceURI, String localName, String 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 namespaceURI, String localName, String 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 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 (ch, start, length) + "\""); 150 } 151 } 152 | Popular Tags |