-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComment.java
85 lines (72 loc) · 2.29 KB
/
Comment.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
package org.jsoup.nodes;
import org.jsoup.Jsoup;
import org.jsoup.parser.Parser;
import java.io.IOException;
/**
A comment node.
@author Jonathan Hedley, [email protected] */
public class Comment extends LeafNode {
private static final String COMMENT_KEY = "comment";
/**
Create a new comment node.
@param data The contents of the comment
*/
public Comment(String data) {
value = data;
}
/**
Create a new comment node.
@param data The contents of the comment
@param baseUri base URI not used. This is a leaf node.
@deprecated
*/
public Comment(String data, String baseUri) {
this(data);
}
public String nodeName() {
return "#comment";
}
/**
Get the contents of the comment.
@return comment content
*/
public String getData() {
return coreValue();
}
void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
if (out.prettyPrint())
indent(accum, depth, out);
accum
.append("<!--")
.append(getData())
.append("-->");
}
void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) {}
@Override
public String toString() {
return outerHtml();
}
/**
* Check if this comment looks like an XML Declaration.
* @return true if it looks like, maybe, it's an XML Declaration.
*/
public boolean isXmlDeclaration() {
String data = getData();
return (data.length() > 1 && (data.startsWith("!") || data.startsWith("?")));
}
/**
* Attempt to cast this comment to an XML Declaration note.
* @return an XML declaration if it could be parsed as one, null otherwise.
*/
public XmlDeclaration asXmlDeclaration() {
String data = getData();
Document doc = Jsoup.parse("<" + data.substring(1, data.length() -1) + ">", baseUri(), Parser.xmlParser());
XmlDeclaration decl = null;
if (doc.childNodeSize() > 0) {
Element el = doc.child(0);
decl = new XmlDeclaration(NodeUtils.parser(doc).settings().normalizeTag(el.tagName()), data.startsWith("!"));
decl.attributes().addAll(el.attributes());
}
return decl;
}
}