commit d28fa7e89b7ac1e3c2dc7652ced1da1c44cf003f parent 391e7dac5ebeae2c33e27c7faa4608f19c7a2a61 Author: Marcello Stanisci <stanisci.m@gmail.com> Date: Sun, 15 Sep 2019 18:24:49 +0200 Drafting XSD import from disk, and XML validation. Diffstat:
| D | src/main/java/XMLManagement.java | | | 12 | ------------ |
| A | src/main/java/tech/libeufin/XMLManagement.java | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 53 insertions(+), 12 deletions(-)
diff --git a/src/main/java/XMLManagement.java b/src/main/java/XMLManagement.java @@ -1,11 +0,0 @@ - -/** - * This class takes care of importing XSDs and validate - * XMLs against those. - */ - -public class XMLManagement { - public void printtheline(){ - System.out.println("Hello Java World."); - } -}; -\ No newline at end of file diff --git a/src/main/java/tech/libeufin/XMLManagement.java b/src/main/java/tech/libeufin/XMLManagement.java @@ -0,0 +1,52 @@ +package tech.libeufin; + +import org.xml.sax.SAXException; +import java.io.IOException; +import javax.xml.XMLConstants; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.Source; +import javax.xml.validation.*; // has SchemaFactory +import java.io.File; + +/** + * This class takes care of importing XSDs and validate + * XMLs against those. + */ + +public class XMLManagement { + + /** + * Bundle of all the XSDs loaded in memory. + */ + Schema bundle; + Validator validator; + + /** + * Load all the XSDs from disk. + */ + public XMLManagement(){ + + Source ebics_hev_file = new StreamSource(new File("resources/ebics_hev.xsd")); + Source xsds[] = {ebics_hev_file}; + SchemaFactory sf = new SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); + + try{ + this.bundle = sf.newSchema(xsds); + this.validator = this.bundle.newValidator(); + } catch (IOException e){ + System.out.println("Could not import all XSDs from disk" + "(" + e + ")"); + } + } + + public boolean validate(Source xmlDoc){ + try{ + this.validator.validate(xmlDoc); + } catch (SAXException e) { + return false; + } catch (IOException e) { + System.out.println("Could not pass XML to validator."); + return false; + } + + } +}; +\ No newline at end of file