public void TestAddNew() { if (Globals.prefs == null) { Globals.prefs = JabRefPreferences.getInstance(); } ParserResult result; using (var parser = new BibtexParser(new StreamReader(@"C:\Users\Peter\Desktop\bibtest.bib"))) { result = parser.Parse(); } var db = result.Database; BibtexEntry entry = new BibtexEntry(); entry.setType(BibtexEntryType.ARTICLE.Instance); db.insertEntry(entry); db.setCiteKeyForEntry(entry.getId(), "ABC"); foreach (var f in entry.getRequiredFields()) { string value = BibtexFields.isNumeric(f) ? "1300" : "Another Test"; entry.setField(f, value); } db.saveDatabase(File.Open(@"C:\Users\Peter\Desktop\bibtest.bib", FileMode.Open)); }
public void TestCreateNew() { if (Globals.prefs == null) { Globals.prefs = JabRefPreferences.getInstance(); } BibtexDatabase db = new BibtexDatabase(); BibtexEntry entry = new BibtexEntry(); entry.setType(BibtexEntryType.ARTICLE.Instance); db.insertEntry(entry); db.setCiteKeyForEntry(entry.getId(), "ZXY"); foreach (var f in entry.getRequiredFields()) { string value = BibtexFields.isNumeric(f) ? "1000" : "Test"; entry.setField(f, value); } db.saveDatabase(File.Open(@"C:\Users\Peter\Desktop\bibtest.bib", FileMode.Create)); }
/** * Returns a clone of this entry. Useful for copying. */ public object clone() { BibtexEntry clone = new BibtexEntry(_id, _type); clone._fields = new Dictionary<string, string>(_fields); return clone; }
protected bool isCoupledFieldSet(string field, BibtexEntry entry, BibtexDatabase database) { if (reqSets == null) return false; for (int i=0; i<reqSets.Length; i++) { bool takesPart = false, oneSet = false; for (int j=0; j<reqSets[i].Length; j++) { // If this is the field we're looking for, note that the field is part of the set: if (reqSets[i][j].Equals(field, System.StringComparison.CurrentCultureIgnoreCase)) takesPart = true; // If it is a different field, check if it is set: else if (BibtexDatabase.getResolvedField(reqSets[i][j], entry, database) != null) oneSet = true; } // Ths the field is part of the set, and at least one other field is set, return true: if (takesPart && oneSet) return true; } // No hits, so return false: return false; }
/** * Check whether this entry's required fields are set, taking crossreferenced entries and * either-or fields into account: * @param entry The entry to check. * @param database The entry's database. * @return True if required fields are set, false otherwise. */ public override bool hasAllRequiredFields(BibtexEntry entry, BibtexDatabase database) { // First check if the bibtex key is set: if (entry.getField(Globals.KEY_FIELD) == null) return false; // Then check other fields: bool[] isSet = new bool[req.Length]; // First check for all fields, whether they are set here or in a crossref'd entry: for (int i=0; i<req.Length; i++) isSet[i] = BibtexDatabase.getResolvedField(req[i], entry, database) != null; // Then go through all fields. If a field is not set, see if it is part of an either-or // set where another field is set. If not, return false: for (int i=0; i<req.Length; i++) { if (!isSet[i]) { if (!isCoupledFieldSet(req[i], entry, database)) return false; } } // Passed all fields, so return true: return true; }
/** * Returns the text stored in the given field of the given bibtex entry * which belongs to the given database. * * If a database is given, this function will try to resolve any string * references in the field-value. * Also, if a database is given, this function will try to find values for * unset fields in the entry linked by the "crossref" field, if any. * * @param field * The field to return the value of. * @param bibtex maybenull * The bibtex entry which Contains the field. * @param database maybenull * The database of the bibtex entry. * @return The resolved field value or null if not found. */ public static string getResolvedField(string field, BibtexEntry bibtex, BibtexDatabase database) { if (field.Equals("bibtextype")) return bibtex.getType().getName(); object o = bibtex.getField(field); // If this field is not set, and the entry has a crossref, try to look up the // field in the referred entry: Do not do this for the bibtex key. if ((o == null) && (database != null) && database.followCrossrefs && !field.Equals(Globals.KEY_FIELD) && (database != null)) { object crossRef = bibtex.getField("crossref"); if (crossRef != null) { BibtexEntry referred = database.getEntryByKey((string)crossRef); if (referred != null) { // Ok, we found the referred entry. Get the field value from that // entry. If it is unset there, too, stop looking: o = referred.getField(field); } } } return getText((string)o, database); }
/** * Take the given BibtexEntry and resolve any string references. * * @param entry * A BibtexEntry in which all strings of the form #xxx# will be * resolved against the hash map of string references stored in * the databasee. * * @param inPlace * If inPlace is true then the given BibtexEntry will be * modified, if false then a copy is made using close made before * resolving the strings. * * @return a BibtexEntry with all string references resolved. It is * dependent on the value of inPlace whether a copy is made or the * given BibtexEntries is modified. */ public BibtexEntry resolveForStrings(BibtexEntry entry, bool inPlace) { if (!inPlace){ entry = (BibtexEntry)entry.clone(); } foreach (object field in entry.getAllFields()){ entry.setField(field.ToString(), this.resolveForStrings(entry.getField(field.ToString()).ToString())); } return entry; }
/** * Inserts the entry, given that its ID is not already in use. * use Util.createId(...) to make up a unique ID for an entry. */ public bool insertEntry(BibtexEntry entry) { lock (_lock) { string id = entry.getId(); if (getEntryById(id) != null) { throw new KeyCollisionException( "ID is already in use, please choose another"); } _entries.Add(id, entry); return checkForDuplicateKeyAndAdd(null, entry.getCiteKey(), false); } }
public override bool hasAllRequiredFields(BibtexEntry entry, BibtexDatabase database) { return true; }
private void AddBibTeXEntry(BibtexEntry entry) { string title = entry.getField("title"); string subtitle = ""; string author = entry.getField("author"); string editor = entry.getField("editor"); title = TrimEntry(title); author = TrimEntry(author); editor = TrimEntry(editor); if (author == null) { Console.WriteLine("Warning: empty author field in entry {0}", entry.getId()); author = ""; } if (editor == null) { Console.WriteLine("Warning: empty editor field in entry {0}", entry.getId()); editor = ""; } string[] authors = author.Split(new string[] { " and ", "," }, StringSplitOptions.RemoveEmptyEntries); string new_author = ""; foreach (string a in authors) { if (a.Length > 0 && new_author != "") new_author = new_author + ";" + TrimEntry(a); else if (a.Length > 0) new_author = TrimEntry(a); } if (editor == "") editor = new_author; if (new_author == "") new_author = editor; string booktitle = entry.getField("booktitle"); string edition = entry.getField("edition"); string isbn = entry.getField("isbn"); string journalorseriestitle = entry.getType() == BibtexEntryType.getType("ARTICLE") ? entry.getField("journal") : entry.getField("series"); string volume = entry.getField("volume"); string issue = entry.getField("issue"); string pages = entry.getField("pages"); string startpage = ""; string endpage = ""; if (pages != null) { string[] splittedPages = pages.Split(new string[] { "-", "--" }, StringSplitOptions.RemoveEmptyEntries); if (splittedPages.Length > 0) startpage = splittedPages[0].Trim(); if (splittedPages.Length > 1) endpage = splittedPages[1].Trim(); } string issn = entry.getField("issn"); string publisher = entry.getField("publisher"); string publicationdate = entry.getField("year"); string abstract_txt = entry.getField("abstract"); string doi = entry.getField("doi"); string fulltexturl = entry.getField("url"); string notes = entry.getField("notes"); string publicationtype = ""; string orgcode = BibTex2eCitation.Properties.Settings.Default.DefaultOrgCode.ToString(); if (entry.getType() == BibtexEntryType.getType("ARTICLE")) publicationtype = "Journal Items"; else if (entry.getType() == BibtexEntryType.getType("BOOK")) publicationtype = "Monographs/ Monograph Items"; else if (entry.getType() == BibtexEntryType.getType("PHDTHESIS")) publicationtype = "Doctoral Thesis and Habilitation"; else if (entry.getType() == BibtexEntryType.getType("MASTERSTHESIS")) publicationtype = "Master Thesis and Bachelor Thesis"; else if (entry.getType() == BibtexEntryType.getType("INPROCEEDINGS")) publicationtype = "Conference Contributions"; string language = "English"; dataGridView1.Rows.Add(new string[] { title, new_author, publicationdate, editor, publicationtype, "", orgcode, subtitle, booktitle, edition, "", "", isbn, journalorseriestitle, volume, issue, startpage, endpage, issn, publisher, "", "", abstract_txt, doi, fulltexturl, "", "", "", notes, "", language }); }
/** Returns true iff the entry has a nonzero value in its field. */ private static bool nonZeroField(BibtexEntry be, string field) { string o = (be.getField(field)); return ((o != null) && !o.Equals("0")); }
/** * Saves the database to file, including only the entries included in the * supplied input array bes. * * @return A List containing warnings, if any. */ public static void savePartOfDatabase(BibtexDatabase database, Stream file, JabRefPreferences prefs, BibtexEntry[] bes, string encoding) { var types = new Dictionary<string, BibtexEntryType>(); // Map // to // collect // entry // type // definitions // that we must save along with entries using them. BibtexEntry be = null; bool backup = prefs.getBoolean("backup"); try { // Define our data stream. var fw = new StreamWriter(file); // Write signature. writeBibFileHeader(fw, encoding); // Write preamble if there is one. writePreamble(fw, database.getPreamble()); // Write strings if there are any. writeStrings(fw, database); // Write database entries. Take care, using CrossRefEntry- // IComparable, that referred entries occur after referring // ones. Apart from crossref requirements, entries will be // sorted as they appear on the screen. string pri, sec, ter; bool priD, secD, terD; if (!prefs.getBoolean("saveInStandardOrder")) { // The setting is to save according to the current table order. pri = prefs.get("priSort"); sec = prefs.get("secSort"); // sorted as they appear on the screen. ter = prefs.get("terSort"); priD = prefs.getBoolean("priDescending"); secD = prefs.getBoolean("secDescending"); terD = prefs.getBoolean("terDescending"); } else { // The setting is to save in standard order: author, editor, year pri = "author"; sec = "editor"; ter = "year"; priD = false; secD = false; terD = true; } var comparators = new List<IComparer<BibtexEntry>>(); comparators.Add(new CrossRefEntryComparator()); comparators.Add(new FieldComparator(pri, priD)); comparators.Add(new FieldComparator(sec, secD)); comparators.Add(new FieldComparator(ter, terD)); comparators.Add(new FieldComparator(Globals.KEY_FIELD)); // Use glazed lists to get a sorted view of the entries: List<BibtexEntry> entries = new List<BibtexEntry>(); if ((bes != null) && (bes.Length > 0)) for (int i = 0; i < bes.Length; i++) { entries.Add(bes[i]); } entries.Sort(new FieldComparatorStack<BibtexEntry>(comparators)); FieldFormatter ff = new LatexFieldFormatter(); for (var i = entries.GetEnumerator(); i.MoveNext(); ) { be = i.Current; // Check if we must write the type definition for this // entry, as well. Our criterion is that all non-standard // types (*not* customized standard types) must be written. BibtexEntryType tp = be.getType(); if (BibtexEntryType.getStandardType(tp.getName()) == null) { types.Add(tp.getName(), tp); } be.write(fw, ff, true); fw.Write(Environment.NewLine); } // Write type definitions, if any: if (types.Count > 0) { foreach (var i in types.Keys) { CustomEntryType tp = (CustomEntryType)types[i]; tp.save(fw); fw.Write(Environment.NewLine); } } fw.Dispose(); } catch (Exception ex) { try { // TODO: //session.cancel(); //repairAfterError(file, backup, status); } catch (IOException e) { // Argh, another error? Can we do anything? throw new SaveException(ex.Message + "\n" + Globals.lang("Warning: could not complete file repair; your file may " + "have been corrupted. Error message: ") + e.Message); } throw new SaveException(ex.Message, be); } }