Taylohtio/CondoUpdate/CondoUpdate.SetPermissionsF.../Parser.cs

87 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace CondoUpdate.SetPermissionsForDomusPage
{
public class SCInfo
{
public string Url;
public string Username;
public string Pwd;
}
public class Parser
{
private UpdaterImpl log;
public Parser(UpdaterImpl log)
{
this.log = log;
}
public List<SCInfo> Parse(string fileName)
{
try
{
if (string.IsNullOrEmpty(fileName))
{
this.log.error("Can't parse file: file name is empty");
return new List<SCInfo>();
}
this.log.info("Start parsing file '{0}'", fileName);
var lines = new List<string>(File.ReadAllLines(fileName)).Where(s => !string.IsNullOrEmpty(s)).ToList();
var result = new List<SCInfo>();
int num = 1;
foreach (string line in lines)
{
var info = this.parse(line, num++);
if (info != null)
{
result.Add(info);
}
}
this.log.info("Parsing is finished. Number of parsed site collections: {0} (total number of rows: {1})", result.Count, num);
return result;
}
catch (Exception x)
{
this.log.error("Error occured during parsing file '{0}':\n{1}\n{2}",
fileName, x.Message, x.StackTrace);
return new List<SCInfo>();
}
}
private SCInfo parse(string line, int num)
{
try
{
if (string.IsNullOrEmpty(line))
{
this.log.error("Can't parse line {0} because it is empty", num);
return null;
}
string[] vals = line.Split(new[]{","}, StringSplitOptions.None);
if (vals.Length != 3)
{
this.log.error("Can't parse line {0} ({1}): it doesn't contain 3 columns", num, line);
return null;
}
var info = new SCInfo {Url = vals[0], Username = vals[1], Pwd = vals[2] };
return info;
}
catch (Exception x)
{
this.log.error("Error occured during parsing line {0} ('{1}'):\n{2}\n{3}",
num, line, x.Message, x.StackTrace);
return null;
}
}
}
}