From 7ff13da3340b69712b013271da3c4b66e3222716 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Fri, 4 Mar 2016 16:18:51 +0100 Subject: [PATCH] More switch --- .../sf/jabref/logic/layout/LayoutEntry.java | 111 ++++++++++-------- .../jabref/logic/layout/LayoutEntryTest.java | 26 ++-- .../logic/layout/format/HTMLCharsTest.java | 6 + 3 files changed, 83 insertions(+), 60 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java b/src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java index f841568c1b9..ef21b27127c 100644 --- a/src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java +++ b/src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java @@ -51,8 +51,10 @@ class LayoutEntry { private static final Log LOGGER = LogFactory.getLog(LayoutEntry.class); + private final JournalAbbreviationRepository repository; public LayoutEntry(StringInt si, JournalAbbreviationRepository repository) { + this.repository = repository; type = si.i; switch (type) { case LayoutHelper.IS_LAYOUT_TEXT: @@ -62,24 +64,7 @@ public LayoutEntry(StringInt si, JournalAbbreviationRepository repository) { text = si.s.trim(); break; case LayoutHelper.IS_OPTION_FIELD: - List v = StringUtil.tokenizeToList(si.s, "\n"); - - if (v.size() == 1) { - text = v.get(0); - } else { - text = v.get(0).trim(); - - option = LayoutEntry.getOptionalLayout(v.get(1), repository); - // See if there was an undefined formatter: - for (LayoutFormatter anOption : option) { - if (anOption instanceof NotFoundFormatter) { - String notFound = ((NotFoundFormatter) anOption).getNotFound(); - - invalidFormatter.add(notFound); - } - } - - } + doOptionField(si.s); break; case LayoutHelper.IS_FIELD_START: case LayoutHelper.IS_FIELD_END: @@ -89,6 +74,7 @@ public LayoutEntry(StringInt si, JournalAbbreviationRepository repository) { } public LayoutEntry(List parsedEntries, int layoutType, JournalAbbreviationRepository repository) { + this.repository = repository; List blockEntries = null; List tmpEntries = new ArrayList<>(); LayoutEntry le; @@ -120,6 +106,7 @@ public LayoutEntry(List parsedEntries, int layoutType, JournalAbbrevi } else { LOGGER.warn("Nested field entries are not implemented!"); } + break; case LayoutHelper.IS_LAYOUT_TEXT: case LayoutHelper.IS_SIMPLE_FIELD: case LayoutHelper.IS_OPTION_FIELD: @@ -303,18 +290,22 @@ public String doLayout(BibEntry bibtex, BibDatabase database, Optional * @return */ public String doLayout(BibDatabase database, Charset encoding) { - if (type == LayoutHelper.IS_LAYOUT_TEXT) { + switch (type) { + case LayoutHelper.IS_LAYOUT_TEXT: return text; - } else if (type == LayoutHelper.IS_SIMPLE_FIELD) { - throw new UnsupportedOperationException( - "bibtex entry fields not allowed in begin or end layout"); - } else if ((type == LayoutHelper.IS_FIELD_START) || (type == LayoutHelper.IS_GROUP_START)) { - throw new UnsupportedOperationException( - "field and group starts not allowed in begin or end layout"); - } else if ((type == LayoutHelper.IS_FIELD_END) || (type == LayoutHelper.IS_GROUP_END)) { - throw new UnsupportedOperationException( - "field and group ends not allowed in begin or end layout"); - } else if (type == LayoutHelper.IS_OPTION_FIELD) { + + case LayoutHelper.IS_SIMPLE_FIELD: + throw new UnsupportedOperationException("bibtex entry fields not allowed in begin or end layout"); + + case LayoutHelper.IS_FIELD_START: + case LayoutHelper.IS_GROUP_START: + throw new UnsupportedOperationException("field and group starts not allowed in begin or end layout"); + + case LayoutHelper.IS_FIELD_END: + case LayoutHelper.IS_GROUP_END: + throw new UnsupportedOperationException("field and group ends not allowed in begin or end layout"); + + case LayoutHelper.IS_OPTION_FIELD: String field = BibDatabase.getText(text, database); if (option != null) { for (LayoutFormatter anOption : option) { @@ -327,19 +318,45 @@ public String doLayout(BibDatabase database, Charset encoding) { } return field; - } else if (type == LayoutHelper.IS_ENCODING_NAME) { + + case LayoutHelper.IS_ENCODING_NAME: return encoding.displayName(); - } else if (type == LayoutHelper.IS_FILENAME) { + + case LayoutHelper.IS_FILENAME: File f = Globals.prefs.databaseFile; return f == null ? "" : f.getName(); - } else if (type == LayoutHelper.IS_FILEPATH) { - File f = Globals.prefs.databaseFile; - return f == null ? "" : f.getPath(); + + case LayoutHelper.IS_FILEPATH: + File f2 = Globals.prefs.databaseFile; + return f2 == null ? "" : f2.getPath(); + + default: + break; } return ""; } + private void doOptionField(String s) { + List v = StringUtil.tokenizeToList(s, "\n"); + + if (v.size() == 1) { + text = v.get(0); + } else { + text = v.get(0).trim(); + + option = LayoutEntry.getOptionalLayout(v.get(1), repository); + // See if there was an undefined formatter: + for (LayoutFormatter anOption : option) { + if (anOption instanceof NotFoundFormatter) { + String notFound = ((NotFoundFormatter) anOption).getNotFound(); + invalidFormatter.add(notFound); + } + } + + } + + } // added section - end (arudert) private static LayoutFormatter getLayoutFormatterByClassName(String className, @@ -375,15 +392,15 @@ private static LayoutFormatter getLayoutFormatterByClassName(String className, private static List getOptionalLayout(String formatterName, JournalAbbreviationRepository repository) { - List formatterStrings = parseMethodsCalls(formatterName); + List> formatterStrings = parseMethodsCalls(formatterName); List results = new ArrayList<>(formatterStrings.size()); Map userNameFormatter = NameFormatter.getNameFormatters(); - for (String[] strings : formatterStrings) { + for (List strings : formatterStrings) { - String className = strings[0].trim(); + String className = strings.get(0).trim(); // Check if this is a name formatter defined by this export filter: if (Globals.prefs.customExportNameFormatters != null) { @@ -401,13 +418,13 @@ private static List getOptionalLayout(String formatterName, LayoutFormatter f = LayoutEntry.getLayoutFormatterByClassName(className, repository); // If this formatter accepts an argument, check if we have one, and // set it if so: - if ((f instanceof ParamLayoutFormatter) && (strings.length >= 2)) { - ((ParamLayoutFormatter) f).setArgument(strings[1]); + if ((f instanceof ParamLayoutFormatter) && (strings.size() >= 2)) { + ((ParamLayoutFormatter) f).setArgument(strings.get(1)); } results.add(f); continue; - } catch (Exception ignored) { - // Ignored + } catch (Exception ex) { + LOGGER.info("Problem with formatter", ex); } // Then check whether this is a user defined formatter @@ -433,9 +450,9 @@ public List getInvalidFormatters() { return invalidFormatter; } - public static List parseMethodsCalls(String calls) { + public static List> parseMethodsCalls(String calls) { - List result = new ArrayList<>(); + List> result = new ArrayList<>(); char[] c = calls.toCharArray(); @@ -478,7 +495,7 @@ public static List parseMethodsCalls(String calls) { String param = calls.substring(startParam, i); - result.add(new String[]{method, param}); + result.add(Arrays.asList(method, param)); } else { // Parameter is in format xxx @@ -490,16 +507,16 @@ public static List parseMethodsCalls(String calls) { String param = calls.substring(startParam, i); - result.add(new String[]{method, param}); + result.add(Arrays.asList(method, param)); } } else { // Incorrectly terminated open brace - result.add(new String[]{method}); + result.add(Arrays.asList(method)); } } else { String method = calls.substring(start, i); - result.add(new String[]{method}); + result.add(Arrays.asList(method)); } } i++; diff --git a/src/test/java/net/sf/jabref/logic/layout/LayoutEntryTest.java b/src/test/java/net/sf/jabref/logic/layout/LayoutEntryTest.java index 06b413ad5e5..30efe6aa8ac 100644 --- a/src/test/java/net/sf/jabref/logic/layout/LayoutEntryTest.java +++ b/src/test/java/net/sf/jabref/logic/layout/LayoutEntryTest.java @@ -184,29 +184,29 @@ public void testHighlightingMoreWordsCaseSesitive() throws Exception { public void testParseMethodCalls() { Assert.assertEquals(1, LayoutEntry.parseMethodsCalls("bla").size()); - Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla").get(0))[0]); + Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla").get(0)).get(0)); Assert.assertEquals(1, LayoutEntry.parseMethodsCalls("bla,").size()); - Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla,").get(0))[0]); + Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla,").get(0)).get(0)); Assert.assertEquals(1, LayoutEntry.parseMethodsCalls("_bla.bla.blub,").size()); - Assert.assertEquals("_bla.bla.blub", (LayoutEntry.parseMethodsCalls("_bla.bla.blub,").get(0))[0]); + Assert.assertEquals("_bla.bla.blub", (LayoutEntry.parseMethodsCalls("_bla.bla.blub,").get(0)).get(0)); Assert.assertEquals(2, LayoutEntry.parseMethodsCalls("bla,foo").size()); - Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla,foo").get(0))[0]); - Assert.assertEquals("foo", (LayoutEntry.parseMethodsCalls("bla,foo").get(1))[0]); + Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla,foo").get(0)).get(0)); + Assert.assertEquals("foo", (LayoutEntry.parseMethodsCalls("bla,foo").get(1)).get(0)); Assert.assertEquals(2, LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").size()); - Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(0))[0]); - Assert.assertEquals("foo", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(1))[0]); - Assert.assertEquals("test", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(0))[1]); - Assert.assertEquals("fark", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(1))[1]); + Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(0)).get(0)); + Assert.assertEquals("foo", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(1)).get(0)); + Assert.assertEquals("test", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(0)).get(1)); + Assert.assertEquals("fark", (LayoutEntry.parseMethodsCalls("bla(\"test\"),foo(\"fark\")").get(1)).get(1)); Assert.assertEquals(2, LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").size()); - Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(0))[0]); - Assert.assertEquals("foo", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(1))[0]); - Assert.assertEquals("test", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(0))[1]); - Assert.assertEquals("fark", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(1))[1]); + Assert.assertEquals("bla", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(0)).get(0)); + Assert.assertEquals("foo", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(1)).get(0)); + Assert.assertEquals("test", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(0)).get(1)); + Assert.assertEquals("fark", (LayoutEntry.parseMethodsCalls("bla(test),foo(fark)").get(1)).get(1)); } } diff --git a/src/test/java/net/sf/jabref/logic/layout/format/HTMLCharsTest.java b/src/test/java/net/sf/jabref/logic/layout/format/HTMLCharsTest.java index 8ca51523f6e..f623d6ef3bc 100644 --- a/src/test/java/net/sf/jabref/logic/layout/format/HTMLCharsTest.java +++ b/src/test/java/net/sf/jabref/logic/layout/format/HTMLCharsTest.java @@ -73,6 +73,12 @@ public void testEquations() { layout.format("A 32~{mA} {$\\Sigma\\Delta$}-modulator")); } + @Test + public void testNewLine() { + LayoutFormatter layout = new HTMLChars(); + Assert.assertEquals("a
b", layout.format("a\nb")); + Assert.assertEquals("a

b", layout.format("a\n\nb")); + } /* * Is missing a lot of test cases for the individual chars... */