public static string CreateMetadata(Publication item) { string str = ""; str += "@" + item.type + "{" + item.tag + ",\n"; if (item.title != "") { str += "title = \"" + item.title + "\",\n"; } string editors = ""; for (int i = 0, authCnt = item.authors.Count; i < authCnt; i++) { if (i != authCnt - 1) { editors += item.authors[i] + " and "; } else { editors += item.authors[i]; } } if (editors != "") { str += "editor = \"" + editors + "\",\n"; } if (item.booktitle != "") { str += "booktitle = \"" + item.booktitle + "\",\n"; } if (item.publisher != "") { str += "publisher = \"" + item.publisher + "\",\n"; } if (item.journal != "") { str += "journal = \"" + item.journal + "\",\n"; } if (item.volume != "") { str += "volume = \"" + item.volume + "\",\n"; } if (item.pages != "") { str += "pages = \"" + item.pages + "\",\n"; } if (item.year != "") { str += "year = \"" + item.year + "\",\n"; } if (item.note != "") { str += "note = \"" + item.note + "\",\n"; } if (item.isbn != "") { str += "isbn = \"" + item.isbn + "\",\n"; } if (item.doi != "") { str += "doi = \"" + item.doi + "\",\n"; } string authors = ""; for (int i = 0, authCnt = item.authors.Count; i < authCnt; i++) { if (i != authCnt - 1) { authors += item.authors[i] + " and "; } else { authors += item.authors[i]; } } if (authors != "") { str += "author = \"" + authors + "\",\n"; } string keywords = ""; for (int i = 0, kwrdCnt = item.keywords.Count; i < kwrdCnt; i++) { if (i != kwrdCnt - 1) { keywords += item.keywords[i] + ", "; } else { keywords += item.keywords[i]; } } if (keywords != "") { str += "keywords = \"" + keywords + "\"\n"; } str += "}"; return(str); }
//Чтение информации (739 мс) private bool ReadInformation(string path, bool first_time) { //Список публикаций только для текущего файла List <Publication> thisPublications = new List <Publication>(); try { //id в зависимости от количества файлов if (first_time) { Clear(); } int id = publications.Count; using (StreamReader sr = new StreamReader(path, Encoding.Default)) { bool isTitle = false; string line, buffer = ""; string tag = "", type = "", title = "", booktitle = "", publisher = "", journal = "", volume = "", year = "", note = "", pages = "", isbn = "", doi = ""; List <string> authors = new List <string>(); List <string> keywords = new List <string>(); List <string> editor = new List <string>(); while ((line = sr.ReadLine()) != null) { buffer += line; if (buffer == "}") { Publication item = new Publication(id, type, tag, title, booktitle, publisher, journal, volume, pages, year, note, isbn, doi, authors, keywords, editor); if (!CheckDuplicate(item)) { thisPublications.Add(item); id++; } isTitle = false; keywords.Clear(); authors.Clear(); editor.Clear(); buffer = tag = type = title = booktitle = publisher = journal = volume = year = note = pages = isbn = doi = ""; } else { if (line[line.Length - 1] != '"' && line[line.Length - 2] != '"' && line[0] != '@') { continue; } if (buffer[0] == '@') { type = buffer.Substring(1, buffer.IndexOf('{') - 1); tag = buffer.Substring(buffer.IndexOf('{') + 1, buffer.LastIndexOf(',') - buffer.IndexOf('{') - 1); } if (buffer.Contains("title = ") && isTitle == false) { title = ParseLine(buffer); isTitle = true; } if (buffer.Contains("booktitle = ")) { booktitle = ParseLine(buffer); } if (buffer.Contains("publisher = ")) { publisher = ParseLine(buffer); } if (buffer.Contains("journal = ")) { journal = ParseLine(buffer); } if (buffer.Contains("volume = ")) { volume = ParseLine(buffer); } if (buffer.Contains("pages = ")) { pages = ParseLine(buffer); } if (buffer.Contains("year = ")) { year = ParseLine(buffer); } if (buffer.Contains("note = ")) { note = ParseLine(buffer); } if (buffer.Contains("isbn = ")) { isbn = ParseLine(buffer); } if (buffer.Contains("doi = ")) { doi = ParseLine(buffer); } if (buffer.Contains("author = ")) { string[] result = ParseLine(buffer).Split(new string[] { " and " }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < result.Length; i++) { string str = result[i].TrimStart().TrimEnd(); if (str != "") { authors.Add(str); } } } if (buffer.Contains("keywords = ")) { string[] result = ParseLine(buffer).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < result.Length; i++) { string str = result[i].TrimStart().TrimEnd(); if (str != "") { keywords.Add(str); } } } if (buffer.Contains("editor = ")) { string[] result = ParseLine(buffer).Split(new string[] { " and " }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < result.Length; i++) { string str = result[i].TrimStart().TrimEnd(); if (str != "") { editor.Add(str); } } } buffer = ""; } } sr.Close(); } publications.AddRange(thisPublications); } //Файл не удалось считать (ошибка) catch { return(false); } return(true); }