/// <summary>
 /// Called as part for the serialization chain. Returns false if it cannot serialize the XML element.
 /// </summary>
 public override bool Serialize(XElement node, object obj, ITypeData expectedType)
 {
     if (expectedType.IsA(typeof(PackageDependency)) == false)
     {
         return(false);
     }
     node.Name = "Package";
     node.Name = "PackageDependency"; // TODO: remove when server is updated (this is only here for support of the TAP 8.x Repository server that does not yet have a parser that can handle the new name)
     node.SetAttributeValue("type", null);
     foreach (var prop in expectedType.GetMembers())
     {
         object val  = prop.GetValue(obj);
         string name = prop.Name;
         if (val == null)
         {
             continue;
         }
         if (name == "Name")
         {
             name = "Package"; // TODO: remove when server is updated (this is only here for support of the TAP 8.x Repository server that does not yet have a parser that can handle the new name)
         }
         node.SetAttributeValue(name, val);
     }
     return(true);
 }
        /// <summary>
        /// Called as part for the deserialization chain. Returns false if it cannot serialize the XML element.
        /// </summary>
        public override bool Deserialize(XElement node, ITypeData t, Action <object> setter)
        {
            if (t.IsA(typeof(PackageDependency)))
            {
                string name    = null;
                string version = null;
                foreach (XAttribute attr in node.Attributes())
                {
                    switch (attr.Name.LocalName)
                    {
                    case "Version":
                        version = attr.Value;
                        break;

                    case "Package":     // TAP 8.0 support
                    case "Name":
                        name = attr.Value;
                        break;

                    default:
                        Debug.Assert(false);
                        break;
                    }
                }
                setter.Invoke(new PackageDependency(name, ConvertVersion(version), version));
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        /// <summary> Deserialization implementation for SecureString. </summary>
        /// <param name="node"></param>
        /// <param name="targetType"></param>
        /// <param name="setResult"></param>
        /// <returns></returns>
        public override bool Deserialize(XElement node, ITypeData targetType, Action <object> setResult)
        {
            if (targetType.IsA(typeof(System.Security.SecureString)) == false)
            {
                return(false);
            }
            string valueString = node.Value;
            var    result      = new System.Security.SecureString();

            setResult(result);
            try
            {
                string encryptedString = CryptographyHelper.Decrypt(valueString, password);
                foreach (var c in encryptedString)
                {
                    result.AppendChar(c);
                }

                return(true);
            }
            catch
            {
                return(true);
            }
        }
Exemplo n.º 4
0
        /// <summary> Deserialization implementation. </summary>
        public override bool Deserialize(XElement elem, ITypeData t, Action <object> setResult)
        {
            if (t.IsA(typeof(TestStepList)) == false)
            {
                return(false);
            }
            var steps = new TestStepList();

            foreach (var subnode in elem.Elements())
            {
                ITestStep result = null;
                try
                {
                    if (!Serializer.Deserialize(subnode, x => result = (ITestStep)x))
                    {
                        Log.Warning(subnode, "Unable to deserialize step.");
                        continue; // skip to next step.
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    continue;
                }

                if (result != null)
                {
                    steps.Add(result);
                }
            }
            setResult(steps);
            return(true);
        }
Exemplo n.º 5
0
        /// <summary> Tries to deserialize a MacroString. This is just a simple string value XML element, but it tries to find the step context for the MacroString.</summary>
        public override bool Deserialize(XElement node, ITypeData t, Action <object> setter)
        {
            if (t.IsA(typeof(MacroString)) == false)
            {
                return(false);
            }
            string text = node.Value;
            var    obj  = Serializer.SerializerStack.OfType <ObjectSerializer>().FirstOrDefault();

            if (obj != null)
            {
                setter(new MacroString(obj.Object as ITestStep)
                {
                    Text = text
                });
            }
            else
            {
                setter(new MacroString {
                    Text = text
                });
            }

            return(true);
        }
Exemplo n.º 6
0
        public override bool Deserialize(XElement node, ITypeData t, Action <object> setter)
        {
            if (t.IsA(typeof(PackageVersion[])))
            {
                var list = new List <PackageVersion>();
                foreach (var element in node.Elements())
                {
                    list.Add(DeserializePackageVersion(element));
                }

                setter(list.ToArray());
                return(true);
            }
            if (t.IsA(typeof(PackageVersion)))
            {
                setter(DeserializePackageVersion(node));
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        public bool Deserialize(XElement node, ITypeData t, Action <object> setter)
        {
            if (t.IsA(typeof(Verdict)))
            {
                if (node.Value == legacyNotSetName)
                {
                    node.SetValue(nameof(Verdict.NotSet));
                    return(TapSerializer.GetCurrentSerializer().Serialize(node, setter, t));
                }
            }

            if (node.Name == TestStepName && t.DescendsTo(typeof(DialogStep)))
            {
                if (currentBox != null)
                {
                    return(false);
                }

                currentBox = new Box();
                var serializer = TapSerializer.GetCurrentSerializer();
                return(serializer.Deserialize(node, x =>
                {
                    currentBox.Step = (DialogStep)x;
                    setter(x);
                }, t));
            }
            else if (currentBox != null && node.Name == legacyDefaultAnswerPropertyName)
            {
                if (bool.TryParse(node.Value, out bool defaultAnswer))
                {
                    node.Remove();
                    var serializer = TapSerializer.GetCurrentSerializer();
                    var thisbox    = currentBox;
                    serializer.DeferLoad(() =>
                    {
                        if (thisbox.Step == null)
                        {
                            return;
                        }
                        if (defaultAnswer)
                        {
                            thisbox.Step.DefaultAnswer = thisbox.Step.PositiveAnswer;
                        }
                        else
                        {
                            thisbox.Step.DefaultAnswer = thisbox.Step.NegativeAnswer;
                        }
                    });
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Deserializes a test plan from XML.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="_t"></param>
        /// <param name="setter"></param>
        /// <returns></returns>
        public override bool Deserialize(XElement element, ITypeData _t, Action <object> setter)
        {
            if (_t.IsA(typeof(TestPlan)) == false)
            {
                return(false);
            }
            var prevPlan = Plan;

            Plan = new TestPlan {
                Path = Serializer.ReadPath
            };
            try
            {
                return(TryDeserializeObject(element, _t, setter, Plan));
            }
            finally
            {
                Plan = prevPlan;
            }
        }
        /// <summary>
        /// Called as part for the deserialization chain. Returns false if it cannot serialize the XML element.
        /// </summary>
        public override bool Deserialize(XElement node, ITypeData t, Action <object> setter)
        {
            if ((node.Name.LocalName == "Package" && t.IsA(typeof(PackageDef))) ||
                (node.Name.LocalName == nameof(PackageIdentifier) && t.IsA(typeof(PackageIdentifier))))
            {
                var pkg = new PackageDef();
                foreach (XAttribute attr in node.Attributes())
                {
                    switch (attr.Name.LocalName)
                    {
                    case "Version":
                        pkg.RawVersion = attr.Value;
                        if (SemanticVersion.TryParse(attr.Value, out var semver))
                        {
                            pkg.Version = semver;
                        }
                        break;

                    case "Date":
                        if (DateTime.TryParse(attr.Value, out DateTime date))
                        {
                            pkg.Date = date;
                        }
                        else
                        {
                            pkg.Date = DateTime.MinValue;
                        }
                        break;

                    case "Architecture":
                        pkg.Architecture = (CpuArchitecture)Enum.Parse(typeof(CpuArchitecture), attr.Value);
                        break;

                    default:
                        var prop = pkg.GetType().GetProperty(attr.Name.LocalName);
                        if (prop != null)
                        {
                            prop.SetValue(pkg, attr.Value);
                        }
                        break;
                    }
                }
                foreach (var elm in node.Elements())
                {
                    switch (elm.Name.LocalName)
                    {
                    case "Description":
                        pkg.Description = Regex.Match(elm.ToString().Replace("\r", ""), "^<Description.*?>((?:.|\\n)+)</Description>", RegexOptions.Multiline).Groups[1].Value.Trim();
                        break;

                    case "PackageRepositoryUrl":
#pragma warning disable 618
                        pkg.Location = elm.Value;
#pragma warning restore 618
                        break;

                    default:
                        var prop = pkg.GetType().GetProperty(elm.Name.LocalName);
                        if (prop != null)
                        {
                            Serializer.Deserialize(elm, o => prop.SetValue(pkg, o), prop.PropertyType);
                        }
                        break;
                    }
                }

                if (t.IsA(typeof(PackageIdentifier)))
                {
                    setter.Invoke(new PackageIdentifier(pkg));
                }
                else
                {
                    setter.Invoke(pkg);
                }

                // If the Version XML attribute is missing, default to same behavior as if Version="" was specified. We depend on packages having a version.
                if (pkg.Version is null && string.IsNullOrEmpty(pkg.RawVersion))
                {
                    pkg.RawVersion = "";
                    if (SemanticVersion.TryParse("0.0.0", out var semver))
                    {
                        pkg.Version = semver;
                    }
                }

                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Called as part for the serialization chain. Returns false if it cannot serialize the XML element.
        /// </summary>
        public override bool Serialize(XElement node, object obj, ITypeData expectedType)
        {
            if (expectedType.IsA(typeof(PackageDef)) == false && expectedType.IsA(typeof(PackageIdentifier)) == false)
            {
                return(false);
            }
            XNamespace ns = "http://opentap.io/schemas/package";

            node.Name = ns + (expectedType.IsA(typeof(PackageIdentifier)) ? nameof(PackageIdentifier) : "Package");
            node.SetAttributeValue("type", null);
            foreach (var prop in expectedType.GetMembers())
            {
                object val = prop.GetValue(obj);
                if (false == val is string && val is IEnumerable && (val as IEnumerable).GetEnumerator().MoveNext() == false)
                {
                    continue;  // don't write empty enumerables
                }
                var defaultAttr = prop.GetAttribute <DefaultValueAttribute>();
                if (defaultAttr != null && object.Equals(defaultAttr.Value, val))
                {
                    continue;
                }
                switch (prop.Name)
                {
                case "RawVersion":
                    continue;

                case "Date":
                    if (((DateTime)val) != DateTime.MinValue)
                    {
                        node.SetAttributeValue("Date", ((DateTime)val).ToString(CultureInfo.InvariantCulture));
                    }
                    break;

                case "Description":
                    var mngr = new XmlNamespaceManager(new NameTable());
                    mngr.AddNamespace("", ns.NamespaceName);     // or proper URL
                    var parserContext = new XmlParserContext(null, mngr, null, XmlSpace.None, null);
                    var txtReader     = new XmlTextReader($"<Description>{val}</Description>", XmlNodeType.Element, parserContext);
                    var ele           = XElement.Load(txtReader);
                    node.Add(ele);
                    break;

                default:
                    var xmlAttr = prop.GetAttributes <XmlAttributeAttribute>().FirstOrDefault();
                    if (xmlAttr != null)
                    {
                        string name = prop.Name;
                        if (!String.IsNullOrWhiteSpace(xmlAttr.AttributeName))
                        {
                            name = xmlAttr.AttributeName;
                        }
                        node.SetAttributeValue(name, val);
                    }
                    else
                    {
                        var elm = new XElement(prop.Name);
                        if (obj != null)
                        {
                            Serializer.Serialize(elm, val, expectedType: prop.TypeDescriptor);
                        }
                        void SetNs(XElement e)
                        {
                            e.Name = ns + e.Name.LocalName;
                            foreach (var n in e.Elements())
                            {
                                SetNs(n);
                            }
                        }

                        SetNs(elm);
                        node.Add(elm);
                    }
                    break;
                }
            }
            node.SetAttributeValue("xmlns", ns);

            // ask the TestPlanPackageDependency serializer (the one that writes the
            // <Package.Dependencies> tag in the bottom of e.g. TestPlan files) to
            // not write the tag for this file.
            var depSerializer = Serializer.GetSerializer <TestPlanPackageDependency>();

            if (depSerializer != null)
            {
                depSerializer.WritePackageDependencies = false;
            }

            return(true);
        }
Exemplo n.º 11
0
        public override bool Serialize(XElement node, object obj, ITypeData expectedType)
        {
            if (expectedType.IsA(typeof(PackageFile)) == false)
            {
                return(false);
            }

            foreach (IMemberData prop in expectedType.GetMembers().Where(s => !s.HasAttribute <XmlIgnoreAttribute>()))
            {
                object val              = prop.GetValue(obj);
                string name             = prop.Name;
                var    defaultValueAttr = prop.GetAttribute <DefaultValueAttribute>();
                if (defaultValueAttr != null)
                {
                    if (Object.Equals(defaultValueAttr.Value, val))
                    {
                        continue;
                    }
                    if (defaultValueAttr.Value == null)
                    {
                        var enu = val as IEnumerable;
                        if (enu != null && enu.GetEnumerator().MoveNext() == false) // the value is an empty IEnumerable
                        {
                            continue;                                               // We take an empty IEnumerable to be the same as null
                        }
                    }
                }

                if (name == "RelativeDestinationPath")
                {
                    name = "Path";
                }

                if (name == "DoObfuscate")
                {
                    name = "Obfuscate";
                }

                if (name == "Plugins")
                {
                    XElement plugins = new XElement("Plugins");
                    Serializer.Serialize(plugins, val, prop.TypeDescriptor);
                    node.Add(plugins);
                    continue;
                }
                if (name == "IgnoredDependencies")
                {
                    if (val is List <string> igDeps)
                    {
                        foreach (string igDep in igDeps)
                        {
                            node.Add(new XElement("IgnoreDependency")
                            {
                                Value = igDep
                            });
                        }
                        continue;
                    }
                }
                if (name == "CustomData")
                {
                    if (val is List <ICustomPackageData> packageActions)
                    {
                        foreach (ICustomPackageData action in packageActions)
                        {
                            if (action is MissingPackageData)
                            {
                                continue;
                            }

                            XElement xAction = new XElement(action.GetType().GetDisplayAttribute().Name);
                            Serializer.Serialize(xAction, action, TypeData.GetTypeData(action));
                            node.Add(xAction);
                        }
                    }
                    continue;
                }

                node.SetAttributeValue(name, val);
            }


            return(true);
        }
Exemplo n.º 12
0
        public override bool Deserialize(XElement node, ITypeData t, Action <object> setter)
        {
            if (node.Name.LocalName == "File" && t.IsA(typeof(PackageFile)))
            {
                PackageFile packageFile = new PackageFile();

                foreach (XAttribute attr in node.Attributes())
                {
                    switch (attr.Name.LocalName)
                    {
                    case "Path":
                        packageFile.RelativeDestinationPath = attr.Value;
                        break;

                    case "SourcePath":
                        packageFile.SourcePath = attr.Value;
                        break;

                    case "LicenseRequired":
                        packageFile.LicenseRequired = attr.Value;
                        break;
                    }
                }

                foreach (XElement elm in node.Elements())
                {
                    if (elm.Name.LocalName == "Plugins")
                    {
                        Serializer.Deserialize(elm, o => packageFile.Plugins = o as List <PluginFile>, typeof(List <PluginFile>));
                        continue;
                    }

                    if (elm.Name.LocalName == "IgnoreDependency")
                    {
                        packageFile.IgnoredDependencies.Add(elm.Value);
                        continue;
                    }

                    var handlingPlugins = plugins.Where(s => s.GetType().GetDisplayAttribute().Name == elm.Name.LocalName).ToArray();


                    if (handlingPlugins.Length > 0)
                    {
                        if (handlingPlugins.Length > 1)
                        {
                            Log.Warning($"Detected multiple plugins able to handle XMl tag {elm.Name.LocalName}. Unexpected behavior may occur.");
                        }

                        ICustomPackageData p = handlingPlugins[0];
                        if (elm.HasAttributes || !elm.IsEmpty)
                        {
                            Serializer.Deserialize(elm, o => p = (ICustomPackageData)o, p.GetType());
                        }
                        packageFile.CustomData.Add(p);
                        continue;
                    }

                    packageFile.CustomData.Add(new MissingPackageData(elm));
                }

                setter.Invoke(packageFile);
                return(true);
            }

            return(false);
        }