public static void Run(PeriodicalList periodicalList)
        {
            var    journalUrl        = @"http://journal-abbreviations.library.ubc.ca/dump.php";
            var    project           = periodicalList.Project;
            var    journalCollection = new List <Periodical>();
            string completeList;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                using (var webClient = new WebClient())
                {
                    using (var stream = webClient.OpenRead(journalUrl))
                    {
                        using (var streamReader = new StreamReader(stream))
                        {
                            completeList = streamReader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(periodicalList, ImportJournalsResources.PubMedMacroReadErrorMessage.FormatString(journalUrl, e.Message), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var refCounter = 0;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                completeList = Regex.Match(completeList, @"(<tbody>.*?<\\/tbody)").ToString();
                completeList = Regex.Replace(completeList, @"\\/", @"/");


                var journals = Regex.Matches(completeList, "(?<=<tr>).*?(?=</tr>)");

                foreach (var journalAndAbbrev in journals)
                {
                    var journalTitle  = string.Empty;
                    var abbreviation1 = string.Empty; // this one should have full stops
                    var abbreviation2 = string.Empty; // this one shouldn't
                    var abbreviation3 = string.Empty; // this one should be any all uppercase acronym after a colon in JournalTitl

                    var journalData = Regex.Matches(journalAndAbbrev.ToString(), "(?<=<td>).*?(?=</td>)");
                    if (journalData.Count < 2)
                    {
                        continue;
                    }

                    abbreviation1 = journalData[0].Value;
                    journalTitle  = Regex.Replace(journalData[1].Value, @"\bfur\b", "für");

                    // generate abbreviation2 by removing full stops from abbreviation1
                    var abbreviation1Words = abbreviation1.Split(' ');
                    for (int i = 0; i < abbreviation1Words.Length; i++)
                    {
                        abbreviation1Words[i] = abbreviation1Words[i].TrimEnd('.');
                    }
                    abbreviation2 = String.Join(" ", abbreviation1Words);


                    // try to establish Abbreviation3
                    abbreviation3 = Regex.Match(journalTitle, @"(?:: )[A-Z]{2,6}$").ToString();



                    Periodical journal = new Periodical(project, journalTitle);
                    if (!String.IsNullOrEmpty(abbreviation1))
                    {
                        journal.StandardAbbreviation = abbreviation1;
                    }
                    if (!String.IsNullOrEmpty(abbreviation2))
                    {
                        journal.UserAbbreviation1 = abbreviation2;
                    }
                    if (!String.IsNullOrEmpty(abbreviation3))
                    {
                        journal.UserAbbreviation2 = abbreviation3;
                    }

                    journalCollection.Add(journal);
                }

                DialogResult updateReferences = MessageBox.Show(periodicalList, ImportJournalsResources.WoodwardMacroUpdateMessage, periodicalList.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                                                MessageBoxDefaultButton.Button2);

                if (updateReferences == DialogResult.Yes)
                {
                    var references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences();
                    foreach (var reference in references)
                    {
                        if (reference.Periodical == null)
                        {
                            continue;
                        }
                        if (journalCollection.Any(item => item.Name == reference.Periodical.Name) && !String.IsNullOrEmpty(reference.Periodical.Name))
                        {
                            reference.Periodical = journalCollection.Where(item => item.Name == reference.Periodical.Name).FirstOrDefault();
                            refCounter++;
                        }
                    }
                    project.Periodicals.AddRange(journalCollection);
                }
                else
                {
                    project.Periodicals.AddRange(journalCollection);
                }
            }
            catch (Exception e)
            {
                Cursor.Current    = Cursors.Default;
                journalCollection = null;
                MessageBox.Show(periodicalList, ImportJournalsResources.MacroImportingErrorMessage.FormatString(e.Message), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;

                if (journalCollection != null)
                {
                    MessageBox.Show(periodicalList, ImportJournalsResources.WoodwardMacroResultMessage.FormatString(journalCollection.Count, refCounter), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    journalCollection = null;
                }
            }
        }
Beispiel #2
0
        public static void Run(PeriodicalList periodicalList)
        {
            var project           = periodicalList.Project;
            var journalUrl        = @"http://ftp.ncbi.nih.gov/pubmed/J_Entrez.txt"; // URL for journal list text file
            var journalCollection = new List <Periodical>();

            string completeList;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                using (var webClient = new WebClient2()
                {
                    Timeout = 60000
                })
                {
                    using (var stream = webClient.OpenRead(journalUrl))
                    {
                        using (var streamReader = new StreamReader(stream))
                        {
                            completeList = streamReader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(periodicalList, ImportJournalsResources.PubMedMacroReadErrorMessage.FormatString(journalUrl, e.Message), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                var entrySplitters           = new string[] { @"--------------------------------------------------------" };
                var individualJournalEntries = completeList.Split(entrySplitters, StringSplitOptions.RemoveEmptyEntries).ToList();

                var counter = 0;

                var splitEntry = new Regex(@"^(?:JrId: )(?<JournalId>\d+?)(?:\nJournalTitle: )(?<JournalTitle>.*?)(?:\nMedAbbr: )(?<Abbreviation2>.*?)(?:\nISSN \(Print\): )(?<IssnPrint>.*?)(?:\nISSN \(Online\): )(?<IssnOnline>.*?)(?:\nIsoAbbr: )(?<Abbreviation1>.*?)(?:\nNlmId: )(?<NlmId>.*?)$",
                                           RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.Multiline);

                foreach (string journalEntry in individualJournalEntries)
                {
                    counter++;

                    string journalTitle;
                    string abbreviation1;
                    string abbreviation2;
                    string abbreviation3;
                    string issnPrint;
                    string issnOnline;
                    string nlmId;


                    var match = splitEntry.Match(journalEntry);
                    journalTitle  = match.Groups["JournalTitle"].Value;
                    abbreviation1 = match.Groups["Abbreviation1"].Value;
                    abbreviation2 = match.Groups["Abbreviation2"].Value;
                    issnPrint     = match.Groups["IssnPrint"].Value;
                    issnOnline    = match.Groups["IssnOnline"].Value;
                    nlmId         = match.Groups["NlmId"].Value;

                    if (string.IsNullOrEmpty(abbreviation1))
                    {
                        abbreviation1 = abbreviation2;
                    }
                    if (!abbreviation1.Contains(".") && !String.IsNullOrEmpty(abbreviation1))
                    {
                        var journalTitleWords          = journalTitle.ToLowerInvariant().Split(new char[] { ' ', '.', ';', ',', ':', '&', '-' }, StringSplitOptions.RemoveEmptyEntries);
                        var abbreviation1Words         = abbreviation1.Split(' ');
                        var abbreviation1WithFullStops = new List <string>();


                        foreach (var word in abbreviation1Words)
                        {
                            if (word.StartsWith("(") || word.EndsWith(")"))
                            {
                                abbreviation1WithFullStops.Add(word);
                            }
                            else if (!Array.Exists(journalTitleWords, x => x == word.ToLowerInvariant()))
                            {
                                abbreviation1WithFullStops.Add(word + ".");
                            }
                            else
                            {
                                abbreviation1WithFullStops.Add(word);
                            }
                        }

                        abbreviation1 = string.Join(" ", abbreviation1WithFullStops);
                    }

                    abbreviation3 = Regex.Match(journalTitle, @"(?:: )[A-Z]{2,6}$").ToString();

                    var journal = new Periodical(project, journalTitle);
                    if (!string.IsNullOrEmpty(abbreviation1))
                    {
                        journal.StandardAbbreviation = abbreviation1;
                    }
                    if (!string.IsNullOrEmpty(abbreviation2))
                    {
                        journal.UserAbbreviation1 = abbreviation2;
                    }
                    if (!string.IsNullOrEmpty(abbreviation3))
                    {
                        journal.UserAbbreviation2 = abbreviation3;
                    }

                    if (!string.IsNullOrEmpty(issnPrint) && IssnValidator.IsValid(issnPrint))
                    {
                        journal.Issn = issnPrint;
                    }
                    else if (!string.IsNullOrEmpty(issnOnline) && IssnValidator.IsValid(issnOnline))
                    {
                        journal.Issn = issnOnline;
                    }

                    if (!string.IsNullOrEmpty(issnPrint) && IssnValidator.IsValid(issnPrint) && !string.IsNullOrEmpty(issnOnline) && IssnValidator.IsValid(issnOnline))
                    {
                        journal.Notes = "ISSN (Online): " + issnOnline;
                    }

                    if (!string.IsNullOrEmpty(nlmId))
                    {
                        journal.Notes = journal.Notes + "\nNlmID: " + nlmId;
                    }

                    journalCollection.Add(journal);
                }
                project.Periodicals.AddRange(journalCollection);
            }
            catch (Exception exception)
            {
                Cursor.Current    = Cursors.Default;
                journalCollection = null;
                MessageBox.Show(periodicalList, ImportJournalsResources.MacroImportingErrorMessage.FormatString(exception.Message), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;

                if (journalCollection != null)
                {
                    MessageBox.Show(periodicalList, ImportJournalsResources.PubMedMacroResultMessage.FormatString(journalCollection.Count.ToString()), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    journalCollection = null;
                }
            }
        }
Beispiel #3
0
        public static void Run(PeriodicalList periodicalList)
        {
            var    project  = periodicalList.Project;
            string fileName = null;

            var journalCollection = new List <Periodical>();

            try
            {
                using (var openFileDialog = new OpenFileDialog
                {
                    Filter = ImportJournalsResources.FileMacroOpenFileDialogFilters,
                    Title = ImportJournalsResources.FileMacroOpenFileDialogSubject
                })
                {
                    if (openFileDialog.ShowDialog(periodicalList) != DialogResult.OK)
                    {
                        return;
                    }

                    fileName = openFileDialog.FileName;
                }

                Cursor.Current = Cursors.WaitCursor;

                string journalList;
                var    enc = GetFileEncoding(fileName);
                using (var streamReader = new StreamReader(fileName, enc))
                {
                    journalList = streamReader.ReadToEnd();
                    streamReader.Close();
                }

                var testRegex = new Regex("[\x00-\x1f-[\t\n\r]]", RegexOptions.CultureInvariant | RegexOptions.Compiled);
                if (testRegex.IsMatch(journalList))
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(periodicalList, ImportJournalsResources.FileMacroNotSupportedCharactersMessage, periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }


                var matchRegex = new Regex(
                    @"^(?<FullName>[^#=;|\t\n]+?)(?: *[#=;|\t] *(?<Abbreviation1>[^#=;|\t\n]*))?(?: *[#=;|\t] *(?<Abbreviation2>[^#=;|\t\n]*))?(?: *[#=;|\t] *(?<Abbreviation3>[^#=;|\t\n]*))?(?: *[#=;|\t] *(?<ISSN>[^#=;|\t\n]*))??$",
                    RegexOptions.CultureInvariant
                    | RegexOptions.Compiled
                    | RegexOptions.Multiline    //IMPORTANT!
                    );


                var    matchCollection = matchRegex.Matches(journalList);
                string sISSN           = string.Empty;

                foreach (Match match in matchCollection)
                {
                    if (string.IsNullOrEmpty(match.Groups["FullName"].Value))
                    {
                        continue;
                    }

                    var journal = new Periodical(project, match.Groups["FullName"].Value);
                    if (!string.IsNullOrEmpty(match.Groups["Abbreviation1"].Value))
                    {
                        journal.StandardAbbreviation = match.Groups["Abbreviation1"].Value;
                    }
                    if (!string.IsNullOrEmpty(match.Groups["Abbreviation2"].Value))
                    {
                        journal.UserAbbreviation1 = match.Groups["Abbreviation2"].Value;
                    }
                    if (!string.IsNullOrEmpty(match.Groups["Abbreviation3"].Value))
                    {
                        journal.UserAbbreviation2 = match.Groups["Abbreviation3"].Value;
                    }

                    if (!string.IsNullOrEmpty(match.Groups["ISSN"].Value))
                    {
                        sISSN = match.Groups["ISSN"].Value;
                        if (IssnValidator.IsValid(sISSN))
                        {
                            journal.Issn = sISSN;
                        }
                    }

                    journalCollection.Add(journal);
                }

                project.Periodicals.AddRange(journalCollection);
            }

            catch (Exception exception)
            {
                Cursor.Current    = Cursors.Default;
                journalCollection = null;
                MessageBox.Show(periodicalList, exception.ToString(), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            finally
            {
                Cursor.Current = Cursors.Default;

                if (journalCollection != null)
                {
                    MessageBox.Show(periodicalList, ImportJournalsResources.FileMacroResultMessage.FormatString(journalCollection.Count), periodicalList.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    journalCollection = null;
                }
            }
        }