|
| 1 | +/******************************************************************************* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Jean-David Gadina - www.imazing.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | +using System.IO; |
| 28 | +using System.Linq; |
| 29 | +using System.Text; |
| 30 | +using System.Xml; |
| 31 | + |
| 32 | +namespace ColorSetKit |
| 33 | +{ |
| 34 | + static class PropertyListSerialization |
| 35 | + { |
| 36 | + public static object PropertyListFromData( Data data ) |
| 37 | + { |
| 38 | + if( data == null || data.Count == 0 ) |
| 39 | + { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + byte[] bytes = new byte[ data.Count ]; |
| 46 | + |
| 47 | + data.CopyBytes( bytes, 0, data.Count ); |
| 48 | + |
| 49 | + if( !( Encoding.UTF8.GetString( bytes ) is string plist ) ) |
| 50 | + { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + XmlDocument xml = new XmlDocument(); |
| 55 | + XmlReaderSettings settings = new XmlReaderSettings |
| 56 | + { |
| 57 | + DtdProcessing = DtdProcessing.Ignore |
| 58 | + }; |
| 59 | + |
| 60 | + using( XmlReader reader = XmlReader.Create( new StringReader( plist ), settings ) ) |
| 61 | + { |
| 62 | + xml.Load( reader ); |
| 63 | + |
| 64 | + if( xml.ChildNodes.Count != 1 ) |
| 65 | + { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + if( xml.FirstChild.Name != "plist" ) |
| 70 | + { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + if( xml.FirstChild.ChildNodes.Count != 1 ) |
| 75 | + { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + return PropertyListFromXmlNode( xml.FirstChild.FirstChild ); |
| 80 | + } |
| 81 | + } |
| 82 | + catch |
| 83 | + {} |
| 84 | + |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + private static object PropertyListFromXmlNode( XmlNode node ) |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + switch( node.Name ) |
| 93 | + { |
| 94 | + case "integer": return LongFromXmlNode( node ); |
| 95 | + case "real": return DoubleFromXmlNode( node ); |
| 96 | + case "string": return StringFromXmlNode( node ); |
| 97 | + case "true": return true; |
| 98 | + case "false": return false; |
| 99 | + case "array": return ListFromXmlNode( node ); |
| 100 | + case "dict": return DictionaryFromXmlNode( node ); |
| 101 | + } |
| 102 | + } |
| 103 | + catch |
| 104 | + {} |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + private static long LongFromXmlNode( XmlNode node ) |
| 110 | + { |
| 111 | + try |
| 112 | + { |
| 113 | + return Convert.ToInt64( node.Value ); |
| 114 | + } |
| 115 | + catch |
| 116 | + {} |
| 117 | + |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + private static double DoubleFromXmlNode( XmlNode node ) |
| 122 | + { |
| 123 | + try |
| 124 | + { |
| 125 | + return Convert.ToDouble( node.Value ); |
| 126 | + } |
| 127 | + catch |
| 128 | + {} |
| 129 | + |
| 130 | + return 0.0; |
| 131 | + } |
| 132 | + |
| 133 | + private static string StringFromXmlNode( XmlNode node ) |
| 134 | + { |
| 135 | + return node.Value; |
| 136 | + } |
| 137 | + |
| 138 | + private static List< object > ListFromXmlNode( XmlNode node ) |
| 139 | + { |
| 140 | + List< object > list = new List< object >(); |
| 141 | + |
| 142 | + foreach( XmlNode child in node.ChildNodes ) |
| 143 | + { |
| 144 | + try |
| 145 | + { |
| 146 | + if( PropertyListFromXmlNode( child ) is object o ) |
| 147 | + { |
| 148 | + list.Add( o ); |
| 149 | + } |
| 150 | + } |
| 151 | + catch |
| 152 | + {} |
| 153 | + } |
| 154 | + |
| 155 | + return list; |
| 156 | + } |
| 157 | + |
| 158 | + private static Dictionary< string, object > DictionaryFromXmlNode( XmlNode node ) |
| 159 | + { |
| 160 | + Dictionary< string, object > dict = new Dictionary< string, object >(); |
| 161 | + string key = null; |
| 162 | + |
| 163 | + foreach( XmlNode child in node.ChildNodes ) |
| 164 | + { |
| 165 | + try |
| 166 | + { |
| 167 | + if( child.Name == "key" ) |
| 168 | + { |
| 169 | + key = child.Value; |
| 170 | + |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + if( key == null || key.Length == 0 ) |
| 175 | + { |
| 176 | + continue; |
| 177 | + } |
| 178 | + |
| 179 | + if( PropertyListFromXmlNode( child ) is object o ) |
| 180 | + { |
| 181 | + dict[ key ] = o; |
| 182 | + key = null; |
| 183 | + } |
| 184 | + } |
| 185 | + catch |
| 186 | + {} |
| 187 | + } |
| 188 | + |
| 189 | + return dict; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments