Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions api-sdk/src/main/java/com/smartling/api/sdk/file/FileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public enum FileType
{
JAVA_PROPERTIES("text/plain", true), // Java resources
Expand Down Expand Up @@ -44,13 +47,29 @@ public enum FileType
STRINGSDICT("application/xml", true), // iOS/OSX resources in dictionary format
MADCAP("application/octet-stream", false), // MADCAP file
SRT("text/plain", false), // SubRip Text Format
MARKDOWN("text/markdown", true); // Markdown Text Format
MARKDOWN("text/markdown", true), // Markdown Text Format
PHP_RESOURCE("text/plain", false), // PHP resources
TMX("application/xml", false), // TMX translation memory file format
XLIFF_CAT("application/xml", false), // XLIFF for offline CAT integration
DITA("application/xml", false); // DITA file format

private final String identifier;
private final String mimeType;
private final boolean isTextFormat;

private static String createIdentifier(String s)
// Lookup map
public final static Map<String, FileType> BY_NAME_LOOKUP = new HashMap<>();

static
{
for (final FileType value : FileType.values())
{
BY_NAME_LOOKUP.put(value.identifier.toLowerCase(), value);
BY_NAME_LOOKUP.put(value.name().toLowerCase(), value);
}
}

private static String toLowerCamel(String s)
{
StringBuilder buf = new StringBuilder();
String[] parts = s.split("_");
Expand All @@ -61,9 +80,9 @@ private static String createIdentifier(String s)
return buf.toString();
}

private FileType(final String mimeType, final boolean isTextFormat)
FileType(final String mimeType, final boolean isTextFormat)
{
this.identifier = createIdentifier(name());
this.identifier = toLowerCamel(name());
this.mimeType = mimeType;
this.isTextFormat = isTextFormat;
}
Expand All @@ -85,12 +104,6 @@ public boolean isTextFormat()

public static FileType lookup(final String fileTypeString)
{
for (final FileType fileType : FileType.values())
{
if (fileType.identifier.equalsIgnoreCase(fileTypeString))
return fileType;
}

return null;
return BY_NAME_LOOKUP.get(StringUtils.lowerCase(fileTypeString));
}
}
20 changes: 20 additions & 0 deletions api-sdk/src/test/java/com/smartling/api/sdk/file/FileTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.smartling.api.sdk.file;

import org.junit.Assert;
import org.junit.Test;

import static com.smartling.api.sdk.file.FileType.JAVA_PROPERTIES;

public class FileTypeTest
{
@Test
public void testLookup()
{
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup("javaProperties"));
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup("JaVaPrOpErTiEs"));
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup("JaVa_PrOpErTiEs"));
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup(JAVA_PROPERTIES.name()));
Assert.assertEquals(JAVA_PROPERTIES, FileType.lookup(JAVA_PROPERTIES.toString()));
Assert.assertNull(FileType.lookup("A_NON_EXISTING_CONTENT_TYPE"));
}
}