#region Parser
/// <summary>
/// Parser Analysis.
/// </summary>
/// <returns></returns>
[WebMethod]
public bool Parser()
{
#region Initialization Variables
string FileName, strXmlTemplateFolder, extendName, commandName, strParserDestFolder, TxtFile;
string sLine, xmlFilePath, sql, Element = "", ElementValue = "", ElementValueTemp;
string str = "", strPattern, FaxInfoXMLTempID, ReceiveNumber = "";
string[] filesDestFolder;
int TemplateID = 0, SemicolonPos, FaxInfoID;
string sqlUpFaxInfo = "", sqlUpFaxInfo2 = "";
bool isNoKeyWord = true, isFindFile = false, Flag = true;
List<string> TxtList = new List<string>();
string KeyWord = "", Hint = "";
int Line = 0, Column = 0;
int EntityID = 0, xmlTemplateID = 0;
string sqlEntity = "", sqlTemplateByEntity = "";
#endregion
//Get parser and xmlTemplate folder's location
sql = "SELECT parserfilepath,xmlTemplateFilePath FROM FilePath";
DataSet ds = DbHelperSQL.Query(sql);
if (ds.Tables[0].Rows.Count > 0)
{
//Get ParserFilePath
strParserDestFolder = ds.Tables[0].Rows[0]["parserfilepath"].ToString();
//GetxmlTemplateFolder
strXmlTemplateFolder = ds.Tables[0].Rows[0]["xmlTemplateFilePath"].ToString();
//Check Dest folder is existed or not.
filesDestFolder = Directory.GetFiles(strParserDestFolder);
if (filesDestFolder.Length > 0)
{
try
{
foreach (string s in filesDestFolder)
{
//Get Extent Name "txt" Or "tif".
extendName = s.Substring(s.Length - 3, 3);
//Get command Name like "051-010-28663122~051-010-88663122~2007-7-6~1" etc.
commandName = s.Substring(s.LastIndexOf("//") + 1, s.LastIndexOf(".") - s.LastIndexOf("//") - 1);
//Only Get Txt File
if (extendName == "txt")
{
//Get FileName When State=1 In FaxInfo Table.
sql = "SELECT * FROM FaxInfo WHERE status=1 ORDER BY ReceiveDate";
DataSet ds2 = DbHelperSQL.Query(sql);
if (ds2.Tables[0].Rows.Count > 0)
{
//Traversing All equal condition data.
foreach (DataRow row in ds2.Tables[0].Rows)
{
//2007-8-13: Get ReceiveNumber Field
ReceiveNumber = row["ReceiveNumber"].ToString();
FileName = row["FileName"].ToString();
FaxInfoID = Convert.ToInt32(row["ID"]);
FaxInfoXMLTempID = row["XMLTempID"].ToString();
#region Compare FaxInfo.FileName whether equal FilesDestFolder.commandName
if (FileName == commandName)
{
#region Initialization Local Variables
isFindFile = true;
TxtFile = strParserDestFolder + FileName + ".txt";
xmlFilePath = strParserDestFolder + FileName + ".xml";
StreamReader objReader = null;
XmlTextWriter xtw = new XmlTextWriter(xmlFilePath, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.Indentation = 2;
//Write XML Start Tag.
xtw.WriteStartDocument();
//"Details"as Root Element.
xtw.WriteStartElement("Details");
//Load Txt File
StreamReader txtReader = new StreamReader(TxtFile, Encoding.UTF8);
TxtList.Add(txtReader.ReadToEnd());
txtReader.Close();
#endregion
//2007-8-13:Identity Entity By Fax Tel(Need parse txt ReceiveNumber)
sqlEntity = "SELECT EntityID FROM EntityDetail WHERE Info= " + "'" + ReceiveNumber + "'";
DataSet dsEntity = DbHelperSQL.Query(sqlEntity);
if (dsEntity.Tables[0].Rows.Count > 0)
{
foreach (DataRow rowEntity in dsEntity.Tables[0].Rows)
{
//Get EntityID
EntityID = Convert.ToInt32(rowEntity["EntityID"]);
//2007-8-13:Get Template Name By EntityID
sqlTemplateByEntity = "SELECT XmlTemplate.id FROM XmlTemplate WHERE XmlTemplate.EntityID=" + EntityID;
DataSet dsTemplateByEntity = DbHelperSQL.Query(sqlTemplateByEntity);
if (dsTemplateByEntity.Tables[0].Rows.Count > 0)
{
foreach (DataRow rowTemplateByEntity in dsTemplateByEntity.Tables[0].Rows)
{
//GetTemplateID
xmlTemplateID = Convert.ToInt32(rowTemplateByEntity["id"]);
#region Execute Inner Code (Include KeyWord,Fuzzy,ExportXML)
sql = "SELECT elemName,XmlTemplateID,Line,[Column],Hint,FuzzyFields FROM XmlEditTemp" +
" WHERE elemName IS NOT NULL AND XmlTemplateID=" + xmlTemplateID +
" ORDER BY ID ";
DataSet ds3 = DbHelperSQL.Query(sql);
if (ds3.Tables[0].Rows.Count > 0)
{
#region Traversing database find Keyword
foreach (DataRow row3 in ds3.Tables[0].Rows)
{
#region Get KeyWord,Hint,Line,Column,TemplateID
//KeyWord <-XmlEditTemp.elemName
if (row3["elemName"] + "" != string.Empty)
{
KeyWord = row3["elemName"].ToString();
}
//Hint <-XmlEditTemp.Hint
if (row3["Hint"] + "" != string.Empty)
{
Hint = row3["Hint"].ToString();
}
//Line <-XmlEditTemp.Line
if (row3["Line"] + "" != string.Empty)
{
Line = Convert.ToInt32(row3["Line"]);
}
//Column <-XmlEditTemp.Column
if (row3["Column"] + "" != string.Empty)
{
Column = Convert.ToInt32(row3["Column"]);
}
//TemplateID <-XmlEditTemp.XmlTemplateID
if (row3["XmlTemplateID"] != null)
{
TemplateID = Convert.ToInt32(row3["XmlTemplateID"]);
}
#endregion
//Judgemant whether find template from XmlTemplate.
string sql2 = "SELECT COUNT(*) FROM XmlTemplate WHERE ID=" + TemplateID;
int tempcount = DbHelperSQL.ExecuteId(sql2);
if (tempcount > 0)
{
try
{
objReader = new StreamReader(TxtFile);
sLine = "";
//KeyWordManage KeyWord Fields.
string sKeyWord = KeyWord;
//Save Line and Column Value.
List<int> list = new List<int>();
//From the first line,first Column starts.
int iRowNo = 0;
int iColumnNo = 1;
sLine = objReader.ReadLine();
int flag = 0;
while (sLine != null)
{
iRowNo++;
MatchCollection mc1 = Regex.Matches(sLine, sKeyWord, RegexOptions.IgnoreCase);
string sTemp = sLine;
#region Search By KeyWord
foreach (Match m1 in mc1)
{
if (m1.Success)
{
iColumnNo = sTemp.IndexOf(m1.Value);
sTemp = sLine.Substring(iColumnNo + sKeyWord.Length);
list.Clear();
list.Add(iRowNo);
list.Add(iColumnNo + 1);
int rn = list[0];
int cn = list[1];
#region Judgement whether find keyword.
//Judgment LineNo and ColumnNo in txtFile,
//whether equal KeyWord table Line, Column Data.
if ((rn == Line) && (cn == Column))
{
//Markup Whether has KeyWord. Default Value is False.
isNoKeyWord = false;
// Extraction ':' As elements
if (sLine.Contains(":") == true)
{
#region Handling "#" Use "NO." Replace it.
if (sLine.Contains("#"))
{
string sLineTemp = sLine;
int JPos;
JPos = sLineTemp.IndexOf("#");
sLineTemp = sLineTemp.Replace(sLineTemp.Substring(JPos, 1), "NO.");
sLine = sLineTemp;
strPattern = @"/D+/.+:.*?(?=/D+/.+:|$)";
}
else
{
strPattern = @"/w+:.*?(?=/w+:|$)";
}
#endregion
MatchCollection mc = Regex.Matches(sLine, strPattern);
foreach (Match m in mc)
{
str = m.Value;
SemicolonPos = str.IndexOf(":");
Element = str.Substring(0, SemicolonPos);
ElementValueTemp = str.Substring(SemicolonPos, (str.Length - str.Substring(0, SemicolonPos).Length));
ElementValue = ElementValueTemp.Remove(0, 1).Trim();
this.WriteXML(TxtFile, sLine, xtw, Element, ElementValue, TemplateID, TxtList, false, "", "");
break;
}
}
}
#endregion
}
break;
}
#endregion
sLine = objReader.ReadLine();
if (sLine == null)
{
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
catch (Exception ex2)
{
#region Update FaxInfo Status=2, ErrorMessage=3(Data Problem)
sqlUpFaxInfo2 = "UPDATE [FaxInfo] SET [Status] = 2," + "[XMLTempID]= " + TemplateID +
" ,[ErrorMessage] = 3 WHERE ID=" + FaxInfoID;
DbHelperSQL.ExecuteSql(sqlUpFaxInfo2);
return false;
#endregion
}
}
else
{
#region Don't find Template,throw exception
//Update FaxInfo Status=2, ErrorMessage=4(No Template), XMLTempID=0
sqlUpFaxInfo = "UPDATE [FaxInfo] SET [Status] = 2," +
" [ErrorMessage] = 4,[XMLTempID] = 0 WHERE ID=" + FaxInfoID;
DbHelperSQL.ExecuteSql(sqlUpFaxInfo);
return false;
#endregion
}
}
#endregion
#region No KeyWord (Execute Fuzzy Search)
if (isNoKeyWord == true)
{
#region By Fuzzy Search
this.WriteXML(TxtFile, "", xtw, "", "", TemplateID, TxtList, true, "", "");
#endregion
}
#endregion
xtw.WriteEndDocument();
xtw.Close();
objReader.Close();
#region Success:Update FaxInfo Status=4
string sqlSuccess = "UPDATE [FaxInfo] SET [Status] = 4, " + "ErrorMessage=0," +
"[XMLTempID]= " + TemplateID +
" WHERE ID=" + FaxInfoID;
DbHelperSQL.ExecuteSql(sqlSuccess);
return true;
#endregion
}
else
{
#region Template ID equal 0
string sqlNoTemplate = "UPDATE [FaxInfo] SET [Status] = 2," +
" [ErrorMessage] = 4 WHERE ID=" + FaxInfoID;
DbHelperSQL.ExecuteSql(sqlNoTemplate);
return false;
#endregion
}
#endregion
}
}
}
}
}
#region No find match file.
if (isFindFile == false)
{
//If haven't find match file,throw exception.
string sqlIsFindFile = "UPDATE [FaxInfo] SET [Status] = 2, " + "ErrorMessage=6" +
" WHERE ID=" + FaxInfoID;
DbHelperSQL.ExecuteSql(sqlIsFindFile);
return false;
}
#endregion
#endregion
}
}
}
}
}
catch (Exception ex1)
{
return false;
}
}
}
return Flag;
}
#endregion
#region Export XML For Parser
/// <summary>
/// According to Find Txt Write XML
/// </summary>
/// <param name="filePath">Txt File Path</param>
/// <param name="sLine">Current Read Line</param>
/// <param name="xtw">XmlTextWriter Object</param>
/// <param name="element">Line Element</param>
/// <param name="elementValue">Line ElementValue</param>
/// <param name="templateID">Template ID</param>
private void WriteXML(string filePath, string sLine, XmlTextWriter xtw,
string element, string elementValue, int templateID,
List<string> TxtList, bool isFuzzyField, string FuzzyField, string RightFields)
{
string Sql, ElemName, ElemName2, strTxtLine = "";
string strPattern2 = "", FuzzyTemp = "";
StreamReader txt = new StreamReader(filePath, Encoding.UTF8);
#region In XmlEditTemp table Find Template,When Match Sucessful,Write XML File.
Sql = "SELECT XmlEditTemp.ElemName,XmlEditTemp.FuzzyFields FROM XmlEditTemp " +
" WHERE XmlEditTemp.XmlTemplateID= " + templateID +
" ORDER BY XmlEditTemp.ID";
if (isFuzzyField == false)
{
#region Find match template according to KeyWord
strTxtLine = txt.ReadLine();
while (strTxtLine != null)
{
//Select txt contain ":" and don't contain "/" data.
if (strTxtLine.Contains(":") && (strTxtLine.Contains("/") == false))
{
#region Handling "#" Use "NO." Replace it.
if (strTxtLine.Contains("#"))
{
string sLineTemp = strTxtLine;
int JPos;
JPos = sLineTemp.IndexOf("#");
sLineTemp = sLineTemp.Replace(sLineTemp.Substring(JPos, 1), "NO.");
strTxtLine = sLineTemp;
strPattern2 = @"/D+/.+:.*?(?=/D+/.+:|$)";
}
else
{
strPattern2 = @"/w+:.*?(?=/w+:|$)";
}
#endregion
//match correspond pattern.
MatchCollection mc2 = Regex.Matches(strTxtLine, strPattern2);
foreach (Match m in mc2)
{
DataSet ds2 = DbHelperSQL.Query(Sql);
if (ds2.Tables[0].Rows.Count > 0)
{
foreach (DataRow row2 in ds2.Tables[0].Rows)
{
ElemName2 = row2["elemname"].ToString();
string mvalueTemp = m.Value;
string valueTemp2 = mvalueTemp;
int SemicolonPos1 = mvalueTemp.IndexOf(":");
mvalueTemp = mvalueTemp.Substring(0, SemicolonPos1);
valueTemp2 = valueTemp2.Substring(0, SemicolonPos1);
if (mvalueTemp.Contains(" "))
{
int lastBlank2 = mvalueTemp.LastIndexOf(" ");
mvalueTemp = mvalueTemp.Substring(lastBlank2).Trim();
if (valueTemp2.ToUpper().IndexOf(ElemName2.ToUpper()) > 0)
{
int posTemp = valueTemp2.IndexOf(ElemName2);
valueTemp2 = valueTemp2.Substring(posTemp);
mvalueTemp = valueTemp2.Replace(" ", "_");
}
}
if (mvalueTemp.ToUpper() == ElemName2.ToUpper())
{
string str2 = m.Value;
int SemicolonPos2 = str2.LastIndexOf(":");
string Element2 = str2.Substring(0, SemicolonPos2);
string ElementValueTemp2 = str2.Substring(SemicolonPos2, (str2.Length - str2.Substring(0, SemicolonPos2).Length));
string ElementValue2 = ElementValueTemp2.Remove(0, 1).Trim();
if (Element2.Contains(" "))
{
int lastBlank = Element2.LastIndexOf(" ");
Element2 = Element2.Substring(lastBlank).Trim();
}
if (mvalueTemp.IndexOf("_") > 0)
{
xtw.WriteElementString(mvalueTemp, ElementValue2);
}
else
{
xtw.WriteElementString(ElemName2, ElementValue2);
}
}
}
}
}
}
strTxtLine = txt.ReadLine();
}
#endregion
}
else
{
#region Find match template according to FuzzyField
strTxtLine = txt.ReadLine();
while (strTxtLine != null)
{
//Select txt contain ":" and don't contain "/" data.
if (strTxtLine.Contains(":") && (strTxtLine.Contains("/") == false))
{
//string sqlFuzzy = "Select ElemName,FuzzyFields From XmlEditTemp " +
// " WHERE ElemName IS NOT NULL " +
// " AND XmlEditTemp.XmlTemplateID= " + templateID;
DataSet dsFuzzy = DbHelperSQL.Query(Sql);
if (dsFuzzy.Tables[0].Rows.Count > 0)
{
foreach (DataRow rowFuzzy in dsFuzzy.Tables[0].Rows)
{
FuzzyTemp = rowFuzzy["FuzzyFields"].ToString();
RightFields = rowFuzzy["ElemName"].ToString();
//If current line contain FuzzyField,replace FuzzyField to ElemName
if (FuzzyTemp.Length != 0)
{
if (strTxtLine.ToUpper().Contains(FuzzyTemp.ToUpper()))
{
strTxtLine = strTxtLine.Replace(FuzzyTemp, RightFields);
}
}
#region Handling "#" Use "NO." Replace it.
if (strTxtLine.Contains("#"))
{
string sLineTemp = strTxtLine;
int JPos;
JPos = sLineTemp.IndexOf("#");
sLineTemp = sLineTemp.Replace(sLineTemp.Substring(JPos, 1), "NO.");
strTxtLine = sLineTemp;
strPattern2 = @"/D+/.+:.*?(?=/D+/.+:|$)";
}
else
{
strPattern2 = @"/w+:.*?(?=/w+:|$)";
}
#endregion
#region Match correspond pattern,Write XML
MatchCollection mc2 = Regex.Matches(strTxtLine, strPattern2);
foreach (Match m in mc2)
{
string mvalueTemp = m.Value;
string valueTemp2 = mvalueTemp;
int SemicolonPos1 = mvalueTemp.IndexOf(":");
mvalueTemp = mvalueTemp.Substring(0, SemicolonPos1);
valueTemp2 = valueTemp2.Substring(0, SemicolonPos1);
#region Handling " "
if (mvalueTemp.Contains(" "))
{
int lastBlank2 = mvalueTemp.LastIndexOf(" ");
mvalueTemp = mvalueTemp.Substring(lastBlank2).Trim();
if (valueTemp2.ToUpper().IndexOf(RightFields.ToUpper()) > 0)
{
int posTemp = valueTemp2.IndexOf(RightFields);
valueTemp2 = valueTemp2.Substring(posTemp);
mvalueTemp = valueTemp2.Replace(" ", "_");
}
}
#endregion
if (mvalueTemp.Contains(RightFields))
{
string str2 = m.Value;
int SemicolonPos2 = str2.LastIndexOf(":");
string Element2 = str2.Substring(0, SemicolonPos2);
string ElementValueTemp2 = str2.Substring(SemicolonPos2, (str2.Length - str2.Substring(0, SemicolonPos2).Length));
string ElementValue2 = ElementValueTemp2.Remove(0, 1).Trim();
if (Element2.Contains(" "))
{
int lastBlank = Element2.LastIndexOf(" ");
Element2 = Element2.Substring(lastBlank).Trim();
}
if (mvalueTemp.IndexOf("_") > 0)
{
xtw.WriteElementString(mvalueTemp, ElementValue2);
}
else
{
xtw.WriteElementString(RightFields, ElementValue2);
}
}
}
#endregion
}
}
}
strTxtLine = txt.ReadLine();
}
#endregion
}
// }
//}
txt.Close();
#endregion
}
#endregion
#region SpellCheck
/// <summary>
/// CheckSpell
/// </summary>
/// <param name="word">Need Check Word</param>
/// <returns></returns>
public string CheckSpelling(string word)
{
System.Type wordType = System.Type.GetTypeFromProgID("Word.Application");
object wd = System.Activator.CreateInstance(wordType);
StringBuilder sb = new StringBuilder();
object start = 0;
object end = 0;
object missing = System.Type.Missing;
object IgnoreUppercase = true;
object AlwaysSuggest = true;
((ApplicationClass)wd).Visible = false;
((ApplicationClass)wd).Documents.Add(ref missing, ref missing, ref missing, ref missing);
((ApplicationClass)wd).ActiveDocument.Range(ref start, ref end).Text = word;
((ApplicationClass)wd).Options.CheckGrammarWithSpelling = true;
((ApplicationClass)wd).Options.SuggestSpellingCorrections = true;
((ApplicationClass)wd).CheckSpelling(word, ref IgnoreUppercase, ref AlwaysSuggest, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (((ApplicationClass)wd).ActiveDocument.SpellingErrors.Count > 0)
{
foreach (SpellingSuggestion Suggestion in
((ApplicationClass)wd).GetSpellingSuggestions(word, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing))
{
sb.Append(Suggestion.Name);
}
}
else
{
//Right
}
object savechanges = false;
((ApplicationClass)wd).ActiveDocument.Close(ref savechanges, ref missing, ref missing);
((ApplicationClass)wd).Quit(ref savechanges, ref missing, ref missing);
Marshal.ReleaseComObject(((ApplicationClass)wd));
wd = null;
return sb.ToString();
}
#endregion