public static void ConvertOutputXmlFileToOutputFlatFile(TransformBase map, string outputXmlFilename, string outputFlatFileFilename, bool validateFlatFile)
        {
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            ITOMErrorInfo[] creationErrors = null;

            string            targetSchemaClassName = map.TargetSchemas[0];
            CMapperSchemaTree targetSchemaTree      = BizTalkMapSchemaUtility.CreateSchemaTreeAndLoadSchema(map, targetSchemaClassName);

            if (!targetSchemaTree.CreateNativeInstanceFromXMLInstance(outputXmlFilename, outputFlatFileFilename, out creationErrors))
            {
                string message = "No details provided.";

                if (creationErrors != null)
                {
                    var messages = creationErrors.Select(e => $"Line:{e.LineNumber} Position:{e.LinePosition} {(e.IsWarning ? "Warning: " : "Error: ")} {e.ErrorInfo}");
                    message = string.Join(". " + Environment.NewLine, messages);
                }

                throw new InvalidDataException($"An error occurred while converting from XML to a flat file format: {Environment.NewLine}{message}");
            }

            if (validateFlatFile)
            {
                BizTalkXmlMapTestValidator.ValidateFlatFile(outputFlatFileFilename, targetSchemaTree, targetSchemaClassName);
            }
        }
Ejemplo n.º 2
0
        public static CMapperSchemaTree CreateSchemaTreeAndLoadSchema(TransformBase map, string schemaClassName)
        {
            string errorMessage = null;

            CMapperSchemaTree schemaTree = BizTalkMapSchemaUtility.CreateSchemaTree(map);

            if (!schemaTree.LoadFromDotNetPath(schemaClassName, null, out errorMessage))
            {
                throw new TypeLoadException($"An error occurred while loading the schema type '{schemaClassName}': {errorMessage}");
            }

            return(schemaTree);
        }
        /// <summary>
        /// Load a schema from the assembly containing the map.
        /// </summary>
        /// <param name="strLoadPath">This is the schema type name to load.</param>
        /// <returns></returns>
        public string GetSchemaFromLoadPath(string strLoadPath)
        {
            if (!string.IsNullOrWhiteSpace(strLoadPath))
            {
                Type schemaType = BizTalkMapSchemaUtility.FindSchemaViaAssembly(strLoadPath, this.mapperType.Assembly);
                if (schemaType != null)
                {
                    return(BizTalkMapSchemaUtility.LoadSchemaBase(schemaType).XmlContent);
                }
            }

            return(null);
        }
        public static XNode ConvertInputFlatFileContentsToInputXml(TransformBase map, string inputFlatFileContents)
        {
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            string            sourceSchemaClassName = map.SourceSchemas[0];
            CMapperSchemaTree sourceSchemaTree      = BizTalkMapSchemaUtility.CreateSchemaTreeAndLoadSchema(map, sourceSchemaClassName);
            string            convertedXml          = BizTalkXmlMapTestValidator.ValidateFlatFileContents(inputFlatFileContents, sourceSchemaTree, sourceSchemaClassName);

            return(XElement.Parse(convertedXml));
        }
        /// <summary>
        /// Perform the transform using the map.
        /// </summary>
        /// <param name="map"></param>
        /// <param name="xsltArguments"></param>
        /// <param name="inputXml"></param>
        /// <returns>The resulting XML output from the map.</returns>
        private static string PerformTransform(TransformBase map, XsltArgumentList xsltArguments, XNode inputXml)
        {
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }
            if (inputXml == null)
            {
                throw new ArgumentNullException(nameof(inputXml));
            }

            try
            {
                XslCompiledTransform transform = BizTalkMapSchemaUtility.LoadStylesheetFromMap(map);

                using (var input = XmlReader.Create(new StringReader(inputXml.ToString())))
                {
                    var sb       = new StringBuilder();
                    var settings = new XmlWriterSettings
                    {
                        Indent = true
                    };

                    using (var results = XmlWriter.Create(sb, settings))
                    {
                        transform.Transform(input, xsltArguments, results);
                    }

                    return(sb.ToString());
                }
            }
            catch (Exception exception)
            {
                var sb = new StringBuilder("An error occurred while executing the transform:" + Environment.NewLine);

                var currentException = exception;
                while (currentException != null)
                {
                    sb.AppendLine(currentException.ToString());

                    currentException = currentException.InnerException;
                }

                throw new InvalidDataException(sb.ToString());
            }
        }
        public static void ValidateXml(string xmlToValidate, IEnumerable <string> schemas, IEnumerable <SchemaReferenceAttribute> schemaReferenceAttributes)
        {
            if (schemas == null)
            {
                throw new ArgumentNullException(nameof(schemas));
            }

            var schemaSet = new XmlSchemaSet();

            foreach (var schema in schemas)
            {
                schemaSet.Add(BizTalkMapSchemaUtility.LoadSchema(schema, schemaReferenceAttributes));
            }

            var xmlValidator = new XmlValidator();

            xmlValidator.ValidateXml(xmlToValidate, schemaSet);
        }
        public static void ValidateOutputXml(TransformBase map, XNode outputXml, ISimpleLogger logger)
        {
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }
            if (outputXml == null)
            {
                throw new ArgumentNullException(nameof(outputXml));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            logger.Log("Validating the output XML from the BizTalk map");
            ValidateXml(outputXml.ToString(), map.TargetSchemas, BizTalkMapSchemaUtility.GetSchemaReferenceAttributes(map));
        }