/// <summary>
        /// Deserializes an XML representation of a <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see>
        /// </summary>
        /// <param name="element">An <c ref="System.Linq.Xml.XElement">XElement</c> containing a cs-entry element</param>
        /// <returns>a <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see> object</returns>
        public static CSEntryChange Deserialize(XElement element)
        {
            CSEntryChange csentry = CSEntryChange.Create();

            CSEntryChangeDeserializer.Deserialize(element, csentry);
            return(csentry);
        }
 /// <summary>
 /// Reads the <![CDATA[<anchor-attributes>]]> node of the XML representation of the <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see>
 /// </summary>
 /// <param name="element">An <c ref="System.Linq.Xml.XElement">XElement</c> containing an <![CDATA[<ancchor-attributes>]]> element</param>
 /// <param name="csentry">The <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see> to populate</param>
 private static void XmlReadAnchorAttributesNode(XElement element, CSEntryChange csentry)
 {
     foreach (XElement child in element.Elements().Where(t => t.Name.LocalName == "anchor-attribute"))
     {
         CSEntryChangeDeserializer.XmlReadAnchorAttributeNode(child, csentry);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Loads the queue from the specified file
        /// </summary>
        /// <param name="filename">The name of the file</param>
        public static void LoadQueue(string filename)
        {
            lock (CSEntryChangeQueue.queue)
            {
                CSEntryChangeQueue.queue.Clear();

                foreach (CSEntryChange csentry in CSEntryChangeDeserializer.Deserialize(filename))
                {
                    CSEntryChangeQueue.Add(csentry);
                }
            }
        }
        /// <summary>
        /// Reads the <![CDATA[<value-changes>]]> node of the XML representation of the <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see>
        /// </summary>
        /// <param name="element">An <c ref="System.Linq.Xml.XElement">XElement</c> containing an <![CDATA[<value-changes>]]> element</param>
        /// <param name="attributeType">The type of attribute to read</param>
        /// <returns>A list of ValueChanges</returns>
        private static List <ValueChange> XmlReadValueChangesNode(XElement element, AttributeType attributeType)
        {
            List <ValueChange> valueChanges = new List <ValueChange>();

            foreach (XElement child in element.Elements().Where(t => t.Name.LocalName == "value-change"))
            {
                ValueChange change = CSEntryChangeDeserializer.XmlReadValueChangeNode(child, attributeType);
                if (change != null)
                {
                    valueChanges.Add(change);
                }
            }

            return(valueChanges);
        }
        /// <summary>
        /// Deserializes an XML representation of a <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see> into an existing object
        /// </summary>
        /// <param name="element">An <c ref="System.Linq.Xml.XElement">XElement</c> containing a cs-entry element</param>
        /// <param name="csentry">The <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see> to populate</param>
        private static void Deserialize(XElement element, CSEntryChange csentry)
        {
            if (element.Name.LocalName != "object-change")
            {
                throw new ArgumentException("The XML node provided was not an <object-change> node", "node");
            }

            foreach (XElement child in element.Elements())
            {
                if (child.Name.LocalName == "modification-type")
                {
                    ObjectModificationType modificationType;
                    string modificationTypeString = (string)child;

                    if (Enum.TryParse <ObjectModificationType>(modificationTypeString, out modificationType))
                    {
                        csentry.ObjectModificationType = modificationType;
                    }
                    else
                    {
                        throw new InvalidCastException(string.Format("Cannot convert '{0}' to type {1}", modificationTypeString, typeof(ObjectModificationType).Name));
                    }
                }
                else if (child.Name.LocalName == "object-class")
                {
                    csentry.ObjectType = (string)child;
                }
                else if (child.Name.LocalName == "dn")
                {
                    csentry.DN = (string)child;
                }
                else if (child.Name.LocalName == "attribute-changes")
                {
                    CSEntryChangeDeserializer.XmlReadAttributeChangesNode(child, csentry);
                }
                else if (child.Name.LocalName == "anchor-attributes")
                {
                    CSEntryChangeDeserializer.XmlReadAnchorAttributesNode(child, csentry);
                }
            }
        }
        /// <summary>
        /// Deserializes a file into a list of <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see> objects
        /// </summary>
        /// <param name="file">The file of <see ref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChanges</see> to deserialize</param>
        /// <returns>A list of <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChanges</see></returns>
        public static IList <CSEntryChange> Deserialize(string file)
        {
            List <CSEntryChange> csentries = new List <CSEntryChange>();

            using (StreamReader r = new StreamReader(file))
            {
                using (XmlReader reader = XmlReader.Create(r))
                {
                    XDocument doc = XDocument.Load(reader);

                    foreach (XElement node in doc.Root.Elements("object-change"))
                    {
                        CSEntryChange csentry = CSEntryChangeDeserializer.Deserialize(node);
                        csentries.Add(csentry);
                    }

                    reader.Close();
                }
                return(csentries);
            }
        }
        /// <summary>
        /// Reads the <![CDATA[<attribute-change]]> node of the XML representation of the <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see>
        /// </summary>
        /// <param name="element">An <c ref="System.Linq.Xml.XElement">XElement</c> containing an <![CDATA[<attribute-change>]]> element</param>
        /// <param name="csentry">The <see cref="Microsoft.MetadirectoryServices.CSEntryChange">CSEntryChange</see> to populate</param>
        private static void XmlReadAttributeChangeNode(XElement element, CSEntryChange csentry)
        {
            string name = null;
            AttributeModificationType modificationType = AttributeModificationType.Unconfigured;
            AttributeType             dataType         = AttributeType.Undefined;
            List <ValueChange>        valueChanges     = null;
            AttributeChange           attributeChange  = null;

            foreach (XElement child in element.Elements())
            {
                if (child.Name.LocalName == "name")
                {
                    name = (string)child;
                }
                else if (child.Name.LocalName == "modification-type")
                {
                    string modificationTypeString = (string)child;

                    if (!Enum.TryParse <AttributeModificationType>(modificationTypeString, out modificationType))
                    {
                        throw new InvalidCastException(string.Format("Cannot convert '{0}' to type {1}", modificationTypeString, typeof(AttributeModificationType).Name));
                    }
                }
                else if (child.Name.LocalName == "data-type")
                {
                    string dataTypeString = (string)child;

                    if (!Enum.TryParse <AttributeType>(dataTypeString, out dataType))
                    {
                        throw new InvalidCastException(string.Format("Cannot convert '{0}' to type '{1}'", dataTypeString, typeof(AttributeType).Name));
                    }
                }
                else if (child.Name.LocalName == "value-changes")
                {
                    if (string.IsNullOrWhiteSpace(name))
                    {
                        throw new ArgumentException("The attribute name must appear first in the list of <attribute-change> elements");
                    }

                    if (dataType == AttributeType.Undefined)
                    {
                        dataType = AttributeType.String;
                    }

                    valueChanges = CSEntryChangeDeserializer.XmlReadValueChangesNode(child, dataType);
                }
            }

            if (valueChanges == null)
            {
                return;
            }

            switch (modificationType)
            {
            case AttributeModificationType.Add:
                if (valueChanges.Count == 0)
                {
                    // discard attribute change with no values
                    return;
                }

                attributeChange = AttributeChange.CreateAttributeAdd(name, (valueChanges.Where(t => t.ModificationType == ValueModificationType.Add)).Select(t => t.Value).ToList());
                break;

            case AttributeModificationType.Replace:
                if (valueChanges.Count == 0)
                {
                    // discard attribute change with no values
                    return;
                    //throw new ArgumentException("The attribute replace in the CSEntry provided no values");
                }

                attributeChange = AttributeChange.CreateAttributeReplace(name, (valueChanges.Where(t => t.ModificationType == ValueModificationType.Add)).Select(t => t.Value).ToList());
                break;

            case AttributeModificationType.Delete:
                attributeChange = AttributeChange.CreateAttributeDelete(name);
                break;

            case AttributeModificationType.Update:
                if (valueChanges.Count == 0)
                {
                    // discard attribute change with no values
                    return;
                    //throw new ArgumentException("The attribute update in the CSEntry provided no values");
                }

                attributeChange = AttributeChange.CreateAttributeUpdate(name, valueChanges);

                break;

            case AttributeModificationType.Unconfigured:
            default:
                throw new NotSupportedException($"The modification type is not supported {modificationType}");
            }

            csentry.AttributeChanges.Add(attributeChange);
        }