Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 6bf9480

Browse files
author
Denis Kurochkin
authored
Merge pull request #59 from Smartling/REPO-239-filetype-lookup-to-fit-with-fileapi
Repo 239 new FileType lookup() and missed file types
2 parents f7729ef + bd95813 commit 6bf9480

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

api-sdk/src/main/java/com/smartling/api/sdk/file/FileType.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import org.apache.commons.lang3.StringUtils;
1919

20+
import java.util.HashMap;
21+
import java.util.Map;
22+
2023
public enum FileType
2124
{
2225
JAVA_PROPERTIES("text/plain", true), // Java resources
@@ -44,13 +47,29 @@ public enum FileType
4447
STRINGSDICT("application/xml", true), // iOS/OSX resources in dictionary format
4548
MADCAP("application/octet-stream", false), // MADCAP file
4649
SRT("text/plain", false), // SubRip Text Format
47-
MARKDOWN("text/markdown", true); // Markdown Text Format
50+
MARKDOWN("text/markdown", true), // Markdown Text Format
51+
PHP_RESOURCE("text/plain", false), // PHP resources
52+
TMX("application/xml", false), // TMX translation memory file format
53+
XLIFF_CAT("application/xml", false), // XLIFF for offline CAT integration
54+
DITA("application/xml", false); // DITA file format
4855

4956
private final String identifier;
5057
private final String mimeType;
5158
private final boolean isTextFormat;
5259

53-
private static String createIdentifier(String s)
60+
// Lookup map
61+
public final static Map<String, FileType> BY_NAME_LOOKUP = new HashMap<>();
62+
63+
static
64+
{
65+
for (final FileType value : FileType.values())
66+
{
67+
BY_NAME_LOOKUP.put(value.identifier.toLowerCase(), value);
68+
BY_NAME_LOOKUP.put(value.name().toLowerCase(), value);
69+
}
70+
}
71+
72+
private static String toLowerCamel(String s)
5473
{
5574
StringBuilder buf = new StringBuilder();
5675
String[] parts = s.split("_");
@@ -61,9 +80,9 @@ private static String createIdentifier(String s)
6180
return buf.toString();
6281
}
6382

64-
private FileType(final String mimeType, final boolean isTextFormat)
83+
FileType(final String mimeType, final boolean isTextFormat)
6584
{
66-
this.identifier = createIdentifier(name());
85+
this.identifier = toLowerCamel(name());
6786
this.mimeType = mimeType;
6887
this.isTextFormat = isTextFormat;
6988
}
@@ -85,12 +104,6 @@ public boolean isTextFormat()
85104

86105
public static FileType lookup(final String fileTypeString)
87106
{
88-
for (final FileType fileType : FileType.values())
89-
{
90-
if (fileType.identifier.equalsIgnoreCase(fileTypeString))
91-
return fileType;
92-
}
93-
94-
return null;
107+
return BY_NAME_LOOKUP.get(StringUtils.lowerCase(fileTypeString));
95108
}
96109
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.smartling.api.sdk.file;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import static com.smartling.api.sdk.file.FileType.JAVA_PROPERTIES;
7+
8+
public class FileTypeTest
9+
{
10+
@Test
11+
public void testLookup()
12+
{
13+
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup("javaProperties"));
14+
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup("JaVaPrOpErTiEs"));
15+
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup("JaVa_PrOpErTiEs"));
16+
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup(JAVA_PROPERTIES.name()));
17+
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup(JAVA_PROPERTIES.toString()));
18+
Assert.assertNull(FileType.lookup("A_NON_EXISTING_CONTENT_TYPE"));
19+
}
20+
}

0 commit comments

Comments
 (0)