package com.epam.wilma.service.configuration.stub.helper.common; /*========================================================================== Copyright 2013-2017 EPAM Systems This file is part of Wilma. Wilma is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Wilma is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Wilma. If not, see <http://www.gnu.org/licenses/>. ===========================================================================*/ import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; /** * This validator class is used to check the generated config is valid or not. * * @author tkohegyi */ public class StubConfigurationValidator { /** * Validates the specified string as stub configuration XML against the XSD. * * @param candidateConfiguration is the candidate * @throws StubConfigurationException when the (structure) validation fails */ public void validate(String candidateConfiguration) throws StubConfigurationException { try { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream("StubConfig.xsd"); Source schemaFile = new StreamSource(is); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(schemaFile); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); dbFactory.setSchema(schema); DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new InputSource(new ByteArrayInputStream(candidateConfiguration.getBytes("utf-8")))); document.getDocumentElement().normalize(); //if we are here then the xml is valid } catch (SAXException | ParserConfigurationException | IOException e) { // instance document is invalid! throw new StubConfigurationException("Stub Configuration structure is invalid.", e); } } }