summaryrefslogtreecommitdiffstats
path: root/server/src/org/jsoup/nodes/DataNode.java
blob: cc377a4cc86c65633cb6836088711719ee9a3e55 (plain)
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
package org.jsoup.nodes;

/**
 * A data node, for contents of style, script tags etc, where contents should
 * not show in text().
 * 
 * @author Jonathan Hedley, jonathan@hedley.net
 */
public class DataNode extends Node {
    private static final String DATA_KEY = "data";

    /**
     * Create a new DataNode.
     * 
     * @param data
     *            data contents
     * @param baseUri
     *            base URI
     */
    public DataNode(String data, String baseUri) {
        super(baseUri);
        attributes.put(DATA_KEY, data);
    }

    @Override
    public String nodeName() {
        return "#data";
    }

    /**
     * Get the data contents of this node. Will be unescaped and with original
     * new lines, space etc.
     * 
     * @return data
     */
    public String getWholeData() {
        return attributes.get(DATA_KEY);
    }

    /**
     * Set the data contents of this node.
     * 
     * @param data
     *            unencoded data
     * @return this node, for chaining
     */
    public DataNode setWholeData(String data) {
        attributes.put(DATA_KEY, data);
        return this;
    }

    @Override
    void outerHtmlHead(StringBuilder accum, int depth,
            Document.OutputSettings out) {
        accum.append(getWholeData()); // data is not escaped in return from data
                                      // nodes, so " in script, style is plain
    }

    @Override
    void outerHtmlTail(StringBuilder accum, int depth,
            Document.OutputSettings out) {
    }

    @Override
    public String toString() {
        return outerHtml();
    }

    /**
     * Create a new DataNode from HTML encoded data.
     * 
     * @param encodedData
     *            encoded data
     * @param baseUri
     *            bass URI
     * @return new DataNode
     */
    public static DataNode createFromEncoded(String encodedData, String baseUri) {
        String data = Entities.unescape(encodedData);
        return new DataNode(data, baseUri);
    }
}