Exemplo n.º 1
0
        public void TestWriteStructures(string file)
        {
            var structureReader = new StructureParsingManager();
            var fileInfo = new FileInfo(file);
            IStructureWorkspace structureWorkspace;
            using (var readable = new FileReadableDataLocation(fileInfo))
            {
                structureWorkspace = structureReader.ParseStructures(readable);
            }

            ISdmxObjects structureBeans = structureWorkspace.GetStructureObjects(false);

            string output = string.Format(CultureInfo.InvariantCulture, "test-sdmxv2.1-{0}", fileInfo.Name);
            var writtingManager = new StructureWriterManager();
            using (var outputStream = new FileStream(output, FileMode.Create))
            {
                writtingManager.WriteStructures(structureBeans, new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV21StructureDocument)), outputStream);
            }

            using (var readable = new FileReadableDataLocation(output))
            {
                XMLParser.ValidateXml(readable, SdmxSchemaEnumType.VersionTwoPointOne);
                var structures = structureReader.ParseStructures(readable);
                Assert.NotNull(structures);
            }
        }
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        public static void Main(string[] args)
        {
            // 1. Create the StructureWritingManager
            IStructureWriterManager writingManager = new StructureWriterManager();

            // 2. Create a test mutable codelist with Count codes.
            const int Count = 1000;
            string countStr = Count.ToString(CultureInfo.InvariantCulture);
            ICodelistMutableObject codelist = new CodelistMutableCore();
            codelist.Id = "CL_K" + countStr;
            codelist.AgencyId = "TEST";
            codelist.AddName("en", "Test CL with " + countStr);
            for (int i = 0; i < Count; i++)
            {
                ICodeMutableObject code = new CodeMutableCore();
                code.Id = i.ToString(CultureInfo.InvariantCulture);
                code.AddName("en", "Code " + code.Id);
                codelist.AddItem(code);
            }

            // 3. Select the out filename
            string output = string.Format(CultureInfo.InvariantCulture, "{0}.xml", codelist.Id);

            // 4. Create the immutable instance of the codelist
            ICodelistObject immutableInstance = codelist.ImmutableInstance;

            using (FileStream writer = File.OpenWrite(output))
            {
                // 5. Write the codelist as a SDMX-ML v2.1 Structure document. Other options include SDMX-ML 2.0 Structure or Registry Interface documents.
                writingManager.WriteStructure(immutableInstance, new HeaderImpl("ZZ9", "ZZ9"), new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV21StructureDocument)), writer);
            }
        }
Exemplo n.º 3
0
        public void SaveSDMXFile(ISdmxObjects sdmxObjects, StructureOutputFormatEnumType version, string outputFileName)
        {

            StructureWriterManager swm = new StructureWriterManager();

            StructureOutputFormat soFormat = StructureOutputFormat.GetFromEnum(version);
            IStructureFormat outputFormat = new SdmxStructureFormat(soFormat);

            MemoryStream memoryStream = new MemoryStream();

            swm.WriteStructures(sdmxObjects, outputFormat, memoryStream);


            byte[] bytesInStream = memoryStream.ToArray();
            memoryStream.Close();

            SendAttachment(bytesInStream, outputFileName + ".xml");
        }
        /// <summary>
        /// The main method.
        /// </summary>
        /// <param name="args">
        /// The parameter is not used.
        /// </param>
        public static void Main(string[] args)
        {
            // Steps 1 - 4 are performed in the new class DatabaseRetrievalManager, an implementation of the ISdmxObjectRetrievalManager interface.
            // DatabaseRetrievalManager: Step 1. Creating an implementation of  the ISdmxObjectRetrievalManager interface
            // DatabaseRetrievalManager: Step 2. Implementing ISdmxObjectRetrievalManager GetMaintainableObjects<> method.
            // DatabaseRetrievalManager: Step 3. Retrieving codelists from database. 
            // DatabaseRetrievalManager: Step 4. Retrieving Codes from the database.

            // Step 5. Create a ISdmxObjectRetrievalManager instance
            ISdmxObjectRetrievalManager retrievalManager = new DatabaseRetrievalManager();

            // Step 6. Create an IHeader  instance.
            // Create a new HeaderImpl instance with ID “IDREF0001”, Sender ID “ZZ9” 
            IHeader header = new HeaderImpl("IDREF001", "ZZ9"); // can be an instance or even a static field.

            // Step 7. Create the query
            IMaintainableRefObject query = new MaintainableRefObjectImpl("TEST_AGENCY", "TEST", "1.2");

            // Step 8. Get the codelist objects
            var codelistObjects = retrievalManager.GetMaintainableObjects<ICodelistObject>(query);

            // Step 9. Build the immutable object container.
            // Create an immutable container with the header and the codelist objects.
            // Note that immutable container can be modified.
            ISdmxObjects immutableObjects = new SdmxObjectsImpl(header, codelistObjects);

            // Step 10. Create an instance of the IStructureWriterManager.
            IStructureWriterManager structureWriterManager = new StructureWriterManager(); // can be an instance or even static field.

            // Step 11. Create a SdmxStructureFormat instance with StructureOutputFormat SdmxV21StructureDocument
            // SDMX v2.1. Create a SdmxStructureFormat instance with StructureOutputFormat SdmxV21StructureDocument. It can also be an instance or even a static field.
            IStructureFormat formatV21 = new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV21StructureDocument));

            // Step 12. Create a SdmxStructureFormat instance with StructureOutputFormat SdmxV2StructureDocument
            // SDMX v2.0. Create a SdmxStructureFormat instance with StructureOutputFormat SdmxV2StructureDocument. It can also be an instance or even a static field.
            IStructureFormat formatV20 = new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV2StructureDocument));


            // Step 13. Write to  the SDMX v2.1 structure file.
            using (Stream fileStream = File.Create(@"..\..\Exercise Output\outputv21.xml"))
            {
                // Write the objects in SDMX v2.1 format. Then flush the output.
                structureWriterManager.WriteStructures(immutableObjects, formatV21, fileStream);
                fileStream.Flush();
            }

            // Step 14. Write to  the SDMX v2.0 structure file. 
            using (Stream fileStream = File.Create(@"..\..\Exercise Output\outputv20.xml"))
            {
                // Write the objects in SDMX v2.0 format. Then flush the output.
                structureWriterManager.WriteStructures(immutableObjects, formatV20, fileStream);
                fileStream.Flush();
            }
        }
        private static System.IO.MemoryStream GetStream(ISdmxObjects sdmxObjects, Org.Sdmxsource.Sdmx.Api.Constants.StructureOutputFormatEnumType version)
        {
            Org.Sdmxsource.Sdmx.Api.Constants.StructureOutputFormat soFormat =
                Org.Sdmxsource.Sdmx.Api.Constants.StructureOutputFormat.GetFromEnum(version);
            Org.Sdmxsource.Sdmx.Api.Model.Format.IStructureFormat outputFormat =
                new Org.Sdmxsource.Sdmx.SdmxObjects.Model.SdmxStructureFormat(soFormat);

            Org.Sdmxsource.Sdmx.Structureparser.Manager.StructureWriterManager swm =
                new Org.Sdmxsource.Sdmx.Structureparser.Manager.StructureWriterManager();

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

            swm.WriteStructures(sdmxObjects, outputFormat, memoryStream);

            return memoryStream;
        }
        public void TestWriteStructuresV2DefaultObjectCount(string file)
        {
            ISdmxObjects objects;
            var fileInfo = new FileInfo(file);
            using (IReadableDataLocation location = new FileReadableDataLocation(fileInfo))
            {
                var structureWorkspace = this._parsingManager.ParseStructures(location);
                objects = structureWorkspace.GetStructureObjects(false);
            }

            StructureOutputFormat format = StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV2StructureDocument);
            var sdmxStructureFormat = new SdmxStructureFormat(format);
            var outputFileName = string.Format("{0}-output.xml", fileInfo.Name);
            using (var stream = File.Create(outputFileName))
            {
                var settings = new XmlWriterSettings { Indent = true };
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    IStructureWriterManager structureWritingManager = new StructureWriterManager(new SdmxStructureWriterFactory(writer));
                    structureWritingManager.WriteStructures(objects, sdmxStructureFormat, null);
                    writer.Flush();
                }
            }

            ISdmxObjects objects2;
            using (IReadableDataLocation location = new FileReadableDataLocation(outputFileName))
            {
                var structureWorkspace = this._parsingManager.ParseStructures(location);
                objects2 = structureWorkspace.GetStructureObjects(false);
            }

            Assert.AreEqual(objects.GetAllMaintainables().Count, objects2.GetAllMaintainables().Count);
        }
Exemplo n.º 7
0
        private void SaveSdmxOBJ(ISdmxObjects SdmxOBJ, string FileName)
        {
            if (SdmxOBJ == null)
                return;

            StructureWriterManager swm = new StructureWriterManager();
            StructureOutputFormat sofType = StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV2RegistryQueryResponseDocument);
            SdmxStructureFormat sof = new SdmxStructureFormat(sofType);
            string FullNamePath = Path.Combine(Utils.GetTreeCachePath(), FileName);
            using (Stream ms = File.Create(FullNamePath))
            {
                swm.WriteStructures(SdmxOBJ, sof, ms);
            }
        }
        public void TestWriteStructuresV2Default(string file)
        {
            ISdmxObjects objects;
            var fileInfo = new FileInfo(file);
            using (IReadableDataLocation location = new FileReadableDataLocation(fileInfo))
            {
                var structureWorkspace = this._parsingManager.ParseStructures(location);
                objects = structureWorkspace.GetStructureObjects(false);
            }

            StructureOutputFormat format = StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV2StructureDocument);
            var sdmxStructureFormat = new SdmxStructureFormat(format);
            var outputFileName = string.Format("{0}-output.xml", fileInfo.Name);
            using (var stream = File.Create(outputFileName))
            {
                var settings = new XmlWriterSettings { Indent = true };
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    IStructureWriterManager structureWritingManager = new StructureWriterManager(new SdmxStructureWriterFactory(writer));
                    structureWritingManager.WriteStructures(objects, sdmxStructureFormat, null);
                    writer.Flush();
                }
            }

            using (var stream = File.OpenRead(outputFileName))
            {
                XMLParser.ValidateXml(stream, SdmxSchemaEnumType.VersionTwo);
            }
        }
        public RestService()
        {
            // 1.a To support additional data formats pass additional IDataWriterFactory implementations to the DataWriterManager constructor  
            IDataWriterFactory dataWriterFactory = new DataWriterFactory();
            IDataWriterManager dataWriterManager = new DataWriterManager(dataWriterFactory/*, add your factories here) */);

            // 1.b To support additional data formats pass additional IStructureWriterFactory implementations to the DataWriterManager constructor  
            IStructureWriterManager structureWriterManager = new StructureWriterManager(new SdmxStructureWriterFactory() /*, add your factories here */);

            // a map between the HTTP Accept header value and the IDataFormat
            this._acceptToDataFormats = new Dictionary<string, IDataFormat>(StringComparer.Ordinal) {
                                                { "application/vnd.sdmx.genericdata+xml", this._genericV21Format },
                                                { "application/xml", this._genericV21Format },
                                                { "application/vnd.sdmx.structurespecificdata+xml", this._structureSpecificFormat },
                                                /* 2. add new data formats here */ };

            // a map between the HTTP Accept header value and the IStructureFormat
            this._acceptToStructureFormats = new Dictionary<string, IStructureFormat>(StringComparer.Ordinal)
                                                 {
                                                     { "application/vnd.sdmx.structure+xml", this._structureFormatv21 },
                                                     { "application/xml", this._structureFormatv21 }
                                                    /* 2. add new structure formats here */ };
            
            // load the structures
            ISdmxObjects objects;
            IReadableDataLocationFactory dataLocationFactory = new ReadableDataLocationFactory();
            IStructureParsingManager structureParsingManager = new StructureParsingManager();

            using (IReadableDataLocation structureLocation = dataLocationFactory.GetReadableDataLocation(HttpContext.Current.Server.MapPath("~/App_Data/structures.xml")))
            {
                IStructureWorkspace structureWorkspace = structureParsingManager.ParseStructures(structureLocation);
                objects = structureWorkspace.GetStructureObjects(false);
            }

            ISdmxObjectRetrievalManager retrievalManager = new InMemoryRetrievalManager(objects);

            _dataQueryManager = new RestDataQueryManager(new SampleDataRetrieval(), dataWriterManager, retrievalManager);
            _restStructureQueryManager = new RestStructureQueryManager(structureWriterManager, retrievalManager);
        }
        public void TestWriteReadBigCodeListV21(int count)
        {
            string countStr = count.ToString(CultureInfo.InvariantCulture);
            ICodelistMutableObject codelist = new CodelistMutableCore();
            codelist.Id = "CL_K" + countStr;
            codelist.AgencyId = "TEST";
            codelist.AddName("en", "Test CL with " + countStr);
            for (int i = 0; i < count; i++)
            {
                ICodeMutableObject code = new CodeMutableCore();
                code.Id = i.ToString(CultureInfo.InvariantCulture);
                code.AddName("en", "Code " + code.Id);
                codelist.AddItem(code);
            }

            var sw = new Stopwatch();
            string output = string.Format(CultureInfo.InvariantCulture, "big-2.1-immutable-codelist-{0}.xml", countStr);
            var writingManager = new StructureWriterManager();
            sw.Start();
            ICodelistObject immutableInstance = codelist.ImmutableInstance;
            Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "To immutable {0} took {1}", countStr, sw.Elapsed));
            CollectionAssert.IsNotEmpty(immutableInstance.Items);
            sw.Restart();
            using (FileStream writer = File.Create(output))
            {
                writingManager.WriteStructure(immutableInstance, new HeaderImpl("ZZ9", "ZZ9"), new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV21StructureDocument)), writer);
            }

            sw.Stop();
            Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Writing {0} took {1}", countStr, sw.Elapsed));

            sw.Reset();
            IStructureParsingManager parsingManager = new StructureParsingManager(SdmxSchemaEnumType.VersionTwoPointOne);
            sw.Start();
            using (IReadableDataLocation fileReadableDataLocation = new FileReadableDataLocation(output))
            {
                IStructureWorkspace structureWorkspace = parsingManager.ParseStructures(fileReadableDataLocation);
                Assert.NotNull(structureWorkspace);
                ISdmxObjects structureBeans = structureWorkspace.GetStructureObjects(false);
                Assert.NotNull(structureBeans);
            }

            sw.Stop();
            Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Reading {0} took {1}", countStr, sw.Elapsed));
        }
        public void TestReadWriteSdmxV21ObjectCount(string file)
        {
            IStructureParsingManager parsingManager = new StructureParsingManager(SdmxSchemaEnumType.VersionTwoPointOne);
            ISdmxObjects structureBeans;
            using (IReadableDataLocation fileReadableDataLocation = new FileReadableDataLocation(file))
            {
                IStructureWorkspace structureWorkspace = parsingManager.ParseStructures(fileReadableDataLocation);
                Assert.NotNull(structureWorkspace);
                structureBeans = structureWorkspace.GetStructureObjects(false);
                Assert.NotNull(structureBeans);
            }

            IStructureWriterManager writerManager = new StructureWriterManager();

            var outputFileName = file + "-out.xml";
            using (Stream outputStream = File.Create(outputFileName))
            {
                writerManager.WriteStructures(structureBeans, new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV21StructureDocument)), outputStream);
                outputStream.Flush();
            }

            ISdmxObjects objects2;
            using (IReadableDataLocation location = new FileReadableDataLocation(outputFileName))
            {
                var structureWorkspace = parsingManager.ParseStructures(location);
                objects2 = structureWorkspace.GetStructureObjects(false);
            }

            Assert.AreEqual(structureBeans.GetAllMaintainables().Count, objects2.GetAllMaintainables().Count);
        }
        public void TestReadWriteSdmxV21(string file)
        {
            IStructureParsingManager parsingManager = new StructureParsingManager(SdmxSchemaEnumType.VersionTwoPointOne);
            ISdmxObjects structureBeans;
            using (IReadableDataLocation fileReadableDataLocation = new FileReadableDataLocation(file))
            {
                IStructureWorkspace structureWorkspace = parsingManager.ParseStructures(fileReadableDataLocation);
                Assert.NotNull(structureWorkspace);
                structureBeans = structureWorkspace.GetStructureObjects(false);
                Assert.NotNull(structureBeans);
            }

            IStructureWriterManager writerManager = new StructureWriterManager();

            var outputFileName = file + "-out.xml";
            using (Stream outputStream = File.Create(outputFileName))
            {
                writerManager.WriteStructures(structureBeans, new SdmxStructureFormat(StructureOutputFormat.GetFromEnum(StructureOutputFormatEnumType.SdmxV21StructureDocument)), outputStream);
                outputStream.Flush();
            }

            using (var stream = File.OpenRead(outputFileName))
            {
                XMLParser.ValidateXml(stream, SdmxSchemaEnumType.VersionTwoPointOne);
            }
        }