Ejemplo n.º 1
0
        public void ParseLoggerTest()
        {
            RtfParser  parser        = new RtfParser();
            const bool enableLogging = false;

            parser.AddParserListener(new RtfParserListenerLogger(new RtfParserLoggerSettings(enableLogging)));
            parser.Parse(new RtfSource(@"{\rtf1foobar}"));
            parser.Parse(new RtfSource(GetTestResource("minimal.rtf")));
        }         // ParseLoggerTest
Ejemplo n.º 2
0
        public static string RtfToHtml(this string str, RtfHtmlConvertSettings htmlConvertSettings, bool throwOnError = false, IRtfParserListener listener = null, string destinationDirectory = null, RtfVisualImageAdapter imageAdapter = null, string imageAdapterLogFile = null, RtfImageConvertSettings imageConvertSettings = null)
        {
            IRtfGroup    rtfStructure;
            IRtfDocument rtfDocument = null;

            try
            {
                using (var stream = str.ToStream())
                {
                    // parse the rtf structure
                    var           structureBuilder = new RtfParserListenerStructureBuilder();
                    var           parser           = new RtfParser(structureBuilder);
                    DirectoryInfo destination;

                    if (destinationDirectory != null)
                    {
                        destination = new DirectoryInfo(destinationDirectory);
                    }

                    parser.IgnoreContentAfterRootGroup = true; // support WordPad documents

                    if (listener != null)
                    {
                        parser.AddParserListener(listener);
                    }

                    parser.Parse(new RtfSource(stream));
                    rtfStructure = structureBuilder.StructureRoot;

                    ThrowOnUnexpectedExitCode();

                    rtfDocument = InterpretRtf(rtfStructure, imageAdapter, imageAdapterLogFile, imageConvertSettings, throwOnError);

                    if (throwOnError)
                    {
                        ThrowOnUnexpectedExitCode();
                    }

                    // convert to hmtl

                    string html = ConvertHmtl(rtfDocument, imageAdapter, htmlConvertSettings, throwOnError);

                    if (throwOnError)
                    {
                        ThrowOnUnexpectedExitCode();
                    }

                    return(html);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("RtfToHtml parser failed with error: {0}", e.Message);
            }

            return(null);
        }
Ejemplo n.º 3
0
        }         // ParseResourcesTest

        // ----------------------------------------------------------------------
        protected override void DoTest(string kind, Stream testRes, string testCaseName)
        {
            RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
            RtfParser parser = new RtfParser();

            //parser.AddParserListener( new RtfParserListenerLogger() );
            parser.AddParserListener(structureBuilder);

            parser.Parse(new RtfSource(testRes));
            Assert.IsNotNull(structureBuilder.StructureRoot);
        }         // DoTest
Ejemplo n.º 4
0
        }         // ValidateProgramSettings

        // ----------------------------------------------------------------------
        private IRtfGroup ParseRtf()
        {
            IRtfGroup rtfStructure;
            RtfParserListenerFileLogger parserLogger = null;

            try
            {
                // logger
                if (settings.LogParser)
                {
                    string logFileName = settings.BuildDestinationFileName(
                        settings.LogDirectory,
                        RtfParserListenerFileLogger.DefaultLogFileExtension);
                    parserLogger = new RtfParserListenerFileLogger(logFileName);
                }

                // rtf parser
                // open readonly - in case of dominant locks...
                using (FileStream stream = File.Open(settings.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    // parse the rtf structure
                    RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
                    RtfParser parser = new RtfParser(structureBuilder);
                    parser.IgnoreContentAfterRootGroup = true;                     // support WordPad documents
                    if (parserLogger != null)
                    {
                        parser.AddParserListener(parserLogger);
                    }
                    parser.Parse(new RtfSource(stream));
                    rtfStructure = structureBuilder.StructureRoot;
                }
            }
            catch (Exception e)
            {
                if (parserLogger != null)
                {
                    parserLogger.Dispose();
                }

                Console.WriteLine("error while parsing rtf: " + e.Message);
                ExitCode = ProgramExitCode.ParseRtf;
                return(null);
            }

            return(rtfStructure);
        }         // ParseRtf
Ejemplo n.º 5
0
        } // Parse

        public static IRtfGroup Parse(IRtfSource rtfTextSource, params IRtfParserListener[] listeners)
        {
            var structureBuilder = new RtfParserListenerStructureBuilder();
            var parser           = new RtfParser(structureBuilder);

            if (listeners != null)
            {
                foreach (var listener in listeners)
                {
                    if (listener != null)
                    {
                        parser.AddParserListener(listener);
                    }
                }
            }
            parser.Parse(rtfTextSource);
            return(structureBuilder.StructureRoot);
        } // Parse
Ejemplo n.º 6
0
        }         // ConvertRtf2Html

        // ----------------------------------------------------------------------
        private IRtfGroup ParseRtf(string fileName)
        {
            IRtfGroup rtfStructure;

            using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
                RtfParser parser = new RtfParser(structureBuilder);
                parser.IgnoreContentAfterRootGroup = true;                 // support WordPad documents
                if (!string.IsNullOrEmpty(ParserLogFileName))
                {
                    parser.AddParserListener(new RtfParserListenerFileLogger(ParserLogFileName));
                }
                parser.Parse(new RtfSource(stream));
                rtfStructure = structureBuilder.StructureRoot;
            }
            return(rtfStructure);
        } // ParseRtf
Ejemplo n.º 7
0
        public void ParseStructureBuilderTest()
        {
            RtfParser parser = new RtfParser();
            RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();

            parser.AddParserListener(structureBuilder);
            parser.Parse(new RtfSource(@"{\rtf1foobar}"));

            IRtfGroup rtfStructure = structureBuilder.StructureRoot;

            Assert.IsNotNull(rtfStructure);

            Assert.AreEqual(RtfElementKind.Group, rtfStructure.Kind);
            Assert.AreEqual(2, rtfStructure.Contents.Count);

            Assert.AreEqual(RtfElementKind.Tag, rtfStructure.Contents[0].Kind);
            Assert.AreEqual("rtf", ((IRtfTag)rtfStructure.Contents[0]).Name);
            Assert.AreEqual(true, ((IRtfTag)rtfStructure.Contents[0]).HasValue);
            Assert.AreEqual("1", ((IRtfTag)rtfStructure.Contents[0]).ValueAsText);
            Assert.AreEqual(1, ((IRtfTag)rtfStructure.Contents[0]).ValueAsNumber);

            Assert.AreEqual(RtfElementKind.Text, rtfStructure.Contents[1].Kind);
            Assert.AreEqual("foobar", ((IRtfText)rtfStructure.Contents[1]).Text);
        }         // ParseStructureBuilderTest