Skip to content

Commit f14bd09

Browse files
committed
It is now possible to disable indentation for individual elements.
1 parent c06d61d commit f14bd09

File tree

8 files changed

+97
-6
lines changed

8 files changed

+97
-6
lines changed

src/main/java/org/simpleframework/xml/stream/Formatter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,20 @@ public void writeComment(String comment) throws Exception {
159159
* front of the tag, this is done for all but the first start tag.
160160
*
161161
* @param name this is the name of the start tag to be written
162+
* @param indent set to <code>false</code> if indentation should be omitted.
162163
*
163164
* @throws Exception thrown if there is an I/O exception
164165
*/
165-
public void writeStart(String name, String prefix) throws Exception{
166+
public void writeStart(String name, String prefix, boolean indent) throws Exception{
166167
String text = indenter.push();
167168

168169
if(last == Tag.START) {
169170
append('>');
170171
}
171172
flush();
172-
append(text);
173+
if (indent) {
174+
append(text);
175+
}
173176
append('<');
174177

175178
if(!isEmpty(prefix)) {
@@ -270,17 +273,18 @@ public void writeText(String text, Mode mode) throws Exception{
270273
* some text was written then a full end tag is written.
271274
*
272275
* @param name this is the name of the element to be closed
276+
* @param indent set to <code>false</code> if indentation should be omitted.
273277
*
274278
* @throws Exception thrown if there is an I/O exception
275279
*/
276-
public void writeEnd(String name, String prefix) throws Exception {
280+
public void writeEnd(String name, String prefix, boolean indent) throws Exception {
277281
String text = indenter.pop();
278282

279283
if(last == Tag.START) {
280284
write('/');
281285
write('>');
282286
} else {
283-
if(last != Tag.TEXT) {
287+
if(last != Tag.TEXT && indent) {
284288
write(text);
285289
}
286290
if(last != Tag.START) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.simpleframework.xml.stream;
2+
3+
public enum IndentationMode {
4+
ENABLED,
5+
DISABLED
6+
}

src/main/java/org/simpleframework/xml/stream/NodeWriter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,12 @@ private void writeName(OutputNode node) throws Exception {
276276
String prefix = node.getPrefix(verbose);
277277
String name = node.getName();
278278

279+
// Don't indent if it is disabled on the parent.
280+
boolean indent = node.getParent() == null || node.getParent()
281+
.getIndentationMode() == IndentationMode.ENABLED;
282+
279283
if(name != null) {
280-
writer.writeStart(name, prefix);
284+
writer.writeStart(name, prefix, indent);
281285
}
282286
}
283287

@@ -326,7 +330,7 @@ private void writeEnd(OutputNode node) throws Exception {
326330
writeValue(node);
327331
}
328332
if(name != null) {
329-
writer.writeEnd(name, prefix);
333+
writer.writeEnd(name, prefix, node.getIndentationMode() == IndentationMode.ENABLED);
330334
writer.flush();
331335
}
332336
}

src/main/java/org/simpleframework/xml/stream/OutputAttribute.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,11 @@ public boolean isCommitted() {
350350
public String toString() {
351351
return String.format("attribute %s='%s'", name, value);
352352
}
353+
354+
public IndentationMode getIndentationMode() {
355+
return null;
356+
}
357+
358+
public void setIndentationMode(IndentationMode mode) {
359+
}
353360
}

src/main/java/org/simpleframework/xml/stream/OutputDocument.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,11 @@ public void commit() throws Exception {
361361
public boolean isCommitted() {
362362
return stack.isEmpty();
363363
}
364+
365+
public IndentationMode getIndentationMode() {
366+
return null;
367+
}
368+
369+
public void setIndentationMode(IndentationMode mode) {
370+
}
364371
}

src/main/java/org/simpleframework/xml/stream/OutputElement.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class OutputElement implements OutputNode {
7474
*/
7575
private Mode mode;
7676

77+
/**
78+
* The {@link IndentationMode} is used to indicate if the serializer should apply indentation
79+
* when writing this node. If the mode is disabled, this node will not be indented. If the mode
80+
* is enabled, the indentation set in the {@link Format} will be applied.
81+
*/
82+
private IndentationMode indentationMode;
83+
7784
/**
7885
* Constructor for the <code>OutputElement</code> object. This is
7986
* used to create an output element that can create elements for
@@ -91,6 +98,7 @@ public OutputElement(OutputNode parent, NodeWriter writer, String name) {
9198
this.writer = writer;
9299
this.parent = parent;
93100
this.name = name;
101+
this.indentationMode = IndentationMode.ENABLED;
94102
}
95103

96104
/**
@@ -389,4 +397,12 @@ public boolean isCommitted() {
389397
public String toString() {
390398
return String.format("element %s", name);
391399
}
400+
401+
public IndentationMode getIndentationMode() {
402+
return indentationMode;
403+
}
404+
405+
public void setIndentationMode(IndentationMode mode) {
406+
this.indentationMode = mode;
407+
}
392408
}

src/main/java/org/simpleframework/xml/stream/OutputNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,19 @@ public interface OutputNode extends Node {
241241
* @return true if this node has already been committed
242242
*/
243243
boolean isCommitted();
244+
245+
/**
246+
* The {@link IndentationMode} is used to indicate if the serializer should apply indentation
247+
* when writing this node. If the mode is disabled, this node will not be indented. If the mode
248+
* is enabled, the indentation set in the {@link Format} will be applied. The default is to
249+
* enable indentation
250+
*/
251+
IndentationMode getIndentationMode();
252+
253+
/**
254+
* The {@link IndentationMode} is used to indicate if the serializer should apply indentation
255+
* when writing this node. If the mode is disabled, this node will not be indented. If the mode
256+
* is enabled, the indentation set in the {@link Format} will be applied.
257+
*/
258+
void setIndentationMode(IndentationMode mode);
244259
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.simpleframework.xml.stream;
2+
3+
import java.io.StringWriter;
4+
5+
import org.simpleframework.xml.ValidationTestCase;
6+
7+
/**
8+
* Indentation can be disabled on individual nodes.
9+
*/
10+
public class DisableIndentationTest extends ValidationTestCase {
11+
public static final String EXPECTED =
12+
"<root>\n"+
13+
" <a><b>B</b></a>\n" +
14+
"</root>";
15+
16+
public void testMixedContent() throws Exception {
17+
StringWriter out = new StringWriter();
18+
OutputNode root = NodeBuilder.write(out, new Format(2))
19+
.getChild("root");
20+
21+
OutputNode a = root.getChild("a");
22+
a.setIndentationMode(IndentationMode.DISABLED);
23+
24+
OutputNode b = a.getChild("b");
25+
b.setValue("B");
26+
27+
root.commit();
28+
validate(out.toString());
29+
30+
assertEquals(EXPECTED, out.toString());
31+
}
32+
}

0 commit comments

Comments
 (0)