/
XmlDeclaration.java
95 lines (83 loc) · 2.8 KB
/
XmlDeclaration.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.jsoup.nodes;
import org.jsoup.SerializationException;
import org.jsoup.internal.StringUtil;
import org.jsoup.helper.Validate;
import java.io.IOException;
/**
* An XML Declaration.
*/
public class XmlDeclaration extends LeafNode {
// todo this impl isn't really right, the data shouldn't be attributes, just a run of text after the name
private final boolean isProcessingInstruction; // <! if true, <? if false, declaration (and last data char should be ?)
/**
* Create a new XML declaration
* @param name of declaration
* @param isProcessingInstruction is processing instruction
*/
public XmlDeclaration(String name, boolean isProcessingInstruction) {
Validate.notNull(name);
value = name;
this.isProcessingInstruction = isProcessingInstruction;
}
public String nodeName() {
return "#declaration";
}
/**
* Get the name of this declaration.
* @return name of this declaration.
*/
public String name() {
return coreValue();
}
/**
* Get the unencoded XML declaration.
* @return XML declaration
*/
public String getWholeDeclaration() {
StringBuilder sb = StringUtil.borrowBuilder();
try {
getWholeDeclaration(sb, new Document.OutputSettings());
} catch (IOException e) {
throw new SerializationException(e);
}
return StringUtil.releaseBuilder(sb).trim();
}
private void getWholeDeclaration(Appendable accum, Document.OutputSettings out) throws IOException {
for (Attribute attribute : attributes()) {
String key = attribute.getKey();
String val = attribute.getValue();
if (!key.equals(nodeName())) { // skips coreValue (name)
accum.append(' ');
// basically like Attribute, but skip empty vals in XML
accum.append(key);
if (!val.isEmpty()) {
accum.append("=\"");
Entities.escape(accum, val, out, true, false, false, false);
accum.append('"');
}
}
}
}
@Override
void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
accum
.append("<")
.append(isProcessingInstruction ? "!" : "?")
.append(coreValue());
getWholeDeclaration(accum, out);
accum
.append(isProcessingInstruction ? "!" : "?")
.append(">");
}
@Override
void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) {
}
@Override
public String toString() {
return outerHtml();
}
@Override
public XmlDeclaration clone() {
return (XmlDeclaration) super.clone();
}
}