@@ -64,7 +64,9 @@ public class Svg2Xml
6464 private XmlConfig destConfigDoc = null ;
6565
6666 //user identifier, mxGraph is default, but every user should come up with his own identifier, to avoid conflicts with mxGraph standard stencils and shapes
67- private final String stencilUserMarker = "mxGraph" ;
67+ private final String stencilUserMarker = "mxgraph" ;
68+
69+ private String sourceFolder = "" ;
6870
6971 public Svg2Xml (Svg2XmlGui gui )
7072 {
@@ -131,15 +133,48 @@ public void convertToXml(File[] sourceFiles, File destPath) {
131133 boolean isLastInGroup = true ;
132134 boolean isNewGroup = true ;
133135 String groupXml = new String ();
134- ByteArrayOutputStream groupBaos = new ByteArrayOutputStream ();
136+ ByteArrayOutputStream groupBaos = new ByteArrayOutputStream ();String groupName = stencilUserMarker ;
137+
138+
139+ // =============================================================================================================
140+ // NOTE: commented from upstream/master - in this repository, we don't have GUI code in the processing code!
141+ // =============================================================================================================
142+ // boolean areFiles = false;
143+
144+ //checks if files are selected too, or only folders
145+ // for (int i = 0; i < gui.sourceFileListComponent.getSelectedFiles().length; i++)
146+ // {
147+ // if (gui.sourceFileListComponent.getSelectedFiles()[i].isFile())
148+ // {
149+ // areFiles = true;
150+ // }
151+ // }
152+
153+ //if files are selected too, the parent folder is the root of the group naming
154+ // if (areFiles)
155+ // {
156+ // sourceFolder = gui.sourceFileListComponent.getSelectedFiles()[0].getParentFile().getParent();
157+ // }
158+ // else
159+ // {
160+ // sourceFolder = gui.sourceFileListComponent.getSelectedFiles()[0].getParent();
161+ // }
162+ // =============================================================================================================
163+ // END OF "NOTE"
164+ // =============================================================================================================
165+
166+ boolean isGroupNameInConfig = false ;
167+ String lastGroupName = "" ;
135168
136169 // construct destConfigDoc based on default values, groupConfigDoc and stencilConfigDoc
137170 for (int i = 0 ; i < sourceFiles .length ; i ++)
138171 {
139172 System .out .println ("Processing " + sourceFiles [i ].getAbsolutePath ());
140173 groupBaos = new ByteArrayOutputStream ();
141174 isLastInGroup = false ;
142- isNewGroup = false ;
175+ isNewGroup = true ;
176+
177+ groupName = stencilUserMarker ;
143178
144179 String shapeName = sourceFiles [i ].getName ();
145180 shapeName = shapeName .substring (0 , shapeName .lastIndexOf ("." ));
@@ -208,6 +243,11 @@ public void convertToXml(File[] sourceFiles, File destPath) {
208243 destConfigDoc .setAspect (aspectType .VARIABLE );
209244 }
210245
246+ if (groupConfigRootElement .getAttribute ("groupname" ) != null && groupConfigRootElement .getAttribute ("groupname" ) != "" )
247+ {
248+ groupName = groupConfigRootElement .getAttribute ("groupname" );
249+ isGroupNameInConfig = true ;
250+ }
211251 //TODO implement strokewidth reading
212252 // strokeWidth = groupConfigRootElement.getAttribute("strokewidth");
213253 groupConnection = getConstraintsFromXml (groupConfigString );
@@ -338,11 +378,11 @@ else if (aspectRatio.toLowerCase().equals("variable"))
338378 Constraint currConstraint = constraints .get (j );
339379
340380 double x = currConstraint .getX () - bounds .getMinX ();
341- x = Math .round (x * 100 .0 / bounds .getWidth ()) / 100 .0 ;
381+ x = Math .round (x * 1000 .0 / bounds .getWidth ()) / 1000 .0 ;
342382 currConstraint .setX (x );
343383
344384 double y = currConstraint .getY () - bounds .getMinY ();
345- y = Math .round (y * 100 .0 / bounds .getHeight ()) / 100 .0 ;
385+ y = Math .round (y * 1000 .0 / bounds .getHeight ()) / 1000 .0 ;
346386 currConstraint .setY (y );
347387 }
348388
@@ -543,7 +583,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))
543583 String currParent = sourceFiles [i ].getParent ();
544584 String oldParent = sourceFiles [i -1 ].getParent ();
545585
546- if (currParent .equals (oldParent ))
586+ if (currParent .equals (oldParent ) && i != 0 )
547587 {
548588 isNewGroup = false ;
549589 }
@@ -553,7 +593,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))
553593 }
554594 }
555595
556- //check if this is the last file in the group
596+ //if this is the last file in the group
557597 if (i + 1 == sourceFiles .length )
558598 {
559599 isLastInGroup = true ;
@@ -573,36 +613,33 @@ else if (aspectRatio.toLowerCase().equals("variable"))
573613 }
574614 }
575615
576- // here we need some group naming check
616+ // group naming check
577617 String currentPath = sourceFiles [i ].getAbsolutePath ();
578618 currentPath = currentPath .substring (2 , currentPath .lastIndexOf ("." ));
579619
620+ File currFile = new File (sourceFiles [i ].getAbsolutePath ());
621+
580622 if (isNewGroup )
581623 {
582- // if new group then we save the old file and open a new one
583- String groupName = stencilUserMarker ;
584- File currFile = new File (sourceFiles [i ].getAbsolutePath ());
585- ArrayList <String > folders = new ArrayList <String >();
586-
587- while (!currFile .getParentFile ().getName ().equals ("svgroot" ) && currFile .getParent ().length () > 4 )
624+ //if group name wasn't in config, generate it based on folder structure
625+ if (!isGroupNameInConfig )
588626 {
589- currFile = currFile .getParentFile ();
590- folders . add ( 0 , currFile . getName () );
591- }
627+ String fullStr = currFile .getParent (). toLowerCase ();
628+ String currName = fullStr . replace ( sourceFolder . toLowerCase (), "" );
629+ currName = currName . replace ( File . separator , "." );
592630
593- for (int j = 0 ; j < folders .size (); j ++)
594- {
595- groupName += "." + folders .get (j );
631+ groupName = stencilUserMarker + currName ;
596632 }
597633
634+ lastGroupName = groupName ;
598635 groupXml = "<shapes name=\" " + groupName + "\" >" + System .getProperty ("line.separator" );
599636 String tmp = Svg2Xml .printDocumentString (destDoc , groupBaos );
600637 tmp = tmp .replaceAll ("\\ .0\" " , "\" " );
601638 groupXml += tmp ;
602639 }
603640 else
604641 {
605- // if not a new group then we just add the xml to the group xml
642+ // if not a new group, just add the xml to the group xml
606643 groupXml += Svg2Xml .printDocumentString (destDoc , groupBaos );
607644 }
608645
@@ -615,8 +652,8 @@ else if (aspectRatio.toLowerCase().equals("variable"))
615652
616653 try
617654 {
618- String currentDestPath = destPath .getAbsolutePath ();
619- currentDestPath += sourceFiles [ i ]. getParent (). substring ( 2 , sourceFiles [ i ]. getParent (). length ()) + ".xml" ;
655+ String currentDestPath = destPath .getAbsolutePath () + File . separator + lastGroupName . replace ( "." , File . separator ) + ".xml" ;
656+
620657 currentDestPath = currentDestPath .toLowerCase ();
621658 currentDestPath = currentDestPath .replaceAll ("\\ s" , "_" );
622659 File myDestFile = new File (currentDestPath );
@@ -630,7 +667,6 @@ else if (aspectRatio.toLowerCase().equals("variable"))
630667 writer .close ();
631668 System .out .println ("File written" );
632669
633- // JOptionPane.showMessageDialog(gui.getFrame(), "Conversion completed.\nThe library is genereted in:\n" + myDestRoot);
634670 if (!destPaths .contains (myDestRoot ))
635671 {
636672 destPaths .add (myDestRoot );
@@ -855,7 +891,39 @@ else if(!styleDiff.get("fillcolor").equals("none"))
855891 if (!fontSizeStr .equals ("" ))
856892 {
857893 fontSizeStr = removeUnits (fontSizeStr );
858- double fs = Double .parseDouble (fontSizeStr ) * configDoc .getRelativeScalingRatio ();
894+ double fs = 16 ;
895+
896+ if (isNumeric (fontSizeStr ))
897+ {
898+ fs = Double .parseDouble (fontSizeStr ) * configDoc .getRelativeScalingRatio ();
899+ }
900+ else
901+ {
902+ switch (fontSizeStr )
903+ {
904+ case "xx-small" :
905+ fs = 9 ;
906+ break ;
907+ case "x-small" :
908+ fs = 10 ;
909+ break ;
910+ case "small" :
911+ fs = 13 ;
912+ break ;
913+ case "medium" :
914+ fs = 16 ;
915+ break ;
916+ case "large" :
917+ fs = 18 ;
918+ break ;
919+ case "x-large" :
920+ fs = 24 ;
921+ break ;
922+ case "xx-large" :
923+ fs = 32 ;
924+ }
925+
926+ }
859927 el .setAttribute ("size" , Double .toString (fs ));
860928 node .appendChild (el );
861929 }
@@ -2224,8 +2292,16 @@ private static void createBackbone(Document destDoc, XmlConfig destConfigDoc, St
22242292
22252293 if (destConfigDoc .isCalculateBorder ())
22262294 {
2227- double w = destConfigDoc .getStencilBoundsX () * s ;
2228- double h = destConfigDoc .getStencilBoundsY () * s ;
2295+ double w = Math .round (destConfigDoc .getStencilBoundsX () * s * 1000.0 ) / 1000.0 ;
2296+ double h = Math .round (destConfigDoc .getStencilBoundsY () * s * 1000.0 ) / 1000.0 ;
2297+
2298+ if (!destConfigDoc .isRelativeScaling ())
2299+ {
2300+ double sc = Math .min (destConfigDoc .getAbsoluteScalingX () / destConfigDoc .getStencilBoundsX (), destConfigDoc .getAbsoluteScalingY () / destConfigDoc .getStencilBoundsY ());
2301+ w = Math .round (destConfigDoc .getStencilBoundsX () * sc * 1000.0 ) / 1000.0 ;
2302+ h = Math .round (destConfigDoc .getStencilBoundsY () * sc * 1000.0 ) / 1000.0 ;
2303+ }
2304+
22292305 root .setAttribute ("w" , String .valueOf (w ));
22302306 root .setAttribute ("h" , String .valueOf (h ));
22312307 }
@@ -2556,5 +2632,9 @@ private Document removeConnections(Document svgDoc)
25562632
25572633 return svgDoc ;
25582634 }
2635+
2636+ public static boolean isNumeric (String str ) {
2637+ return str .matches ("-?\\ d+(\\ .\\ d+)?" ); //match a number with optional '-' and decimal.
2638+ }
25592639};
25602640
0 commit comments