Skip to content

Commit

Permalink
Merge pull request #78 from davidwatkins73/master
Browse files Browse the repository at this point in the history
Another attempt at getting visio to work
  • Loading branch information
davidwatkins73 committed Feb 17, 2016
2 parents e0e4645 + a3ded48 commit 1edda6f
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 62 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jool</artifactId>
<version>0.9.10</version>
</dependency>



<!-- PROVIDED -->

Expand Down
6 changes: 6 additions & 0 deletions waltz-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<artifactId>pcollections</artifactId>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jool</artifactId>
</dependency>


<!-- LOGGING -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.khartec.waltz.common;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.stream.Stream;

public class XmlUtilities {

public static Stream<Node> stream(NodeList nodeList) {
Node[] nodes = new Node[nodeList.getLength()];

for (int i = 0; i < nodeList.getLength(); i++) {
nodes[i] = nodeList.item(i);
}
return Stream.of(nodes);
}


public static String printDocument(Document doc) throws IOException, TransformerException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
printDocument(doc, baos);
return new String(baos.toByteArray());
}


public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

transformer.transform(new DOMSource(doc),
new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

public static DocumentBuilderFactory createNonValidatingDocumentBuilderFactory() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

return factory;
}
}
6 changes: 3 additions & 3 deletions waltz-ng/client/capabilities/list-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function controller(capabilities, appCapabilityStore, svgStore, $state) {

svgStore.findByKind('CAPABILITY').then(xs => vm.diagrams = xs);

vm.blockProcessor = block => {
block.parent.onclick = () => $state.go('main.capabilities.view', { id: block.value });
angular.element(block.parent).addClass('clickable');
vm.blockProcessor = b => {
b.block.onclick = () => $state.go('main.capabilities.view', { id: b.value });
angular.element(b.block).addClass('clickable');
};
}

Expand Down
6 changes: 3 additions & 3 deletions waltz-ng/client/org-units/list-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function controller(orgUnits, tallies, svgStore, $state) {

svgStore.findByKind('ORG_UNIT').then(xs => vm.diagrams = xs);

vm.blockProcessor = block => {
block.parent.onclick = () => $state.go('main.org-units.unit', { id: block.value });
angular.element(block.parent).addClass('clickable');
vm.blockProcessor = b => {
b.block.onclick = () => $state.go('main.org-units.unit', { id: b.value });
angular.element(b.block).addClass('clickable');
};
}

Expand Down
68 changes: 15 additions & 53 deletions waltz-ng/client/svg-diagram/directives/svg-diagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,28 @@ function resize(elem) {


function controller($scope, $window) {
const blockParsers = {
'draw.io': (diagram, svg) => {
const anchors = svg.find('a');
return _.chain(anchors)
.map(anchor => {
const attr = anchor.attributes['xlink:href'];
if (! attr) return null;
const empId = attr.value;
anchor.removeAttribute('xlink:href');
return {
rawProperty: empId,
parent: anchor,
value: empId,
name: 'xlink:href'
};
})
.compact()
.value();
},
'visio': (diagram, svg) => {
$window.setTimeout(() => resize(svg), 100);

const custProps = svg.find('v:cp');

// get rid of auto generated svg titles
_.each(svg.find('title'), t => t.remove());

return _.chain(custProps)
.filter(cp => cp
&& cp.attributes
&& cp.attributes['v:lbl']
&& cp.attributes['v:lbl'].value == diagram.keyProperty)
.map(cp => {
const valAttr = cp.attributes['v:val'];
const value = valAttr
? valAttr.value.replace(/.*\((.*)\).*/, '$1')
: '';

return {
rawProperty: cp,
parent: cp.parentNode.parentNode,
value,
name: cp.attributes['v:lbl'].value
};
})
.value();
}
};

const vm = this;

angular.element($window)
.on('resize', () => resize($scope.elem));


$scope.$watch('ctrl.diagram', f => {
if (!f) return;
$scope.$watch('ctrl.diagram', diagram => {
if (!diagram) return;

const svg = $scope.elem.append(diagram.svg);

if (diagram.product === 'visio') {
$window.setTimeout(() => resize(svg), 100);
}

const dataProp = 'data-' + diagram.keyProperty;
const dataBlocks = svg.querySelectorAll('[' + dataProp + ']');

const svg = $scope.elem.append(vm.diagram.svg);
const blocks = blockParsers[vm.diagram.product](vm.diagram, svg);
const blocks = _.map(dataBlocks, b => ({
block: b,
value: b.attributes[dataProp].value
}));

_.each(blocks, vm.blockProcessor);
});
Expand Down
4 changes: 2 additions & 2 deletions waltz-ng/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"start": "node lib/server-production"
},
"dependencies": {
"angular": "^1.4.4",
"angular-animate": "^1.4.8",
"angular": "1.4.7",
"angular-animate": "1.4.7",
"angular-formly": "^6.24.23",
"angular-formly-templates-bootstrap": "^6.0.0",
"angular-local-storage": "^0.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,33 @@
package com.khartec.waltz.service.svg;

import com.khartec.waltz.data.svg.SvgDiagramDao;
import com.khartec.waltz.model.svg.ImmutableSvgDiagram;
import com.khartec.waltz.model.svg.SvgDiagram;
import org.jooq.lambda.Unchecked;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static com.khartec.waltz.common.XmlUtilities.createNonValidatingDocumentBuilderFactory;
import static com.khartec.waltz.common.XmlUtilities.printDocument;
import static com.khartec.waltz.common.XmlUtilities.stream;
import static java.util.stream.Collectors.toList;

@Service
public class SvgDiagramService {
Expand All @@ -34,7 +56,52 @@ public SvgDiagramService(SvgDiagramDao svgDiagramDao) {
this.svgDiagramDao = svgDiagramDao;
}


private String convertProductSpecificSvg(SvgDiagram diagram) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException, TransformerException {
if (diagram.product().equals("visio")) {
return convertVisoSvg(diagram);
} else {
return diagram.svg();
}
}


private String convertVisoSvg(SvgDiagram diagram) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException, TransformerException {
DocumentBuilder builder = createNonValidatingDocumentBuilderFactory().newDocumentBuilder();
InputSource svgSource = new InputSource(new ByteArrayInputStream(diagram.svg().getBytes()));
Document svg = builder.parse(svgSource);

XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate("//*", svg, XPathConstants.NODESET);

String key = diagram.keyProperty();

stream(nodes)
.forEach(n -> stream(n.getChildNodes())
.filter(c -> c.getNodeName().contains("custProps"))
.forEach(c -> stream(c.getChildNodes())
.filter(cp -> cp.getNodeName().contains("cp"))
.map(cp -> (Element) cp)
.filter(cp -> key.equals(cp.getAttribute("v:lbl")))
.map(cp -> cp.getAttribute("v:val"))
.map(v -> v.replaceAll("^.*\\((.*)\\)$", "$1"))
.forEach(v -> ((Element) n).setAttribute("data-"+key, v))
)
);

return printDocument(svg);
}


public List<SvgDiagram> findByKind(String kind) {
return svgDiagramDao.findByKind(kind);
return svgDiagramDao.findByKind(kind)
.stream()
.map(Unchecked.function(diag -> {
String updatedSvg = convertProductSpecificSvg(diag);
return ImmutableSvgDiagram.copyOf(diag)
.withSvg(updatedSvg);
}))
.collect(toList());
}

}

0 comments on commit 1edda6f

Please sign in to comment.