DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ· RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://issues.apache.org/bugzilla/show_bug.cgi?id=36161>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ· INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=36161 Summary: Patch to add validation of xml configuration file Product: Commons Version: 1.1.0 Platform: All OS/Version: other Status: NEW Severity: normal Priority: P2 Component: Configuration AssignedTo: [email protected] ReportedBy: [EMAIL PROTECTED] Validtion of xml file configuration is a lack in commons-configuration. Imagine the nightmare of a not well-form document where you're searching about properties. So I write this 'five-minutes' patch to match it! Patch: ---------------------------------------------------------- 41a42 > import org.apache.commons.configuration.reloading.ReloadingStrategy; 51c52,54 < import org.apache.commons.configuration.reloading.ReloadingStrategy; --- > import org.xml.sax.SAXException; > import org.xml.sax.SAXParseException; > import org.xml.sax.helpers.DefaultHandler; 83a87,89 > /** Indicates if the xml file has to validated */ > private boolean validating; > 100a107,120 > this(fileName, false); > } > > > /** > * Creates a new instance of <code>XMLConfiguration</code>. > * The configuration is loaded from the specified file > * Indicates if validation required. > * @param fileName the name of the file to load > * @param validating if xml has to validated > * @throws ConfigurationException if the file cannot be loaded > */ > public XMLConfiguration(String fileName, boolean validating) throws ConfigurationException > { 102a123 > setValidating(validating); 105a127,128 > > 114a138,153 > this(file, false); > > } > > > /** > * Creates a new instance of <code>XMLConfiguration</code>. > * The configuration is loaded from the specified file. > * Indicates if validation required. > * > * @param file the file > * @param validating if xml has to validated > * @throws ConfigurationException if an error occurs while loading the > file > */ > public XMLConfiguration(File file, boolean validating) throws ConfigurationException > { 116a156,157 > setValidating(validating); > 122a164,165 > > 131a175,191 > this(url, false); > } > > > > > /** > * Creates a new instance of <code>XMLConfiguration</code>. > * The configuration is loaded from the specified URL. > * Indicates if validation required. > * > * @param url the URL > * @param validating if xml has to validated > * @throws ConfigurationException if loading causes an error > */ > public XMLConfiguration(URL url, boolean validating) throws ConfigurationException > { 133a194 > setValidating(validating); 136a198 > 384c446 < * @throws ConfigurationException --- > * @throws An InputStream. 387a450,465 > load (in , validating); > } > > /** > * Load the properties from the given reader. > * Note that the <code>clear()</code> method is not called, so > * the properties contained in the loaded file will be added to the > * actual set of properties.Can validate the xml file passes as reader. > * > * @param in An InputStream. > * @param validating indicates if to valditate. > * @throws ConfigurationException > */ > public void load(Reader in, boolean validating) throws ConfigurationException { > > 390c468,478 < DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); --- > DocumentBuilderFactory factory = > DocumentBuilderFactory.newInstance(); > factory.setValidating(validating); > > > DocumentBuilder builder = factory.newDocumentBuilder(); > > if (isValidating()) { > builder.setErrorHandler(new XmlConfigurationDefaultHandler()); > } > > 400a489,490 > > 530a621,629 > public boolean isValidating() { > return validating; > } > > public void setValidating(boolean validating) { > this.validating = validating; > } > > 843a943,956 > > > private class XmlConfigurationDefaultHandler extends DefaultHandler { > > > public void error(SAXParseException e) throws SAXException { > // TODO Auto-generated method stub > throw new SAXException (new ConfigurationException ("Parsing error in configuration file : "+e.getMessage() )); > } > public void fatalError(SAXParseException e) throws SAXException { > // TODO Auto-generated method stub > throw new SAXException (new ConfigurationException ("Parsing error in configuration file : "+e.getMessage())); > } > } ------------------------------------------------------------- -- Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are the assignee for the bug, or are watching the assignee. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
