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.ArrayList;
39 import java.util.List;
40
41 import org.jdom.Attribute;
42 import org.jdom.Element;
43 import org.jdom.Namespace;
44 import org.jdom.output.Format;
45 import org.jdom.output.XMLOutputter;
46
47
48
49
50 public class DocumentViewOutputter extends XMLOutputter {
51
52 private String separatorChars = "\n";
53 private String apostropheChars = "\"";
54 private String asignmentChars = "=";
55
56 private int currentLevel;
57
58 public DocumentViewOutputter() {
59 final Format format = Format.getPrettyFormat();
60 format.setTextMode(Format.TextMode.NORMALIZE);
61 format.setOmitDeclaration(true);
62 setFormat(format);
63 }
64
65 @Override
66 protected void printElement(Writer out, Element element, int level, NamespaceStack namespaces) throws IOException {
67 currentLevel = level;
68 super.printElement(out, element, level, namespaces);
69 }
70
71 @Override
72 protected void printAttributes(Writer out, List attributes, Element parent, NamespaceStack namespaces) throws IOException {
73 attributes = sortAttributes(attributes);
74 for (int i = 0; i < attributes.size(); i++) {
75 Attribute attribute = (Attribute) attributes.get(i);
76 Namespace ns = attribute.getNamespace();
77 if (ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
78 printNamespace(out, ns, namespaces);
79 }
80 attributeSeparator(out);
81 if (separatorChars.contains("\n")) {
82 indent(out, currentLevel + 1);
83 }
84 printQualifiedName(out, attribute);
85 out.write(asignmentChars);
86 out.write(apostropheChars);
87 out.write(escapeAttributeEntities(attribute.getValue()));
88 out.write(apostropheChars);
89 }
90 }
91
92 protected void attributeSeparator(Writer out) throws IOException {
93 out.write(separatorChars);
94 }
95
96 protected void indent(Writer out, int level) throws IOException {
97 if (getFormat().getIndent() == null || getFormat().getIndent().equals("")) {
98 return;
99 }
100 for (int i = 0; i < level; i++) {
101 out.write(getFormat().getIndent());
102 }
103 }
104
105 protected void newline(Writer out) throws IOException {
106 if (getFormat().getIndent() != null) {
107 out.write(getFormat().getLineSeparator());
108 }
109 }
110
111 protected void printQualifiedName(Writer out, Attribute a) throws IOException {
112 String prefix = a.getNamespace().getPrefix();
113 if ((prefix != null) && (!prefix.equals(""))) {
114 out.write(prefix);
115 out.write(':');
116 }
117 printName(out, a);
118 }
119
120 protected void printName(Writer out, Attribute a) throws IOException {
121 out.write(a.getName());
122 }
123
124 private void printNamespace(Writer out, Namespace ns, NamespaceStack namespaces) throws IOException {
125 if (namespaces == null) {
126 return;
127 }
128 String prefix = ns.getPrefix();
129 String uri = ns.getURI();
130
131
132 if (uri.equals(namespaces.getURI(prefix))) {
133 return;
134 }
135
136 out.write(" xmlns");
137 if (!prefix.equals("")) {
138 out.write(":");
139 out.write(prefix);
140 }
141 out.write("=\"");
142 out.write(escapeAttributeEntities(uri));
143 out.write("\"");
144 namespaces.push(ns);
145 }
146
147 protected List<Attribute> sortAttributes(List attributes) throws IOException {
148 List<Attribute> sortedAttributes = new ArrayList<>(attributes);
149 sortedAttributes.sort((o1, o2) -> o1.getQualifiedName().compareTo(o2.getQualifiedName()));
150 return sortedAttributes;
151 }
152
153 protected int getCurrentLevel() {
154 return currentLevel;
155 }
156
157 protected void setCurrentLevel(int currentLevel) {
158 this.currentLevel = currentLevel;
159 }
160
161 public String getSeparatorChars() {
162 return separatorChars;
163 }
164
165 public void setSeparatorChars(String separatorChars) {
166 this.separatorChars = separatorChars;
167 }
168
169 public String getApostropheChars() {
170 return apostropheChars;
171 }
172
173 public void setApostropheChars(String apostropheChars) {
174 this.apostropheChars = apostropheChars;
175 }
176
177 public String getAsignmentChars() {
178 return asignmentChars;
179 }
180
181 public void setAsignmentChars(String asignmentChars) {
182 this.asignmentChars = asignmentChars;
183 }
184 }