diff options
Diffstat (limited to 'src/WixToolset.Data/Serialize/CodeDomReader.cs')
| -rw-r--r-- | src/WixToolset.Data/Serialize/CodeDomReader.cs | 161 |
1 files changed, 0 insertions, 161 deletions
diff --git a/src/WixToolset.Data/Serialize/CodeDomReader.cs b/src/WixToolset.Data/Serialize/CodeDomReader.cs deleted file mode 100644 index 76013936..00000000 --- a/src/WixToolset.Data/Serialize/CodeDomReader.cs +++ /dev/null | |||
| @@ -1,161 +0,0 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixToolset.Data.Serialize | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Diagnostics.CodeAnalysis; | ||
| 7 | using System.Globalization; | ||
| 8 | using System.Reflection; | ||
| 9 | using System.Xml; | ||
| 10 | |||
| 11 | /// <summary> | ||
| 12 | /// Class used for reading XML files in to the CodeDom. | ||
| 13 | /// </summary> | ||
| 14 | public class CodeDomReader | ||
| 15 | { | ||
| 16 | private Assembly[] assemblies; | ||
| 17 | |||
| 18 | /// <summary> | ||
| 19 | /// Creates a new CodeDomReader, using the current assembly. | ||
| 20 | /// </summary> | ||
| 21 | public CodeDomReader() | ||
| 22 | { | ||
| 23 | this.assemblies = new Assembly[] { Assembly.GetExecutingAssembly() }; | ||
| 24 | } | ||
| 25 | |||
| 26 | /// <summary> | ||
| 27 | /// Creates a new CodeDomReader, and takes in a list of assemblies in which to | ||
| 28 | /// look for elements. | ||
| 29 | /// </summary> | ||
| 30 | /// <param name="assemblies">Assemblies in which to look for types that correspond | ||
| 31 | /// to elements.</param> | ||
| 32 | public CodeDomReader(Assembly[] assemblies) | ||
| 33 | { | ||
| 34 | this.assemblies = assemblies; | ||
| 35 | } | ||
| 36 | |||
| 37 | /// <summary> | ||
| 38 | /// Loads an XML file into a strongly-typed code dom. | ||
| 39 | /// </summary> | ||
| 40 | /// <param name="filePath">File to load into the code dom.</param> | ||
| 41 | /// <returns>The strongly-typed object at the root of the tree.</returns> | ||
| 42 | [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.InvalidOperationException.#ctor(System.String)")] | ||
| 43 | public ISchemaElement Load(string filePath) | ||
| 44 | { | ||
| 45 | XmlDocument document = new XmlDocument(); | ||
| 46 | document.Load(filePath); | ||
| 47 | ISchemaElement schemaElement = null; | ||
| 48 | |||
| 49 | foreach (XmlNode node in document.ChildNodes) | ||
| 50 | { | ||
| 51 | XmlElement element = node as XmlElement; | ||
| 52 | if (element != null) | ||
| 53 | { | ||
| 54 | if (schemaElement != null) | ||
| 55 | { | ||
| 56 | throw new InvalidOperationException(WixDataStrings.EXP_MultipleRootElementsFoundInFile); | ||
| 57 | } | ||
| 58 | |||
| 59 | schemaElement = this.CreateObjectFromElement(element); | ||
| 60 | this.ParseObjectFromElement(schemaElement, element); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | return schemaElement; | ||
| 64 | } | ||
| 65 | |||
| 66 | /// <summary> | ||
| 67 | /// Sets an attribute on an ISchemaElement. | ||
| 68 | /// </summary> | ||
| 69 | /// <param name="schemaElement">Schema element to set attribute on.</param> | ||
| 70 | /// <param name="name">Name of the attribute to set.</param> | ||
| 71 | /// <param name="value">Value to set on the attribute.</param> | ||
| 72 | [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.InvalidOperationException.#ctor(System.String)")] | ||
| 73 | private static void SetAttributeOnObject(ISchemaElement schemaElement, string name, string value) | ||
| 74 | { | ||
| 75 | ISetAttributes setAttributes = schemaElement as ISetAttributes; | ||
| 76 | if (setAttributes == null) | ||
| 77 | { | ||
| 78 | throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, WixDataStrings.EXP_ISchemaElementDoesnotImplementISetAttribute, schemaElement.GetType().FullName)); | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | setAttributes.SetAttribute(name, value); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | /// <summary> | ||
| 87 | /// Parses an ISchemaElement from the XmlElement. | ||
| 88 | /// </summary> | ||
| 89 | /// <param name="schemaElement">ISchemaElement to fill in.</param> | ||
| 90 | /// <param name="element">XmlElement to parse from.</param> | ||
| 91 | [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.InvalidOperationException.#ctor(System.String)")] | ||
| 92 | private void ParseObjectFromElement(ISchemaElement schemaElement, XmlElement element) | ||
| 93 | { | ||
| 94 | foreach (XmlAttribute attribute in element.Attributes) | ||
| 95 | { | ||
| 96 | SetAttributeOnObject(schemaElement, attribute.LocalName, attribute.Value); | ||
| 97 | } | ||
| 98 | |||
| 99 | foreach (XmlNode node in element.ChildNodes) | ||
| 100 | { | ||
| 101 | XmlElement childElement = node as XmlElement; | ||
| 102 | if (childElement != null) | ||
| 103 | { | ||
| 104 | ISchemaElement childSchemaElement = null; | ||
| 105 | ICreateChildren createChildren = schemaElement as ICreateChildren; | ||
| 106 | if (createChildren == null) | ||
| 107 | { | ||
| 108 | throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, WixDataStrings.EXP_ISchemaElementDoesnotImplementICreateChildren, element.LocalName)); | ||
| 109 | } | ||
| 110 | else | ||
| 111 | { | ||
| 112 | childSchemaElement = createChildren.CreateChild(childElement.LocalName); | ||
| 113 | } | ||
| 114 | |||
| 115 | if (childSchemaElement == null) | ||
| 116 | { | ||
| 117 | childSchemaElement = this.CreateObjectFromElement(childElement); | ||
| 118 | if (childSchemaElement == null) | ||
| 119 | { | ||
| 120 | throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture, WixDataStrings.EXP_XmlElementDoesnotHaveISchemaElement, childElement.LocalName)); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | this.ParseObjectFromElement(childSchemaElement, childElement); | ||
| 125 | IParentElement parentElement = (IParentElement)schemaElement; | ||
| 126 | parentElement.AddChild(childSchemaElement); | ||
| 127 | } | ||
| 128 | else | ||
| 129 | { | ||
| 130 | XmlText childText = node as XmlText; | ||
| 131 | if (childText != null) | ||
| 132 | { | ||
| 133 | SetAttributeOnObject(schemaElement, "Content", childText.Value); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | /// <summary> | ||
| 140 | /// Creates an object from an XML element by digging through the assembly list. | ||
| 141 | /// </summary> | ||
| 142 | /// <param name="element">XML Element to create an ISchemaElement from.</param> | ||
| 143 | /// <returns>A constructed ISchemaElement.</returns> | ||
| 144 | private ISchemaElement CreateObjectFromElement(XmlElement element) | ||
| 145 | { | ||
| 146 | ISchemaElement schemaElement = null; | ||
| 147 | foreach (Assembly assembly in this.assemblies) | ||
| 148 | { | ||
| 149 | foreach (Type type in assembly.GetTypes()) | ||
| 150 | { | ||
| 151 | if (type.FullName.EndsWith(element.LocalName, StringComparison.Ordinal) | ||
| 152 | && typeof(ISchemaElement).IsAssignableFrom(type)) | ||
| 153 | { | ||
| 154 | schemaElement = (ISchemaElement)Activator.CreateInstance(type); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | return schemaElement; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
