Example #1
0
 public static JabRefPreferences getInstance()
 {
     if (singleton == null)
     singleton = new JabRefPreferences();
     return singleton;
 }
Example #2
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);
            }
        }