| 1 |
namespace Xmpp { |
| 2 |
|
| 3 |
public class StanzaAttribute : StanzaEntry { |
| 4 |
|
| 5 |
internal const string ATTRIBUTE_STRING_FORMAT = "{%s}:%s='%s'"; |
| 6 |
internal const string ATTRIBUTE_STRING_NO_NS_FORMAT = "%s='%s'"; |
| 7 |
internal const string ATTRIBUTE_STRING_ANSI_FORMAT = ANSI_COLOR_GRAY+"{%s}:"+ANSI_COLOR_END+"%s="+ANSI_COLOR_GREEN+"'%s'"+ANSI_COLOR_END; |
| 8 |
internal const string ATTRIBUTE_STRING_ANSI_NO_NS_FORMAT = "%s="+ANSI_COLOR_GREEN+"'%s'"+ANSI_COLOR_END; |
| 9 |
internal const string ATTRIBUTE_XML_FORMAT = "%s:%s='%s'"; |
| 10 |
internal const string ATTRIBUTE_XML_NO_NS_FORMAT = "%s='%s'"; |
| 11 |
internal const string ATTRIBUTE_XML_ANSI_FORMAT = "%s:%s="+ANSI_COLOR_GREEN+"'%s'"+ANSI_COLOR_END; |
| 12 |
internal const string ATTRIBUTE_XML_ANSI_NO_NS_FORMAT = "%s="+ANSI_COLOR_GREEN+"'%s'"+ANSI_COLOR_END; |
| 13 |
|
| 14 |
internal StanzaAttribute() { |
| 15 |
} |
| 16 |
|
| 17 |
public StanzaAttribute.build(string ns_uri, string name, string val) { |
| 18 |
this.ns_uri = ns_uri; |
| 19 |
this.name = name; |
| 20 |
this.val = val; |
| 21 |
} |
| 22 |
|
| 23 |
public bool equals(StanzaAttribute other) { |
| 24 |
if (other.ns_uri != ns_uri) return false; |
| 25 |
if (other.name != name) return false; |
| 26 |
if (other.val != val) return false; |
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
internal string printf(string fmt, bool no_ns = false, string? ns_name = null) { |
| 31 |
if (no_ns) { |
| 32 |
return fmt.printf(name, (!)encoded_val); |
| 33 |
} else { |
| 34 |
if (ns_name == null) { |
| 35 |
return fmt.printf((!)ns_uri, name, (!)encoded_val); |
| 36 |
} else { |
| 37 |
return fmt.printf((!)ns_name, name, (!)encoded_val); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
public override string to_string(int i = 0) { |
| 43 |
return printf(ATTRIBUTE_STRING_FORMAT); |
| 44 |
} |
| 45 |
|
| 46 |
public string to_ansi_string(bool hide_ns = false) { |
| 47 |
if (hide_ns) { |
| 48 |
return printf(ATTRIBUTE_STRING_ANSI_NO_NS_FORMAT, true); |
| 49 |
} else { |
| 50 |
return printf(ATTRIBUTE_STRING_ANSI_FORMAT); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public string to_xml(NamespaceState? state_ = null) { |
| 55 |
NamespaceState state = state_ ?? new NamespaceState(); |
| 56 |
if (ns_uri == state.current_ns_uri || (ns_uri == XMLNS_URI && name == "xmlns")) { |
| 57 |
return printf(ATTRIBUTE_XML_NO_NS_FORMAT, true); |
| 58 |
} else { |
| 59 |
return printf(ATTRIBUTE_XML_FORMAT, false, state.find_name((!)ns_uri)); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public string to_ansi_xml(NamespaceState? state_ = null) { |
| 64 |
NamespaceState state = state_ ?? new NamespaceState(); |
| 65 |
if (ns_uri == state.current_ns_uri || (ns_uri == XMLNS_URI && name == "xmlns")) { |
| 66 |
return printf(ATTRIBUTE_XML_ANSI_NO_NS_FORMAT, true); |
| 67 |
} else { |
| 68 |
return printf(ATTRIBUTE_XML_ANSI_FORMAT, false, state.find_name((!)ns_uri)); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
} |