Skip to content

Commit

Permalink
feat(#64): Add Output abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 15, 2023
1 parent a8a15dc commit 0e18e55
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
48 changes: 48 additions & 0 deletions src/main/java/org/xembly/Output.java
@@ -0,0 +1,48 @@
package org.xembly;

import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;

public interface Output {


void prepareTransformer(final Transformer transformer);


final class Document implements Output {

private final Map<String, String> properties;

public Document() {
this((Document.defaultProperties()));
}

public Document(final Map<String, String> properties) {
this.properties = properties;
}

@Override
public void prepareTransformer(final Transformer transformer) {
this.properties.entrySet().stream()
.forEach((e) -> transformer.setOutputProperty(e.getKey(), e.getValue()));
}

private static Map<String, String> defaultProperties() {
final HashMap<String, String> res = new HashMap<>();
res.put(OutputKeys.INDENT, "yes");
res.put(OutputKeys.ENCODING, "UTF-8");
return res;
}
}

final class Node implements Output {

@Override
public void prepareTransformer(final Transformer transformer) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}
}

}
19 changes: 16 additions & 3 deletions src/main/java/org/xembly/Xembler.java
Expand Up @@ -101,6 +101,8 @@ public final class Xembler {
*/
private final Iterable<Directive> directives;

private final Output output;

static {
Xembler.BFACTORY.setNamespaceAware(true);
Xembler.BFACTORY.setValidating(false);
Expand All @@ -112,7 +114,17 @@ public final class Xembler {
* @param dirs Directives
*/
public Xembler(final Iterable<Directive> dirs) {
this.directives = dirs;
this(dirs, new Output.Document());
}

/**
* Public ctor.
* @param directives Directives
* @param output Output type
*/
public Xembler(final Iterable<Directive> directives, final Output output) {
this.directives = directives;
this.output = output;
}

/**
Expand Down Expand Up @@ -247,8 +259,9 @@ public String xml() throws ImpossibleModificationException {
ex
);
}
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
this.output.prepareTransformer(transformer);
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
final StringWriter writer = new StringWriter();
try {
transformer.transform(
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/xembly/XemblerTest.java
Expand Up @@ -208,4 +208,19 @@ void concurrentInvocationWithNoExceptions() throws Exception {
)
);
}

@Test
void omitsHeader() {
MatcherAssert.assertThat(
"Xembler should omit XML header for Node output type",
new Xembler(
new Directives()
.add("animals")
.add("cow")
.set("mu-mu"),
new Output.Node()
).xmlQuietly(),
Matchers.equalTo("<animals><cow>mu-mu</cow></animals>")
);
}
}

0 comments on commit 0e18e55

Please sign in to comment.