Skip to content

Commit 7212452

Browse files
committed
fix(JF-1177): check type before casting
1 parent c37e353 commit 7212452

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/main/java/org/jadice/filetype/Context.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,9 @@ public void warning(final Object src, final String message) {
9393
public Locale getLocale() {
9494
return locale;
9595
}
96+
97+
@Override
98+
public String toString() {
99+
return "Context{" + "result=" + result + ", locale=" + locale + ", statedExtension='" + statedExtension + '\'' + '}';
100+
}
96101
}

src/main/java/org/jadice/filetype/database/DescriptionAction.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class DescriptionAction extends Action {
2828
public static class Description implements Serializable {
2929
private static final long serialVersionUID = 1L;
3030

31-
private List<Map<String, String>> content = new ArrayList<Map<String, String>>();
31+
private final List<Map<String, String>> content = new ArrayList<>();
3232

3333
private final Locale locale;
3434

@@ -48,7 +48,7 @@ public void append(String lang, String description, boolean replace) {
4848
// default desc. ist der indikator für eine neue map,
4949
// alle lok. sprachen müssen danach in der xml datei definiert werden
5050
if (content.isEmpty() || lang.equalsIgnoreCase(DEFAULT_LANG)) {
51-
content.add(new HashMap<String, String>());
51+
content.add(new HashMap<>());
5252
}
5353

5454
Map<String, String> map = content.get(content.size() - 1);
@@ -94,7 +94,14 @@ public String toString() {
9494

9595
@Override
9696
public void perform(Context ctx) {
97-
Description desc = (Description) ctx.getProperty(KEY);
97+
Description desc = null;
98+
99+
if (ctx.getProperty(KEY) instanceof Description) {
100+
desc = (Description) ctx.getProperty(KEY);
101+
} else if (ctx.getProperty(KEY) instanceof String) {
102+
desc = new Description(ctx.getLocale());
103+
desc.append(null, (String) ctx.getProperty(KEY), replace);
104+
}
98105

99106
if (desc == null) {
100107
desc = new Description(ctx.getLocale());

src/main/java/org/jadice/filetype/matchers/TextMatcher.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public boolean matches(final Context context) {
108108
boolean isCSV = false;
109109
String mimeType = "text/plain";
110110
final String statedExtension = context.getStatedExtension();
111-
if(matches && statedExtension != null && statedExtension.equals("csv")) {
111+
LOGGER.debug("stated extension: {}", statedExtension);
112+
if (matches && statedExtension != null && statedExtension.equals("csv")) {
112113
isCSV = true;
113114
mimeType = "text/csv";
114115
}
@@ -117,7 +118,7 @@ public boolean matches(final Context context) {
117118
// Inject charset in MIME type for later usage
118119
context.setProperty(MimeTypeAction.KEY, mimeType + ";charset=" + bom.charset.name());
119120
context.info(this, String.format("Determined charset: %s", bom.charset.name()));
120-
if(isCSV){
121+
if (isCSV) {
121122
context.setProperty(ExtensionAction.KEY, "csv");
122123
context.setProperty(DescriptionAction.KEY, "Comma-separated values (CSV)");
123124
}
@@ -159,10 +160,10 @@ private float countCharacters(final String text, final Context context) {
159160
|| Character.isIdentifierIgnorable(c) //
160161
|| Character.isSpaceChar(c) //
161162
|| Character.isWhitespace(c)) {
162-
LOGGER.debug("Recognized char '" + c + "' @ " + i);
163+
LOGGER.debug("Recognized char '{}' @ {}", c, i);
163164
defined++;
164165
} else if (PUNCTUATION.contains(c)) {
165-
LOGGER.debug("Ignoring punctuation char '" + c + "' @ " + i);
166+
LOGGER.debug("Ignoring punctuation char '{}' @ {}", c, i);
166167
ignored++;
167168
}
168169
}

0 commit comments

Comments
 (0)