////////////////////////////////////////////////////////////////////// /// <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>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>Generates the Google Base specific parameters</summary> /////////////////////////////////////////////////////////////////////// protected override string CalculateQuery(string basePath) { string path = base.CalculateQuery(basePath); StringBuilder newPath = new StringBuilder(path, 2048); char paramInsertion = InsertionParameter(path); if (this.bq != null) { newPath.Append(paramInsertion); newPath.Append(BqParameter); newPath.Append('='); newPath.Append(Utilities.UriEncodeReserved(bq)); paramInsertion = '&'; } if (this.maxValues >= 0) { newPath.Append(paramInsertion); newPath.Append(MaxValuesParameter); newPath.Append('='); newPath.Append(NumberFormat.ToString(maxValues)); paramInsertion = '&'; } if (this.orderby != null) { newPath.Append(paramInsertion); newPath.Append(OrderByParameter); newPath.Append('='); newPath.Append(Utilities.UriEncodeReserved(orderby)); paramInsertion = '&'; } if (this.ascending) { newPath.Append(paramInsertion); newPath.Append(SortOrderParameter); newPath.Append("=ascending"); paramInsertion = '&'; } if (this.refine) { newPath.Append(paramInsertion); newPath.Append(RefineParameter); newPath.Append("=true"); paramInsertion = '&'; } if (this.content != null) { newPath.Append(paramInsertion); newPath.Append(ContentParameter); newPath.Append('='); newPath.Append(Utilities.UriEncodeReserved(content)); paramInsertion = '&'; } return(newPath.ToString()); }
////////////////////////////////////////////////////////////////////// /// <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); }
/////////////////////////////////////////////////////////////////////// /// <summary>Looks for an attribute of type int with this name. /// </summary> /// <param name="name">attribute name</param> /// <param name="value">value to set if the attribute is found</param> /// <returns>true if an attribute was found, in which case /// the value will have been set.</returns> /////////////////////////////////////////////////////////////////////// public bool ExtractIntAttribute(string name, out int value) { String stringValue = GetAttributeAsString(name, GBaseAttributeType.Int); if (stringValue == null) { value = 0; return(false); } value = NumberFormat.ToInt(stringValue); return(true); }
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); }
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>Returns the values of all the attribute of type /// int with this name.</summary> /// <param name="name">attribute name</param> /// <returns>all the values found, never nul</returns> /////////////////////////////////////////////////////////////////////// public List <int> GetIntAttributes(string name) { List <int> retval = new List <int>(); foreach (GBaseAttribute attribute in GetAttributes(name, GBaseAttributeType.Int)) { String content = attribute.Content; if (content != null) { retval.Add(NumberFormat.ToInt(content)); } } return(retval); }
////////////////////////////////////////////////////////////////////// /// <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); }
/////////////////////////////////////////////////////////////////////// /// <summary>Parses the Google-Base specific parameters /// in the Uri.</summary> /////////////////////////////////////////////////////////////////////// protected override Uri ParseUri(Uri targetUri) { base.ParseUri(targetUri); if (targetUri != null) { char[] deli = { '?', '&' }; TokenCollection tokens = new TokenCollection(targetUri.Query, deli); foreach (String token in tokens) { if (token.Length > 0) { char[] otherDeli = { '=' }; String[] parameters = token.Split(otherDeli, 2); switch (parameters[0]) { case BqParameter: this.bq = System.Web.HttpUtility.UrlDecode(parameters[1]); break; case MaxValuesParameter: this.maxValues = NumberFormat.ToInt(parameters[1]); break; case OrderByParameter: this.orderby = System.Web.HttpUtility.UrlDecode(parameters[1]); break; case SortOrderParameter: this.ascending = "ascending".Equals(parameters[1]); break; case RefineParameter: this.refine = Utilities.XSDTrue.Equals(parameters[1]); break; case ContentParameter: this.content = System.Web.HttpUtility.UrlDecode(parameters[1]); break; } } } } return(this.Uri); }
/////////////////////////////////////////////////////////////////////// /// <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); }
/////////////////////////////////////////////////////////////////////// /// <summary>Generates XML code for the current tag.</summary> /////////////////////////////////////////////////////////////////////// public void Save(XmlWriter writer) { writer.WriteStartElement(XmlPrefix, "attribute", XmlNameSpace); writer.WriteAttributeString("name", name); writer.WriteAttributeString("type", type.Name); writer.WriteAttributeString("count", NumberFormat.ToString(count)); foreach (HistogramValue value in values) { writer.WriteStartElement(GBaseNameTable.GBaseMetaPrefix, "value", GBaseNameTable.NSGBaseMeta); writer.WriteAttributeString("count", NumberFormat.ToString(value.Count)); writer.WriteString(value.Content); writer.WriteEndElement(); } writer.WriteEndElement(); }
/////////////////////////////////////////////////////////////////////// /// <summary>Generates an XML representation for this object.</summary> /////////////////////////////////////////////////////////////////////// public void Save(string tagName, XmlWriter writer) { if (total > 0) { writer.WriteStartElement(GBaseNameTable.GBaseMetaPrefix, tagName, GBaseNameTable.NSGBaseMeta); writer.WriteAttributeString("total", NumberFormat.ToString(total)); if (sourceCount != null) { foreach (string source in sourceCount.Keys) { int count = (int)sourceCount[source]; writer.WriteStartElement(GBaseNameTable.GBaseMetaPrefix, "source", GBaseNameTable.NSGBaseMeta); writer.WriteAttributeString("name", source); writer.WriteAttributeString("count", NumberFormat.ToString(count)); writer.WriteEndElement(); } } writer.WriteEndElement(); } }
/////////////////////////////////////////////////////////////////////// /// <summary>Parses an XML representation and updates the object</summary> /////////////////////////////////////////////////////////////////////// public void Parse(XmlNode xml) { Reset(); String value = Utilities.GetAttributeValue("total", xml); if (value != null) { total = NumberFormat.ToInt(value); } for (XmlNode child = xml.FirstChild; child != null; child = child.NextSibling) { if ("source".Equals(child.LocalName) && GBaseNameTable.NSGBaseMeta.Equals(child.NamespaceURI)) { string name = Utilities.GetAttributeValue("name", child); string countString = Utilities.GetAttributeValue("count", child); if (name != null && countString != null) { this[name] = NumberFormat.ToInt(countString); } } } }
/////////////////////////////////////////////////////////////////////// /// <summary>Extracts the number and unit from a string /// of the form: <c>number " " unit</c></summary> /// <exception cref="FormatException">Thrown when the string /// representation could not be used to generate a valid /// FloatUnit.</exception> /////////////////////////////////////////////////////////////////////// public FloatUnit(String str) : base(ExtractUnit(str)) { this.number = NumberFormat.ToFloat(ExtractNumber(str)); }
/////////////////////////////////////////////////////////////////////// /// <summary>Adds a new attribute of type number.</summary> /// <param name="name">attribute name</param> /// <param name="value">value</param> /// <returns>the newly-created GBaseAttribute object</returns> /////////////////////////////////////////////////////////////////////// public GBaseAttribute AddNumberAttribute(string name, int value) { return(Add(new GBaseAttribute(name, GBaseAttributeType.Number, NumberFormat.ToString(value)))); }
/////////////////////////////////////////////////////////////////////// /// <summary>Adds a new attribute of type float.</summary> /// <param name="name">attribute name</param> /// <param name="value">value</param> /// <returns>the newly-created GBaseAttribute object</returns> /////////////////////////////////////////////////////////////////////// public GBaseAttribute AddFloatAttribute(string name, float value) { return(Add(new GBaseAttribute(name, GBaseAttributeType.Float, NumberFormat.ToString(value)))); }
/////////////////////////////////////////////////////////////////////// /// <summary>Extracts the number and unit from a string /// of the form: <c>number " " unit</c></summary> /// <exception cref="FormatException">Thrown when the string /// representation could not be used to generate a valid /// IntUnit.</exception> /////////////////////////////////////////////////////////////////////// public IntUnit(string str) : base(ExtractUnit(str)) { this.number = NumberFormat.ToInt(ExtractNumber(str)); }
/////////////////////////////////////////////////////////////////////// /// <summary>Returns a string representation for the FloatUnit that /// can be used to re-created another FloatUnit.</summary> /////////////////////////////////////////////////////////////////////// public override string ToString() { return(NumberFormat.ToString(number) + " " + Unit); }