Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/org/xembly/Transformers.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
public Compact() {
this.original = new Transformers.Formatted(
new Transformers.Default(),
Collections.singletonMap(OutputKeys.ENCODING, "UTF-8")

Check failure on line 77 in src/main/java/org/xembly/Transformers.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "UTF-8" 3 times.

See more on https://sonarcloud.io/project/issues?id=yegor256_xembly&issues=AZ7dS1f6L49KgC-o0hdy&open=AZ7dS1f6L49KgC-o0hdy&pullRequest=321
);
}

Expand All @@ -84,6 +84,58 @@
}
}

/**
* Transformer factory that emits an explicit {@code standalone}
* attribute in the XML declaration.
* Pass {@code true} to emit {@code standalone="yes"} or {@code false} to
* emit {@code standalone="no"}. Indentation and UTF-8 encoding are kept
* to match {@link Transformers.Document}.
* @since 0.33
*/
final class Standalone implements Transformers {

/**
* Original transformer factory.
*/
private final Transformers original;

/**
* Public ctor.
* @param standalone Whether the document is standalone
* @since 0.33
*/
public Standalone(final boolean standalone) {
this.original = new Transformers.Formatted(
new Transformers.Default(),
Transformers.Standalone.props(standalone)
);
}

@Override
public Transformer create() {
return this.original.create();
}

/**
* Build output properties for the given standalone value.
* @param standalone Whether the document is standalone
* @return Properties to configure the output
*/
private static Map<String, String> props(final boolean standalone) {
final Map<String, String> res = new HashMap<>();
res.put(OutputKeys.INDENT, "yes");
res.put(OutputKeys.ENCODING, "UTF-8");
final String value;
if (standalone) {
value = "yes";
} else {
value = "no";
}
res.put(OutputKeys.STANDALONE, value);
return res;
}
}

/**
* Transformer factory that produces document transformers.
* All transformers produced by this factory will be configured to produce
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/xembly/TransformersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ void omitsDeclarationWithNode() throws Exception {
Matchers.not(Matchers.containsString("<?xml"))
);
}

@Test
void rendersStandaloneNoInDeclaration() throws Exception {
MatcherAssert.assertThat(
"Standalone(false) must emit standalone=\"no\"",
new Xembler(
new Directives().add("page"),
new Transformers.Standalone(false)
).xml(),
Matchers.containsString("standalone=\"no\"")
);
}

@Test
void rendersStandaloneYesInDeclaration() throws Exception {
MatcherAssert.assertThat(
"Standalone(true) must emit standalone=\"yes\"",
new Xembler(
new Directives().add("page"),
new Transformers.Standalone(true)
).xml(),
Matchers.containsString("standalone=\"yes\"")
);
}
}
Loading