generator: Properly handle boolean attributes when parsing the XML

In a lot of places, we were only checking the presence of boolean
attributes, like "array", automatically assuming they were true. This
meant that we didn't allow setting them explicitly to false, which
apparently is needed for some bindings.

For all boolean attributes, we now use the GetAttributeAsBoolean method
added in the previous commit, to correctly check the value of the
attribute. As before, if the attribute is not present, it is considered
to be false.

Thanks to Stephan Sundermann for noticing this issue.
This commit is contained in:
Bertrand Lorentz 2012-10-21 18:10:34 +02:00
parent d35ef48f86
commit 4d19e5ac2a
18 changed files with 38 additions and 57 deletions

View file

@ -61,20 +61,13 @@ namespace GtkSharp.Generation {
protected ClassBase (XmlElement ns, XmlElement elem) : base (ns, elem) {
if (elem.HasAttribute ("deprecated")) {
string attr = elem.GetAttribute ("deprecated");
deprecated = attr == "1" || attr == "true";
}
if (elem.HasAttribute ("abstract")) {
string attr = elem.GetAttribute ("abstract");
isabstract = attr == "1" || attr == "true";
}
deprecated = elem.GetAttributeAsBoolean ("deprecated");
isabstract = elem.GetAttributeAsBoolean ("abstract");
foreach (XmlNode node in elem.ChildNodes) {
if (!(node is XmlElement)) continue;
XmlElement member = (XmlElement) node;
if (member.HasAttribute ("hidden"))
if (member.GetAttributeAsBoolean ("hidden"))
continue;
string name;
@ -227,7 +220,7 @@ namespace GtkSharp.Generation {
if (node.Name != "interface")
continue;
XmlElement element = (XmlElement) node;
if (element.HasAttribute ("hidden"))
if (element.GetAttributeAsBoolean ("hidden"))
continue;
if (element.HasAttribute ("cname"))
interfaces.Add (element.GetAttribute ("cname"));

View file

@ -35,8 +35,7 @@ namespace GtkSharp.Generation {
public Ctor (XmlElement elem, ClassBase implementor) : base (elem, implementor)
{
if (elem.HasAttribute ("preferred"))
preferred = true;
preferred = elem.GetAttributeAsBoolean ("preferred");
if (implementor is ObjectGen)
needs_chaining = true;
name = implementor.Name;

View file

@ -62,7 +62,7 @@ namespace GtkSharp.Generation {
public bool IsArray {
get {
return elem.HasAttribute("array_len") || elem.HasAttribute("array");
return elem.HasAttribute ("array_len") || elem.GetAttributeAsBoolean ("array");
}
}
@ -153,7 +153,7 @@ namespace GtkSharp.Generation {
SymbolTable table = SymbolTable.Table;
StreamWriter sw = gen_info.Writer;
string modifiers = elem.HasAttribute ("new_flag") ? "new " : "";
string modifiers = elem.GetAttributeAsBoolean ("new_flag") ? "new " : "";
bool is_struct = table.IsStruct (CType) || table.IsBoxed (CType);
sw.WriteLine (indent + "public " + modifiers + CSType + " " + Name + " {");

View file

@ -58,11 +58,7 @@ namespace GtkSharp.Generation {
public bool IsInternal {
get {
if (elem.HasAttribute ("internal")) {
string attr = elem.GetAttribute ("internal");
return attr == "1" || attr == "true";
}
return false;
return elem.GetAttributeAsBoolean ("internal");
}
}

View file

@ -33,7 +33,7 @@ namespace GtkSharp.Generation {
public InterfaceGen (XmlElement ns, XmlElement elem) : base (ns, elem, true)
{
consume_only = elem.HasAttribute ("consume_only");
consume_only = elem.GetAttributeAsBoolean ("consume_only");
foreach (XmlNode node in elem.ChildNodes) {
if (!(node is XmlElement)) continue;
XmlElement member = (XmlElement) node;

View file

@ -40,15 +40,11 @@ namespace GtkSharp.Generation {
{
this.retval = new ReturnValue (elem["return-type"]);
if (!container_type.IsDeprecated && elem.HasAttribute ("deprecated")) {
string attr = elem.GetAttribute ("deprecated");
deprecated = attr == "1" || attr == "true";
if (!container_type.IsDeprecated) {
deprecated = elem.GetAttributeAsBoolean ("deprecated");
}
if (elem.HasAttribute ("win32_utf8_variant")) {
string attr = elem.GetAttribute ("win32_utf8_variant");
win32_utf8_variant = attr == "1" || attr.ToLower () == "true";
}
win32_utf8_variant = elem.GetAttributeAsBoolean ("win32_utf8_variant");
if (Name == "GetType")
Name = "GetGType";

View file

@ -41,7 +41,7 @@ namespace GtkSharp.Generation {
this.name = elem.GetAttribute ("name");
parms = new Parameters (elem ["parameters"]);
IsStatic = elem.GetAttribute ("shared") == "true";
if (elem.HasAttribute ("new_flag"))
if (elem.GetAttributeAsBoolean ("new_flag"))
mods = "new ";
if (elem.HasAttribute ("accessibility")) {
string attr = elem.GetAttribute ("accessibility");

View file

@ -69,7 +69,7 @@ namespace GtkSharp.Generation {
else
signal_vms.Add (member.GetAttribute ("field_name"), member);
if (member.GetAttribute ("hidden") != "1") {
if (!member.GetAttributeAsBoolean ("hidden")) {
string name = member.GetAttribute("name");
while (sigs.ContainsKey(name))
name += "mangled";
@ -130,7 +130,7 @@ namespace GtkSharp.Generation {
} else
vm = new GObjectVM (vm_elem, this);
if (vm_elem.GetAttribute ("padding") == "true" || vm_elem.GetAttribute ("hidden") == "1")
if (vm_elem.GetAttributeAsBoolean ("padding") || vm_elem.GetAttributeAsBoolean ("hidden"))
hidden_vms.Add (vm);
else {
if (vm is GObjectVM)

View file

@ -34,7 +34,7 @@ namespace GtkSharp.Generation {
protected override bool Writable {
get {
return elem.GetAttribute ("writeable") == "true";
return elem.GetAttributeAsBoolean ("writeable");
}
}

View file

@ -40,7 +40,8 @@ namespace GtkSharp.Generation {
foreach (XmlNode node in elem.ChildNodes) {
if (!(node is XmlElement)) continue;
XmlElement member = (XmlElement) node;
if (member.HasAttribute ("hidden") && member.GetAttribute ("hidden") == "1") continue;
if (member.GetAttributeAsBoolean ("hidden"))
continue;
switch (node.Name) {
case "callback":
@ -93,7 +94,7 @@ namespace GtkSharp.Generation {
private bool DisableVoidCtor {
get {
return Elem.HasAttribute ("disable_void_ctor");
return Elem.GetAttributeAsBoolean ("disable_void_ctor");
}
}

View file

@ -37,7 +37,7 @@ namespace GtkSharp.Generation {
private bool DisableRawCtor {
get {
return Elem.HasAttribute ("disable_raw_ctor");
return Elem.GetAttributeAsBoolean ("disable_raw_ctor");
}
}

View file

@ -81,13 +81,13 @@ namespace GtkSharp.Generation {
public bool IsArray {
get {
return elem.HasAttribute("array") || elem.HasAttribute("null_term_array");
return elem.GetAttributeAsBoolean ("array") || elem.GetAttributeAsBoolean ("null_term_array");
}
}
public bool IsEllipsis {
get {
return elem.HasAttribute("ellipsis");
return elem.GetAttributeAsBoolean ("ellipsis");
}
}
@ -330,7 +330,7 @@ namespace GtkSharp.Generation {
public ArrayParameter (XmlElement elem) : base (elem)
{
null_terminated = elem.HasAttribute ("null_term_array");
null_terminated = elem.GetAttributeAsBoolean ("null_term_array");
}
public override string MarshalType {

View file

@ -107,13 +107,10 @@ namespace GtkSharp.Generation {
if (elem == null)
continue;
if (elem.HasAttribute("hidden"))
if (elem.GetAttributeAsBoolean ("hidden"))
continue;
bool is_opaque = false;
if (elem.GetAttribute ("opaque") == "true" ||
elem.GetAttribute ("opaque") == "1")
is_opaque = true;
bool is_opaque = elem.GetAttributeAsBoolean ("opaque");
switch (def.Name) {
case "alias":

View file

@ -45,22 +45,21 @@ namespace GtkSharp.Generation {
bool Readable {
get {
return elem.GetAttribute ("readable") == "true";
return elem.GetAttributeAsBoolean ("readable");
}
}
bool Writable {
get {
return elem.GetAttribute ("writeable") == "true" &&
!elem.HasAttribute ("construct-only");
return elem.GetAttributeAsBoolean ("writeable") &&
!elem.GetAttributeAsBoolean ("construct-only");
}
}
bool IsDeprecated {
get {
return !container_type.IsDeprecated &&
(elem.GetAttribute ("deprecated") == "1" ||
elem.GetAttribute ("deprecated") == "true");
elem.GetAttributeAsBoolean ("deprecated");
}
}

View file

@ -74,13 +74,13 @@ namespace GtkSharp.Generation {
public bool Hidden {
get {
return elem.HasAttribute("hidden");
return elem.GetAttributeAsBoolean ("hidden");
}
}
protected bool IsNew {
get {
return elem.HasAttribute("new_flag");
return elem.GetAttributeAsBoolean ("new_flag");
}
}

View file

@ -40,11 +40,11 @@ namespace GtkSharp.Generation {
public ReturnValue (XmlElement elem)
{
if (elem != null) {
is_null_term = elem.HasAttribute ("null_term_array");
is_array = elem.HasAttribute ("array") || elem.HasAttribute ("array_length_param");
is_null_term = elem.GetAttributeAsBoolean ("null_term_array");
is_array = elem.GetAttributeAsBoolean ("array") || elem.HasAttribute ("array_length_param");
array_length_param = elem.GetAttribute ("array_length_param");
elements_owned = elem.GetAttribute ("elements_owned") == "true";
owned = elem.GetAttribute ("owned") == "true";
elements_owned = elem.GetAttributeAsBoolean ("elements_owned");
owned = elem.GetAttributeAsBoolean ("owned");
ctype = elem.GetAttribute("type");
default_value = elem.GetAttribute ("default_value");
element_ctype = elem.GetAttribute ("element_type");

View file

@ -76,7 +76,7 @@ namespace GtkSharp.Generation {
public void GenerateDecl (StreamWriter sw)
{
if (elem.HasAttribute("new_flag") || (container_type != null && container_type.GetSignalRecursively (Name) != null))
if (elem.GetAttributeAsBoolean ("new_flag") || (container_type != null && container_type.GetSignalRecursively (Name) != null))
sw.Write("new ");
sw.WriteLine ("\t\tevent " + EventHandlerQualifiedName + " " + Name + ";");
@ -242,7 +242,7 @@ namespace GtkSharp.Generation {
private bool NeedNew (ObjectBase implementor)
{
return elem.HasAttribute ("new_flag") ||
return elem.GetAttributeAsBoolean ("new_flag") ||
(container_type != null && container_type.GetSignalRecursively (Name) != null) ||
(implementor != null && implementor.GetSignalRecursively (Name) != null);
}

View file

@ -103,7 +103,7 @@ namespace GtkSharp.Generation {
private bool DisableNew {
get {
return Elem.HasAttribute ("disable_new");
return Elem.GetAttributeAsBoolean ("disable_new");
}
}