/// <summary> /// Builds a simple address as part of <seealso cref="GetSimpleAddress()"/>. /// </summary> /// <param name="orgElement">The org element.</param> /// <param name="premElement">The prem element.</param> /// <param name="thoroElement">The thoro element.</param> /// <param name="locaElement">The loca element.</param> /// <returns></returns> private SimpleAddress BuildSimpleAddress(StringBuilder orgElement, StringBuilder premElement, StringBuilder thoroElement, StringBuilder locaElement) { string[] lines = new string[5]; int lineCount = 0; if (orgElement.Length > 0) { lines[lineCount] = orgElement.ToString(); lineCount++; } if (this.pOBoxNumber.Length > 0) { lines[lineCount] = Resources.POBoxPrefix + this.pOBoxNumber; lineCount++; } if (premElement.Length > 0) { lines[lineCount] = premElement.ToString(); lineCount++; } if (thoroElement.Length > 0) { lines[lineCount] = thoroElement.ToString(); lineCount++; } if (locaElement.Length > 0) { lines[lineCount] = locaElement.ToString(); lineCount++; } if (this.Postcode.Length > 0) { if (lineCount < 4) { lines[lineCount] = PostcodeUtilities.Format(this.Postcode); } else { lines[4] += " " + PostcodeUtilities.Format(this.Postcode); } } // Put 5 lines into simple address SimpleAddress addr = new SimpleAddress(); addr.AddressLine1 = lines[0]; addr.AddressLine2 = lines[1]; addr.AddressLine3 = lines[2]; addr.AddressLine4 = lines[3]; addr.AddressLine5 = lines[4]; return(addr); }
/// <summary> /// Gets a simple address to use for display. If using on a web page, use <see cref="SimpleAddressControl"/> instead for microformat support. /// </summary> /// <returns>SimpleAddress populated from the BS7666 address</returns> public SimpleAddress GetSimpleAddress() { // Create a copy of the address so we can correct the case without affecting the external properties BS7666Address addrCopy = new BS7666Address(String.IsNullOrEmpty(this.Paon) ? String.Empty : this.Paon.Trim(), String.IsNullOrEmpty(this.Saon) ? String.Empty : this.Saon.Trim(), String.IsNullOrEmpty(this.StreetName) ? String.Empty : this.StreetName.Trim(), String.IsNullOrEmpty(this.Locality) ? String.Empty : this.Locality.Trim(), String.IsNullOrEmpty(this.Town) ? String.Empty : this.Town.Trim(), String.IsNullOrEmpty(this.AdministrativeArea) ? String.Empty : this.AdministrativeArea.Trim(), String.IsNullOrEmpty(this.Postcode) ? String.Empty : this.Postcode.Trim()); if (addrCopy.StreetName != null && addrCopy.StreetName.ToUpper(CultureInfo.CurrentCulture) == addrCopy.StreetName) { addrCopy.StreetName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.StreetName.ToLower(CultureInfo.CurrentCulture)); } if (addrCopy.Locality != null && addrCopy.Locality.ToUpper(CultureInfo.CurrentCulture) == addrCopy.Locality) { addrCopy.Locality = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.Locality.ToLower(CultureInfo.CurrentCulture)); } if (addrCopy.Town != null && addrCopy.Town.ToUpper(CultureInfo.CurrentCulture) == addrCopy.Town) { addrCopy.Town = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.Town.ToLower(CultureInfo.CurrentCulture)); } if (addrCopy.AdministrativeArea != null && addrCopy.AdministrativeArea.ToUpper(CultureInfo.CurrentCulture) == addrCopy.AdministrativeArea) { addrCopy.AdministrativeArea = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.AdministrativeArea.ToLower(CultureInfo.CurrentCulture)); } // Create a presentational address SimpleAddress addr = new SimpleAddress(); addr.AddressLine1 = addrCopy.Saon; if ((addr.AddressLine1 == null || addr.AddressLine1.Trim().Length == 0) && !SimpleAddress.IsFormat1Name(addrCopy.Paon)) { // If line one still spare, we have enough lines addr.AddressLine1 = addrCopy.Paon; addr.AddressLine2 = addrCopy.StreetName; } else { // If line 1 used by SAON or PAON is just a number, cram PAON and StreetName onto one line if (addrCopy.Paon != null && addrCopy.Paon.Length > 0 && addrCopy.StreetName != null && addrCopy.StreetName.Length > 0) { if (Regex.IsMatch(this.Paon, "^[0-9]{1,2}[-/]?[0-9]{0,2}[A-Z]?$", RegexOptions.IgnoreCase)) { addr.AddressLine2 = String.Format(CultureInfo.CurrentCulture, "{0} {1}", addrCopy.Paon, addrCopy.StreetName); } else { addr.AddressLine2 = String.Format(CultureInfo.CurrentCulture, "{0}, {1}", addrCopy.Paon, addrCopy.StreetName); } } if (addrCopy.Paon != null && addrCopy.Paon.Length > 0 && (addrCopy.StreetName == null || addrCopy.StreetName.Length == 0)) { addr.AddressLine2 = addrCopy.Paon; } else if ((addrCopy.Paon == null || addrCopy.Paon.Length == 0) && (addrCopy.StreetName != null && addrCopy.StreetName.Length > 0)) { addr.AddressLine2 = addrCopy.StreetName; } } addr.AddressLine3 = addrCopy.Locality; addr.AddressLine4 = addrCopy.Town; addr.AddressLine5 = String.Format(CultureInfo.CurrentCulture, "{0} {1}", addrCopy.AdministrativeArea, addrCopy.Postcode).Trim(); return(addr); }
/// <summary> /// Get a 5-line Simple Address from the ADDRESS-POINT address, formatted according to Royal Mail guidelines /// </summary> /// <returns></returns> public SimpleAddress GetSimpleAddress() { // *** Build six elements of an ADDRESS-POINT postal address *** TextInfo textInfo = System.Globalization.CultureInfo.CurrentCulture.TextInfo; string separator = textInfo.ListSeparator + " "; // *** Organisation elements *** StringBuilder orgElement = new StringBuilder(textInfo.ToTitleCase(this.organisationName.ToLower(CultureInfo.CurrentCulture))); if (this.departmentName.Length > 0) { if (orgElement.Length > 0) { orgElement.Append(separator); } orgElement.Append(textInfo.ToTitleCase(this.departmentName.ToLower(CultureInfo.CurrentCulture))); } // *** PO box element *** goes here, but there's only one field so nothing to do // *** Premises elements *** StringBuilder premElement = new StringBuilder(); bool subBuildingNameIsFormat1 = SimpleAddress.IsFormat1Name(this.SubBuildingName); bool buildingNameIsFormat1 = SimpleAddress.IsFormat1Name(this.BuildingName); bool premisesOnSeparateLine = ((this.SubBuildingName.Length > 0 && !subBuildingNameIsFormat1) || (this.BuildingName.Length > 0 && !buildingNameIsFormat1)); // sub-building name should not exist without a building name or building number if (this.SubBuildingName.Length > 0 && (this.BuildingName.Length > 0 || this.BuildingNumber.Length > 0)) { premElement.Append(textInfo.ToTitleCase(this.SubBuildingName.ToLower(CultureInfo.CurrentCulture))); } if (this.BuildingName.Length > 0) { if (premElement.Length > 0) { if (subBuildingNameIsFormat1) { premElement.Append(" "); } else { premElement.Append(separator); } } premElement.Append(textInfo.ToTitleCase(this.BuildingName.ToLower(CultureInfo.CurrentCulture))); } // *** Thoroughfare elements *** StringBuilder thoroElement = new StringBuilder(); // Dependent thoroughfare name should not exist without thoroughfare name if (this.DependentThoroughfareName.Length > 0 && this.ThoroughfareName.Length > 0) { thoroElement.Append(textInfo.ToTitleCase(this.DependentThoroughfareName.ToLower(CultureInfo.CurrentCulture))); } if (this.ThoroughfareName.Length > 0) { if (thoroElement.Length > 0) { thoroElement.Append(separator); } thoroElement.Append(textInfo.ToTitleCase(this.ThoroughfareName.ToLower(CultureInfo.CurrentCulture))); } // *** Locality elements *** StringBuilder locaElement = new StringBuilder(); // Double-dependent locality should not exist without a dependent locality if (this.DoubleDependentLocalityName.Length > 0 && this.DependentLocalityName.Length > 0) { locaElement.Append(textInfo.ToTitleCase(this.DoubleDependentLocalityName.ToLower(CultureInfo.CurrentCulture))); } if (this.DependentLocalityName.Length > 0) { if (locaElement.Length > 0) { locaElement.Append(separator); } locaElement.Append(textInfo.ToTitleCase(this.DependentLocalityName.ToLower(CultureInfo.CurrentCulture))); } if (this.PostTown.Length > 0) { if (locaElement.Length > 0) { locaElement.Append(separator); } locaElement.Append(textInfo.ToTitleCase(this.PostTown.ToLower(CultureInfo.CurrentCulture))); } if (this.PostalCounty.Length > 0) { if (locaElement.Length > 0) { locaElement.Append(separator); } locaElement.Append(textInfo.ToTitleCase(this.PostalCounty.ToLower(CultureInfo.CurrentCulture))); } // Building number goes in front of thoroughfare element or locality element if (this.BuildingNumber.Length > 0) { if (thoroElement.Length > 0) { thoroElement.Insert(0, this.BuildingNumber + " "); } else if (locaElement.Length > 0) { locaElement.Insert(0, this.BuildingNumber + " "); } } // Sub-building name and building name may have to go in front again if Format 1 if (!premisesOnSeparateLine && premElement.Length > 0) { if (thoroElement.Length > 0) { thoroElement.Insert(0, premElement.ToString() + " "); premElement.Remove(0, premElement.Length); } else if (locaElement.Length > 0) { locaElement.Insert(0, premElement.ToString() + " "); premElement.Remove(0, premElement.Length); } } // *** Postcode element *** goes here, but there's only one field so nothing to do // Now combine into five lines return(BuildSimpleAddress(orgElement, premElement, thoroElement, locaElement)); }