Exemple #1
0
        private bool TryAddExpressionParameterr([NotNull] DynamicContextSettings dynamicContextSettings,
                                                [NotNull] XmlQualifiedName parameterName,
                                                [NotNull] string select,
                                                [NotNull] XPathSettings settings)
        {
            Assert.ArgumentNotNull(dynamicContextSettings, "dynamicContextSettings");
            Assert.ArgumentNotNull(parameterName, "parameterName");
            Assert.ArgumentNotNull(select, "select");
            Assert.ArgumentNotNull(settings, "settings");

            Log.LogMessage(MessageImportance.Low,
                           "adding parameter '{0}' as expression '{1}'", parameterName,
                           select);

            try
            {
                // TODO: Set base URI, handle errors differently
                var selectExpr = XPath.Compile(select, settings);

                if (selectExpr == null)
                {
                    Log.LogWarning("Parameter '{0}' has invalid value '{1}'", parameterName, select);
                    return(false);
                }

                var definition = selectExpr.Evaluate(dynamicContextSettings);
                dynamicContextSettings.Parameters.Add(parameterName, definition);
                return(true);
            }
            catch (XdmException e)
            {
                LogXdmException(Severity.Error, e.InnerException);
                return(false);
            }
        }
Exemple #2
0
        public override bool TestAssertion(string assertion)
        {
            bool DocOK  = true;
            bool FileOK = true;

            if (resultDocument != null)
            {
                XPathSettings xpathSettings = new XPathSettings(nameTable)
                {
                    ContextItemType = XdmType.Node
                };
                var xpath       = XPath.Compile(assertion, xpathSettings);
                var contextItem = resultDocument.CreateNavigator();
                var settings    = new DynamicContextSettings {
                    ContextItem = contextItem
                };
                DocOK = xpath.EvaluateToItem(contextItem).ValueAsBoolean;
            }
            if (resultFile != null)
            {
                XdmDocument resultDoc;
                using (var reader = XmlReader.Create(resultFile, xmlReaderSettings))
                {
                    resultDoc = new XdmDocument(reader);
                    reader.Close();
                }
                XPathSettings xpathSettings = new XPathSettings(nameTable)
                {
                    ContextItemType = XdmType.Node
                };
                var xpath       = XPath.Compile(assertion, xpathSettings);
                var contextItem = resultDoc.CreateNavigator();
                var settings    = new DynamicContextSettings {
                    ContextItem = contextItem
                };
                FileOK = xpath.EvaluateToItem(contextItem).ValueAsBoolean;
            }
            return(DocOK && FileOK);
        }
Exemple #3
0
        /// <summary>
        ///     Sets the parameters from the <see cref = "Parameters" /> property.
        /// </summary>
        /// <param name = "dynamicContextSettings">The dynamic context settings.</param>
        /// <param name = "staticContextSettings">The static context settings.</param>
        /// <returns><see langword="true" /> if settings the parameters succeeded; otherwise, <see langword="false" />.</returns>
        protected bool SetParameters([NotNull] DynamicContextSettings dynamicContextSettings,
                                     [NotNull] StaticContextSettings staticContextSettings)
        {
            Assert.ArgumentNotNull(dynamicContextSettings, "dynamicContextSettings");
            Assert.ArgumentNotNull(staticContextSettings, "staticContextSettings");

            if (string.IsNullOrEmpty(Parameters))
            {
                return(true);
            }

            var settings = new XPathSettings(staticContextSettings.NameTable)
            {
                BaseURI                  = staticContextSettings.BaseURI,
                CollationResolver        = staticContextSettings.CollationResolver,
                CollectionTypeResolver   = staticContextSettings.CollectionTypeResolver,
                DefaultCollation         = staticContextSettings.DefaultCollation,
                DocumentTypeResolver     = staticContextSettings.DocumentTypeResolver,
                DefaultFunctionNamespace = staticContextSettings.DefaultFunctionNamespace,
                Schemas                  = staticContextSettings.Schemas,
                XmlVersion               = staticContextSettings.XmlVersion,
                Libraries                = staticContextSettings.Libraries
            };

            var xmlReaderSettings = new XmlReaderSettings {
                ConformanceLevel = ConformanceLevel.Fragment
            };

            try
            {
                using (var reader = XmlReader.Create(new StringReader(Parameters), xmlReaderSettings))
                {
                    reader.Read();
                    while (!reader.EOF)
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                        {
                            if (reader.LocalName != "Parameter")
                            {
                                continue;
                            }

                            var name         = reader.GetAttribute("Name");
                            var namespaceUri = reader.GetAttribute("Namespace") ?? string.Empty;

                            if (name == null)
                            {
                                Log.LogWarning(
                                    "Invalid parameter declaration: {0}\nParameter must contain a Name attribute",
                                    reader.ReadOuterXml());
                                continue;
                            }

                            var value  = reader.GetAttribute("Value");
                            var select = reader.GetAttribute("Select");
                            var path   = reader.GetAttribute("Path");

                            if (value == null &&
                                select == null &&
                                path == null)
                            {
                                Log.LogWarning(
                                    "Invalid parameter declaration: {0}\nParameter must contain either a Value, Select or Path attribute",
                                    reader.ReadOuterXml());
                                continue;
                            }

                            if (value != null &&
                                (select != null || path != null) ||
                                (select != null && path != null))
                            {
                                Log.LogWarning(
                                    "Invalid parameter declaration: {0}\nParameter must contain either a Value, Select or Path attribute",
                                    reader.ReadOuterXml());
                                continue;
                            }

                            var parameterName = new XmlQualifiedName(name, namespaceUri);

                            if (!dynamicContextSettings.Parameters.Contains(parameterName))
                            {
                                if (value != null)
                                {
                                    Log.LogMessage(MessageImportance.Low, "adding parameter '{0}' as value '{1}'",
                                                   parameterName, value);

                                    dynamicContextSettings.Parameters.AddUntypedAtomic(parameterName, value);
                                }
                                else if (path != null)
                                {
                                    if (TryAddPathParameterr(dynamicContextSettings,
                                                             parameterName,
                                                             path) == false)
                                    {
                                        return(false);
                                    }
                                }
                                else
                                {
                                    settings.Namespaces = (IXmlNamespaceResolver)reader;

                                    if (TryAddExpressionParameterr(dynamicContextSettings,
                                                                   parameterName,
                                                                   select,
                                                                   settings) == false)
                                    {
                                        return(false);
                                    }
                                }
                            }
                            else
                            {
                                Log.LogWarning("Parameter '{0}' has multiple definitions", parameterName);
                            }

                            reader.Skip();
                            break;
                        }

                        default:
                            reader.Skip();
                            break;
                        }
                    }
                }

                return(true);
            }
            catch (XmlException e)
            {
                LogXmlException(Severity.Error, e);
                return(false);
            }
        }