From 2609a64b720b0c7f97f9a0891a2c9f5bbb663146 Mon Sep 17 00:00:00 2001 From: "daeryong.park" Date: Wed, 24 May 2017 14:38:00 +0900 Subject: [PATCH] RTSDK : Add spec xml file validation APIs - Add spec xml file validation APIs Change-Id: I21db89f1f3465672d7d2ef7fc71bec0a78962a14 Signed-off-by: daeryong.park --- rt-ide/tizen.rt.product.meta/build.properties | 3 +- .../tizen.rt.product.meta/resources/buildSpec.xml | 29 ----- .../tizen.rt.product.meta/resources/debugSpec.xml | 27 ----- .../tizen.rt.product.meta/resources/flashSpec.xml | 14 --- .../tizen/rt/product/meta/resources}/buildSpec.xml | 0 .../tizen/rt/product/meta}/resources/buildSpec.xsd | 0 .../tizen/rt/product/meta/resources}/debugSpec.xml | 0 .../tizen/rt/product/meta}/resources/debugSpec.xsd | 0 .../tizen/rt/product/meta/resources}/flashSpec.xml | 0 .../tizen/rt/product/meta}/resources/flashSpec.xsd | 0 .../tizen/rt/product/meta/util/MetaModelUtil.java | 117 +++++++++++++++++++++ .../tizen/rt/product/meta/model/LoadModelTest.java | 32 +++++- 12 files changed, 147 insertions(+), 75 deletions(-) delete mode 100755 rt-ide/tizen.rt.product.meta/resources/buildSpec.xml delete mode 100755 rt-ide/tizen.rt.product.meta/resources/debugSpec.xml delete mode 100755 rt-ide/tizen.rt.product.meta/resources/flashSpec.xml rename rt-ide/tizen.rt.product.meta/{test/src/org/tizen/rt/product/meta/model => src/org/tizen/rt/product/meta/resources}/buildSpec.xml (100%) mode change 100755 => 100644 rename rt-ide/tizen.rt.product.meta/{ => src/org/tizen/rt/product/meta}/resources/buildSpec.xsd (100%) mode change 100755 => 100644 rename rt-ide/tizen.rt.product.meta/{test/src/org/tizen/rt/product/meta/model => src/org/tizen/rt/product/meta/resources}/debugSpec.xml (100%) mode change 100755 => 100644 rename rt-ide/tizen.rt.product.meta/{ => src/org/tizen/rt/product/meta}/resources/debugSpec.xsd (100%) mode change 100755 => 100644 rename rt-ide/tizen.rt.product.meta/{test/src/org/tizen/rt/product/meta/model => src/org/tizen/rt/product/meta/resources}/flashSpec.xml (100%) mode change 100755 => 100644 rename rt-ide/tizen.rt.product.meta/{ => src/org/tizen/rt/product/meta}/resources/flashSpec.xsd (100%) mode change 100755 => 100644 diff --git a/rt-ide/tizen.rt.product.meta/build.properties b/rt-ide/tizen.rt.product.meta/build.properties index 96fb1c8..b5d662f 100755 --- a/rt-ide/tizen.rt.product.meta/build.properties +++ b/rt-ide/tizen.rt.product.meta/build.properties @@ -3,6 +3,5 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ build.properties,\ - OSGI-INF/,\ - resources/ + OSGI-INF/ src.excludes = test/src/ diff --git a/rt-ide/tizen.rt.product.meta/resources/buildSpec.xml b/rt-ide/tizen.rt.product.meta/resources/buildSpec.xml deleted file mode 100755 index d148004..0000000 --- a/rt-ide/tizen.rt.product.meta/resources/buildSpec.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/rt-ide/tizen.rt.product.meta/resources/debugSpec.xml b/rt-ide/tizen.rt.product.meta/resources/debugSpec.xml deleted file mode 100755 index 360fbdc..0000000 --- a/rt-ide/tizen.rt.product.meta/resources/debugSpec.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/rt-ide/tizen.rt.product.meta/resources/flashSpec.xml b/rt-ide/tizen.rt.product.meta/resources/flashSpec.xml deleted file mode 100755 index 6a55910..0000000 --- a/rt-ide/tizen.rt.product.meta/resources/flashSpec.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/buildSpec.xml b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/buildSpec.xml old mode 100755 new mode 100644 similarity index 100% rename from rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/buildSpec.xml rename to rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/buildSpec.xml diff --git a/rt-ide/tizen.rt.product.meta/resources/buildSpec.xsd b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/buildSpec.xsd old mode 100755 new mode 100644 similarity index 100% rename from rt-ide/tizen.rt.product.meta/resources/buildSpec.xsd rename to rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/buildSpec.xsd diff --git a/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/debugSpec.xml b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/debugSpec.xml old mode 100755 new mode 100644 similarity index 100% rename from rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/debugSpec.xml rename to rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/debugSpec.xml diff --git a/rt-ide/tizen.rt.product.meta/resources/debugSpec.xsd b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/debugSpec.xsd old mode 100755 new mode 100644 similarity index 100% rename from rt-ide/tizen.rt.product.meta/resources/debugSpec.xsd rename to rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/debugSpec.xsd diff --git a/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/flashSpec.xml b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/flashSpec.xml old mode 100755 new mode 100644 similarity index 100% rename from rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/flashSpec.xml rename to rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/flashSpec.xml diff --git a/rt-ide/tizen.rt.product.meta/resources/flashSpec.xsd b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/flashSpec.xsd old mode 100755 new mode 100644 similarity index 100% rename from rt-ide/tizen.rt.product.meta/resources/flashSpec.xsd rename to rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/flashSpec.xsd diff --git a/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/util/MetaModelUtil.java b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/util/MetaModelUtil.java index 56d9068..32af49b 100755 --- a/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/util/MetaModelUtil.java +++ b/rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/util/MetaModelUtil.java @@ -26,17 +26,32 @@ package org.tizen.rt.product.meta.util; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; +import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; +import org.tizen.rt.product.meta.RtMetaPlugin; import org.tizen.rt.product.meta.model.build.Build; import org.tizen.rt.product.meta.model.debug.Debugs; import org.tizen.rt.product.meta.model.flash.Flash; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; /** * @since 2017. 5. 18. @@ -44,6 +59,10 @@ import org.tizen.rt.product.meta.model.flash.Flash; */ public class MetaModelUtil { + private enum TYPE { + BUILD, FLASH, DEBUG + } + private MetaModelUtil() { } @@ -164,4 +183,102 @@ public class MetaModelUtil { return showModel(Debugs.class, debugModel); } + private static void validate(InputStream xmlFileStream, InputStream xsdFileStream) + throws ParserConfigurationException, SAXException, IOException, FileNotFoundException { + if (xmlFileStream == null) { + throw new FileNotFoundException("XML file stream is null"); // $NON-NLS-1 + } else if (xsdFileStream == null) { + throw new FileNotFoundException("XSD file stream is null"); // $NON-NLS-1 + } + + // parse an XML document into a DOM tree + DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document document = parser.parse(xmlFileStream); + + // create a SchemaFactory capable of understanding WXS schemas + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + + // load a WXS schema, represented by a Schema instance + Source schemaFile = new StreamSource(xsdFileStream); + Schema schema = factory.newSchema(schemaFile); + + // create a Validator instance, which can be used to validate an instance document + Validator validator = schema.newValidator(); + + // validate the DOM tree + try { + validator.validate(new DOMSource(document)); + } catch (SAXException e) { + throw e; + } + } + + private static void validateFile(File xmlFile, TYPE type) + throws ParserConfigurationException, SAXException, IOException, FileNotFoundException { + if (xmlFile == null) { + throw new FileNotFoundException("XML file is null"); // $NON-NLS-1 + } + + FileInputStream xmlFileStream = new FileInputStream(xmlFile); + + try { + switch (type) { + case BUILD: + validateBuildFile(xmlFileStream); + break; + case FLASH: + validateFlashFile(xmlFileStream); + break; + case DEBUG: + validateDebugFile(xmlFileStream); + break; + default: + break; + } + } catch (SAXException e) { + throw e; + } finally { + if (xmlFileStream != null) { + try { + xmlFileStream.close(); + } catch (IOException e) { + // Do nothing + } + } + } + } + + public static void validateBuildFile(File xmlFile) + throws ParserConfigurationException, SAXException, IOException, FileNotFoundException { + validateFile(xmlFile, TYPE.BUILD); + } + + public static void validateFlashFile(File xmlFile) + throws ParserConfigurationException, SAXException, IOException, FileNotFoundException { + validateFile(xmlFile, TYPE.FLASH); + } + + public static void validateDebugFile(File xmlFile) + throws ParserConfigurationException, SAXException, IOException, FileNotFoundException { + validateFile(xmlFile, TYPE.DEBUG); + } + + public static void validateBuildFile(InputStream xmlFileStream) + throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { + InputStream xsdFileStream = RtMetaPlugin.class.getResourceAsStream("resources/buildSpec.xsd"); // $NON-NLS-1 + validate(xmlFileStream, xsdFileStream); + } + + public static void validateFlashFile(InputStream xmlFileStream) + throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { + InputStream xsdFileStream = RtMetaPlugin.class.getResourceAsStream("resources/flashSpec.xsd"); // $NON-NLS-1 + validate(xmlFileStream, xsdFileStream); + } + + public static void validateDebugFile(InputStream xmlFileStream) + throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { + InputStream xsdFileStream = RtMetaPlugin.class.getResourceAsStream("resources/debugSpec.xsd"); // $NON-NLS-1 + validate(xmlFileStream, xsdFileStream); + } + } diff --git a/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/LoadModelTest.java b/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/LoadModelTest.java index 416c187..b68e89b 100755 --- a/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/LoadModelTest.java +++ b/rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/LoadModelTest.java @@ -25,16 +25,21 @@ */ package org.tizen.rt.product.meta.model; +import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import javax.xml.bind.JAXBException; +import javax.xml.parsers.ParserConfigurationException; import org.junit.Assert; import org.junit.Test; +import org.tizen.rt.product.meta.RtMetaPlugin; import org.tizen.rt.product.meta.model.build.Build; import org.tizen.rt.product.meta.model.debug.Debugs; import org.tizen.rt.product.meta.model.flash.Flash; import org.tizen.rt.product.meta.util.MetaModelUtil; +import org.xml.sax.SAXException; /** * @since 2017. 5. 18. @@ -44,7 +49,7 @@ public class LoadModelTest { @Test public void buildModelTest() throws JAXBException { - InputStream is = LoadModelTest.class.getResourceAsStream("buildSpec.xml"); + InputStream is = RtMetaPlugin.class.getResourceAsStream("resources/buildSpec.xml"); Build model = MetaModelUtil.loadBuildModel(is); Assert.assertNotNull(model); @@ -55,7 +60,7 @@ public class LoadModelTest { @Test public void flashModelTest() throws JAXBException { - InputStream is = LoadModelTest.class.getResourceAsStream("flashSpec.xml"); + InputStream is = RtMetaPlugin.class.getResourceAsStream("resources/flashSpec.xml"); Flash model = MetaModelUtil.loadFlashModel(is); Assert.assertNotNull(model); @@ -66,7 +71,7 @@ public class LoadModelTest { @Test public void debugModelTest() throws JAXBException { - InputStream is = LoadModelTest.class.getResourceAsStream("debugSpec.xml"); + InputStream is = RtMetaPlugin.class.getResourceAsStream("resources/debugSpec.xml"); Debugs model = MetaModelUtil.loadDebugModel(is); Assert.assertNotNull(model); @@ -75,4 +80,25 @@ public class LoadModelTest { System.out.println(result); } + @Test + public void validateBuildFile() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { + InputStream xmlFileStream = RtMetaPlugin.class.getResourceAsStream("resources/buildSpec.xml"); + + MetaModelUtil.validateBuildFile(xmlFileStream); + } + + @Test + public void validateFlashFile() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { + InputStream xmlFileStream = RtMetaPlugin.class.getResourceAsStream("resources/flashSpec.xml"); + + MetaModelUtil.validateFlashFile(xmlFileStream); + } + + @Test + public void validateDebugFile() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException { + InputStream xmlFileStream = RtMetaPlugin.class.getResourceAsStream("resources/debugSpec.xml"); + + MetaModelUtil.validateDebugFile(xmlFileStream); + } + } -- 2.7.4