#region Disclaimer / License // Copyright (C) 2014, Jackie Ng // http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion Disclaimer / License using OSGeo.MapGuide.ObjectModels.IO; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Drawing; using System.IO; using System.Xml; using System.Xml.Serialization; namespace OSGeo.MapGuide.ObjectModels { /// /// Utility methods /// internal static class Utils { /// /// Parses a color in HTML notation (ea. #ffaabbff) /// /// The HTML representation of the color /// The .Net color structure that matches the color public static Color ParseHTMLColor(string color) { if (color.Length == 8) { int a = int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int r = int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); int g = int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); int b = int.Parse(color.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); return Color.FromArgb(a, r, g, b); } else if (color.Length == 6) { int r = int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int g = int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); int b = int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); return Color.FromArgb(r, g, b); } else throw new Exception(string.Format(Strings.ErrorBadHtmlColor, color)); } /// /// Parses a color in HTML notation (ea. #ffaabbff) /// /// The HTML representation of the color /// The .Net color structure that matches the color public static Color ParseHTMLColorRGBA(string color) { if (color.Length == 8) { int r = int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int g = int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); int b = int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); int a = int.Parse(color.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); return Color.FromArgb(a, r, g, b); } else if (color.Length == 6) { int r = int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int g = int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); int b = int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); return Color.FromArgb(r, g, b); } else throw new Exception(string.Format(Strings.ErrorBadHtmlColor, color)); } /// /// Parses a color in HTML notation (ea. #ffaabbff) /// /// The HTML representation of the color /// The .Net color structure that matches the color public static Color ParseHTMLColorARGB(string color) { if (color.Length == 8) { int a = int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int r = int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); int g = int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); int b = int.Parse(color.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); return Color.FromArgb(a, r, g, b); } else if (color.Length == 6) { int r = int.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); int g = int.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); int b = int.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); return Color.FromArgb(r, g, b); } else throw new Exception(string.Format(Strings.ErrorBadHtmlColor, color)); } /// /// Returns the HTML ARGB representation of an .Net color structure /// /// The color to encode /// A flag indicating if the color structures alpha value should be included /// The HTML representation of the color structure public static string SerializeHTMLColor(Color color, bool includeAlpha) { string res = string.Empty; if (includeAlpha) res += color.A.ToString("x02"); //NOXLATE res += color.R.ToString("x02"); //NOXLATE res += color.G.ToString("x02"); //NOXLATE res += color.B.ToString("x02"); //NOXLATE return res; } /// /// Returns the HTML RGBA representation of an .Net color structure /// /// The color to encode /// A flag indicating if the color structures alpha value should be included /// The HTML representation of the color structure public static string SerializeHTMLColorRGBA(Color color, bool includeAlpha) { string res = string.Empty; res += color.R.ToString("x02"); //NOXLATE res += color.G.ToString("x02"); //NOXLATE res += color.B.ToString("x02"); //NOXLATE if (includeAlpha) res += color.A.ToString("x02"); //NOXLATE return res; } /// /// Returns the HTML ARGB representation of an .Net color structure /// /// The color to encode /// A flag indicating if the color structures alpha value should be included /// The HTML representation of the color structure public static string SerializeHTMLColorARGB(Color color, bool includeAlpha) { string res = string.Empty; if (includeAlpha) res += color.A.ToString("x02"); //NOXLATE res += color.R.ToString("x02"); //NOXLATE res += color.G.ToString("x02"); //NOXLATE res += color.B.ToString("x02"); //NOXLATE return res; } /// /// Gets an fdo-related attribute from the specified xml element using the /// unqualified name and trying again with the fdo: qualifier if it didn't exist /// /// /// /// internal static XmlAttribute GetFdoAttribute(XmlNode node, string name) { var att = node.Attributes[name]; if (att == null) return node.Attributes["fdo:" + name]; //NOXLATE return att; } /// /// Gets an fdo-related element from the specified xml element using the /// unqualified name and trying again with the fdo: qualifier if it didn't exist /// /// /// /// internal static XmlElement GetFdoElement(XmlElement el, string name) { var element = el[name]; if (element == null) return el["fdo:" + name]; //NOXLATE return element; } /// /// Copies the content of a stream into another stream. /// Automatically attempts to rewind the source stream. /// /// The source stream /// The target stream internal static void CopyStream(System.IO.Stream source, System.IO.Stream target) { CopyStream(source, target, true); } /// /// Copies the content of a stream into another stream. /// /// The source stream /// The target stream /// True if the source stream should be rewound before being copied internal static void CopyStream(System.IO.Stream source, System.IO.Stream target, bool rewind) { int r; byte[] buf = new byte[1024]; //bool rewound = false; if (rewind) { if (source.CanSeek) { try { source.Position = 0; //rewound = true; } catch { } } else { ReadOnlyRewindableStream roSource = source as ReadOnlyRewindableStream; if (roSource != null && roSource.CanRewind) { roSource.Rewind(); //rewound = true; } } //if (!rewound) // throw new InvalidOperationException("Could not rewind the source stream. Most likely the source stream does not support seeking or rewinding"); //LOCALIZEME } do { r = source.Read(buf, 0, buf.Length); target.Write(buf, 0, r); } while (r > 0); } /// /// Creates a copy of the stream, with removed Utf8 BOM, if any /// /// The stream to fix /// A stream with no Utf8 BOM internal static System.IO.MemoryStream RemoveUTF8BOM(System.IO.MemoryStream ms) { //Skip UTF file header, since the MapGuide XmlParser is broken ms.Position = 0; byte[] utfheader = new byte[3]; if (ms.Read(utfheader, 0, utfheader.Length) == utfheader.Length) if (utfheader[0] == 0xEF && utfheader[1] == 0xBB && utfheader[2] == 0xBF) { ms.Position = 3; System.IO.MemoryStream mxs = new System.IO.MemoryStream(); Utils.CopyStream(ms, mxs, false); mxs.Position = 0; return mxs; } ms.Position = 0; return ms; } /// /// Converts the specified name value collection into a connection string /// /// /// public static string ToConnectionString(NameValueCollection values) { List tokens = new List(); foreach (string name in values.Keys) { string value = values[name]; if (value.Contains(";")) //NOXLATE value = "\"" + value + "\""; //NOXLATE tokens.Add(name + "=" + value); //NOXLATE } return string.Join(";", tokens.ToArray()); //NOXLATE } /// /// Serializes the given object as a UTF-8 encoded XML string. Any BOM is stripped from the XML string /// /// /// /// internal static string NormalizedSerialize(XmlSerializer serializer, object o) { using (var ms = new MemoryStream()) { using (var xw = new Utf8XmlWriter(ms)) { serializer.Serialize(xw, o); using (var ms2 = RemoveUTF8BOM(ms)) { using (var sr = new StreamReader(ms2)) { return sr.ReadToEnd(); } } } } } } }