Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.5k views
in .NET FTP by (160 points)
Hello, our company looks for a ftp/sftp component with support for custom listing parser. According to your documentation, it's possible to create custom parser by creating derived classes from FTPFileFactory and FTPFileParser. I created those classes with overriding Parse methods (just added console output for raw data and call base version of methods). Finally, i wrote the following code:
var ftpConnection = new SecureFTPConnection();
// ... just init ftpConnection (login, password, sftp mode, etc)
ftpConnection.FileInfoParser = new MyFactory(new MyParser());

ftpConnection.Connect();
var files = ftpConnection.GetFiles();

// ... actions with files listing


But i cannot get any output from my custom parser's Parse method. Is there any example of creation custom listing parser? Thanks.

3 Answers

0 votes
by (162k points)
Sure, download the source for edtFTPnet and take a look at the parsers in there.
0 votes
by (160 points)
Sure, download the source for edtFTPnet and take a look at the parsers in there.

Thanks, i looked at the parser's code. I have the following code:
    public class TestFileFactory : FTPFileFactory
    {
        public TestFileFactory(TestFileParser parser)
        {
            FileParser = parser;
        }

        public override FTPFile[] Parse(string[] fileStrings)
        {
            Console.WriteLine("factory->parse");
            return base.Parse(fileStrings);
        }

        public override FTPFile PartialParse(string fileString, System.Collections.ArrayList allFileStrings)
        {
            Console.WriteLine("factory->partialparse");
            return base.PartialParse(fileString, allFileStrings);
        }
    }

    public class TestFileParser : FTPFileParser
    {
        public override bool TimeIncludesSeconds
        {
            get { return false; }
        }

        public override string ToString()
        {
            return "Test";
        }

        public override bool IsValidFormat(string[] listing)
        {
            return false;
        }

        public override FTPFile Parse(string raw)
        {
            Console.WriteLine("parser->parse");

            DateTime dt = DateTime.Now;
            return new FTPFile(0, raw, "test", 10, false, ref dt);
        }
    }

  static void Main()
{
                  var ftpConnection = new SecureFTPConnection();
   // connection init
                ftpConnection.Protocol = FileTransferProtocol.SFTP;
            ftpConnection.FileInfoParser = new TestFileFactory(new TestFileParser());
            ftpConnection.ServerValidation = SecureFTPServerValidationType.None;

            ftpConnection.Connect();

            FTPFile[] files = ftpConnection.GetFileInfos(); 
            // At this moment - no any output from parse methods, so i assume
            List<FTPFile> data = files.Select(ftpFile => ftpConnection.FileInfoParser.FileParser.Parse(ftpFile.Raw)).ToList();
            // At this moment - i have output from parser->parse
        
            // ... work with data and closing connection
}

So, the my question: is it right to use custom parser this way? Custom factory and custom parser are not called on GetFileInfos, so i called my parser's Parse method manually on collection of FTPFile gathered by GetFileInfos method. It works on casual server listing but what will happen when server will answer with non-standard response? Will GetFileInfos return me FTPFile collection with raw data even if it can't parse that data? Thanks.
0 votes
by (162k points)
Your log file would be useful. But try changing IsValidFormat to return true.

Categories

...