///////////////////////////////////////////////////////////////////////
        /// <summary>Returns the value of the first attribute of type
        /// intUnit, floatUnit or numberUnit with this name.</summary>
        /// <param name="name">attribute name</param>
        /// <returns>the value of the first attribute, or null</returns>
        ///////////////////////////////////////////////////////////////////////
        public NumberUnit GetNumberUnitAttribute(string name)
        {
            GBaseAttribute attribute = GetAttribute(name,
                                                    GBaseAttributeType.NumberUnit);

            return(toNumberUnit(attribute));
        }
        //////////////////////////////////////////////////////////////////////
        /// <summary>Creates a shipping object given a shipping attribute.
        /// </summary>
        /// <exception cref="ArgumentException">If the attribute
        /// type is not 'shipping' or unknown</exception>
        /// <exception cref="FormatException">If the attribute contains
        /// invalid sub-elements or lacks required sub-elements</exception>
        //////////////////////////////////////////////////////////////////////
        public Shipping(GBaseAttribute attribute)
        {
            GBaseAttributeType type = attribute.Type;

            if (type != null && type != GBaseAttributeType.Shipping)
            {
                throw new ArgumentException("Expected an attribute of " +
                                            "type 'shipping', got an attribute of type '" + type + "'");
            }
            this.country = GetRequiredAttribute(attribute, "country");
            this.service = GetRequiredAttribute(attribute, "service");
            string priceString = GetRequiredAttribute(attribute, "price");

            try
            {
                FloatUnit priceUnit = new FloatUnit(priceString);
                this.price    = priceUnit.Value;
                this.currency = priceUnit.Unit;
            }
            catch (FormatException)
            {
                this.price    = NumberFormat.ToFloat(priceString);
                this.currency = null;
            }
        }
 private String ExtractContent(GBaseAttribute attribute)
 {
     if (attribute == null)
     {
         return(null);
     }
     return(attribute.Content);
 }
示例#4
0
        ///////////////////////////////////////////////////////////////////////
        /// <summary>Parses an XML node and create the corresponding
        /// GBaseAttribute.</summary>
        ///////////////////////////////////////////////////////////////////////
        public static GBaseAttribute ParseGBaseAttribute(XmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            GBaseAttribute attribute = new GBaseAttribute();

            attribute.Name = FromXmlTagName(node.LocalName);
            String value = Utilities.GetAttributeValue("type", node);

            if (value != null)
            {
                attribute.Type = GBaseAttributeType.ForName(value);
            }
            value = Utilities.GetAttributeValue("access", node);
            attribute.IsPrivate = "private".Equals(value);

            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Element)
                {
                    bool parsed = false;
                    if (child.NamespaceURI == GBaseNameTable.NSGBaseMeta)
                    {
                        object localName = child.LocalName;
                        if (localName.Equals("adjusted_name"))
                        {
                            attribute.AdjustedName = child.InnerText;
                            parsed = true;
                        }
                        else if (localName.Equals("adjusted_value"))
                        {
                            attribute.AdjustedValue = child.InnerText;
                            parsed = true;
                        }
                    }
                    // Keep everything else as XML
                    if (!parsed)
                    {
                        attribute[child.LocalName] = child.InnerXml;
                    }
                }
            }

            // If there are sub-elements, set the Content to null unless
            // there is clearly something in there.
            string content = ExtractDirectTextChildren(node);

            if (!"".Equals(content.Trim(kXmlWhitespaces)))
            {
                attribute.Content = content;
            }
            return(attribute);
        }
        private static string GetRequiredAttribute(GBaseAttribute attribute, string name)
        {
            string value = attribute[name];

            if (value == null)
            {
                throw new FormatException("Missing sub-element for shipping attribute: " + name);
            }
            return(value);
        }
 //////////////////////////////////////////////////////////////////////
 /// <summary>Extracts location information from an attribute.
 /// </summary>
 /// <exception cref="FormatException">If the attribute contains
 /// invalid sub-elements or lacks required sub-elements</exception>
 //////////////////////////////////////////////////////////////////////
 public Location(GBaseAttribute attribute)
 {
     this.address = attribute.Content;
     if (attribute["latitude"] != null && attribute["longitude"] != null)
     {
         this.latitude       = NumberFormat.ToFloat(attribute["latitude"]);
         this.longitude      = NumberFormat.ToFloat(attribute["longitude"]);
         this.hasCoordinates = true;
     }
 }
示例#7
0
        ///////////////////////////////////////////////////////////////////////
        /// <summary>Adds an attribute at the end of the list.
        /// The might exist attributes with the same name, type and even
        /// value. This method will not remove them.</summary>
        /// <param name="name">attribute name</param>
        /// <param name="type">attribute type</param>
        /// <param name="content">value, as a string</param>
        ///////////////////////////////////////////////////////////////////////
        public GBaseAttribute Add(String name,
                                  GBaseAttributeType type,
                                  String content)
        {
            GBaseAttribute attribute =
                new GBaseAttribute(name, type, content);

            Add(attribute);
            return(attribute);
        }
        //////////////////////////////////////////////////////////////////////
        /// <summary>Returns the object representation of an attribute
        /// of type location with this name.</summary>
        //////////////////////////////////////////////////////////////////////
        public Location GetLocationAttributeAsObject(string name)
        {
            GBaseAttribute attribute = GetAttribute(name, GBaseAttributeType.Location);

            if (attribute == null)
            {
                return(null);
            }
            return(new Location(attribute));
        }
        //////////////////////////////////////////////////////////////////////
        /// <summary>Returns the object representation for an attribute
        /// of type shipping.</summary>
        //////////////////////////////////////////////////////////////////////
        public Shipping GetShippingAttribute(string name)
        {
            GBaseAttribute attribute = GetAttribute(name, GBaseAttributeType.Shipping);

            if (attribute == null)
            {
                return(null);
            }
            return(new Shipping(attribute));
        }
 //////////////////////////////////////////////////////////////////////
 /// <summary>Extracts location information from an attribute.
 /// </summary>
 /// <exception cref="FormatException">If the attribute contains
 /// invalid sub-elements or lacks required sub-elements</exception>
 //////////////////////////////////////////////////////////////////////
 public Location(GBaseAttribute attribute)
 {
     this.address = attribute.Content;
     if (attribute["latitude"] != null && attribute["longitude"] != null)
     {
         this.latitude = NumberFormat.ToFloat(attribute["latitude"]);
         this.longitude = NumberFormat.ToFloat(attribute["longitude"]);
         this.hasCoordinates = true;
     }
 }
 //////////////////////////////////////////////////////////////////////
 /// <summary>Creates a GBaseAttribute of type location corresponding
 /// to the current object.</summary>
 /// <param name="name">attribute name</param>
 //////////////////////////////////////////////////////////////////////
 public GBaseAttribute CreateGBaseAttribute(string name)
 {
     GBaseAttribute retval = new GBaseAttribute(name, GBaseAttributeType.Location);
     retval.Content = address;
     if (hasCoordinates)
     {
         retval["latitude"] = NumberFormat.ToString(latitude);
         retval["longitude"] = NumberFormat.ToString(longitude);
     }
     return retval;
 }
 private NumberUnit toNumberUnit(GBaseAttribute attribute)
 {
     if (attribute == null)
     {
         return(null);
     }
     if (attribute.Type == GBaseAttributeType.IntUnit)
     {
         return(new IntUnit(attribute.Content));
     }
     return(new FloatUnit(attribute.Content));
 }
        //////////////////////////////////////////////////////////////////////
        /// <summary>Creates a GBaseAttribute of type location corresponding
        /// to the current object.</summary>
        /// <param name="name">attribute name</param>
        //////////////////////////////////////////////////////////////////////
        public GBaseAttribute CreateGBaseAttribute(string name)
        {
            GBaseAttribute retval = new GBaseAttribute(name, GBaseAttributeType.Location);

            retval.Content = address;
            if (hasCoordinates)
            {
                retval["latitude"]  = NumberFormat.ToString(latitude);
                retval["longitude"] = NumberFormat.ToString(longitude);
            }
            return(retval);
        }
        public override void SetUp()
        {
            base.SetUp();
            aInt = new GBaseAttribute("a", GBaseAttributeType.Int, "12");
            aFloat = new GBaseAttribute("a", GBaseAttributeType.Float, "3.14");
            largeFloat = new GBaseAttribute("large", GBaseAttributeType.Float, "10000000");
            aNumber = new GBaseAttribute("a", GBaseAttributeType.Number, "2.7");

            attrs.Add(aInt);
            attrs.Add(aFloat);
            attrs.Add(aNumber);
            attrs.Add(largeFloat);
        }
        //////////////////////////////////////////////////////////////////////
        /// <summary>Creates a GBaseAttribute of type shipping corresponding
        /// to the current object.</summary>
        /// <param name="name">attribute name</param>
        //////////////////////////////////////////////////////////////////////
        public GBaseAttribute CreateGBaseAttribute(string name)
        {
            GBaseAttribute retval = new GBaseAttribute(name, GBaseAttributeType.Shipping);

            retval["country"] = country;
            retval["service"] = service;
            if (currency != null)
            {
                retval["price"] = price + " " + currency;
            }
            else
            {
                retval["price"] = NumberFormat.ToString(price);
            }
            return(retval);
        }
示例#16
0
        ///////////////////////////////////////////////////////////////////////
        /// <summary>Compares two attribute names, types, content and
        /// private flag.</summary>
        ///////////////////////////////////////////////////////////////////////
        public override bool Equals(object o)
        {
            if (!(o is GBaseAttribute))
            {
                return(false);
            }

            if (Object.ReferenceEquals(this, o))
            {
                return(true);
            }

            GBaseAttribute other = o as GBaseAttribute;

            return(name == other.name && type == other.type &&
                   content == other.content && isPrivate == other.isPrivate &&
                   adjustedName == other.adjustedName && adjustedValue == other.adjustedValue &&
                   HashTablesEquals(subElements, other.subElements));
        }
 //////////////////////////////////////////////////////////////////////
 /// <summary>Creates a shipping object given a shipping attribute.
 /// </summary>
 /// <exception cref="ArgumentException">If the attribute
 /// type is not 'shipping' or unknown</exception>
 /// <exception cref="FormatException">If the attribute contains
 /// invalid sub-elements or lacks required sub-elements</exception>
 //////////////////////////////////////////////////////////////////////
 public Shipping(GBaseAttribute attribute)
 {
     GBaseAttributeType type = attribute.Type;
     if (type != null && type != GBaseAttributeType.Shipping)
     {
         throw new ArgumentException("Expected an attribute of " +
             "type 'shipping', got an attribute of type '" + type + "'");
     }
     this.country = GetRequiredAttribute(attribute, "country");
     this.service = GetRequiredAttribute(attribute, "service");
     string priceString = GetRequiredAttribute(attribute, "price");
     try
     {
         FloatUnit priceUnit = new FloatUnit(priceString);
         this.price = priceUnit.Value;
         this.currency = priceUnit.Unit;
     }
     catch (FormatException)
     {
         this.price = NumberFormat.ToFloat(priceString);
         this.currency = null;
     }
 }
        public void TestContainsAndEqual()
        {
            GBaseAttribute anAttr =
                new GBaseAttribute("w", GBaseAttributeType.Boolean);
            GBaseAttribute sameAttr =
                new GBaseAttribute("w", GBaseAttributeType.Boolean);

            attrs.Add(anAttr);
            Assert.IsTrue(attrs.Contains(sameAttr));
        }
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Checks whether an attribute can be found in the element.
 /// This method looks for an attribute with the exact same name, type
 /// and content.</summary>
 /// <returns>true if the attribute exists</returns>
 ///////////////////////////////////////////////////////////////////////
 public bool Contains(GBaseAttribute value)
 {
     return extensionElements.Contains(value);
 }
        private void AssertRereadIsSame(GBaseAttribute attribute)
        {
            StringWriter sw = new StringWriter();
            XmlWriter writer = new XmlTextWriter(sw);
            attribute.Save(writer);
            writer.Close();

            GBaseAttribute parsed = Parse(sw.ToString());

            Assert.AreEqual(attribute.Name, parsed.Name, "name");
            Assert.AreEqual(attribute.Type, parsed.Type, "type");
            Assert.AreEqual(attribute.Content, parsed.Content, "content");
            Assert.AreEqual(attribute.IsPrivate, parsed.IsPrivate, "private");
            Assert.AreEqual(attribute, parsed, "GBaseAttribute.Equals()");
        }
 public void GeneratePrivateTest()
 {
     GBaseAttribute attribute = new GBaseAttribute("bozo",
                                GBaseAttributeType.Int,
                                "oops !");
     attribute.IsPrivate = true;
     AssertRereadIsSame(attribute);
 }
        public void RemoveSubElementsTest()
        {
            GBaseAttribute attribute = new GBaseAttribute("x");
            Assert.IsFalse(attribute.HasSubElements, "HasSubElements(Unset)");
            attribute["a"] = "hello";
            Assert.IsTrue(attribute.HasSubElements, "HasSubElements(Set)");
            attribute["a"] = null;
            Assert.IsFalse(attribute.HasSubElements, "HasSubElements(Removed)");

            // Make sure this can be set without exceptions
            attribute.Content = "hello";
        }
        ///////////////////////////////////////////////////////////////////////
        /// <summary>Parses an XML node and create the corresponding
        /// GBaseAttribute.</summary>
        ///////////////////////////////////////////////////////////////////////
        public static GBaseAttribute ParseGBaseAttribute(XmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            GBaseAttribute attribute = new GBaseAttribute();
            attribute.Name = FromXmlTagName(node.LocalName);
            String value = Utilities.GetAttributeValue("type", node);
            if (value != null)
            {
                attribute.Type = GBaseAttributeType.ForName(value);
            }
            value =  Utilities.GetAttributeValue("access",node);
            attribute.IsPrivate = "private".Equals(value);

            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Element)
                {
                    bool parsed = false;
                    if (child.NamespaceURI == GBaseNameTable.NSGBaseMeta)
                    {
                        object localName = child.LocalName;
                        if (localName.Equals("adjusted_name"))
                        {
                            attribute.AdjustedName = child.InnerText;
                            parsed = true;
                        }
                        else if (localName.Equals("adjusted_value"))
                        {
                            attribute.AdjustedValue = child.InnerText;
                            parsed = true;
                        }
                    }
                    // Keep everything else as XML
                    if (!parsed)
                    {
                        attribute[child.LocalName] = child.InnerXml;
                    }
                }
            }

            // If there are sub-elements, set the Content to null unless
            // there is clearly something in there.
            string content = ExtractDirectTextChildren(node);
            if (!"".Equals(content.Trim(kXmlWhitespaces)))
            {
                attribute.Content = content;
            }
            return attribute;
        }
示例#24
0
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Adds an attribute at the end of the list.
 /// The might exist attributes with the same name, type and even
 /// value. This method will not remove them.</summary>
 ///////////////////////////////////////////////////////////////////////
 public GBaseAttribute Add(GBaseAttribute value)
 {
     extensionElements.Add(value);
     return(value);
 }
        public override void SetUp()
        {
            base.SetUp();
            aFloatUnit = new GBaseAttribute("a",
                                            GBaseAttributeType.FloatUnit,
                                            aFloatUnitValue.ToString());
            aIntUnit = new GBaseAttribute("a",
                                          GBaseAttributeType.IntUnit,
                                          aIntUnitValue.ToString());
            aNumberUnit = new GBaseAttribute("a",
                                             GBaseAttributeType.NumberUnit,
                                             aNumberUnitValue.ToString());

            attrs.Add(aFloatUnit);
            attrs.Add(aIntUnit);
            attrs.Add(aNumberUnit);
        }
 private NumberUnit toNumberUnit(GBaseAttribute attribute)
 {
     if (attribute == null)
     {
         return null;
     }
     if (attribute.Type == GBaseAttributeType.IntUnit)
     {
         return new IntUnit(attribute.Content);
     }
     return new FloatUnit(attribute.Content);
 }
 private static string GetRequiredAttribute(GBaseAttribute attribute, string name)
 {
     string value = attribute[name];
     if (value == null)
     {
         throw new FormatException("Missing sub-element for shipping attribute: " + name);
     }
     return value;
 }
 //////////////////////////////////////////////////////////////////////
 /// <summary>Creates a GBaseAttribute of type shipping corresponding
 /// to the current object.</summary>
 /// <param name="name">attribute name</param>
 //////////////////////////////////////////////////////////////////////
 public GBaseAttribute CreateGBaseAttribute(string name)
 {
     GBaseAttribute retval = new GBaseAttribute(name, GBaseAttributeType.Shipping);
     retval["country"] = country;
     retval["service"] = service;
     if (currency != null)
     {
         retval["price"] = price + " " + currency;
     }
     else
     {
         retval["price"] = NumberFormat.ToString(price);
     }
     return retval;
 }
        public void GetAttributesWithSubtypes()
        {
            GBaseAttribute aInt =
                new GBaseAttribute("a", GBaseAttributeType.Int, "12");
            GBaseAttribute aFloat =
                new GBaseAttribute("a", GBaseAttributeType.Float, "3.14");
            GBaseAttribute aNumber =
                new GBaseAttribute("a", GBaseAttributeType.Number, "2.7");

            attrs.Add(aInt);
            attrs.Add(aFloat);
            attrs.Add(aNumber);

            Assert.AreEqual(3, attrs.GetAttributes("a").Count, "by name");
            Assert.AreEqual(3, attrs.GetAttributes("a", GBaseAttributeType.Number).Count,
                            "type=number");
            Assert.AreEqual(1, attrs.GetAttributes("a", GBaseAttributeType.Float).Count,
                            "type=float");
            Assert.AreEqual(1, attrs.GetAttributes("a", GBaseAttributeType.Int).Count,
                            "type=int");

            Assert.AreEqual(aInt, attrs.GetAttribute("a", GBaseAttributeType.Int));
            Assert.AreEqual(aFloat, attrs.GetAttribute("a", GBaseAttributeType.Float));
            Assert.AreEqual(aInt, attrs.GetAttribute("a", GBaseAttributeType.Number));

            attrs.Remove(aInt);
            Assert.AreEqual(null, attrs.GetAttribute("a", GBaseAttributeType.Int));
            Assert.AreEqual(aFloat, attrs.GetAttribute("a", GBaseAttributeType.Number));

            attrs.Remove(aFloat);
            Assert.AreEqual(null, attrs.GetAttribute("a", GBaseAttributeType.Float));
            Assert.AreEqual(aNumber, attrs.GetAttribute("a", GBaseAttributeType.Number));
        }
示例#30
0
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Remove a specific attribute, if it's there.</summary>
 /// <param name="attribute">attribute to remove</param>
 ///////////////////////////////////////////////////////////////////////
 public void Remove(GBaseAttribute attribute)
 {
     extensionElements.Remove(attribute);
 }
示例#31
0
 private bool HasNameAndType(GBaseAttribute attr,
                             String name,
                             GBaseAttributeType type)
 {
     return(name == attr.Name && (type == null || type.IsSupertypeOf(attr.Type)));
 }
示例#32
0
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Reads an attribute from XML and add it.</summary>
 ///////////////////////////////////////////////////////////////////////
 public void AddFromXml(XmlNode node)
 {
     Add(GBaseAttribute.ParseGBaseAttribute(node));
 }
 public void GenerateSubElementsAndContentTest()
 {
     GBaseAttribute attribute = new GBaseAttribute("x");
     attribute.Content = "x";
     attribute["a"] = "b";
     attribute["c"] = "d";
     AssertRereadIsSame(attribute);
 }
 public void ReReadAdjustedNameAndValueTest()
 {
     GBaseAttribute attribute = new GBaseAttribute("x");
     attribute.AdjustedName = "hello";
     attribute.AdjustedValue = "world";
     AssertRereadIsSame(attribute);
 }
示例#35
0
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Checks whether an attribute can be found in the element.
 /// This method looks for an attribute with the exact same name, type
 /// and content.</summary>
 /// <returns>true if the attribute exists</returns>
 ///////////////////////////////////////////////////////////////////////
 public bool Contains(GBaseAttribute value)
 {
     return(extensionElements.Contains(value));
 }
 private bool HasNameAndType(GBaseAttribute attr,
                             String name,
                             GBaseAttributeType type)
 {
     return name == attr.Name && (type == null || type.IsSupertypeOf(attr.Type));
 }
 public void ContentAndSubElementsTest()
 {
     GBaseAttribute attribute = new GBaseAttribute("x");
     attribute["a"] = "b";
     attribute.Content = "c";
     Assert.AreEqual("c", attribute.Content);
     Assert.AreEqual("b", attribute["a"]);
 }
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Adds an attribute at the end of the list.
 /// The might exist attributes with the same name, type and even
 /// value. This method will not remove them.</summary>
 ///////////////////////////////////////////////////////////////////////
 public GBaseAttribute Add(GBaseAttribute value)
 {
     extensionElements.Add(value);
     return value;
 }
 public void SubElementsTest()
 {
     GBaseAttribute attribute = new GBaseAttribute("x");
     Assert.IsFalse(attribute.HasSubElements, "HasSubElements(1)");
     attribute["a"] = "b";
     attribute["c"] = "d";
     Assert.IsTrue(attribute.HasSubElements, "HasSubElements(2)");
     Assert.AreEqual("b", attribute["a"], "a");
     Assert.AreEqual("d", attribute["c"], "c");
 }
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Adds an attribute at the end of the list.
 /// The might exist attributes with the same name, type and even
 /// value. This method will not remove them.</summary>
 /// <param name="name">attribute name</param>
 /// <param name="type">attribute type</param>
 /// <param name="content">value, as a string</param>
 ///////////////////////////////////////////////////////////////////////
 public GBaseAttribute Add(String name,
                           GBaseAttributeType type,
                           String content)
 {
     GBaseAttribute attribute =
         new GBaseAttribute(name, type, content);
     Add(attribute);
     return attribute;
 }
 private String ExtractContent(GBaseAttribute attribute)
 {
     if (attribute == null)
     {
         return null;
     }
     return attribute.Content;
 }
 ///////////////////////////////////////////////////////////////////////
 /// <summary>Remove a specific attribute, if it's there.</summary>
 /// <param name="attribute">attribute to remove</param>
 ///////////////////////////////////////////////////////////////////////
 public void Remove(GBaseAttribute attribute)
 {
     extensionElements.Remove(attribute);
 }