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 MetricsConfigurationLoader { 45 private static final String DEFAULT_READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser"; 46 private static final boolean DEFAULT_VALIDATE = false; 47 48 private MetricsConfigurationHandler handler; 49 private String readerClassname; 50 private boolean validate; 51 52 public MetricsConfigurationLoader() { 53 this(new MetricsConfiguration(), DEFAULT_READER_CLASSNAME, DEFAULT_VALIDATE); 54 } 55 56 public MetricsConfigurationLoader(MetricsConfiguration configuration) { 57 this(configuration, DEFAULT_READER_CLASSNAME, DEFAULT_VALIDATE); 58 } 59 60 public MetricsConfigurationLoader(String readerClassname) { 61 this(new MetricsConfiguration(), readerClassname, DEFAULT_VALIDATE); 62 } 63 64 public MetricsConfigurationLoader(boolean validate) { 65 this(new MetricsConfiguration(), DEFAULT_READER_CLASSNAME, validate); 66 } 67 68 public MetricsConfigurationLoader(MetricsConfiguration configuration, String readerClassname) { 69 this(configuration, readerClassname, DEFAULT_VALIDATE); 70 } 71 72 public MetricsConfigurationLoader(MetricsConfiguration configuration, boolean validate) { 73 this(configuration, DEFAULT_READER_CLASSNAME, validate); 74 } 75 76 public MetricsConfigurationLoader(String readerClassname, boolean validate) { 77 this(new MetricsConfiguration(), readerClassname, validate); 78 } 79 80 public MetricsConfigurationLoader(MetricsConfiguration configuration, String readerClassname, boolean validate) { 81 this.handler = new MetricsConfigurationHandler(configuration); 82 this.readerClassname = readerClassname; 83 this.validate = validate; 84 } 85 86 public MetricsConfiguration load(String filename) throws IOException, SAXException { 87 MetricsConfiguration result = null; 88 89 FileReader in = null; 90 91 try { 92 in = new FileReader(filename); 93 result = load(in); 94 } finally { 95 if (in != null) { 96 in.close(); 97 } 98 } 99 100 return result; 101 } 102 103 public MetricsConfiguration load(InputStream in) throws IOException, SAXException { 104 return load(new InputSource(in)); 105 } 106 107 public MetricsConfiguration load(Reader in) throws IOException, SAXException { 108 return load(new InputSource(in)); 109 } 110 111 public MetricsConfiguration load(InputSource in) throws IOException, SAXException { 112 XMLReader reader = XMLReaderFactory.createXMLReader(readerClassname); 113 reader.setDTDHandler(handler); 114 reader.setContentHandler(handler); 115 reader.setErrorHandler(handler); 116 117 try { 118 if (validate) { 119 Logger.getLogger(getClass()).warn("XML validation turned on"); 120 reader.setFeature("http://xml.org/sax/features/validation", true); 121 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); 122 } else { 123 Logger.getLogger(getClass()).info("XML validation turned off"); 124 reader.setFeature("http://xml.org/sax/features/validation", false); 125 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 126 } 127 } catch (Exception ex) { 128 Logger.getLogger(getClass()).warn("Problem setting validation feature on XML reader",ex); 129 } 130 131 reader.parse(in); 132 133 return handler.getMetricsConfiguration(); 134 } 135 } 136 | Popular Tags |