////////////////////////////////////////////////////////////////////// /// <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; } }
/////////////////////////////////////////////////////////////////////// /// <summary>Creates an AttributeHistogram with no example values. /// </summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <param name="count">number of times this attribute appeared /// in the histogram query results</param> /////////////////////////////////////////////////////////////////////// public AttributeHistogram(string name, GBaseAttributeType type, int count) :this(name, type, count, null) { }
private GBaseAttributeType(GBaseAttributeType supertype, StandardGBaseAttributeTypeIds id, string name) { this.supertype = supertype; this.name = name; this.id = id; }
/////////////////////////////////////////////////////////////////////// /// <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); }
/////////////////////////////////////////////////////////////////////// /// <summary>Creates an AttributeHistogram.</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <param name="count">number of times this attribute appeared /// in the histogram query results</param> /// <param name="values">example values, may be empty or null /// in the histogram query results</param> /////////////////////////////////////////////////////////////////////// public AttributeHistogram(string name, GBaseAttributeType type, int count, List <HistogramValue> values) { this.name = name; this.type = type; this.count = count; this.values = values == null ? new List <HistogramValue>() : values; }
/////////////////////////////////////////////////////////////////////// /// <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>Gets all attributes with a specific name and a specific /// type.</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <returns>all attributes on the list that have this name and /// type (or one of its subtypes), in order</returns> /////////////////////////////////////////////////////////////////////// public List <GBaseAttribute> GetAttributes(string name, GBaseAttributeType type) { List <GBaseAttribute> retVal = new List <GBaseAttribute>(); foreach (GBaseAttribute var in GetAttributeList(name, type)) { retVal.Add(var); } return(retVal); }
/////////////////////////////////////////////////////////////////////// /// <summary>Creates an AttributeHistogram.</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <param name="count">number of times this attribute appeared /// in the histogram query results</param> /// <param name="values">example values, may be empty or null /// in the histogram query results</param> /////////////////////////////////////////////////////////////////////// public AttributeHistogram(string name, GBaseAttributeType type, int count, List<HistogramValue> values) { this.name = name; this.type = type; this.count = count; this.values = values == null ? new List<HistogramValue>() : values; }
/////////////////////////////////////////////////////////////////////// /// <summary>Gets the first attribute with a specific name and type. /// </summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <returns>the first attribute found with this name and type (or /// one of its subtypes) or null</returns> /////////////////////////////////////////////////////////////////////// public GBaseAttribute GetAttribute(string name, GBaseAttributeType type) { foreach (GBaseAttribute attribute in this) { if (HasNameAndType(attribute, name, type)) { return(attribute); } } return(null); }
private ExtensionList GetAttributeList(string name, GBaseAttributeType type) { ExtensionList retval = ExtensionList.NotVersionAware(); foreach (GBaseAttribute attribute in this) { if (HasNameAndType(attribute, name, type)) { retval.Add(attribute); } } return(retval); }
/////////////////////////////////////////////////////////////////////// /// <summary>Checks whether this object is a supertype or the /// same as another type.</summary> /// <param name="subtype">other attribute type.</param> /////////////////////////////////////////////////////////////////////// public bool IsSupertypeOf(GBaseAttributeType subtype) { if (this == subtype) { return(true); } GBaseAttributeType otherSupertype = subtype.Supertype; if (otherSupertype == null) { return(false); } return(IsSupertypeOf(otherSupertype)); }
private bool ExtractAttributeAsFloat(string name, out float value, GBaseAttributeType type) { String stringValue = GetAttributeAsString(name, type); if (stringValue == null) { value = 0; return(false); } value = NumberFormat.ToFloat(stringValue); return(true); }
private List <DateTime> GetAttributesAsDateTime(string name, GBaseAttributeType type) { List <DateTime> retval = new List <DateTime>(); foreach (GBaseAttribute attribute in GetAttributes(name, type)) { if (attribute.Content != null) { retval.Add(DateTime.Parse(attribute.Content)); } } return(retval); }
private List <float> GetAttributesAsFloat(string name, GBaseAttributeType type) { List <float> retval = new List <float>(); foreach (GBaseAttribute attribute in GetAttributes(name, type)) { String content = attribute.Content; if (content != null) { retval.Add(NumberFormat.ToFloat(content)); } } return(retval); }
/////////////////////////////////////////////////////////////////////// /// <summary>Get or create an attribute with the given name. /// If the name corresponds to a standard attribute, the global /// instance will be returned. Otherwise, a new GBaseAttributeType /// with Id = otherType will be created.</summary> /////////////////////////////////////////////////////////////////////// public static GBaseAttributeType ForName(String name) { if (name == null) { throw new ArgumentNullException("name"); } GBaseAttributeType standard = StandardTypesDict[name] as GBaseAttributeType; if (standard != null) { return(standard); } return(new GBaseAttributeType(StandardGBaseAttributeTypeIds.otherType, name)); }
/////////////////////////////////////////////////////////////////////// /// <summary>Parses XML code and generates an ItemTypeAttributes /// object.</summary> /////////////////////////////////////////////////////////////////////// public static ItemTypeAttributes Parse(XmlNode xml) { List <AttributeId> attributeIds = new List <AttributeId>(); for (XmlNode child = xml.FirstChild; child != null; child = child.NextSibling) { if ("attribute" == child.LocalName && child.Attributes != null && child.Attributes["type"] != null) { GBaseAttributeType type = GBaseAttributeType.ForName(child.Attributes["type"].Value); attributeIds.Add(new AttributeId(child.Attributes["name"].Value, type)); } } return(new ItemTypeAttributes(attributeIds)); }
/////////////////////////////////////////////////////////////////////// /// <summary>Parses a gm:attribute tag and create the corresponding /// AttributeHistogram object.</summary> /////////////////////////////////////////////////////////////////////// public static AttributeHistogram Parse(XmlNode node) { if (node.Attributes != null) { string name = null; int count = 0; GBaseAttributeType type = null; name = Utilities.GetAttributeValue("name", node); String value = Utilities.GetAttributeValue("type", node); if (value != null) { type = GBaseAttributeType.ForName(value); } value = Utilities.GetAttributeValue("count", node); if (value != null) { count = NumberFormat.ToInt(value); } if (name != null && type != null) { //TODO determine if this is correct. List <HistogramValue> values = new List <HistogramValue>(); for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling) { if (child.LocalName == "value") { value = Utilities.GetAttributeValue("count", child); if (value != null) { int valueCount = NumberFormat.ToInt(value); values.Add(new HistogramValue(child.InnerText, valueCount)); } } } return(new AttributeHistogram(name, type, count, values)); } } return(null); }
private bool ExtractAttributeAsDateTime(string name, GBaseAttributeType type, out DateTime value) { String stringValue = GetAttributeAsString(name, type); if (stringValue == null) { value = NoDateTime; return(false); } try { value = DateTime.Parse(stringValue); return(true); } catch (FormatException e) { throw new FormatException(e.Message + " (" + stringValue + ")", e); } }
/////////////////////////////////////////////////////////////////////// /// <summary>Compares two types by comparing their names.</summary> /////////////////////////////////////////////////////////////////////// public override bool Equals(object o) { if (Object.ReferenceEquals(o, this)) { return(true); } if (!(o is GBaseAttributeType)) { return(false); } GBaseAttributeType other = o as GBaseAttributeType; if (other.id == id) { if (other.id == StandardGBaseAttributeTypeIds.otherType) { return(name.Equals(other.name)); } return(true); } return(false); }
/////////////////////////////////////////////////////////////////////// /// <summary>Gets the first attribute with a specific name and type. /// </summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <returns>the first attribute found with this name and type (or /// one of its subtypes) or null</returns> /////////////////////////////////////////////////////////////////////// public GBaseAttribute GetAttribute(string name, GBaseAttributeType type) { foreach (GBaseAttribute attribute in this) { if (HasNameAndType(attribute, name, type)) { return attribute; } } return null; }
/// <summary>Creates an AttributeId</summary>x public AttributeId(string name, GBaseAttributeType type) { this.name = name; this.type = type; }
private bool HasNameAndType(GBaseAttribute attr, String name, GBaseAttributeType type) { return(name == attr.Name && (type == null || type.IsSupertypeOf(attr.Type))); }
/////////////////////////////////////////////////////////////////////// /// <summary>Gets all attributes with a specific name and a specific /// type.</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <returns>all attributes on the list that have this name and /// type (or one of its subtypes), in order</returns> /////////////////////////////////////////////////////////////////////// public List<GBaseAttribute> GetAttributes(string name, GBaseAttributeType type) { List<GBaseAttribute> retVal = new List<GBaseAttribute>(); foreach (GBaseAttribute var in GetAttributeList(name, type)) { retVal.Add(var); } return retVal; }
/////////////////////////////////////////////////////////////////////// /// <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>Checks whether this object is a supertype or the /// same as another type.</summary> /// <param name="subtype">other attribute type.</param> /////////////////////////////////////////////////////////////////////// public bool IsSupertypeOf(GBaseAttributeType subtype) { if (this == subtype) { return true; } GBaseAttributeType otherSupertype = subtype.Supertype; if (otherSupertype == null) { return false; } return IsSupertypeOf(otherSupertype); }
private void AssertTypesAreSame(GBaseAttributeType typeA, GBaseAttributeType typeB) { Assert.IsTrue(typeA.Equals(typeB), "equals"); Assert.IsFalse(typeA != typeB, "operaton !="); Assert.IsTrue(typeA == typeB, "operator =="); Assert.AreEqual(typeA.GetHashCode(), typeB.GetHashCode(), "HashCode"); }
/////////////////////////////////////////////////////////////////////// /// <summary>Creates an AttributeHistogram with no example values. /// </summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /// <param name="count">number of times this attribute appeared /// in the histogram query results</param> /////////////////////////////////////////////////////////////////////// public AttributeHistogram(string name, GBaseAttributeType type, int count) : this(name, type, count, null) { }
private bool HasNameAndType(GBaseAttribute attr, String name, GBaseAttributeType type) { return name == attr.Name && (type == null || type.IsSupertypeOf(attr.Type)); }
private ExtensionList GetAttributeList(string name, GBaseAttributeType type) { ExtensionList retval = ExtensionList.NotVersionAware(); foreach (GBaseAttribute attribute in this) { if (HasNameAndType(attribute, name, type)) { retval.Add(attribute); } } return retval; }
private void AssertTypesAreDifferent(GBaseAttributeType typeA, GBaseAttributeType typeB) { Assert.IsFalse(typeA.Equals(typeB), "equals"); Assert.IsTrue(typeA != typeB, "operator !="); Assert.IsFalse(typeA == typeB, "operator =="); }
/////////////////////////////////////////////////////////////////////// /// <summary>Remove all attributes with a specific name and type or /// on of its subtypes.</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type</param> /////////////////////////////////////////////////////////////////////// public void RemoveAll(String name, GBaseAttributeType type) { RemoveAll(GetAttributeList(name, type)); }
private bool ExtractAttributeAsFloat(string name, out float value, GBaseAttributeType type) { String stringValue = GetAttributeAsString(name, type); if (stringValue == null) { value = 0; return false; } value = NumberFormat.ToFloat(stringValue); return true; }
/////////////////////////////////////////////////////////////////////// /// <summary>Creates a GBaseAttribute</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type or null if unknown</param> /// <param name="content">value</param> /////////////////////////////////////////////////////////////////////// public GBaseAttribute(String name, GBaseAttributeType type, String content) { this.name = name; this.type = type; this.content = content; }
private List<float> GetAttributesAsFloat(string name, GBaseAttributeType type) { List<float> retval = new List<float>(); foreach (GBaseAttribute attribute in GetAttributes(name, type)) { String content = attribute.Content; if (content != null) { retval.Add(NumberFormat.ToFloat(content)); } } return retval; }
/////////////////////////////////////////////////////////////////////// /// <summary>Creates a GBaseAttribute with a name and a type.</summary> /// <param name="name">attribute name</param> /// <param name="type">attribute type or null if unknown</param> /////////////////////////////////////////////////////////////////////// public GBaseAttribute(String name, GBaseAttributeType type) : this(name, type, null) { }
/////////////////////////////////////////////////////////////////////// /// <summary>Gets the content of the first attribute found with /// a specific name and type, as a string.</summary> /// <param name="name">attribute name</param> /// <param name="type">type of the attribute</param> /// <returns>the value of the first attribute, if found, or null /// </returns> /////////////////////////////////////////////////////////////////////// public String GetAttributeAsString(string name, GBaseAttributeType type) { return ExtractContent(GetAttribute(name, type)); }
private bool ExtractAttributeAsDateTime(string name, GBaseAttributeType type, out DateTime value) { String stringValue = GetAttributeAsString(name, type); if (stringValue == null) { value = NoDateTime; return false; } try { value = DateTime.Parse(stringValue); return true; } catch(FormatException e) { throw new FormatException(e.Message + " (" + stringValue + ")", e); } }
private List<DateTime> GetAttributesAsDateTime(string name, GBaseAttributeType type) { List<DateTime> retval = new List<DateTime>(); foreach (GBaseAttribute attribute in GetAttributes(name, type)) { if (attribute.Content != null) { retval.Add(DateTime.Parse(attribute.Content)); } } return retval; }
/////////////////////////////////////////////////////////////////////// /// <summary>Gets the content of the first attribute found with /// a specific name and type, as a string.</summary> /// <param name="name">attribute name</param> /// <param name="type">type of the attribute</param> /// <returns>the value of the first attribute, if found, or null /// </returns> /////////////////////////////////////////////////////////////////////// public String GetAttributeAsString(string name, GBaseAttributeType type) { return(ExtractContent(GetAttribute(name, type))); }
/////////////////////////////////////////////////////////////////////// /// <summary>Parses a gm:attribute tag and create the corresponding /// AttributeHistogram object.</summary> /////////////////////////////////////////////////////////////////////// public static AttributeHistogram Parse(XmlNode node) { if (node.Attributes != null) { string name = null; int count = 0; GBaseAttributeType type = null; name = Utilities.GetAttributeValue("name", node); String value = Utilities.GetAttributeValue("type", node); if (value != null) { type = GBaseAttributeType.ForName(value); } value = Utilities.GetAttributeValue("count", node); if (value != null) { count = NumberFormat.ToInt(value); } if (name != null && type != null) { //TODO determine if this is correct. List<HistogramValue> values = new List<HistogramValue>(); for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling) { if (child.LocalName == "value") { value = Utilities.GetAttributeValue("count", child); if (value != null) { int valueCount = NumberFormat.ToInt(value); values.Add(new HistogramValue(child.InnerText, valueCount)); } } } return new AttributeHistogram(name, type, count, values); } } return null; }