Beispiel #1
0
        /// <summary>
        /// Returns the Feed Content information for a content object
        /// </summary>
        /// <param name="de">Parent DE</param>
        /// <param name="co">Content Object</param>
        /// <returns>Feed Content Interface</returns>
        public static IFeedContent FeedContent(DEv1_0 de, ContentObject co)
        {
            IFeedContent myContent  = null;
            XElement     xmlContent = co.XMLContent.EmbeddedXMLContent[0];

            //TODO: Fix this hack caused by fix to gml pos element name
            string contentString = xmlContent.ToString();

            contentString = contentString.Replace("gml:Pos", "gml:pos");
            var document = XDocument.Parse(contentString);

            xmlContent = document.Root;
            string deHash = DEUtilities.ComputeDELookupID(de).ToString();

            //TODO: Update FeedContent method to support all supported standards
            //content is a SensorThings Observation
            //if (xmlContent.Name.LocalName.Equals("Observation", StringComparison.InvariantCultureIgnoreCase) &&
            //  xmlContent.Name.NamespaceName.Equals(EDXLSharp.MQTTSensorThingsLib.Constants.STApiNamespace, StringComparison.InvariantCultureIgnoreCase) )
            //{
            //  XmlSerializer serializer = new XmlSerializer(typeof(Observation));
            //  Observation observation = (Observation)serializer.Deserialize(xmlContent.CreateReader());
            //  myContent = new STContent(observation, deHash);
            //}
            //content could be CoT or NIEM EMLC, need to check the namespace to know for sure
            if (xmlContent.Name.LocalName.Equals("event", StringComparison.InvariantCultureIgnoreCase))
            {
                //content could be CoT or NIEM EMLC, need to check the namespace to know for sure
                //if (xmlContent.Name.NamespaceName.Equals(@"urn:cot:mitre:org", StringComparison.InvariantCultureIgnoreCase))
                //{
                //  string cotString = xmlContent.ToString();
                //  //remove namespace or CoTLibrary blows up
                //  //brute force method
                //  int index, end, len;
                //  index = cotString.IndexOf("xmlns=");
                //  while (index != -1)
                //  {
                //    end = cotString.IndexOf('"', index);
                //    end++;
                //    end = cotString.IndexOf('"', end);
                //    end++;
                //    len = end - index;
                //    cotString = cotString.Remove(index, len);
                //    index = cotString.IndexOf("xmlns=");
                //  }
                //  CoT_Library.CotEvent anEvent = new CoT_Library.CotEvent(cotString);

                //  CoTContent mCC = new CoTContent(anEvent, deHash);

                //  myContent = mCC;
                //}
                if (xmlContent.Name.NamespaceName.Equals(@"http://release.niem.gov/niem/domains/emergencyManagement/3.1/emevent/0.1/emlc/", StringComparison.InvariantCultureIgnoreCase))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Event));
                    Event         emlc       = (Event)serializer.Deserialize(xmlContent.CreateReader());
                    myContent = new EMLCContent(emlc, deHash);
                }
            }

            return(myContent);
        }
Beispiel #2
0
        /// <summary>
        /// Will load the referenced schema files and use them to validate the given NIEM xml message.  Returns null
        /// if an error occurs.
        /// </summary>
        /// <param name="xmldata">NIEM message as xml</param>
        /// <param name="errorString">The error list</param>
        /// <returns>True if the message is valid, false otherwise</returns>
        /// <exception cref="IOException">Their was a problem loading the schema files</exception>
        /// <exception cref="FormatException">The schema files could not be parsed</exception>
        public static bool ValidateNiemSchema(string xmldata, out List <string> errorString)
        {
            /*
             * NOTE: When flattening the schema files XMLSpy it will add an "xs:appinfo" element
             * to some of the files.  If this element contains a child element "term:" then the
             * appinfo element must be removed.  Otherwise, visual studio cannot
             * load the schema file properly.
             */

            #region Initialize Readers and Error list

            errorString = null;
            XmlReader         vr      = null;
            XmlReaderSettings xs      = new XmlReaderSettings();
            XmlSchemaSet      coll    = new XmlSchemaSet();
            StringReader      xsdsr   = null;
            StringReader      xmlsr   = new StringReader(xmldata);
            XmlReader         xsdread = null;
            #endregion

            try
            {
                #region Load Schema Files
                string currentFile = ""; // For Error logging, holds schema file being added

                try
                {
                    currentFile = "temporalObjects";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.temporalObjects);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.TEMPORALOBJECTS, xsdread);


                    currentFile = "xs";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.xs);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.XS, xsdread);


                    currentFile = "emlc";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.emlc);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.EMLC, xsdread);


                    currentFile = "fips_10_4";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.fips_10_4);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.FIPS_10_4, xsdread);


                    currentFile = "census_uscounty";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.census_uscounty);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.CENSUS_USCOUNTY, xsdread);


                    currentFile = "mo";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.mo);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.MO, xsdread);


                    currentFile = "em_base";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.em_base);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.EM_BASE, xsdread);


                    currentFile = "fema_rtlt1";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.fema_rtlt1);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.FEMA_RTLT1, xsdread);


                    currentFile = "maid";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.maid);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.MAID, xsdread);


                    currentFile = "emcl";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.emcl);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.EMCL, xsdread);


                    currentFile = "unece_rec20_misc";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.unece_rec20_misc);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.UNECE_REC20_MISC, xsdread);


                    currentFile = "geospatial";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.geospatial);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.GEOSPATIAL, xsdread);


                    currentFile = "fips_5_2";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.fips_5_2);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.FIPS_5_2, xsdread);


                    currentFile = "spatialReferencing";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.spatialReferencing);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.SPATIALREFERENCING, xsdread);


                    currentFile = "core_misc";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.core_misc);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.CORE_MISC, xsdread);


                    currentFile = "niem_core";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.niem_core);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.NIEM_CORE, xsdread);


                    currentFile = "geometry";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.geometry);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.GEOMETRY, xsdread);


                    currentFile = "ols";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.ols);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.OLS, xsdread);


                    currentFile = "structures";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.structures);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.STRUCTURES, xsdread);


                    currentFile = "basicTypes";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.basicTypes);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.BASICTYPES, xsdread);


                    currentFile = "fema_rtlt";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.fema_rtlt);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.FEMA_RTLT, xsdread);


                    currentFile = "xlinks";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.xlinks);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.XLINKS, xsdread);


                    currentFile = "referenceSystems";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.referenceSystems);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.REFERENCESYSTEMS, xsdread);


                    currentFile = "dataQuality";
                    xsdsr       = new StringReader(Fresh.Global.Properties.Resources.dataQuality);
                    xmlsr       = new StringReader(xmldata);
                    xsdread     = XmlReader.Create(xsdsr);
                    coll.Add(ValidationConstants.DATAQUALITY, xsdread);

                    xs.Schemas.Add(coll);
                    currentFile = "";
                }
                catch (Exception Ex)
                {
                    throw new FormatException("There was an error parsing the schema files", Ex);
                    currentFile = "";
                }

                #endregion

                // Will Hold errors found
                List <string> errors = null;

                xs.ValidationType          = ValidationType.Schema;
                xs.ValidationFlags        |= XmlSchemaValidationFlags.ProcessInlineSchema;
                xs.ValidationFlags        |= XmlSchemaValidationFlags.ReportValidationWarnings;
                xs.ValidationEventHandler += new ValidationEventHandler((object sender, ValidationEventArgs args) =>
                {
                    if (args.Severity == XmlSeverityType.Error)
                    {
                        if (errors == null)
                        {
                            errors = new List <string>();
                        }

                        errors.Add(args.Message);
                    }
                });

                vr = XmlReader.Create(xmlsr, xs);
                while (vr.Read())
                {
                }

                // Setting the return value
                errorString = errors;
            }
            catch (IOException Ex)
            {
                DEUtilities.LogMessage(string.Format("[{0}] {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, Ex.Message), DEUtilities.LogLevel.Error, Ex);
                throw;
            }
            catch (FormatException Ex)
            {
                DEUtilities.LogMessage(string.Format("[{0}] {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, Ex.Message), DEUtilities.LogLevel.Error, Ex);
                throw;
            }
            catch (Exception Ex)
            {
                DEUtilities.LogMessage(string.Format("[{0}] {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, Ex.Message), DEUtilities.LogLevel.Error, Ex);

                throw new Exception(string.Format("[{0}] {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, Ex.Message), Ex);
            }
            finally
            {
                // Closing
                if (vr != null)
                {
                    vr.Close();
                }
                if (xmlsr != null)
                {
                    xmlsr.Close();
                }
                if (xsdread != null)
                {
                    xsdread.Close();
                }
                if (xsdsr != null)
                {
                    xsdsr.Close();
                }
            }

            return(errorString == null);
        }