RTSDK : Add spec xml file validation APIs
authordaeryong.park <bdragon.park@samsung.com>
Wed, 24 May 2017 05:38:00 +0000 (14:38 +0900)
committerdaeryong.park <bdragon.park@samsung.com>
Wed, 24 May 2017 05:38:00 +0000 (14:38 +0900)
- Add spec xml file validation APIs

Change-Id: I21db89f1f3465672d7d2ef7fc71bec0a78962a14
Signed-off-by: daeryong.park <bdragon.park@samsung.com>
12 files changed:
rt-ide/tizen.rt.product.meta/build.properties
rt-ide/tizen.rt.product.meta/resources/buildSpec.xml [deleted file]
rt-ide/tizen.rt.product.meta/resources/debugSpec.xml [deleted file]
rt-ide/tizen.rt.product.meta/resources/flashSpec.xml [deleted file]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/buildSpec.xml [moved from rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/buildSpec.xml with 100% similarity, mode: 0644]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/buildSpec.xsd [moved from rt-ide/tizen.rt.product.meta/resources/buildSpec.xsd with 100% similarity, mode: 0644]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/debugSpec.xml [moved from rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/debugSpec.xml with 100% similarity, mode: 0644]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/debugSpec.xsd [moved from rt-ide/tizen.rt.product.meta/resources/debugSpec.xsd with 100% similarity, mode: 0644]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/flashSpec.xml [moved from rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/flashSpec.xml with 100% similarity, mode: 0644]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/resources/flashSpec.xsd [moved from rt-ide/tizen.rt.product.meta/resources/flashSpec.xsd with 100% similarity, mode: 0644]
rt-ide/tizen.rt.product.meta/src/org/tizen/rt/product/meta/util/MetaModelUtil.java
rt-ide/tizen.rt.product.meta/test/src/org/tizen/rt/product/meta/model/LoadModelTest.java

index 96fb1c8..b5d662f 100755 (executable)
@@ -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 (executable)
index d148004..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-    predefined macro values:
-    * BOARD - selected board name by UI
-    * BUILD_OPTION - selected build option by UI
-    * TOOLCHAIN_PATH - user defined toolchain path by UI
--->
-<build name="sidk_s5jt200">
-    <!-- path attribute value must be relative path from project root -->
-    <!-- Don't touch command name. IDE use command with name -->
-    <commands>
-        <command name="config linux" command="./configure.sh" path="os/tools" shellcmd="true">
-            <param name="buildOption" value="${BOARD}/${BUILD_OPTION}"/>
-        </command>
-        <command name="build" command="make" path="os" shellcmd="true">
-            <param name="V" value="V=1"/>
-        </command>
-        <command name="buildWithToolchain" command=". ./setenv.sh ${TOOLCHAIN_PATH};cd ..;make V=1" path="os/tools" shellcmd="true" shell="/bin/bash"/>
-        <command name="clean" command="make clean" path="os" shellcmd="true">
-            <param name="clean" value="clean"/>
-        </command>
-    </commands>
-    <executors> <!-- execute command sequentially on build -->
-        <executor name="linux default excutor" os="linux" bit="all">
-            <commandref name="config linux"/>
-            <commandref name="build"/>
-        </executor>
-    </executors>
-</build>
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 (executable)
index 360fbdc..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-    predefined macro values:
-    * PROJECT_PATH - current project path
-    * BOARD - selected board name
-    * SCRIPT_PATH - selected script path
-    * SCRIPT_FILE - selected script file
--->
-<debugs>
-    <debug name="openocd debug" type="openocd">
-        <!-- path attribute value must be relative path from project root -->
-        <!-- usage : openocd -f debug_all.cfg -->
-        <options name="scriptfile" type="file" path="${PROJECT_PATH}/build/configs/${BOARD}/scripts" filter="*.cfg"/>
-        <execute name="linux 32 debug" os="linux" bit="32" command="${PROJECT_PATH}/build/configs/${BOARD}/tools/openocd/linux32/openocd">
-            <params>
-                <param prefix="-s" value="${SCRIPT_PATH}" />
-                <param prefix="-f" value="${SCRIPT_FILE}"/>
-            </params>
-        </execute>
-        <execute name="linux 64 debug" os="linux" bit="64" command="${PROJECT_PATH}/build/configs/${BOARD}/tools/openocd/linux64/openocd">
-            <params>
-                <param prefix="-s" value="${SCRIPT_PATH}" />
-                <param prefix="-f" value="${SCRIPT_FILE}"/>
-            </params>
-        </execute>
-    </debug>
-</debugs>
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 (executable)
index 6a55910..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<flash name="All Board">
-    <!-- path attribute value must be relative path from project root -->
-    <executors>
-        <execute name="linux flash" path="os" command="make download" os="linux" bit="all"></execute>
-    </executors>
-    <options>
-        <option>ALL</option>
-        <option>RESOURCE</option>
-        <option>FOTA_ALL</option>
-        <option>TINYARA_OTA0</option>
-        <option>TINYARA_OTA1</option>
-    </options>
-</flash>
\ No newline at end of file
index 56d9068..32af49b 100755 (executable)
 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);
+    }
+
 }
index 416c187..b68e89b 100755 (executable)
  */
 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);
+    }
+
 }