Skip to content

Commit

Permalink
More switch
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Mar 4, 2016
1 parent 1ba61e7 commit 2f8d3fa
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 60 deletions.
110 changes: 63 additions & 47 deletions src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -62,24 +64,7 @@ public LayoutEntry(StringInt si, JournalAbbreviationRepository repository) {
text = si.s.trim();
break;
case LayoutHelper.IS_OPTION_FIELD:
List<String> 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:
Expand All @@ -89,6 +74,7 @@ public LayoutEntry(StringInt si, JournalAbbreviationRepository repository) {
}

public LayoutEntry(List<StringInt> parsedEntries, int layoutType, JournalAbbreviationRepository repository) {
this.repository = repository;
List<StringInt> blockEntries = null;
List<LayoutEntry> tmpEntries = new ArrayList<>();
LayoutEntry le;
Expand Down Expand Up @@ -303,18 +289,22 @@ public String doLayout(BibEntry bibtex, BibDatabase database, Optional<Pattern>
* @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) {
Expand All @@ -327,19 +317,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<String> 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,
Expand Down Expand Up @@ -375,15 +391,15 @@ private static LayoutFormatter getLayoutFormatterByClassName(String className,
private static List<LayoutFormatter> getOptionalLayout(String formatterName,
JournalAbbreviationRepository repository) {

List<String[]> formatterStrings = parseMethodsCalls(formatterName);
List<List<String>> formatterStrings = parseMethodsCalls(formatterName);

List<LayoutFormatter> results = new ArrayList<>(formatterStrings.size());

Map<String, String> userNameFormatter = NameFormatter.getNameFormatters();

for (String[] strings : formatterStrings) {
for (List<String> 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) {
Expand All @@ -401,13 +417,13 @@ private static List<LayoutFormatter> 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
Expand All @@ -433,9 +449,9 @@ public List<String> getInvalidFormatters() {
return invalidFormatter;
}

public static List<String[]> parseMethodsCalls(String calls) {
public static List<List<String>> parseMethodsCalls(String calls) {

List<String[]> result = new ArrayList<>();
List<List<String>> result = new ArrayList<>();

char[] c = calls.toCharArray();

Expand Down Expand Up @@ -478,7 +494,7 @@ public static List<String[]> 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

Expand All @@ -490,16 +506,16 @@ public static List<String[]> 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++;
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/net/sf/jabref/logic/layout/LayoutEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>b", layout.format("a\nb"));
Assert.assertEquals("a<p>b", layout.format("a\n\nb"));
}
/*
* Is missing a lot of test cases for the individual chars...
*/
Expand Down

0 comments on commit 2f8d3fa

Please sign in to comment.