Xcos I/O: decode more properties and store them 81/17481/1
Clément DAVID [Wed, 18 Nov 2015 17:29:12 +0000 (18:29 +0100)]
 * manage SuperBlock with an rpar MList (mixed schema)
 * add some properties
 * use the same ordering properties between handlers and writers
 * write the version comment at the beginning

Change-Id: I0ce3415039bb6331884fc9f3fdb2136ac51802d4

scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/sax/CustomHandler.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/sax/RawDataHandler.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/writer/CustomWriter.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/writer/RawDataWriter.java

index b9fbdd6..f263d78 100644 (file)
@@ -19,8 +19,6 @@ import java.util.Optional;
 import java.util.logging.Level;
 import java.util.stream.Stream;
 
-import javax.naming.LimitExceededException;
-
 import org.scilab.modules.xcos.Kind;
 import org.scilab.modules.xcos.ObjectProperties;
 import org.scilab.modules.xcos.VectorOfDouble;
index 37681c1..8bfb365 100644 (file)
@@ -40,6 +40,7 @@ import org.xml.sax.Attributes;
 
 import com.mxgraph.model.mxGeometry;
 import com.mxgraph.util.mxPoint;
+import org.scilab.modules.xcos.VectorOfInt;
 
 class RawDataHandler implements ScilabHandler {
 
@@ -71,17 +72,17 @@ class RawDataHandler implements ScilabHandler {
 
         Map<String, ObjectProperties> localPropertyMap = new HashMap<>();
         localPropertyMap.put("context", ObjectProperties.DIAGRAM_CONTEXT);
-        localPropertyMap.put("dState", ObjectProperties.DSTATE);
-        localPropertyMap.put("equations", ObjectProperties.EQUATIONS);
         localPropertyMap.put("exprs", ObjectProperties.EXPRS);
+        localPropertyMap.put("realParameters", ObjectProperties.RPAR);
         localPropertyMap.put("integerParameters", ObjectProperties.IPAR);
+        localPropertyMap.put("objectsParameters", ObjectProperties.OPAR);
         localPropertyMap.put("nbZerosCrossing", ObjectProperties.NZCROSS);
         localPropertyMap.put("nmode", ObjectProperties.NMODE);
-        localPropertyMap.put("objectsParameters", ObjectProperties.OPAR);
+        localPropertyMap.put("state", ObjectProperties.STATE);
+        localPropertyMap.put("dState", ObjectProperties.DSTATE);
         localPropertyMap.put("oDState", ObjectProperties.ODSTATE);
+        localPropertyMap.put("equations", ObjectProperties.EQUATIONS);
         localPropertyMap.put("points", ObjectProperties.CONTROL_POINTS);
-        localPropertyMap.put("realParameters", ObjectProperties.RPAR);
-        localPropertyMap.put("state", ObjectProperties.STATE);
         propertyMap = Collections.unmodifiableMap(localPropertyMap);
     }
 
@@ -374,15 +375,13 @@ class RawDataHandler implements ScilabHandler {
 
                 // when we are decoding data into a list "as" is null
                 if (fieldValue.as == null) {
-                    if ( !(parent instanceof ScilabList)) {
-                        return;
-                    }
-                    if ( !(fieldValue.value instanceof ScilabType)) {
+                    if (!(parent instanceof RawDataDescriptor)) {
                         return;
                     }
 
-                    ScilabList parentList = (ScilabList) parent;
-                    parentList.add((ScilabType) fieldValue.value);
+                    ArrayList<Object> parentList = (ArrayList<Object>) ((RawDataDescriptor) parent).value;
+                    parentList.add(fieldValue.value);
+                    return;
                 }
 
                 switch (fieldValue.as) {
@@ -416,16 +415,13 @@ class RawDataHandler implements ScilabHandler {
                     }
                     case STATE:
                     case DSTATE:
-                    case NZCROSS:
-                    case NMODE:
-                    case IPAR:
                     case RPAR: {
                         // defensive programming
                         if (!(parent instanceof XcosCell)) {
                             return;
                         }
                         XcosCell cell = (XcosCell) parent;
-                        if (fieldValue.value instanceof ScilabMList) {
+                        if (fieldValue.as == ObjectProperties.RPAR && fieldValue.value instanceof ScilabMList) {
                             // CORNER CASE for partially decoded sub-diagram hierarchy
                             // decode the rpar as a subdiagram using the legacy decoders
                             try {
@@ -449,6 +445,56 @@ class RawDataHandler implements ScilabHandler {
                         }
                         break;
                     }
+                    case NZCROSS:
+                    case NMODE:
+                    case IPAR: {
+                        // defensive programming
+                        if (!(parent instanceof XcosCell)) {
+                            return;
+                        }
+                        XcosCell cell = (XcosCell) parent;
+
+                        VectorOfInt vec = null;
+                        if (fieldValue.value instanceof ScilabDouble) {
+                            // defensive programming against old schema
+                            ScilabDouble value = (ScilabDouble) fieldValue.value;
+
+                            vec = new VectorOfInt(value.getHeight());
+                            for (int i = 0; i < value.getHeight(); i++) {
+                                vec.set(i, (int) value.getRealElement(i, 0));
+                            }
+                        } else if (fieldValue.value instanceof ScilabInteger) {
+                            // defensive programming against old schema
+                            ScilabInteger value = (ScilabInteger) fieldValue.value;
+
+                            vec = new VectorOfInt(value.getHeight());
+                            for (int i = 0; i < value.getHeight(); i++) {
+                                switch (value.getPrec()) {
+                                    case sci_int8:
+                                    case sci_uint8:
+                                        vec.set(i, value.getByteElement(i, 0));
+                                        break;
+                                    case sci_int16:
+                                    case sci_uint16:
+                                        vec.set(i, value.getShortElement(i, 0));
+                                        break;
+                                    case sci_int32:
+                                    case sci_uint32:
+                                        vec.set(i, value.getIntElement(i, 0));
+                                        break;
+                                    case sci_int64:
+                                    case sci_uint64:
+                                        vec.set(i, (int) value.getLongElement(i, 0));
+                                        break;
+                                }
+                            }
+                        }
+                        if (vec != null) {
+                            saxHandler.controller.setObjectProperty(cell.getUID(), cell.getKind(), fieldValue.as, vec);
+                        }
+                        break;
+
+                    }
                     case EXPRS:
                     case EQUATIONS:
                     case OPAR:
index f869493..1a24404 100644 (file)
@@ -41,6 +41,17 @@ public class CustomWriter extends ScilabWriter {
         switch (kind) {
             case DIAGRAM:
                 shared.stream.writeStartDocument();
+
+                /*
+                 * Add a version comment at startup
+                 */
+
+                final Package p = Package.getPackage("org.scilab.modules.xcos");
+                String comment = new StringBuilder().append(Xcos.TRADENAME).append(SEP).append(Xcos.VERSION).append(SEP)
+                .append(p.getSpecificationVersion()).append(SEP).append(p.getImplementationVersion()).toString();
+                shared.stream.writeComment(comment);
+                shared.stream.writeCharacters("\n");
+
                 shared.stream.writeStartElement(HandledElement.XcosDiagram.name());
 
                 /*
@@ -87,6 +98,7 @@ public class CustomWriter extends ScilabWriter {
                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
                 if (children.size() > 0) {
                     shared.stream.writeStartElement(HandledElement.SuperBlockDiagram.name());
+                    shared.stream.writeAttribute("as", "child");
                     writeDiagramAndSuperDiagramContent(uid, kind, children);
                     shared.stream.writeEndElement(); // SuperBlockDiagram
                 }
@@ -110,14 +122,6 @@ public class CustomWriter extends ScilabWriter {
         shared.stream.writeAttribute("title", str[0]);
 
         /*
-         * Add the legacy version comment
-         */
-        final Package p = Package.getPackage("org.scilab.modules.xcos");
-        String comment = new StringBuilder().append(Xcos.TRADENAME).append(SEP).append(Xcos.VERSION).append(SEP)
-        .append(p.getSpecificationVersion()).append(SEP).append(p.getImplementationVersion()).toString();
-        shared.stream.writeComment(comment);
-
-        /*
          * encode some content then the children
          */
         shared.rawDataWriter.write(uid, kind);
index bcf9bff..571d57e 100644 (file)
@@ -112,9 +112,15 @@ public class RawDataWriter extends ScilabWriter {
         shared.controller.getObjectProperty(uid, kind, ObjectProperties.NMODE, vInt);
         write(vInt, "nmode");
 
+        shared.controller.getObjectProperty(uid, kind, ObjectProperties.STATE, vDouble);
+        write(vDouble, "state");
+
         shared.controller.getObjectProperty(uid, kind, ObjectProperties.DSTATE, vDouble);
         write(vDouble, "dState");
 
+        shared.controller.getObjectProperty(uid, kind, ObjectProperties.ODSTATE, vDouble);
+        write(new ScilabTypeCoder().vec2var(vDouble), "oDState");
+
         shared.controller.getObjectProperty(uid, kind, ObjectProperties.EQUATIONS, vDouble);
         write(new ScilabTypeCoder().vec2var(vDouble), "equations");
     }
@@ -232,7 +238,7 @@ public class RawDataWriter extends ScilabWriter {
         shared.stream.writeAttribute("height", Integer.toString(value.getHeight()));
         shared.stream.writeAttribute("width", Integer.toString(value.getWidth()));
         if (intPrecision != null) {
-            shared.stream.writeAttribute("intPrecision", value.getType().name());
+            shared.stream.writeAttribute("intPrecision", intPrecision);
         }
 
         for (int i = 0; i < value.getHeight(); i++) {