Exemple #1
0
        bool validateEventTopic(TopicInfo expectedTopicInfo, NotificationMessageHolderType msg, StringBuilder logger)
        {
            var actualTopicInfo = TopicInfo.ExtractTopicInfoAll(msg.Topic.Any.First().InnerText, msg.Topic.Any.First());

            //return expectedTopicInfo.GetPlainInfo().Topic == actualTopicInfo.GetPlainInfo().Topic;
            if (!fillNamespaces(actualTopicInfo, msg, logger))
            {
                return(false);
            }

            //TO DO
            return(TopicInfo.TopicsMatch(actualTopicInfo, expectedTopicInfo));
        }
        public static TopicInfo ExtractTopicInfo(Event.NotificationMessageHolderType notification,
                                                 XmlElement messageRawElement, XmlNamespaceManager manager, out string err)
        {
            if (notification.Topic == null)
            {
                err = "Topic is null";
                return(null);
            }

            XmlText text = null;

            if (notification.Topic.Any != null)
            {
                foreach (XmlNode any in notification.Topic.Any)
                {
                    XmlText current = any as XmlText;
                    if (any != null)
                    {
                        text = current;
                        break;
                    }
                }
            }

            XmlNode topicNode = messageRawElement.SelectSingleNode("b2:Topic", manager);

            string topic = text != null ? text.Value : "";

            TopicInfo actualTopic = TopicInfo.ExtractTopicInfoAll(topic, topicNode);

            TopicInfo currentTopic = actualTopic;

            while (currentTopic != null)
            {
                if (currentTopic.ParentTopic == null && string.IsNullOrEmpty(currentTopic.NamespacePrefix))
                {
                    err = string.Format("Topic {0} is incorrect: root topic must have namespace defined", topic);
                    return(null);
                }
                if (string.IsNullOrEmpty(currentTopic.Namespace))
                {
                    err = string.Format("Topic {0} is incorrect: namespace prefix {1} not defined", topic, currentTopic.NamespacePrefix);
                    return(null);
                }
                currentTopic = currentTopic.ParentTopic;
            }

            err = string.Empty;
            return(actualTopic);
        }
Exemple #3
0
        public bool Validate(Proxies.Event.NotificationMessageHolderType msg,
                             TopicInfo expectedTopicInfo,
                             string validationScriptPath, StringBuilder logger, Dictionary <string, string> variables)
        {
            //System.Windows.Forms.MessageBox.Show(System.IO.Directory.GetCurrentDirectory());
            try
            {
                if (!validateEventTopic(expectedTopicInfo, msg, logger))
                {
                    var actualTopicInfo = TopicInfo.ExtractTopicInfoAll(msg.Topic.Any.First().InnerText, msg.Topic.Any.First());
                    logger.AppendLine(string.Format("Invalid notification: event with topic {0} is unexpected", actualTopicInfo.GetDescription()));

                    return(false);
                }

                var dst = new StringBuilder();
                var s   = new XmlSerializer(typeof(NotificationMessageHolderType));
                s.Serialize(new StringWriter(dst), msg);


                //get the xml manager
                XmlDataManager lManager = Zorba.getXmlDataManager();
                //create an empty Item to store the iterator of the parsed document
                Item lDoc = Item.createEmptyItem();
                //parse the xml document into the Iterator
                var xml = dst.ToString().Replace("utf-16", "utf-8");
                //LogStepEvent(longName.Count().ToString());
                //LogStepEvent(xml);
                Iterator lDocIter = lManager.parseXML(xml);
                lDocIter.open();
                //get the root node of the document
                lDocIter.next(lDoc);
                //close the Iterator
                lDocIter.close();
                ////Even though C# is a garbage collected language the Iterator must be destroyed manually
                lDocIter.destroy();

                StaticContext stContext = Zorba.createStaticContext();
                stContext.addNamespace("wsnt", "http://docs.oasis-open.org/wsn/b-2");
                stContext.addNamespace("xs", "http://www.w3.org/2001/XMLSchema");

                foreach (var validationScript in resourceLines(validationScriptPath))
                {
                    var query = string.Format("import schema namespace tt = '{0}' at 'file://localhost/{1}';", OnvifMessage.ONVIF, OnvifSchemaPath);
                    query += Environment.NewLine + validationScript;

                    //XQuery compiledQuery = Zorba.createQuery();
                    //compiledQuery.compile(query, stContext);
                    XQuery compiledQuery = Zorba.compileQuery(query, stContext);
                    //get the Dynamic Context of a Query
                    DynamicContext dynamicContext = compiledQuery.getDynamicContext();
                    //set the Context Item of the Query
                    dynamicContext.setContextItem(lDoc);

                    if (null != variables)
                    {
                        foreach (var variable in variables)
                        {
                            dynamicContext.setVariable(variable.Key, Zorba.getItemFactory().createString(variable.Value));
                        }
                    }

                    var options = new SerializationOptions();
                    options.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
                    var q = compiledQuery.execute(options);

                    string queryResult, log;
                    var    flag = parseAnswer(q, out queryResult, out log);
                    if (flag && "false" == queryResult || !flag)
                    {
                        logger.AppendLine(log);
                        return(false);
                    }
                    compiledQuery.destroy();
                }

                return(true);
            }
            catch (Exception e)
            {
                logger.AppendLine(e.ToString());
                return(false);
            }
            catch
            {
                logger.AppendLine("Unknown exception!");
                return(false);
            }
        }