1 module deepmagic.dom.xml.cdata;
2 
3 import deepmagic.dom;
4 
5 /// Thrown during CData constructor
6 class CDataException : XMLException
7 { private this(string msg) { super(msg); } }
8 
9 class CData : Item
10 {
11 	private string content;
12 
13 	/**
14 	 * Construct a character data section
15 	 *
16 	 * Params:
17 	 *	  content = the body of the character data segment
18 	 *
19 	 * Throws: CDataException if the segment body is illegal (contains "]]>")
20 	 *
21 	 * Examples:
22 	 * --------------
23 	 * auto item = new CData("<b>hello</b>");
24 	 *	// constructs <![CDATA[<b>hello</b>]]>
25 	 * --------------
26 	 */
27 	this(string content)
28 	{
29 		if (content.indexOf("]]>") != -1) throw new CDataException(content);
30 		this.content = content;
31 	}
32 
33 	/**
34 	 * Compares two CDatas for equality
35 	 *
36 	 * Examples:
37 	 * --------------
38 	 * CData item1,item2;
39 	 * if (item1 == item2) { }
40 	 * --------------
41 	 */
42 	override bool opEquals(Object o)
43 	{
44 		const item = toType!(const Item)(o);
45 		const t = cast(CData)item;
46 		return t !is null && content == t.content;
47 	}
48 
49 	/**
50 	 * Compares two CDatas
51 	 *
52 	 * You should rarely need to call this function. It exists so that CDatas
53 	 * can be used as associative array keys.
54 	 *
55 	 * Examples:
56 	 * --------------
57 	 * CData item1,item2;
58 	 * if (item1 < item2) { }
59 	 * --------------
60 	 */
61 	override int opCmp(Object o)
62 	{
63 		const item = toType!(const Item)(o);
64 		const t = cast(CData)item;
65 		return t !is null && (content != t.content
66 			? (content < t.content ? -1 : 1 ) : 0 );
67 	}
68 
69 	/**
70 	 * Returns the hash of a CData
71 	 *
72 	 * You should rarely need to call this function. It exists so that CDatas
73 	 * can be used as associative array keys.
74 	 */
75 	override size_t toHash() const { return hash(content); }
76 
77 	/**
78 	 * Returns a string representation of this CData section
79 	 */
80 	override string toString() const { return cdata ~ content ~ "]]>"; }
81 
82 	override @property bool isEmptyXML() const { return false; } /// Returns false always
83 }