コード例 #1
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);
            }
        }