From 7028a9069ae382bf41f691af278c6a3a2f867571 Mon Sep 17 00:00:00 2001 From: Jurrie Overgoor Date: Fri, 12 Feb 2016 12:22:58 +0100 Subject: [PATCH] Add XSD validation for ASConfigCreator XML files. --- .../main/java/nl/eernie/as/ASConfigCreator.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/src/main/java/nl/eernie/as/ASConfigCreator.java b/core/src/main/java/nl/eernie/as/ASConfigCreator.java index 12aeec5..0251ed5 100644 --- a/core/src/main/java/nl/eernie/as/ASConfigCreator.java +++ b/core/src/main/java/nl/eernie/as/ASConfigCreator.java @@ -11,9 +11,12 @@ import java.util.Map; import java.util.Set; +import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; import nl.eernie.as.application_server.ApplicationServer; import nl.eernie.as.aschangelog.ApplicationServerChangeLog; @@ -28,9 +31,12 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.reflections.Reflections; +import org.xml.sax.SAXException; public class ASConfigCreator { + private static final String APPLICATION_SERVER_CONFIG_XSD = "ApplicationServerConfig-1.0.xsd"; + private Configuration configuration; private Map> configurationParsers = new HashMap<>(); private boolean fromTagFound; @@ -149,9 +155,13 @@ private ApplicationServerChangeLog createApplicationServerChangeLog(String chang String fileContent = FileUtils.readFileToString(file); String replacedFile = VariableReplacer.replace(fileContent, configuration.getProperties()); + SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + Schema schema = sf.newSchema(ASConfigCreator.class.getResource("/" + APPLICATION_SERVER_CONFIG_XSD)); + InputStream inputStream = new ByteArrayInputStream(replacedFile.getBytes()); JAXBContext context = JAXBContext.newInstance(ApplicationServerChangeLog.class); Unmarshaller unmarshaller = context.createUnmarshaller(); + unmarshaller.setSchema(schema); return (ApplicationServerChangeLog) unmarshaller.unmarshal(inputStream); } catch (FileNotFoundException e) @@ -162,6 +172,10 @@ private ApplicationServerChangeLog createApplicationServerChangeLog(String chang { throw new RuntimeException("Something went wrong while unmarshalling the file " + changeLogFilePath, e); } + catch (SAXException e) + { + throw new RuntimeException("Something went wrong while parsing file " + changeLogFilePath, e); + } } private boolean configurationHasContext(String contextList)