]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java
An attempt to escape generated SVG content.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLScenegraph.java
index 82115ef0e0825e2c9b8b2c69793f6bffd015344b..559469d4a21ce908aff65f9574355363a989d2f3 100644 (file)
@@ -723,7 +723,11 @@ public class SCLScenegraph {
                result.append(ALL_SECTIONS, "</g>");
 
                StringBuilder res = new StringBuilder();
-               res.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\">");
+               if (width > 0 && height > 0 ) {
+                       res.append("<svg width=\"" + width +"px\" height=\""+height+"px\" stroke=\"black\" xmlns=\"http://www.w3.org/2000/svg\">");
+               } else {
+                       res.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\" xmlns=\"http://www.w3.org/2000/svg\">");
+               }
                res.append("<g transform=\"matrix(").append(matrix[0]).append(",").append(matrix[1]).append(",").append(matrix[2]).append(",").append(matrix[3]).append(",").append(matrix[4]).append(",").append(matrix[5]).append(")\">");
                res.append(result.get(MAIN_SECTION));
                res.append(result.get(SELECTION_SECTION));
@@ -784,15 +788,39 @@ public class SCLScenegraph {
         private String getKey(SingleElementNode node) {
             String key;
                if(node.getKey() != null) {
-                if (mappings.containsKey(node.getKey()))
+                if (mappings.containsKey(node.getKey())) {
                     key = mappings.get(node.getKey()).toString();
-                else
+                    key = escape(key);
+                } else {
                     key = node.getKey().toString();
+                    key = escape(key);
+                }
                } else {
                        key = Long.toString(node.getId());
                }
             return key;
         }
+        
+        private String escape(String key) {
+               // Keys may contain  '<' '>' characters, which causes errors in browser SVG handling.
+               return org.apache.commons.lang.StringEscapeUtils.escapeHtml(key);
+        }
+        
+        private String removeElem(String xml, String elemStart) {
+               // FIXME: This is rather nasty and error prone way of removing elements from XML string.
+               // This only supports elements with /> end element tag. Elements ends with </elem name> are not supported!
+               int start = xml.indexOf("<"+elemStart);
+               if (start>=0) {
+                       int end = xml.indexOf(">",start);
+                       if (end >= 0) {
+                               if (start > 0)
+                                       return xml.substring(0,start)+xml.substring(end+1);
+                               else
+                                       return xml.substring(end+1);
+                       }
+               }
+               return null;
+        }
 
         @Override
         public void enter(IG2DNode node) {
@@ -887,7 +915,21 @@ public class SCLScenegraph {
                          parentBuilder.append(MAIN_SECTION, "\n<g transform=\"" + m + "\">");
                      }
                 }
-                parentBuilder.append(MAIN_SECTION, svg.getSVGText());
+                String svgContent = svg.getSVGText();
+                // SVGNode content may contain SVG/XML elements, which break browser compatibility
+                int start = svgContent.indexOf("<svg");
+                if (start >= 0)
+                       svgContent = svgContent.substring(start);
+                else  {
+                       String s = removeElem(svgContent, "?xml");
+                       if (s != null) {
+                               svgContent = "<g>"+s+"</g>";
+                               s = removeElem(svgContent, "!DOCTYPE");
+                               if (s != null)
+                                       svgContent = s;
+                       }
+                }
+                parentBuilder.append(MAIN_SECTION, svgContent);
                 if (!at.isIdentity()) {
                        parentBuilder.append(MAIN_SECTION, "\n</g>");
                 }