1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.importexport.outputter;
35
36 import java.io.IOException;
37 import java.io.Writer;
38 import java.util.List;
39
40 import org.apache.commons.lang3.StringUtils;
41 import org.apache.jackrabbit.util.ISO9075;
42 import org.jdom.Attribute;
43 import org.jdom.Element;
44 import org.jdom.output.Format;
45
46
47
48
49 public class YamlOutputter extends DocumentViewOutputter {
50
51 public YamlOutputter() {
52 setSeparatorChars("\n");
53 setApostropheChars("");
54 setAsignmentChars(": ");
55
56 final Format format = super.getFormat();
57 format.setLineSeparator("\n");
58 setFormat(format);
59 }
60
61
62 @Override
63 protected void printElement(Writer out, Element element, int level, NamespaceStack namespaces) throws IOException {
64 setCurrentLevel(level);
65 indent(out, level);
66 List attributes = element.getAttributes();
67 if (StringUtils.isNotEmpty(element.getNamespace().getPrefix())) {
68 out.write(element.getNamespace().getPrefix());
69 out.write(":");
70 }
71 final String name = decode(element.getName());
72 out.write(name);
73 out.write(": ");
74 if (attributes != null) {
75 printAttributes(out, attributes, element, null);
76 }
77 newline(out);
78 for (Object childElement : element.getChildren()) {
79 printElement(out, (Element) childElement, level + 1, namespaces);
80 }
81 }
82
83 @Override
84 protected void printName(Writer out, Attribute a) throws IOException {
85 out.write(decode(a.getName()));
86 }
87
88 @Override
89 public String escapeAttributeEntities(String str) {
90 return str;
91 }
92
93 private String decode(String input) {
94 return ISO9075.decode(input);
95 }
96 }