Search in sources :

Example 36 with BibtexString

use of org.jabref.model.entry.BibtexString in project jabref by JabRef.

the class BibtexParser method parseString.

private BibtexString parseString() throws IOException {
    skipWhitespace();
    consume('{', '(');
    skipWhitespace();
    LOGGER.debug("Parsing string name");
    String name = parseTextToken();
    LOGGER.debug("Parsed string name");
    skipWhitespace();
    LOGGER.debug("Now the contents");
    consume('=');
    String content = parseFieldContent(name);
    LOGGER.debug("Now I'm going to consume a }");
    consume('}', ')');
    // Consume new line which signals end of entry
    skipOneNewline();
    LOGGER.debug("Finished string parsing.");
    return new BibtexString(name, content);
}
Also used : BibtexString(org.jabref.model.entry.BibtexString) BibtexString(org.jabref.model.entry.BibtexString)

Example 37 with BibtexString

use of org.jabref.model.entry.BibtexString in project jabref by JabRef.

the class BibDatabase method resolveString.

/**
     * If the label represents a string contained in this database, returns
     * that string's content. Resolves references to other strings, taking
     * care not to follow a circular reference pattern.
     * If the string is undefined, returns null.
     */
private String resolveString(String label, Set<String> usedIds, Set<String> allUsedIds) {
    Objects.requireNonNull(label);
    Objects.requireNonNull(usedIds);
    Objects.requireNonNull(allUsedIds);
    for (BibtexString string : bibtexStrings.values()) {
        if (string.getName().equalsIgnoreCase(label)) {
            // infinite recursion.
            if (usedIds.contains(string.getId())) {
                LOGGER.info("Stopped due to circular reference in strings: " + label);
                return label;
            }
            // If not, log this string's ID now.
            usedIds.add(string.getId());
            if (allUsedIds != null) {
                allUsedIds.add(string.getId());
            }
            // Ok, we found the string. Now we must make sure we
            // resolve any references to other strings in this one.
            String result = string.getContent();
            result = resolveContent(result, usedIds, allUsedIds);
            // Finished with recursing this branch, so we remove our
            // ID again:
            usedIds.remove(string.getId());
            return result;
        }
    }
    // If we get to this point, the string has obviously not been defined locally.
    // Check if one of the standard BibTeX month strings has been used:
    Optional<Month> month = Month.getMonthByShortName(label);
    return month.map(Month::getFullName).orElse(null);
}
Also used : Month(org.jabref.model.entry.Month) BibtexString(org.jabref.model.entry.BibtexString) BibtexString(org.jabref.model.entry.BibtexString)

Example 38 with BibtexString

use of org.jabref.model.entry.BibtexString in project jabref by JabRef.

the class BibtexDatabaseWriterTest method reformatStringIfAskedToDoSo.

@Test
public void reformatStringIfAskedToDoSo() throws Exception {
    BibtexString string = new BibtexString("name", "content");
    string.setParsedSerialization("wrong serialization");
    database.addString(string);
    SavePreferences preferences = new SavePreferences().withReformatFile(true);
    StringSaveSession session = databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);
    assertEquals(OS.NEWLINE + "@String{name = {content}}" + OS.NEWLINE, session.getStringValue());
}
Also used : BibtexString(org.jabref.model.entry.BibtexString) Test(org.junit.Test)

Example 39 with BibtexString

use of org.jabref.model.entry.BibtexString in project jabref by JabRef.

the class BibtexDatabaseWriterTest method roundtripWithUserCommentBeforeStringAndChange.

@Test
public void roundtripWithUserCommentBeforeStringAndChange() throws Exception {
    Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib");
    Charset encoding = StandardCharsets.UTF_8;
    ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
    for (BibtexString string : result.getDatabase().getStringValues()) {
        // Mark them as changed
        string.setContent(string.getContent());
    }
    SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
    BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
    StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
    try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
        assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) Scanner(java.util.Scanner) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Charset(java.nio.charset.Charset) BibtexString(org.jabref.model.entry.BibtexString) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 40 with BibtexString

use of org.jabref.model.entry.BibtexString in project jabref by JabRef.

the class BibtexDatabaseWriterTest method writeSavedSerializationOfStringIfUnchanged.

@Test
public void writeSavedSerializationOfStringIfUnchanged() throws Exception {
    BibtexString string = new BibtexString("name", "content");
    string.setParsedSerialization("serialization");
    database.addString(string);
    StringSaveSession session = databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), new SavePreferences());
    assertEquals("serialization", session.getStringValue());
}
Also used : BibtexString(org.jabref.model.entry.BibtexString) Test(org.junit.Test)

Aggregations

BibtexString (org.jabref.model.entry.BibtexString)40 Test (org.junit.Test)24 ParserResult (org.jabref.logic.importer.ParserResult)8 BibEntry (org.jabref.model.entry.BibEntry)7 StringReader (java.io.StringReader)6 KeyCollisionException (org.jabref.model.database.KeyCollisionException)5 UndoableInsertString (org.jabref.gui.undo.UndoableInsertString)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 NamedCompound (org.jabref.gui.undo.NamedCompound)2 UndoableStringChange (org.jabref.gui.undo.UndoableStringChange)2 BibDatabase (org.jabref.model.database.BibDatabase)2 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Scanner (java.util.Scanner)1 Matcher (java.util.regex.Matcher)1