Xcos src: fix some coverity defects 18/18818/3
Paul Bignier [Mon, 26 Dec 2016 10:40:28 +0000 (11:40 +0100)]
 * CID 1367994 & 1367993 & 1367992 & 1367991 & 1367990

Change-Id: Id61c07115ab9afc43e81128688114e115ca57af5

scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/BlockAutoPositionUtils.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosRoute.java

index d905f46..3438d5b 100644 (file)
@@ -89,6 +89,9 @@ public abstract class BlockAutoPositionUtils {
         while ((port != null) && (port.getParent() instanceof SplitBlock)) {
             port = getSplitInLinkPort(((SplitBlock) port.getParent()));
         }
+        if (port == null) {
+            return null;
+        }
         mxICell edge = port.getEdgeAt(0);
         mxICell cell = ((mxCell) edge).getTarget();
         return ((SplitBlock) cell.getParent());
@@ -352,12 +355,12 @@ public abstract class BlockAutoPositionUtils {
                 || (in1.getEdgeAt(0) == out22.getEdgeAt(0))) {
             return in1;
         } else if ((out11.getEdgeAt(0) == in2.getEdgeAt(0))
-                || (out11.getEdgeAt(0) == out21.getEdgeAt(0))
-                || (out11.getEdgeAt(0) == out22.getEdgeAt(0))) {
+                   || (out11.getEdgeAt(0) == out21.getEdgeAt(0))
+                   || (out11.getEdgeAt(0) == out22.getEdgeAt(0))) {
             return out11;
         } else if ((out12.getEdgeAt(0) == in2.getEdgeAt(0))
-                || (out12.getEdgeAt(0) == out21.getEdgeAt(0))
-                || (out12.getEdgeAt(0) == out22.getEdgeAt(0))) {
+                   || (out12.getEdgeAt(0) == out21.getEdgeAt(0))
+                   || (out12.getEdgeAt(0) == out22.getEdgeAt(0))) {
             return out12;
         }
         return null;
@@ -643,8 +646,8 @@ public abstract class BlockAutoPositionUtils {
                             mxPoint p23 = list2.get(n + 2);
                             if (x11 == x12) { // the segment is vertical
                                 boolean flag2 = !XcosRouteUtils.checkObstacle(p20.getX(), p20.getY(), x11, y21, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x11, y21, x11, y22, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x11, y22, p23.getX(), p23.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x11, y21, x11, y22, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x11, y22, p23.getX(), p23.getY(), allObstacles);
                                 // if the new points of the segment was in one of the ports
                                 for (mxGeometry geometry : listGeo) {
                                     if (XcosRouteUtils.checkPointInGeometry(x11, y21, geometry)
@@ -659,8 +662,8 @@ public abstract class BlockAutoPositionUtils {
                                 }
                             } else if (y11 == y12) { // the segment is horizontal
                                 boolean flag2 = !XcosRouteUtils.checkObstacle(p20.getX(), p20.getY(), x21, y11, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x21, y11, x22, y11, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x22, y11, p23.getX(), p23.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x21, y11, x22, y11, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x22, y11, p23.getX(), p23.getY(), allObstacles);
                                 if (flag2) {
                                     p21.setY(y11);
                                     p22.setY(y11);
@@ -672,8 +675,8 @@ public abstract class BlockAutoPositionUtils {
                             mxPoint p13 = list1.get(m + 2);
                             if (x11 == x12) { // the segment is vertical
                                 boolean flag1 = !XcosRouteUtils.checkObstacle(p10.getX(), p10.getY(), x21, y11, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x21, y11, x21, y12, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x21, y12, p13.getX(), p13.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x21, y11, x21, y12, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x21, y12, p13.getX(), p13.getY(), allObstacles);
                                 for (mxGeometry geometry : listGeo) {
                                     if (XcosRouteUtils.checkPointInGeometry(x21, y11, geometry)
                                             || XcosRouteUtils.checkPointInGeometry(x21, y12, geometry)) {
@@ -687,8 +690,8 @@ public abstract class BlockAutoPositionUtils {
                                 }
                             } else if (y11 == y12) { // the segment is horizontal
                                 boolean flag1 = !XcosRouteUtils.checkObstacle(p10.getX(), p10.getY(), x11, y21, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x11, y21, x12, y21, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x12, y21, p13.getX(), p13.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x11, y21, x12, y21, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x12, y21, p13.getX(), p13.getY(), allObstacles);
                                 if (flag1) {
                                     p11.setY(y21);
                                     p12.setY(y21);
@@ -702,11 +705,11 @@ public abstract class BlockAutoPositionUtils {
                             mxPoint p13 = list1.get(m + 2);
                             if (x11 == x12) { // the segment is vertical
                                 boolean flag2 = !XcosRouteUtils.checkObstacle(p20.getX(), p20.getY(), x11, y21, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x11, y21, x11, y22, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x11, y22, p23.getX(), p23.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x11, y21, x11, y22, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x11, y22, p23.getX(), p23.getY(), allObstacles);
                                 boolean flag1 = !XcosRouteUtils.checkObstacle(p10.getX(), p10.getY(), x21, y11, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x21, y11, x21, y12, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x21, y12, p13.getX(), p13.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x21, y11, x21, y12, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x21, y12, p13.getX(), p13.getY(), allObstacles);
                                 for (mxGeometry geometry : listGeo) {
                                     if (XcosRouteUtils.checkPointInGeometry(x11, y21, geometry)
                                             || XcosRouteUtils.checkPointInGeometry(x11, y22, geometry)) {
@@ -729,11 +732,11 @@ public abstract class BlockAutoPositionUtils {
                                 }
                             } else if (y11 == y12) { // the segment is horizontal
                                 boolean flag2 = !XcosRouteUtils.checkObstacle(p20.getX(), p20.getY(), x21, y11, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x21, y11, x22, y11, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x22, y11, p23.getX(), p23.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x21, y11, x22, y11, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x22, y11, p23.getX(), p23.getY(), allObstacles);
                                 boolean flag1 = !XcosRouteUtils.checkObstacle(p10.getX(), p10.getY(), x11, y21, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x11, y21, x12, y21, allObstacles)
-                                        && !XcosRouteUtils.checkObstacle(x12, y21, p13.getX(), p13.getY(), allObstacles);
+                                                && !XcosRouteUtils.checkObstacle(x11, y21, x12, y21, allObstacles)
+                                                && !XcosRouteUtils.checkObstacle(x12, y21, p13.getX(), p13.getY(), allObstacles);
                                 for (mxGeometry geometry : listGeo) {
                                     if (XcosRouteUtils.checkPointInGeometry(x21, y11, geometry)
                                             || XcosRouteUtils.checkPointInGeometry(x22, y11, geometry)) {
@@ -784,17 +787,13 @@ public abstract class BlockAutoPositionUtils {
 
         // get the route and add all points in the route including source and target.
         List<mxPoint> list = new ArrayList<mxPoint>(0);
-        if (source != null) {
-            list.add(getPortPosition(source));
-        }
+        list.add(getPortPosition(source));
         if (util.computeRoute(source, target, allOtherCells, graph)) {
             for (mxPoint point : util.getNonRedundantPoints()) {
                 list.add(new mxPoint(Math.round(point.getX()), Math.round(point.getY())));
             }
         }
-        if (target != null) {
-            list.add(getPortPosition(target));
-        }
+        list.add(getPortPosition(target));
         return list;
     }
 
index ac8c617..ce75702 100755 (executable)
@@ -104,27 +104,22 @@ public class XcosRoute {
         double srcy = 0;\r
         double tgtx = 0;\r
         double tgty = 0;\r
-        mxPoint sourcePoint = new mxPoint(srcx, srcy);\r
-        mxPoint targetPoint = new mxPoint(tgtx, tgty);\r
+        mxCellState state;\r
         // if source is a port, get a new start point.\r
-        if (sourceCell instanceof BasicPort) {\r
-            mxCellState state = graph.getView().getState(sourceCell);\r
-            if (state != null) {\r
-                srcx = state.getCenterX();\r
-                srcy = state.getCenterY();\r
-                BasicPort sourcePort = (BasicPort) sourceCell;\r
-                sourcePortOrien = getPortRelativeOrientation(sourcePort, graph);\r
-            }\r
+        state = graph.getView().getState(sourceCell);\r
+        if (state != null) {\r
+            srcx = state.getCenterX();\r
+            srcy = state.getCenterY();\r
+            BasicPort sourcePort = (BasicPort) sourceCell;\r
+            sourcePortOrien = getPortRelativeOrientation(sourcePort, graph);\r
         }\r
         // if target is a port, get a new end point.\r
-        if (targetCell instanceof BasicPort) {\r
-            mxCellState state = graph.getView().getState(targetCell);\r
-            if (state != null) {\r
-                tgtx = state.getCenterX();\r
-                tgty = state.getCenterY();\r
-                BasicPort targetPort = (BasicPort) targetCell;\r
-                targetPortOrien = getPortRelativeOrientation(targetPort, graph);\r
-            }\r
+        state = graph.getView().getState(targetCell);\r
+        if (state != null) {\r
+            tgtx = state.getCenterX();\r
+            tgty = state.getCenterY();\r
+            BasicPort targetPort = (BasicPort) targetCell;\r
+            targetPortOrien = getPortRelativeOrientation(targetPort, graph);\r
         }\r
         // if source belongs to a SplitBlock\r
         if (sourceCell.getParent() instanceof SplitBlock) {\r
@@ -162,19 +157,19 @@ public class XcosRoute {
                 targetPortOrien = this.getNewOrientation(targetCell, tgtx, tgty, sourceCell, srcx, srcy, graph);\r
             }\r
         }\r
-        sourcePoint = getPointAwayPort(sourceCell, srcx, srcy, sourcePortOrien, allCells, graph);\r
-        targetPoint = getPointAwayPort(targetCell, tgtx, tgty, targetPortOrien, allCells, graph);\r
+        mxPoint sourcePoint = getPointAwayPort(sourceCell, srcx, srcy, sourcePortOrien, allCells, graph);\r
+        mxPoint targetPoint = getPointAwayPort(targetCell, tgtx, tgty, targetPortOrien, allCells, graph);\r
         allCells = Arrays.copyOf(allCells, allCells.length + 2);\r
         allCells[allCells.length - 2] = sourceCell;\r
         allCells[allCells.length - 1] = targetCell;\r
         List<mxPoint> list = XcosRouteUtils.getSimpleRoute(sourcePoint, sourcePortOrien, targetPoint,\r
-                targetPortOrien, allCells);\r
+                             targetPortOrien, allCells);\r
         if (list != null && list.size() > 0) {\r
             listRoute.addAll(list);\r
             return true;\r
         } else {\r
             list = XcosRouteUtils.getComplexRoute(sourcePoint, sourcePortOrien, targetPoint, targetPortOrien,\r
-                    allCells, XcosRouteUtils.TRY_TIMES);\r
+                                                  allCells, XcosRouteUtils.TRY_TIMES);\r
             if (list != null && list.size() > 0) {\r
                 listRoute.addAll(list);\r
                 return true;\r
@@ -222,45 +217,45 @@ public class XcosRoute {
      * @return\r
      */\r
     private mxPoint getPointAwayPort(mxICell port, double portX, double portY, Orientation orien,\r
-            Object[] allCells, XcosDiagram graph) {\r
+                                     Object[] allCells, XcosDiagram graph) {\r
         mxPoint point = new mxPoint(portX, portY);\r
         double distance = XcosRouteUtils.BEAUTY_AWAY_DISTANCE;\r
         if (port.getParent() instanceof SplitBlock) {\r
             distance = XcosRouteUtils.SPLITBLOCK_AWAY_DISTANCE;\r
         }\r
         switch (orien) {\r
-        case EAST:\r
-            point.setX(point.getX() + distance);\r
-            while (Math.abs(point.getX() - portX) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
-                    && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
+            case EAST:\r
+                point.setX(point.getX() + distance);\r
+                while (Math.abs(point.getX() - portX) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
+                        && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
                             || XcosRouteUtils.checkPointInBlocks(point.getX(), point.getY(), allCells))) {\r
-                point.setX(point.getX() - XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
-            }\r
-            break;\r
-        case SOUTH:\r
-            point.setY(point.getY() + distance);\r
-            while (Math.abs(point.getY() - portY) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
-                    && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
+                    point.setX(point.getX() - XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
+                }\r
+                break;\r
+            case SOUTH:\r
+                point.setY(point.getY() + distance);\r
+                while (Math.abs(point.getY() - portY) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
+                        && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
                             || XcosRouteUtils.checkPointInBlocks(point.getX(), point.getY(), allCells))) {\r
-                point.setY(point.getY() - XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
-            }\r
-            break;\r
-        case WEST:\r
-            point.setX(point.getX() - distance);\r
-            while (Math.abs(point.getX() - portX) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
-                    && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
+                    point.setY(point.getY() - XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
+                }\r
+                break;\r
+            case WEST:\r
+                point.setX(point.getX() - distance);\r
+                while (Math.abs(point.getX() - portX) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
+                        && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
                             || XcosRouteUtils.checkPointInBlocks(point.getX(), point.getY(), allCells))) {\r
-                point.setX(point.getX() + XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
-            }\r
-            break;\r
-        case NORTH:\r
-            point.setY(point.getY() - distance);\r
-            while (Math.abs(point.getY() - portY) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
-                    && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
+                    point.setX(point.getX() + XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
+                }\r
+                break;\r
+            case NORTH:\r
+                point.setY(point.getY() - distance);\r
+                while (Math.abs(point.getY() - portY) > XcosRouteUtils.BEAUTY_AWAY_REVISION\r
+                        && (XcosRouteUtils.checkObstacle(portX, portY, point.getX(), point.getY(), allCells)\r
                             || XcosRouteUtils.checkPointInBlocks(point.getX(), point.getY(), allCells))) {\r
-                point.setY(point.getY() + XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
-            }\r
-            break;\r
+                    point.setY(point.getY() + XcosRouteUtils.BEAUTY_AWAY_REVISION);\r
+                }\r
+                break;\r
         }\r
         return point;\r
     }\r
@@ -316,7 +311,7 @@ public class XcosRoute {
      * @return\r
      */\r
     private Orientation getNewOrientation(mxICell cell, double cx, double cy, mxICell otherCell, double ox,\r
-            double oy, XcosDiagram graph) {\r
+                                          double oy, XcosDiagram graph) {\r
         Orientation orientation = Orientation.EAST;\r
         if (cell.getParent() instanceof SplitBlock) {\r
             SplitBlock block = (SplitBlock) cell.getParent();\r