1818package com .khartec .waltz .service .svg ;
1919
2020import com .khartec .waltz .data .svg .SvgDiagramDao ;
21+ import com .khartec .waltz .model .svg .ImmutableSvgDiagram ;
2122import com .khartec .waltz .model .svg .SvgDiagram ;
23+ import org .jooq .lambda .Unchecked ;
2224import org .springframework .beans .factory .annotation .Autowired ;
2325import org .springframework .stereotype .Service ;
26+ import org .w3c .dom .Document ;
27+ import org .w3c .dom .Element ;
28+ import org .w3c .dom .NodeList ;
29+ import org .xml .sax .InputSource ;
30+ import org .xml .sax .SAXException ;
2431
32+ import javax .xml .parsers .DocumentBuilder ;
33+ import javax .xml .parsers .ParserConfigurationException ;
34+ import javax .xml .transform .TransformerException ;
35+ import javax .xml .xpath .XPath ;
36+ import javax .xml .xpath .XPathConstants ;
37+ import javax .xml .xpath .XPathExpressionException ;
38+ import javax .xml .xpath .XPathFactory ;
39+ import java .io .ByteArrayInputStream ;
40+ import java .io .IOException ;
2541import java .util .List ;
42+ import java .util .stream .Collectors ;
43+
44+ import static com .khartec .waltz .common .XmlUtilities .createNonValidatingDocumentBuilderFactory ;
45+ import static com .khartec .waltz .common .XmlUtilities .printDocument ;
46+ import static com .khartec .waltz .common .XmlUtilities .stream ;
47+ import static java .util .stream .Collectors .toList ;
2648
2749@ Service
2850public class SvgDiagramService {
@@ -34,7 +56,52 @@ public SvgDiagramService(SvgDiagramDao svgDiagramDao) {
3456 this .svgDiagramDao = svgDiagramDao ;
3557 }
3658
59+
60+ private String convertProductSpecificSvg (SvgDiagram diagram ) throws ParserConfigurationException , IOException , SAXException , XPathExpressionException , TransformerException {
61+ if (diagram .product ().equals ("visio" )) {
62+ return convertVisoSvg (diagram );
63+ } else {
64+ return diagram .svg ();
65+ }
66+ }
67+
68+
69+ private String convertVisoSvg (SvgDiagram diagram ) throws ParserConfigurationException , IOException , SAXException , XPathExpressionException , TransformerException {
70+ DocumentBuilder builder = createNonValidatingDocumentBuilderFactory ().newDocumentBuilder ();
71+ InputSource svgSource = new InputSource (new ByteArrayInputStream (diagram .svg ().getBytes ()));
72+ Document svg = builder .parse (svgSource );
73+
74+ XPath xpath = XPathFactory .newInstance ().newXPath ();
75+ NodeList nodes = (NodeList ) xpath .evaluate ("//*" , svg , XPathConstants .NODESET );
76+
77+ String key = diagram .keyProperty ();
78+
79+ stream (nodes )
80+ .forEach (n -> stream (n .getChildNodes ())
81+ .filter (c -> c .getNodeName ().contains ("custProps" ))
82+ .forEach (c -> stream (c .getChildNodes ())
83+ .filter (cp -> cp .getNodeName ().contains ("cp" ))
84+ .map (cp -> (Element ) cp )
85+ .filter (cp -> key .equals (cp .getAttribute ("v:lbl" )))
86+ .map (cp -> cp .getAttribute ("v:val" ))
87+ .map (v -> v .replaceAll ("^.*\\ ((.*)\\ )$" , "$1" ))
88+ .forEach (v -> ((Element ) n ).setAttribute ("data-" +key , v ))
89+ )
90+ );
91+
92+ return printDocument (svg );
93+ }
94+
95+
3796 public List <SvgDiagram > findByKind (String kind ) {
38- return svgDiagramDao .findByKind (kind );
97+ return svgDiagramDao .findByKind (kind )
98+ .stream ()
99+ .map (Unchecked .function (diag -> {
100+ String updatedSvg = convertProductSpecificSvg (diag );
101+ return ImmutableSvgDiagram .copyOf (diag )
102+ .withSvg (updatedSvg );
103+ }))
104+ .collect (toList ());
39105 }
106+
40107}
0 commit comments