Xcos GUI: fix Annotations import and loosen DSTATE decoding 84/17384/3
Paul Bignier [Fri, 23 Oct 2015 09:57:32 +0000 (11:57 +0200)]
 * DSTATE can be an empty string, for backward compatibility
 * Does not handle all Annotations properties but now allows proper creation in the model

// Does not pass yet (need to decode all properties) but goes further
--> xcos(SCI + "/modules/xcos/demos/Simple_Demo.zcos");

Change-Id: Ie08cd0098cf523aefd9eb4abe3ee68299f7356c1

scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/sax/BlockHandler.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/sax/RawDataHandler.java

index e706449..af689c8 100644 (file)
@@ -52,7 +52,13 @@ class BlockHandler implements ScilabHandler {
     public BasicBlock startElement(HandledElement found, Attributes atts) {
         String v;
         BasicBlock block;
-        final long uid = saxHandler.controller.createObject(Kind.BLOCK);
+        Kind kind;
+        if (found == HandledElement.TextBlock) {
+            kind = Kind.ANNOTATION;
+        } else {
+            kind = Kind.BLOCK;
+        }
+        final long uid = saxHandler.controller.createObject(kind);
 
         switch (found) {
             case AfficheBlock:
@@ -133,26 +139,26 @@ class BlockHandler implements ScilabHandler {
 
         v = atts.getValue("interfaceFunctionName");
         if (v != null) {
-            saxHandler.controller.setObjectProperty(uid, Kind.BLOCK, ObjectProperties.INTERFACE_FUNCTION, v);
-        } else {
+            saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.INTERFACE_FUNCTION, v);
+        } else if (kind == Kind.BLOCK) {
             // Setup the interface function accordingly to the mapping table
             // by default it is empty and might not be serialized (depending on
             // the class)
             String defaultInterfaceFunction = Arrays.stream(BlockInterFunction.values()).filter(n -> n.getKlass().equals(block.getClass())).map(e -> e.name())
                                               .findFirst().get();
-            saxHandler.controller.setObjectProperty(uid, Kind.BLOCK, ObjectProperties.INTERFACE_FUNCTION, defaultInterfaceFunction);
+            saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.INTERFACE_FUNCTION, defaultInterfaceFunction);
         }
         v = atts.getValue("simulationFunctionName");
         if (v != null) {
-            saxHandler.controller.setObjectProperty(uid, Kind.BLOCK, ObjectProperties.SIM_FUNCTION_NAME, v);
+            saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, v);
         }
         v = atts.getValue("blockType");
         if (v != null) {
-            saxHandler.controller.setObjectProperty(uid, Kind.BLOCK, ObjectProperties.SIM_BLOCKTYPE, v);
+            saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.SIM_BLOCKTYPE, v);
         }
         v = atts.getValue("style");
         if (v != null) {
-            saxHandler.controller.setObjectProperty(uid, Kind.BLOCK, ObjectProperties.STYLE, v);
+            saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.STYLE, v);
         }
 
         saxHandler.insertChild(block);
index 585c7cd..da73b02 100644 (file)
@@ -99,13 +99,13 @@ class RawDataHandler implements ScilabHandler {
 
         switch (found) {
             case ScilabBoolean:
-                // no break on purpose
+            // no break on purpose
             case ScilabDouble:
-                // no break on purpose
+            // no break on purpose
             case ScilabInteger:
-                // no break on purpose
+            // no break on purpose
             case ScilabString:
-                // no break on purpose
+            // no break on purpose
             case Array: {
                 String as = atts.getValue("as");
 
@@ -305,25 +305,25 @@ class RawDataHandler implements ScilabHandler {
                     switch (ScilabIntegerTypeEnum.valueOf(v)) {
                         case sci_int8:
                             unsigned = false;
-                            // no break on purpose
+                        // no break on purpose
                         case sci_uint8:
                             container = new ScilabInteger(new byte[height][width], unsigned);
                             break;
                         case sci_int16:
                             unsigned = false;
-                            // no break on purpose
+                        // no break on purpose
                         case sci_uint16:
                             container = new ScilabInteger(new short[height][width], unsigned);
                             break;
                         case sci_int32:
                             unsigned = false;
-                            // no break on purpose
+                        // no break on purpose
                         case sci_uint32:
                             container = new ScilabInteger(new int[height][width], unsigned);
                             break;
                         case sci_int64:
                             unsigned = false;
-                            // no break on purpose
+                        // no break on purpose
                         case sci_uint64:
                             container = new ScilabInteger(new long[height][width], unsigned);
                             break;
@@ -357,13 +357,13 @@ class RawDataHandler implements ScilabHandler {
     public void endElement(HandledElement found) {
         switch (found) {
             case Array:
-                // no break on purpose
+            // no break on purpose
             case ScilabBoolean:
-                // no break on purpose
+            // no break on purpose
             case ScilabDouble:
-                // no break on purpose
+            // no break on purpose
             case ScilabInteger:
-                // no break on purpose
+            // no break on purpose
             case ScilabString: {
                 // defensive programming
                 if (!(saxHandler.parents.peek() instanceof RawDataDescriptor)) {
@@ -421,14 +421,19 @@ class RawDataHandler implements ScilabHandler {
                             }
                             return;
                         }
-                        ScilabDouble value = (ScilabDouble) fieldValue.value;
 
-                        VectorOfDouble vec = new VectorOfDouble(value.getHeight());
-                        for (int i = 0; i < value.getHeight(); i++) {
-                            vec.set(i, value.getRealElement(i, 0));
-                        }
+                        VectorOfDouble vec;
+                        if (fieldValue.value instanceof ScilabDouble) {
+                            // defensive programming against old schema
+                            ScilabDouble value = (ScilabDouble) fieldValue.value;
+
+                            vec = new VectorOfDouble(value.getHeight());
+                            for (int i = 0; i < value.getHeight(); i++) {
+                                vec.set(i, value.getRealElement(i, 0));
+                            }
 
-                        saxHandler.controller.setObjectProperty(cell.getUID(), cell.getKind(), fieldValue.as, vec);
+                            saxHandler.controller.setObjectProperty(cell.getUID(), cell.getKind(), fieldValue.as, vec);
+                        }
                         break;
                     }
                     case EXPRS:
@@ -459,4 +464,4 @@ class RawDataHandler implements ScilabHandler {
                 throw new IllegalArgumentException();
         }
     }
-}
\ No newline at end of file
+}