예제 #1
0
        private static Saxon.Api.XsltTransformer LoadXsltDataInTransformer(Saxon.Api.XsltCompiler xslt, Stream stream)
        {
            XmlReader xml = XmlReader.Create(stream);

            xslt.BaseUri = new Uri(string.IsNullOrEmpty(xml.BaseURI) ? "http://NOT.SET" : xml.BaseURI);
            Saxon.Api.XsltExecutable  executable  = xslt.Compile(xml);
            Saxon.Api.XsltTransformer transformer = executable.Load();
            return(transformer);
        }
예제 #2
0
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            return(inputs.AsParallel().Select(input =>
            {
                try
                {
                    Saxon.Api.Processor processor = new Saxon.Api.Processor();
                    Saxon.Api.XsltCompiler xslt = processor.NewXsltCompiler();
                    Saxon.Api.XsltTransformer transformer;
                    if (_xsltPath != null)
                    {
                        FilePath path = _xsltPath.Invoke <FilePath>(input, context);
                        if (path != null)
                        {
                            IFile file = context.FileSystem.GetInputFile(path);
                            if (file.Exists)
                            {
                                using (Stream fileStream = file.OpenRead())
                                {
                                    Saxon.Api.XsltExecutable executable = xslt.Compile(fileStream);
                                    transformer = executable.Load();
                                }
                            }
                            else
                            {
                                throw new FileNotFoundException("Couldn't find XSLT file", file.ToString());
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Provided file path was not valid");
                        }
                    }
                    else if (_xsltGeneration != null)
                    {
                        IDocument xsltDocument = context.Execute(_xsltGeneration, new[] { input }).Single();
                        using (Stream stream = xsltDocument.GetStream())
                        {
                            var xml = XmlReader.Create(stream);
                            xslt.BaseUri = new Uri(string.IsNullOrEmpty(xml.BaseURI)?"http://temp.org": xml.BaseURI);
                            Saxon.Api.XsltExecutable executable = xslt.Compile(xml);
                            transformer = executable.Load();
                        }
                    }
                    else
                    {
                        //Should never happen, because of null check in Constructor.
                        throw new InvalidOperationException();
                    }

                    using (Stream stream = input.GetStream())
                    {
                        var documentBuilder = processor.NewDocumentBuilder();
                        documentBuilder.BaseUri = xslt.BaseUri;
                        var xdmNode = documentBuilder.Build(stream);
                        transformer.InitialContextNode = xdmNode;

                        using (System.IO.StringWriter writer = new StringWriter())
                        {
                            Saxon.Api.Serializer serializer = new Saxon.Api.Serializer();
                            serializer.SetOutputWriter(writer);
                            transformer.Run(serializer);
                            return context.GetDocument(input, writer.ToString());
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.Error($"An {e.GetType().Name} occurred: {e.Message}");
                    return null;
                }
            }).Where(x => x != null));
        }