@Test public void shouldFailXMLValidation() throws Exception { boolean valid = XMLUtils.validate(new File("src/test/resources/invalid.xml")); Assert.assertFalse(valid); }
public static boolean validate( File f ) { try { SAXParser parser = factory.newSAXParser(); parser.setProperty( Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE ); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader( parser.getXMLReader() ); reader.setValidation( true ); reader.setErrorHandler( errorHandler ); reader.read( f ); return errorHandler.isValid(); } catch ( ParserConfigurationException e ) { LOG.error( "ParserConfigurationException: {}", e.getMessage() ); } catch ( SAXException e ) { LOG.error( "SAXException: {}", e.getMessage() ); } catch ( DocumentException e ) { LOG.error( "DocumentException: {}", e.getMessage() ); } catch ( NullPointerException e ) { LOG.warn( "Factory is not initialized. Did you call init()" ); } return false; }
XMLUtils { public static boolean validate( File f ) { try { SAXParser parser = factory.newSAXParser(); parser.setProperty( Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE ); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader( parser.getXMLReader() ); reader.setValidation( true ); reader.setErrorHandler( errorHandler ); reader.read( f ); return errorHandler.isValid(); } catch ( ParserConfigurationException e ) { LOG.error( "ParserConfigurationException: {}", e.getMessage() ); } catch ( SAXException e ) { LOG.error( "SAXException: {}", e.getMessage() ); } catch ( DocumentException e ) { LOG.error( "DocumentException: {}", e.getMessage() ); } catch ( NullPointerException e ) { LOG.warn( "Factory is not initialized. Did you call init()" ); } return false; } }
XMLUtils { public static boolean validate( File f ) { try { SAXParser parser = factory.newSAXParser(); parser.setProperty( Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE ); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader( parser.getXMLReader() ); reader.setValidation( true ); reader.setErrorHandler( errorHandler ); reader.read( f ); return errorHandler.isValid(); } catch ( ParserConfigurationException e ) { LOG.error( "ParserConfigurationException: {}", e.getMessage() ); } catch ( SAXException e ) { LOG.error( "SAXException: {}", e.getMessage() ); } catch ( DocumentException e ) { LOG.error( "DocumentException: {}", e.getMessage() ); } catch ( NullPointerException e ) { LOG.warn( "Factory is not initialized. Did you call init()" ); } return false; } private  XMLUtils(); }
XMLUtils { public static boolean validate( File f ) { try { SAXParser parser = factory.newSAXParser(); parser.setProperty( Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE ); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader( parser.getXMLReader() ); reader.setValidation( true ); reader.setErrorHandler( errorHandler ); reader.read( f ); return errorHandler.isValid(); } catch ( ParserConfigurationException e ) { LOG.error( "ParserConfigurationException: {}", e.getMessage() ); } catch ( SAXException e ) { LOG.error( "SAXException: {}", e.getMessage() ); } catch ( DocumentException e ) { LOG.error( "DocumentException: {}", e.getMessage() ); } catch ( NullPointerException e ) { LOG.warn( "Factory is not initialized. Did you call init()" ); } return false; } private  XMLUtils(); static void init(); static boolean validate( File f ); }
XMLUtils { public static boolean validate( File f ) { try { SAXParser parser = factory.newSAXParser(); parser.setProperty( Constants.XML_SCHEMA_PROPERTY, Constants.XML_SCHEMA_LANGUAGE ); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); SAXReader reader = new SAXReader( parser.getXMLReader() ); reader.setValidation( true ); reader.setErrorHandler( errorHandler ); reader.read( f ); return errorHandler.isValid(); } catch ( ParserConfigurationException e ) { LOG.error( "ParserConfigurationException: {}", e.getMessage() ); } catch ( SAXException e ) { LOG.error( "SAXException: {}", e.getMessage() ); } catch ( DocumentException e ) { LOG.error( "DocumentException: {}", e.getMessage() ); } catch ( NullPointerException e ) { LOG.warn( "Factory is not initialized. Did you call init()" ); } return false; } private  XMLUtils(); static void init(); static boolean validate( File f );  }