public static string GetGenericPublication(BibTexItem bibtex_item)
        {
            string generic_publication = null;

            generic_publication = GetField(bibtex_item, "journal");
            if (!String.IsNullOrEmpty(generic_publication))
            {
                return(generic_publication);
            }

            generic_publication = GetField(bibtex_item, "booktitle");
            if (!String.IsNullOrEmpty(generic_publication))
            {
                return(generic_publication);
            }

            generic_publication = GetField(bibtex_item, "container-title");
            if (!String.IsNullOrEmpty(generic_publication))
            {
                return(generic_publication);
            }

            generic_publication = GetField(bibtex_item, "publisher");
            if (!String.IsNullOrEmpty(generic_publication))
            {
                return(generic_publication);
            }

            return(null);
        }
        public static bool DoesBibTeXMatchDocument(BibTexItem bibtex_item, PDFDocument pdf_document, out PDFSearchResultSet search_result_set)
        {
            try
            {
                string authors_string = BibTexTools.GetAuthor(bibtex_item);
                if (!String.IsNullOrEmpty(authors_string))
                {
                    List <NameTools.Name> names = NameTools.SplitAuthors(authors_string, PDFDocument.UNKNOWN_AUTHORS);
                    StringBuilder         sb    = new StringBuilder();
                    foreach (NameTools.Name name in names)
                    {
                        sb.AppendFormat("\"{0}\" ", name.last_name);
                    }

                    string names_search_string = sb.ToString();
                    if (!String.IsNullOrEmpty(names_search_string))
                    {
                        search_result_set = PDFSearcher.Search(pdf_document, 1, names_search_string, PDFSearcher.MATCH_CONTAINS);
                        if (0 < search_result_set.Count)
                        {
                            return(true);
                        }
                    }
                }
            }
            catch (Exception) { }

            search_result_set = new PDFSearchResultSet();
            return(false);
        }
Esempio n. 3
0
            public BibTexItem ToBibTeX()
            {
                BibTexItem bibtex_item = new BibTexItem();

                bibtex_item.Key = GenerateReasonableKey();

                // Process the type
                {
                    string ris_type = "";
                    if (attributes.ContainsKey("%0"))
                    {
                        ris_type = attributes["%0"][0];
                    }
                    bibtex_item.Type = MapRISToBibTeXType(ris_type);
                }

                // Process the generic fields -* note that we will silently throw away all "multiple" items with this field name
                foreach (var attribute_pair in attributes)
                {
                    string bibtex_field_type = MapRISToBibTeXFieldType(attribute_pair.Key);
                    if (!String.IsNullOrEmpty(bibtex_field_type))
                    {
                        bibtex_item[bibtex_field_type] = attribute_pair.Value[0];
                    }
                }

                // Create the authors
                if (attributes.ContainsKey("%A"))
                {
                    StringBuilder sb = new StringBuilder();

                    List <string> author_list = attributes["%A"];
                    foreach (string author in author_list)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(" and ");
                        }

                        sb.Append(author);
                    }

                    string authors = sb.ToString();
                    if (!String.IsNullOrEmpty(authors))
                    {
                        bibtex_item["author"] = authors;
                    }
                }


                // Create the keywords
                if (attributes.ContainsKey("%K"))
                {
                    string keywords = (attributes["%K"][0]).Replace("\n", ",");
                    bibtex_item["keywords"] = keywords;
                }

                return(bibtex_item);
            }
 public static bool HasField(BibTexItem bibtex_item, string field)
 {
     if (null == bibtex_item)
     {
         return(false);
     }
     return(bibtex_item.ContainsField(field));
 }
        private void BuildTextFromBibTeX(string bibtex, BibTexItem bibtex_item)
        {
            if (updating_from_text)
            {
                return;
            }

            ObjBibTeXText.Text = bibtex;
        }
        private static void PopulateTentativeField(BibTexItem bibtex_item, SQLiteDataReader reader, string mendeley_key, string bibtex_key)
        {
            object value = reader[mendeley_key];

            if (DBNull.Value != value)
            {
                bibtex_item[bibtex_key] = Convert.ToString(value);
            }
        }
        // ----------------------------------------------------------------------------------------

        public static string GetField(BibTexItem bibtex_item, string field)
        {
            if (null != bibtex_item)
            {
                return(bibtex_item[field]);
            }
            else
            {
                return("");
            }
        }
 public static string GetField(string bibtex, string field)
 {
     try
     {
         BibTexItem bibtex_item = BibTexParser.ParseOne(bibtex, false);
         return(GetField(bibtex_item, field));
     }
     catch (Exception ex)
     {
         Logging.Warn(ex, "There was a problem extracting from the BibTeX");
         return("");
     }
 }
Esempio n. 9
0
        public static int FindCitations(PDFDocument pdf_document)
        {
            int total_found = 0;

            BibTexItem bibtex_item = pdf_document.BibTexItem;

            if (null != bibtex_item)
            {
                string patent_id = bibtex_item["patent_id"];
                if (!String.IsNullOrEmpty(patent_id))
                {
                    foreach (PDFDocument pdf_document_other in pdf_document.Library.PDFDocuments)
                    {
                        // Let's not work on the same document
                        if (pdf_document.Fingerprint == pdf_document_other.Fingerprint)
                        {
                            continue;
                        }

                        // Lets not do work that has already been done before...
                        {
                            bool already_found = true;
                            already_found = already_found && pdf_document.PDFDocumentCitationManager.ContainsInboundCitation(pdf_document_other.Fingerprint);
                            already_found = already_found && pdf_document_other.PDFDocumentCitationManager.ContainsOutboundCitation(pdf_document.Fingerprint);
                            if (already_found)
                            {
                                Logging.Info("Skipping check for citation from {0} to {1} because we know it already.", pdf_document_other.Fingerprint, pdf_document.Fingerprint);
                                continue;
                            }
                        }

                        BibTexItem bibtex_item_other = pdf_document_other.BibTexItem;
                        if (null != bibtex_item_other)
                        {
                            string cites_other = bibtex_item_other["cites"];
                            if (!String.IsNullOrEmpty(cites_other))
                            {
                                if (cites_other.Contains(patent_id))
                                {
                                    pdf_document.PDFDocumentCitationManager.AddInboundCitation(pdf_document_other.Fingerprint);
                                    pdf_document_other.PDFDocumentCitationManager.AddOutboundCitation(pdf_document.Fingerprint);
                                    ++total_found;
                                }
                            }
                        }
                    }
                }
            }

            return(total_found);
        }
        private static void AddDocumentMetadata_BibTex(Document document, BibTexItem bibtex_item)
        {
            if (null == bibtex_item)
            {
                return;
            }

            document.Add(new Field("type", bibtex_item.Type, Field.Store.NO, Field.Index.ANALYZED));
            document.Add(new Field("key", bibtex_item.Key, Field.Store.NO, Field.Index.ANALYZED));

            foreach (var pair in bibtex_item.EnumerateFields())
            {
                document.Add(new Field(pair.Key, pair.Value, Field.Store.NO, Field.Index.ANALYZED));
            }
        }
        public static bool DoesBibTeXMatchDocument(string bibtex, PDFDocument pdf_document, out PDFSearchResultSet search_result_set)
        {
            try
            {
                if (!String.IsNullOrEmpty(bibtex))
                {
                    BibTexItem bibtex_item = BibTexParser.ParseOne(bibtex, true);

                    return(DoesBibTeXMatchDocument(bibtex_item, pdf_document, out search_result_set));
                }
            }
            catch (Exception) { }

            search_result_set = new PDFSearchResultSet();
            return(false);
        }
        private void ButtonApplyBibTeX_Click(object sender, RoutedEventArgs e)
        {
            List <PDFDocument> selected_pdf_documents = SelectedPDFDocuments;

            if (null == selected_pdf_documents)
            {
                return;
            }

            if (!MessageBoxes.AskQuestion("Are you sure you want to mass-edit {0} documents?", selected_pdf_documents.Count))
            {
                return;
            }

            BibTexItem bibtex_item_global = BibTexParser.ParseOne(bibtex_stub.BibTex, true);

            int non_updateable_documents = 0;

            foreach (var pdf_document in selected_pdf_documents)
            {
                BibTexItem bibtex_item = pdf_document.BibTexItem;
                if (null != bibtex_item)
                {
                    if (!String.IsNullOrEmpty(bibtex_item_global.Type))
                    {
                        bibtex_item.Type = bibtex_item_global.Type;
                    }

                    foreach (var field_pair in bibtex_item_global.Fields)
                    {
                        bibtex_item[field_pair.Key] = field_pair.Value;
                    }

                    pdf_document.BibTex = bibtex_item.ToBibTex();
                    pdf_document.Bindable.NotifyPropertyChanged(() => pdf_document.BibTex);
                }
                else
                {
                    ++non_updateable_documents;
                }
            }

            if (0 < non_updateable_documents)
            {
                MessageBoxes.Warn("There was a problem updating {0} documents as they do not have an existing BibTeX record associated with them.", non_updateable_documents);
            }
        }
        public static void Test()
        {
            string ris_text = File.ReadAllText(@"c:\TEMP\SAMPLEENDNOTE.TXT");
            List <List <string> > lines_set = SplitMultipleRISLines(ris_text);

            foreach (List <string> lines in lines_set)
            {
                RISRecord record = MapRISLinesToDictionary(lines);
                if (record.errors.Count > 0)
                {
                    Logging.Warn("Errors!");
                }

                BibTexItem bibtex = record.ToBibTeX();
                string     result = bibtex.ToBibTex();
            }
        }
        private static MatchingBibTeXRecord LocateBibTexForCitationItem_FOCUS(string reference_key, Library library)
        {
            foreach (PDFDocument pdf_document in library.PDFDocuments)
            {
                BibTexItem bibtex_item = pdf_document.BibTexItem;
                if (null != bibtex_item)
                {
                    if (bibtex_item.Key == reference_key)
                    {
                        return(new MatchingBibTeXRecord {
                            pdf_document = pdf_document, bibtex_item = bibtex_item
                        });
                    }
                }
            }

            return(null);
        }
Esempio n. 15
0
        private void ButtonUseSummary_Click(object sender, RoutedEventArgs e)
        {
            AugmentedBindable <PDFDocument> pdf_document_bindable = DataContext as AugmentedBindable <PDFDocument>;

            if (null == pdf_document_bindable)
            {
                return;
            }

            if (!String.IsNullOrEmpty(pdf_document_bindable.Underlying.BibTex))
            {
                if (!MessageBoxes.AskQuestion("You already have BibTeX associated with this record.  Are you sure you want to overwrite it?"))
                {
                    return;
                }
            }

            BibTexItem bibtem_item = new BibTexItem();

            bibtem_item.Type = "article";
            bibtem_item.Key  = String.Format(
                "{0}{1}{2}"
                , Constants.UNKNOWN_AUTHORS != pdf_document_bindable.Underlying.AuthorsCombined ? StringTools.GetFirstWord(pdf_document_bindable.Underlying.AuthorsCombined) : ""
                , Constants.UNKNOWN_YEAR != pdf_document_bindable.Underlying.YearCombined ? StringTools.GetFirstWord(pdf_document_bindable.Underlying.YearCombined) : ""
                , Constants.TITLE_UNKNOWN != pdf_document_bindable.Underlying.TitleCombined ? StringTools.GetFirstWord(pdf_document_bindable.Underlying.TitleCombined) : ""
                );

            if (Constants.TITLE_UNKNOWN != pdf_document_bindable.Underlying.TitleCombined)
            {
                bibtem_item["title"] = pdf_document_bindable.Underlying.TitleCombined;
            }
            if (Constants.UNKNOWN_AUTHORS != pdf_document_bindable.Underlying.AuthorsCombined)
            {
                bibtem_item["author"] = pdf_document_bindable.Underlying.AuthorsCombined;
            }
            if (Constants.UNKNOWN_YEAR != pdf_document_bindable.Underlying.YearCombined)
            {
                bibtem_item["year"] = pdf_document_bindable.Underlying.YearCombined;
            }

            pdf_document_bindable.Underlying.BibTex = bibtem_item.ToBibTex();
            pdf_document_bindable.NotifyPropertyChanged(nameof(pdf_document_bindable.Underlying.BibTex));
        }
        // ----------------------------------------------------------------------------------------

        public static void Test()
        {
            string sample_bibtext = @"@conference{kamp1984theory,title =       {{A theory of truth and semantic representation}},author =       {Kamp, H.},booktitle={Truth, Interpretation and Information: Selected Papers from the Third Amsterdam Colloquium},pages={1--41},year={1984}";

            Logging.Info("BibTex is:\n" + sample_bibtext);

            BibTexItem bibtex_item = BibTexParser.ParseOne(sample_bibtext, false);

            Logging.Info("Title is: " + GetTitle(bibtex_item));
            Logging.Info("Author is: " + GetAuthor(bibtex_item));
            Logging.Info("Year is: " + GetYear(bibtex_item));

            string replaced_bibtex = sample_bibtext;

            replaced_bibtex = SetTitle(replaced_bibtex, "New title");
            replaced_bibtex = SetAuthor(replaced_bibtex, "New author");
            replaced_bibtex = SetYear(replaced_bibtex, "New year");

            Logging.Info("Replaced BibTex is:\n" + replaced_bibtex);
        }
        void RebuidTextAndGrid()
        {
            string bibtex = BibTeX;

            if (String.IsNullOrEmpty(bibtex) && !ForceHideNoBibTeXInstructions)
            {
                ObjNoBibTeXInstructions.Visibility = Visibility.Visible;
            }
            else
            {
                ObjNoBibTeXInstructions.Visibility = Visibility.Collapsed;
            }

            BibTexItem bibtex_item = BibTexParser.ParseOne(bibtex, true);

            if (null == bibtex_item)
            {
                bibtex_item = new BibTexItem();
            }

            // If there were any exceptions, go pink and jump to the text editor
            if (bibtex_item.Exceptions.Count > 0 || bibtex_item.Warnings.Count > 0)
            {
                TextBlock tb = new TextBlock();
                tb.FontFamily   = new FontFamily("Courier New");
                tb.Text         = bibtex_item.GetExceptionsAndMessagesString();
                tb.TextWrapping = TextWrapping.Wrap;
                tb.MaxWidth     = 400;

                ImageBibTeXParseError.ToolTip    = tb;
                ImageBibTeXParseError.Visibility = ObjErrorPanel.Visibility = Visibility.Visible;
            }
            else
            {
                ObjErrorPanel.ToolTip            = null;
                ImageBibTeXParseError.Visibility = ObjErrorPanel.Visibility = Visibility.Collapsed;
            }

            BuildGridFromBibTeX(bibtex, bibtex_item);
            BuildTextFromBibTeX(bibtex, bibtex_item);
        }
 /// <summary>
 /// After setting the field, returns the WHOLE bibtex again
 /// </summary>
 /// <param name="bibtex"></param>
 /// <param name="field"></param>
 /// <param name="field_value"></param>
 /// <returns></returns>
 public static string SetField(string bibtex, string field, string field_value)
 {
     try
     {
         BibTexItem item = BibTexParser.ParseOne(bibtex, false);
         if (null != item)
         {
             item[field] = field_value;
             return(item.ToBibTex());
         }
         else
         {
             return(bibtex);
         }
     }
     catch (Exception ex)
     {
         Logging.Warn(ex, "There was a problem setting the BibTeX");
         return(null);
     }
 }
        public static void SetGenericPublication(BibTexItem bibtex_item, string generic_publication)
        {
            if (null == bibtex_item)
            {
                return;
            }

            bool set_a_field = false;

            if (bibtex_item.ContainsField("journal"))
            {
                set_a_field            = true;
                bibtex_item["journal"] = generic_publication;
            }

            if (bibtex_item.ContainsField("booktitle"))
            {
                set_a_field = true;
                bibtex_item["booktitle"] = generic_publication;
            }

            if (bibtex_item.ContainsField("container-title"))
            {
                set_a_field = true;
                bibtex_item["container-title"] = generic_publication;
            }

            if (bibtex_item.ContainsField("publisher"))
            {
                set_a_field = true;
                bibtex_item["publisher"] = generic_publication;
            }

            // If no field was ever set, insert a new field.
            // NB: This could get smarter in that we don't always want to insert journal, depending on bibtex type
            if (!set_a_field)
            {
                bibtex_item["journal"] = generic_publication;
            }
        }
        private void EnsureStartPageOffset()
        {
            try
            {
                // -1 means we are not initialised
                if (-1 == start_page_offset)
                {
                    // 0 means that there is no known offset
                    start_page_offset = 0;

                    BibTexItem bibtex_item = pdf_document.BibTexItem;
                    if (null != bibtex_item)
                    {
                        string start_page_offset_text = null;
                        if (String.IsNullOrEmpty(start_page_offset_text) && bibtex_item.ContainsField("page"))
                        {
                            start_page_offset_text = bibtex_item["page"];
                        }
                        if (String.IsNullOrEmpty(start_page_offset_text) && bibtex_item.ContainsField("pages"))
                        {
                            start_page_offset_text = bibtex_item["pages"];
                        }

                        if (!String.IsNullOrEmpty(start_page_offset_text))
                        {
                            MatchCollection matches = Regex.Matches(start_page_offset_text, @"(\d+).*");
                            if (0 < matches.Count && 1 < matches[0].Groups.Count)
                            {
                                string start_page_offset_string = matches[0].Groups[1].Value;
                                start_page_offset = Convert.ToInt32(start_page_offset_string);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ex, "Problem calculating start_page_offset");
            }
        }
Esempio n. 21
0
        private string CreatePaperTweet()
        {
            var pdf_document_bindable = DataContext as AugmentedBindable <PDFDocument>;

            if (null == pdf_document_bindable)
            {
                return(null);
            }

            PDFDocument pdf_document = pdf_document_bindable.Underlying;

            BibTexItem bibtex_item = pdf_document.BibTexItem;

            if (!BibTexTools.HasTitle(bibtex_item))
            {
                return(null);
            }

            if (!BibTexTools.HasAuthor(bibtex_item))
            {
                return(null);
            }
            List <NameTools.Name> names = NameTools.SplitAuthors(BibTexTools.GetAuthor(bibtex_item));

            if (0 == names.Count)
            {
                return(null);
            }

            string tweet = String.Format("I'm reading {1}'s '{0}' with @Qiqqa http://qiqqa.com", BibTexTools.GetTitle(bibtex_item), names[0].last_name);

            if (140 < tweet.Length)
            {
                return(null);
            }

            return(tweet);
        }
Esempio n. 22
0
        internal static void Export(WebLibraryDetail web_library_detail, List <PDFDocument> pdf_documents, string base_path, Dictionary <string, PDFDocumentExportItem> pdf_document_export_items)
        {
            Logging.Info("Exporting entries to BibTeXTAB separated");

            // First work out what fields are available
            List <string> field_names = null;
            {
                HashSet <string> field_names_set = new HashSet <string>();
                for (int i = 0; i < pdf_documents.Count; ++i)
                {
                    PDFDocument pdf_document = pdf_documents[i];
                    if (!String.IsNullOrEmpty(pdf_document.BibTex))
                    {
                        BibTexItem item = BibTexParser.ParseOne(pdf_document.BibTex, true);
                        if (null != item)
                        {
                            foreach (var field in item.Fields)
                            {
                                field_names_set.Add(field.Key.ToLower());
                            }
                        }
                    }
                }

                field_names = new List <string>(field_names_set);
                field_names.Sort();
            }

            // Write out the header
            DateTime      now = DateTime.Now;
            StringBuilder sb  = new StringBuilder();

            sb.AppendLine("% -------------------------------------------------------------------------");
            sb.AppendLine(String.Format("% This tab separated file was generated by Qiqqa ({0}?ref=EXPTAB)", Common.Configuration.WebsiteAccess.Url_Documentation4Qiqqa));
            sb.AppendLine(String.Format("% {0} {1}", now.ToLongDateString(), now.ToLongTimeString()));
            sb.AppendLine("% Version 1");
            sb.AppendLine("% -------------------------------------------------------------------------");
            sb.AppendLine();

            // Headers
            sb.AppendFormat("{0}\t", "Fingerprint");
            sb.AppendFormat("{0}\t", "Filename");
            sb.AppendFormat("{0}\t", "BibTexKey");
            sb.AppendFormat("{0}\t", "BibTexType");
            foreach (string field_name in field_names)
            {
                sb.AppendFormat("{0}\t", FormatFreeText(field_name));
            }
            sb.AppendLine();

            // Write out the entries
            for (int i = 0; i < pdf_documents.Count; ++i)
            {
                StatusManager.Instance.UpdateStatus("TabExport", String.Format("Exporting entry {0} of {1}", i, pdf_documents.Count), i, pdf_documents.Count);

                PDFDocument pdf_document = pdf_documents[i];
                sb.AppendFormat("{0}\t", pdf_document.Fingerprint);
                sb.AppendFormat("{0}\t", pdf_document_export_items.ContainsKey(pdf_document.Fingerprint) ? pdf_document_export_items[pdf_document.Fingerprint].filename : "");

                try
                {
                    if (!String.IsNullOrEmpty(pdf_document.BibTex))
                    {
                        BibTexItem item = BibTexParser.ParseOne(pdf_document.BibTex, true);
                        if (null != item)
                        {
                            sb.AppendFormat("{0}\t", item.Key);
                            sb.AppendFormat("{0}\t", item.Type);
                            foreach (string field_name in field_names)
                            {
                                sb.AppendFormat("{0}\t", item.ContainsField(field_name) ? FormatFreeText(item[field_name]) : "");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.Error(ex, "There was a problem exporting the tab representation for document {0}", pdf_document.Fingerprint);
                }

                sb.AppendLine();
            }

            // Write to disk
            string filename = Path.GetFullPath(Path.Combine(base_path, @"Qiqqa.BibTeX.tab"));

            File.WriteAllText(filename, sb.ToString());

            StatusManager.Instance.UpdateStatus("TabExport", String.Format("Exported your BibTeX tab entries to {0}", filename));
        }
        private void ButtonGet_Click(object sender, RoutedEventArgs e)
        {
            if (null == library)
            {
                MessageBoxes.Error("You must choose a library...");
                return;
            }

            TxtData.Text = "";

            int MaxRecordCount;

            if (!int.TryParse(MaxNumberOfRecords.Text, out MaxRecordCount))
            {
                MaxRecordCount = 0;
            }

            var items = library.LibraryDB.GetLibraryItems(TxtFingerprint.Text, TxtExtension.Text, MaxRecordCount);

            if (0 == items.Count)
            {
                MessageBoxes.Warn("No entry was found.");
            }
            else if (1 == items.Count)
            {
                byte[] data = items[0].data;
                string json = Encoding.UTF8.GetString(data);
                TxtData.Text = json;
            }
            else
            {
                MessageBoxes.Warn("{0} entries were found; we're showing them all but you'll only be able to PUT/WRITE the first one!", items.Count);

                StringBuilder allstr = new StringBuilder();
                for (int i = 0; i < items.Count; i++)
                {
                    if (i > 0)
                    {
                        allstr.Append("\n\n==========================================================\n\n");
                    }

                    LibraryDB.LibraryItem item = items[i];
                    byte[] data = item.data;
                    string json = Encoding.UTF8.GetString(data);
                    allstr.AppendLine(json);
                    allstr.Append("\n--------------(decoded metadata)--------------------------\n");
                    allstr.AppendLine(string.Format("fingerprint: {0}", item.fingerprint));
                    allstr.AppendLine(string.Format("extension: {0}", item.extension));
                    allstr.AppendLine(string.Format("MD5 hash: {0}", item.md5));

                    try
                    {
                        PDFDocument doc       = PDFDocument.LoadFromMetaData(library, item.data, null);
                        string      bibtexStr = doc.BibTex;
                        if (null == bibtexStr)
                        {
                            bibtexStr = "--(NULL)--";
                        }
                        else if (String.IsNullOrWhiteSpace(bibtexStr))
                        {
                            bibtexStr = "--(EMPTY)--";
                        }
                        else
                        {
                            BibTexItem bibtex = doc.BibTexItem;
                            string     bibtexParseErrors;
                            string     formattedBibStr;
                            string     rawStr;

                            if (bibtex != null)
                            {
                                if (bibtex.Exceptions.Count > 0 || bibtex.Warnings.Count > 0)
                                {
                                    bibtexParseErrors = bibtex.GetExceptionsAndMessagesString();
                                }
                                else
                                {
                                    bibtexParseErrors = String.Empty;
                                }
                                formattedBibStr = bibtex.ToBibTex();
                                if (String.IsNullOrEmpty(formattedBibStr))
                                {
                                    formattedBibStr = "--(EMPTY)--";
                                }
                                rawStr = bibtex.ToString();
                                if (String.IsNullOrEmpty(rawStr))
                                {
                                    rawStr = "--(EMPTY)--";
                                }
                            }
                            else
                            {
                                bibtexParseErrors = "ERROR: This content is utterly INVALID BibTeX as far as the BibTeX parser is concerned!";
                                formattedBibStr   = String.Empty;
                                rawStr            = String.Empty;
                            }

                            if (!String.IsNullOrEmpty(formattedBibStr))
                            {
                                allstr.AppendLine(string.Format("\nBibTeX Formatted:\n    {0}", formattedBibStr.Replace("\n", "\n    ")));
                            }
                            if (!String.IsNullOrEmpty(rawStr))
                            {
                                allstr.AppendLine(string.Format("\nBibTeX RAW FMT:\n    {0}", rawStr.Replace("\n", "\n    ")));
                            }
                            if (!String.IsNullOrEmpty(bibtexParseErrors))
                            {
                                allstr.AppendLine(string.Format("\nBibTeX Parse Diagnostics:\n    {0}", bibtexParseErrors.Replace("\n", "\n    ")));
                            }
                        }
                        allstr.AppendLine(string.Format("\nBibTeX RAW INPUT:\n    {0}", bibtexStr.Replace("\n", "\n    ")));
                    }
                    catch (Exception ex)
                    {
                        allstr.AppendLine(string.Format("*** PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    ")));
                    }
                }

                // also dump the output to file (for diagnostics)
                string path = Path.GetFullPath(Path.Combine(library.LIBRARY_BASE_PATH, @"Qiqqa.DBexplorer.QueryDump.txt"));

                // overwrite previous query dump:
                using (StreamWriter sr = new StreamWriter(path, false /* overwrite */))
                {
                    sr.WriteLine(allstr);
                }

                TxtData.Text = allstr.ToString();
            }
        }
Esempio n. 24
0
        private void ButtonGet_Click(object sender, RoutedEventArgs e)
        {
            if (null == web_library_detail)
            {
                MessageBoxes.Error("You must choose a library...");
                return;
            }

            TxtData.Text = "";

            int MaxRecordCount;

            if (!int.TryParse(MaxNumberOfRecords.Text, out MaxRecordCount))
            {
                MaxRecordCount = 0;
            }

            var items = web_library_detail.Xlibrary.LibraryDB.GetLibraryItems(TxtExtension.Text, new List <string>()
            {
                TxtFingerprint.Text
            }, MaxRecordCount);

            if (0 == items.Count)
            {
                MessageBoxes.Warn("No entry was found.");
            }
            else if (1 == items.Count)
            {
                byte[] data = items[0].data;
                string json = Encoding.UTF8.GetString(data);
                TxtData.Text = json;
            }
            else
            {
                MessageBoxes.Warn("{0} entries were found; we're showing them all but you'll only be able to PUT/WRITE the first one!", items.Count);

                StringBuilder allstr = new StringBuilder();
                for (int i = 0; i < items.Count; i++)
                {
                    if (i > 0)
                    {
                        allstr.Append("\n\n==========================================================\n\n");
                    }

                    LibraryDB.LibraryItem item = items[i];
                    byte[] data = item.data;
                    string json = Encoding.UTF8.GetString(data);
                    allstr.AppendLine(json);
                    allstr.Append("\n--------------(decoded metadata)--------------------------\n");
                    allstr.AppendLine(string.Format("fingerprint: {0}", item.fingerprint));
                    allstr.AppendLine(string.Format("extension: {0}", item.extension));
                    allstr.AppendLine(string.Format("MD5 hash: {0}", item.md5));

                    if (item.data.Length > 0)
                    {
                        switch (item.extension)
                        {
                        case "citations":
                            try
                            {
                                string citations = Encoding.UTF8.GetString(data);
                                // format: (hash, hash_REF, unknown_int) on every line. e.g.
                                //
                                // 99D81E4872BD14C8766C57B298175B92C9CA749,024F6E48744443D5B7D22DE2007EFA7C_REF,0
                                // E62116BFF03E2D6AF99D596C8EB5C3D3B6111B5,024F6E48744443D5B7D22DE2007EFA7C_REF,0
                                // EB9BAE68C451CEEC70E4FE352078AEA4050A427,024F6E48744443D5B7D22DE2007EFA7C_REF,0
                                // ...
                                allstr.AppendLine(string.Format("citations = [\n{0}\n]", citations));
                            }
                            catch (Exception ex)
                            {
                                allstr.AppendLine(string.Format("*** CITATIONS RECORD PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    ")));
                            }
                            break;

                        // highlights format: JSON:
                        //
                        // [
                        //  {
                        //    "P": 3,
                        //    "L": 0.21154,
                        //    "T": 0.1395,
                        //    "W": 0.09829,
                        //    "H": 0.01615,
                        //    "C": 0
                        //  },
                        //  ...
                        //

                        /*
                         * example annotation: JSON format:
                         *
                         *  [
                         *     {
                         *       "Guid": "329abf6b-59b4-450a-b015-65402c25068d",
                         *       "DocumentFingerprint": "0E294EABE45DD6B903A3F6EEF964D80645F272C",
                         *       "Page": 4,
                         *       "DateCreated": "20120427153803358",
                         *       "Deleted": false,
                         *       "ColorWrapper": "#FF87CEEB",
                         *       "Left": 0.11479289940828402,
                         *       "Top": 0.06685699621479417,
                         *       "Width": 0.41301775147928993,
                         *       "Height": 0.25688073394495414,
                         *       "FollowUpDate": "00010101000000000",
                         *       "Text": "rgrgdrgdrgdrgdrgdrgdrdrgdrg"
                         *     }
                         *   ]
                         */

                        case "annotations":
                            try
                            {
                                string s = Encoding.UTF8.GetString(data);
                                allstr.AppendLine(string.Format("{1} = [\n{0}\n]", s, item.extension));
                            }
                            catch (Exception ex)
                            {
                                allstr.AppendLine(string.Format("*** {1} RECORD PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    "), item.extension));
                            }
                            break;

                        case "highlights":
                            try
                            {
                                string s = Encoding.UTF8.GetString(data);
                                allstr.AppendLine(string.Format("{1} = [\n{0}\n]", s, item.extension));
                            }
                            catch (Exception ex)
                            {
                                allstr.AppendLine(string.Format("*** {1} RECORD PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    "), item.extension));
                            }
                            break;

                        // inks format: binary serialized
                        //
                        case "inks":
                            try
                            {
                                string s = Encoding.UTF8.GetString(data);
                                allstr.AppendLine(string.Format("{1} = [\n{0}\n]", s, item.extension));
                            }
                            catch (Exception ex)
                            {
                                allstr.AppendLine(string.Format("*** {1} RECORD PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    "), item.extension));
                            }
                            break;

                        case "metadata":
                            try
                            {
                                PDFDocument doc       = PDFDocument.LoadFromMetaData(web_library_detail, item.fingerprint, item.data);
                                string      bibtexStr = doc.BibTex;
                                if (null == bibtexStr)
                                {
                                    bibtexStr = "--(NULL)--";
                                }
                                else if (String.IsNullOrWhiteSpace(bibtexStr))
                                {
                                    bibtexStr = "--(EMPTY)--";
                                }
                                else
                                {
                                    BibTexItem bibtex = doc.BibTexItem;
                                    string     bibtexParseErrors;
                                    string     formattedBibStr;
                                    string     rawStr;

                                    if (bibtex != null)
                                    {
                                        if (bibtex.Exceptions.Count > 0 || bibtex.Warnings.Count > 0)
                                        {
                                            bibtexParseErrors = bibtex.GetExceptionsAndMessagesString();
                                        }
                                        else
                                        {
                                            bibtexParseErrors = String.Empty;
                                        }
                                        formattedBibStr = bibtex.ToBibTex();
                                        if (String.IsNullOrEmpty(formattedBibStr))
                                        {
                                            formattedBibStr = "--(EMPTY)--";
                                        }
                                        rawStr = bibtex.ToString();
                                        if (String.IsNullOrEmpty(rawStr))
                                        {
                                            rawStr = "--(EMPTY)--";
                                        }
                                    }
                                    else
                                    {
                                        bibtexParseErrors = "ERROR: This content is utterly INVALID BibTeX as far as the BibTeX parser is concerned!";
                                        formattedBibStr   = String.Empty;
                                        rawStr            = String.Empty;
                                    }

                                    if (!String.IsNullOrEmpty(formattedBibStr))
                                    {
                                        allstr.AppendLine(string.Format("\nBibTeX Formatted:\n    {0}", formattedBibStr.Replace("\n", "\n    ")));
                                    }
                                    if (!String.IsNullOrEmpty(rawStr))
                                    {
                                        allstr.AppendLine(string.Format("\nBibTeX RAW FMT:\n    {0}", rawStr.Replace("\n", "\n    ")));
                                    }
                                    if (!String.IsNullOrEmpty(bibtexParseErrors))
                                    {
                                        allstr.AppendLine(string.Format("\nBibTeX Parse Diagnostics:\n    {0}", bibtexParseErrors.Replace("\n", "\n    ")));
                                    }
                                }
                                allstr.AppendLine(string.Format("\nBibTeX RAW INPUT:\n    {0}", bibtexStr.Replace("\n", "\n    ")));
                            }
                            catch (Exception ex)
                            {
                                allstr.AppendLine(string.Format("*** PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    ")));
                            }
                            break;

                        default:
                            try
                            {
                                string s = Encoding.UTF8.GetString(data);
                                allstr.AppendLine(string.Format("{1} = [\n{0}\n]", s, item.extension));
                            }
                            catch (Exception ex)
                            {
                                allstr.AppendLine(string.Format("*** XXX = {1} RECORD PARSE ERROR ***:\n    {0}", ex.ToString().Replace("\n", "\n    "), item.extension));
                            }
                            break;
                        }
                    }
                }

                // also dump the output to file (for diagnostics)
                string path = Path.GetFullPath(Path.Combine(web_library_detail.LIBRARY_BASE_PATH, @"Qiqqa.DBexplorer.QueryDump.txt"));

                // overwrite previous query dump:
                using (StreamWriter sr = new StreamWriter(path, false /* overwrite */))
                {
                    sr.WriteLine(allstr);
                }

                TxtData.Text = allstr.ToString();
            }
        }
        private static ImportingIntoLibrary.FilenameWithMetadataImport ConvertEndnoteToFilenameWithMetadataImport(string endnote_database_filename, MYDRecord record)
        {
            BibTexItem bibtex_item = new BibTexItem();

            string type = "article";

            TransformType(record.reference_type, ref type);
            bibtex_item.Type = type;
            bibtex_item.Key  = BibTexTools.GenerateRandomBibTeXKey();

            foreach (var pair in record.fields)
            {
                string key   = pair.Key;
                string value = pair.Value;

                TransformKeyValue(record.reference_type, ref key, ref value);

                if ("notes" == key)
                {
                    continue;
                }
                if ("keywords" == key)
                {
                    continue;
                }
                if ("link_to_pdf" == key)
                {
                    continue;
                }

                bibtex_item[key] = value;
            }

            ImportingIntoLibrary.FilenameWithMetadataImport fwmi = new ImportingIntoLibrary.FilenameWithMetadataImport();
            fwmi.tags.Add("import_endnote");
            fwmi.tags.Add("import_endnote_" + Path.GetFileNameWithoutExtension(endnote_database_filename));
            fwmi.bibtex = bibtex_item.ToBibTex();

            if (record.fields.ContainsKey("notes"))
            {
                fwmi.notes = record.fields["notes"];
            }

            if (record.fields.ContainsKey("keywords"))
            {
                string   keywords = record.fields["keywords"];
                string[] tags     = keywords.Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                fwmi.tags.AddRange(tags);
            }

            // Handle the attachments
            if (record.fields.ContainsKey("link_to_pdf"))
            {
                string   links_string = record.fields["link_to_pdf"];
                string[] links        = links_string.Split(new string[] { ",", "internal-pdf://", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                // Build up the list of candidates
                string        base_directory = endnote_database_filename.Substring(0, endnote_database_filename.Length - 4) + ".Data\\PDF\\";
                List <string> pdf_links      = new List <string>();
                {
                    // First candidates are those in the subdirectory corresponding to the .ENL file
                    foreach (string link in links)
                    {
                        pdf_links.Add(base_directory + link);
                    }

                    // Second candidates are raw pathnames
                    foreach (string link in links)
                    {
                        pdf_links.Add(link);
                    }
                }

                // Use the first PDF file that exists in the file system
                foreach (string pdf_link in pdf_links)
                {
                    if (pdf_link.ToLower().EndsWith(".pdf") && File.Exists(pdf_link))
                    {
                        fwmi.filename = pdf_link;
                        break;
                    }
                }
            }

            return(fwmi);
        }
        internal static MendeleyDatabaseDetails DetectMendeleyDatabaseDetails()
        {
            MendeleyDatabaseDetails mdd = new MendeleyDatabaseDetails();

            string BASE_DIR_FOR_MENDELEY_DATABASE = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Mendeley Ltd\Mendeley Desktop\";

            if (!Directory.Exists(BASE_DIR_FOR_MENDELEY_DATABASE))
            {
                Logging.Info("Mendeley not found.");
                mdd.databases_found = 0;
                mdd.documents_found = 0;
                mdd.pdfs_found      = 0;
                return(mdd);
            }

            try
            {
                string[] sqlite_filenames = Directory.GetFiles(BASE_DIR_FOR_MENDELEY_DATABASE, "*.sqlite", SearchOption.TopDirectoryOnly);
                foreach (string sqlite_filename in sqlite_filenames)
                {
                    // Skip the monitor database
                    if (sqlite_filename.EndsWith("monitor.sqlite"))
                    {
                        continue;
                    }

                    try
                    {
                        using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + sqlite_filename))
                        {
                            connection.Open();

                            // Build the authors lookup
                            Dictionary <long, string> authors_lookup = new Dictionary <long, string>();
                            {
                                string command_string = "SELECT * FROM DocumentContributors";
                                using (var command = new SQLiteCommand(command_string, connection))
                                {
                                    SQLiteDataReader reader = command.ExecuteReader();
                                    while (reader.Read())
                                    {
                                        long   document_id   = (long)reader["documentId"];
                                        string surname       = reader["lastName"] as string;
                                        string firstnames    = reader["firstNames"] as string;
                                        string compound_name = (String.IsNullOrEmpty(surname)) ? firstnames : (String.IsNullOrEmpty(firstnames) ? surname : (surname + ", " + firstnames));
                                        if (!String.IsNullOrEmpty(compound_name))
                                        {
                                            if (!authors_lookup.ContainsKey(document_id))
                                            {
                                                authors_lookup[document_id] = compound_name;
                                            }
                                            else
                                            {
                                                authors_lookup[document_id] = authors_lookup[document_id] + " AND " + compound_name;
                                            }
                                        }
                                    }
                                }
                            }

                            Dictionary <long, List <string> > tags_lookup = new Dictionary <long, List <string> >();
                            {
                                string command_string = "SELECT * FROM DocumentKeywords";
                                using (var command = new SQLiteCommand(command_string, connection))
                                {
                                    SQLiteDataReader reader = command.ExecuteReader();
                                    while (reader.Read())
                                    {
                                        long   document_id = (long)reader["documentId"];
                                        string keyword     = reader["keyword"] as string;
                                        if (!String.IsNullOrEmpty(keyword))
                                        {
                                            if (!tags_lookup.ContainsKey(document_id))
                                            {
                                                tags_lookup[document_id] = new List <string>();
                                            }

                                            tags_lookup[document_id].Add(keyword);
                                        }
                                    }
                                }
                            }


                            // Get the bibtexes
                            {
                                //string command_string = "SELECT * FROM Documents WHERE 1=1 ";
                                string command_string =
                                    ""
                                    + "SELECT * "
                                    + "FROM Documents "
                                    + "LEFT OUTER JOIN DocumentFiles ON Documents.id == DocumentFiles.documentId "
                                    + "LEFT OUTER JOIN Files ON DocumentFiles.Hash = Files.Hash "
                                ;

                                using (var command = new SQLiteCommand(command_string, connection))
                                {
                                    SQLiteDataReader reader = command.ExecuteReader();

                                    ++mdd.databases_found;

                                    while (reader.Read())
                                    {
                                        try
                                        {
                                            BibTexItem bibtex_item = new BibTexItem();

                                            bibtex_item.Type = reader["type"] as string;
                                            bibtex_item.Key  = reader["citationKey"] as string;
                                            if (String.IsNullOrEmpty(bibtex_item.Key))
                                            {
                                                bibtex_item.Key = BibTexTools.GenerateRandomBibTeXKey();
                                            }

                                            PopulateTentativeField(bibtex_item, reader, "title");
                                            PopulateTentativeField(bibtex_item, reader, "abstract");
                                            PopulateTentativeField(bibtex_item, reader, "advisor");
                                            PopulateTentativeField(bibtex_item, reader, "city");
                                            PopulateTentativeField(bibtex_item, reader, "country");
                                            PopulateTentativeField(bibtex_item, reader, "day");
                                            PopulateTentativeField(bibtex_item, reader, "month");
                                            PopulateTentativeField(bibtex_item, reader, "dateAccessed", "accessed");
                                            PopulateTentativeField(bibtex_item, reader, "department");
                                            PopulateTentativeField(bibtex_item, reader, "doi");
                                            PopulateTentativeField(bibtex_item, reader, "edition");
                                            PopulateTentativeField(bibtex_item, reader, "institution");
                                            PopulateTentativeField(bibtex_item, reader, "isbn");
                                            PopulateTentativeField(bibtex_item, reader, "issn");
                                            PopulateTentativeField(bibtex_item, reader, "issue");
                                            PopulateTentativeField(bibtex_item, reader, "medium");
                                            PopulateTentativeField(bibtex_item, reader, "pages");
                                            PopulateTentativeField(bibtex_item, reader, "pmid");
                                            PopulateTentativeField(bibtex_item, reader, "publication");
                                            PopulateTentativeField(bibtex_item, reader, "publisher");
                                            PopulateTentativeField(bibtex_item, reader, "sections", "section");
                                            PopulateTentativeField(bibtex_item, reader, "series");
                                            PopulateTentativeField(bibtex_item, reader, "session");
                                            PopulateTentativeField(bibtex_item, reader, "volume");
                                            PopulateTentativeField(bibtex_item, reader, "year");

                                            long document_id = (long)reader["id"];
                                            if (authors_lookup.ContainsKey(document_id))
                                            {
                                                bibtex_item["author"] = authors_lookup[document_id];
                                            }

                                            ImportingIntoLibrary.FilenameWithMetadataImport fwmi = new ImportingIntoLibrary.FilenameWithMetadataImport();
                                            fwmi.tags.Add("import_mendeley");
                                            fwmi.bibtex = bibtex_item.ToBibTex();

                                            string filename = reader["localUrl"] as string;
                                            if (!String.IsNullOrEmpty(filename))
                                            {
                                                const string FILE_PREFIX = "file:///";
                                                if (filename.StartsWith(FILE_PREFIX))
                                                {
                                                    filename = filename.Substring(FILE_PREFIX.Length);
                                                }

                                                filename = Uri.UnescapeDataString(filename);
                                                filename = filename.Replace('/', '\\');

                                                fwmi.filename = filename;

                                                ++mdd.pdfs_found;
                                            }

                                            if (tags_lookup.ContainsKey(document_id))
                                            {
                                                fwmi.tags.AddRange(tags_lookup[document_id]);
                                            }

                                            string note = reader["note"] as string;
                                            if (!String.IsNullOrEmpty(note))
                                            {
                                                note = note.Replace("<m:italic>", "");
                                                note = note.Replace("</m:italic>", "");
                                                note = note.Replace("<m:bold>", "");
                                                note = note.Replace("</m:bold>", "");
                                                note = note.Replace("<m:note>", "");
                                                note = note.Replace("</m:note>", "");
                                                note = note.Replace("<m:underline>", "");
                                                note = note.Replace("</m:underline>", "");
                                                note = note.Replace("<m:right>", "");
                                                note = note.Replace("</m:right>", "");
                                                note = note.Replace("<m:center>", "");
                                                note = note.Replace("</m:center>", "");
                                                note = note.Replace("<m:linebreak/>", "\n");

                                                fwmi.notes = note;
                                            }

                                            mdd.metadata_imports.Add(fwmi);

                                            ++mdd.documents_found;
                                        }

                                        catch (Exception ex)
                                        {
                                            Logging.Error(ex, "Exception while extracting a Mendeley document.");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        Logging.Error(ex, "Exception while exploring for Mendeley instance in file '{0}'.", sqlite_filename);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Error(ex, "Exception while exploring for Mendeley instances.");
            }

            Logging.Info("Got {0} libraries with {1} documents and {2} PDFs.", mdd.databases_found, mdd.documents_found, mdd.pdfs_found);

            return(mdd);
        }
 public static string GetAuthor(BibTexItem bibtex_item)
 {
     return(GetField(bibtex_item, "author"));
 }
 private static void PopulateTentativeField(BibTexItem bibtex_item, SQLiteDataReader reader, string shared_key)
 {
     PopulateTentativeField(bibtex_item, reader, shared_key, shared_key);
 }
        //public static string GetYear(string bibtex)
        //{
        //    return GetField(bibtex, "year");
        //}

        public static string GetYear(BibTexItem bibtex_item)
        {
            return(GetField(bibtex_item, "year"));
        }
        public void AddDocumentMetadata(bool is_deleted, string fingerprint, string title, string author, string year, string comment, string tag, string annotation, string bibtex, BibTexItem bibtex_item)
        {
            Document document = null;

            // Create the document only if it is not to be deleted
            if (!is_deleted)
            {
                document = new Document();
                document.Add(new Field("fingerprint", fingerprint, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.Add(new Field("page", "0", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

                StringBuilder content_sb = new StringBuilder();

                AddDocumentMetadata_SB(document, content_sb, "title", title);
                AddDocumentMetadata_SB(document, content_sb, "author", author);
                AddDocumentMetadata_SB(document, content_sb, "year", year);
                AddDocumentMetadata_SB(document, content_sb, "comment", comment);
                AddDocumentMetadata_SB(document, content_sb, "tag", tag);
                AddDocumentMetadata_SB(document, content_sb, "annotation", annotation);
                AddDocumentMetadata_SB(document, content_sb, "bibtex", bibtex);

                AddDocumentMetadata_BibTex(document, bibtex_item);

                string content = content_sb.ToString();
                document.Add(new Field("content", content, Field.Store.NO, Field.Index.ANALYZED));
            }

            AddDocumentPage_INTERNAL(fingerprint, 0, document);
        }