Example #1
0
        private string TransformRecord(string stext, string sXSLT)
        {
            //use Saxon; which will be installed with MarcEdit; to allow xslt 1, 2, and 3

            System.IO.StringReader sreader      = new System.IO.StringReader(stext);
            System.Xml.XmlReader   xreader      = System.Xml.XmlReader.Create(sreader);
            Saxon.Api.Processor    processor    = new Saxon.Api.Processor();
            Saxon.Api.XsltCompiler xsltCompiler = processor.NewXsltCompiler();
            Saxon.Api.XdmNode      input        = processor.NewDocumentBuilder().Build(xreader);


            // Create a transformer for the stylesheet.
            Saxon.Api.XsltTransformer transformer = null;
            if (System.IO.File.Exists(sXSLT))
            {
                System.IO.FileStream xstream = new System.IO.FileStream(sXSLT, System.IO.FileMode.Open);
                xsltCompiler.BaseUri = new Uri(System.IO.Path.GetDirectoryName(sXSLT) + System.IO.Path.DirectorySeparatorChar.ToString(), UriKind.Absolute);
                transformer          = xsltCompiler.Compile(xstream).Load();
            }
            else
            {
                lbStatus.Text = "ERROR applying the cutom rules file";
                return("");
            }



            // Set the root node of the source document to be the initial context node
            transformer.InitialContextNode = input;

            // Create a serializer
            Saxon.Api.Serializer serializer   = new Saxon.Api.Serializer();
            System.IO.TextWriter stringWriter = new System.IO.StringWriter();
            serializer.SetOutputWriter(stringWriter);
            transformer.Run(serializer);

            string tmp = stringWriter.ToString();

            //System.Windows.Forms.MessageBox.Show(tmp);
            //****Remove these lines because all the white space was being removed on translation***
            //System.Text.RegularExpressions.Regex objRegEx = new System.Text.RegularExpressions.Regex(@"\n +=LDR ", System.Text.RegularExpressions.RegexOptions.None);
            //tmp = objRegEx.Replace(tmp, "\n" + @"=LDR ");

            return(tmp);
        }
Example #2
0
        public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context)
        {
            return(inputs.AsParallel().Select(context, 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())
                                {
                                    transformer = LoadXsltDataInTransformer(xslt, fileStream);
                                }
                            }
                            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())
                        {
                            transformer = LoadXsltDataInTransformer(xslt, stream);
                        }
                    }
                    else
                    {
                        //Should never happen, because of null check in Constructor.
                        throw new InvalidOperationException();
                    }

                    using (Stream stream = input.GetStream())
                    {
                        XmlReader xml = XmlReader.Create(stream);

                        Saxon.Api.DocumentBuilder documentBuilder = processor.NewDocumentBuilder();
                        documentBuilder.BaseUri = xslt.BaseUri;
                        Saxon.Api.XdmNode xdmNode = documentBuilder.Build(xml);
                        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));
        }