Skip to content

Commit e6cee85

Browse files
committed
fix test
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent a46a07e commit e6cee85

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,16 @@ Processors can be registered to futher customize what goes in and out of the gra
372372

373373
[//]: # (processor)
374374
```java
375-
Graphviz g = Graphviz.fromGraph(graph().with(node("bad word").link("god word")))
376-
.preProcessor((source, options, processOptions) -> source.replace("bad word", "unicorn"));
375+
Graph graph = graph().with(node("bad word").link("god word"));
376+
Graphviz g = Graphviz.fromGraph(graph)
377+
.preProcessor((source, options, processOptions) -> source.replace("bad word", "unicorn"))
378+
.postProcessor(((result, options, processOptions) ->
379+
result.mapString(svg ->
380+
SvgElementFinder.use(svg, finder -> {
381+
finder.findNode("unicorn").setAttribute("class", "red");
382+
})
383+
)
384+
));
377385
g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex9.png"));
378386
```
379387
[//]: # (end)

graphviz-java/example/ex9.png

179 Bytes
Loading

graphviz-java/src/main/java/guru/nidi/graphviz/model/SvgElementFinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SvgElementFinder {
3939
private static final XPathExpression EXPR_G = pathExpression(X_PATH, "//g");
4040
private static final XPathExpression EXPR_TITLE = pathExpression(X_PATH, "//title[text()=$var]");
4141
private static final XPathExpression EXPR_TITLE_OR = pathExpression(X_PATH, "//title[text()=$var or text()=$alt]");
42+
private final boolean hasHeader;
4243
private final Document doc;
4344

4445
public static String use(String svg, Consumer<SvgElementFinder> actions) {
@@ -50,6 +51,7 @@ public static String use(String svg, Consumer<SvgElementFinder> actions) {
5051
public SvgElementFinder(String svg) {
5152
try {
5253
doc = builder().parse(new InputSource(new StringReader(svg)));
54+
hasHeader = svg.startsWith("<?xml");
5355
} catch (SAXException | IOException e) {
5456
throw new AssertionError("Could not read SVG", e);
5557
}
@@ -59,7 +61,8 @@ public String getSvg() {
5961
final StringWriter sw = new StringWriter();
6062
try {
6163
TRANSFORMER_FACTORY.newTransformer().transform(new DOMSource(doc), new StreamResult(sw));
62-
return sw.toString();
64+
final String out = sw.toString().replace("xmlns=\"\"", ""); //rasterizer don't like empty xmlns !?
65+
return hasHeader ? out : out.substring(out.indexOf("?>") + 2);
6366
} catch (TransformerException e) {
6467
throw new AssertionError("Could not generate string from DOM", e);
6568
}

graphviz-java/src/test/java/guru/nidi/graphviz/model/ReadmeTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,16 @@ void ex8() throws IOException {
191191
@Test
192192
void ex9() throws IOException {
193193
//## processor
194-
Graphviz g = Graphviz.fromGraph(graph().with(node("bad word").link("god word")))
195-
.preProcessor((source, options, processOptions) -> source.replace("bad word", "unicorn"));
194+
Graph graph = graph().with(node("bad word").link("good word"));
195+
Graphviz g = Graphviz.fromGraph(graph)
196+
.preProcessor((source, options, processOptions) -> source.replace("bad word", "unicorn"))
197+
.postProcessor(((result, options, processOptions) ->
198+
result.mapString(svg ->
199+
SvgElementFinder.use(svg, finder -> {
200+
finder.findNode("unicorn").setAttribute("class", "pink");
201+
})
202+
)
203+
));
196204
g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex9.png"));
197205
//## image
198206
}

graphviz-java/src/test/java/guru/nidi/graphviz/model/SvgElementFinderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import guru.nidi.graphviz.attribute.Label;
1919
import guru.nidi.graphviz.engine.Graphviz;
20-
import guru.nidi.graphviz.model.*;
2120
import org.junit.jupiter.api.Test;
2221

2322
import static guru.nidi.graphviz.engine.Format.SVG;

0 commit comments

Comments
 (0)