Author: painter
Date: Tue Jan 15 15:53:18 2019
New Revision: 1851387
URL: http://svn.apache.org/viewvc?rev=1851387&view=rev
Log:
Java 8 updates, use JUnit 5 for testing, PMD and FindBug cleanup, use
commons-lang3 for string comparisons, moved changes.xml to new dir for report
generation
Removed:
turbine/fulcrum/trunk/mimetype/xdocs/changes.xml
Modified:
turbine/fulcrum/trunk/mimetype/pom.xml
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/DefaultMimeTypeService.java
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/MimeTypeService.java
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/CharSetMap.java
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeType.java
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMap.java
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMapper.java
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/MimetypeTest.java
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/util/MimeTypeMapperTest.java
Modified: turbine/fulcrum/trunk/mimetype/pom.xml
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/pom.xml?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
--- turbine/fulcrum/trunk/mimetype/pom.xml (original)
+++ turbine/fulcrum/trunk/mimetype/pom.xml Tue Jan 15 15:53:18 2019
@@ -50,11 +50,18 @@
</developers>
<dependencies>
+
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.8.1</version>
+ </dependency>
<dependency>
<groupId>org.apache.avalon.framework</groupId>
<artifactId>avalon-framework-api</artifactId>
<version>4.3.1</version>
- </dependency>
+ </dependency>
+
<!-- testing dependencies -->
<dependency>
<groupId>org.apache.fulcrum</groupId>
Modified:
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/DefaultMimeTypeService.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/DefaultMimeTypeService.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/DefaultMimeTypeService.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/DefaultMimeTypeService.java
Tue Jan 15 15:53:18 2019
@@ -91,7 +91,8 @@ public class DefaultMimeTypeService exte
/**
* Constructs a new service.
*/
- public DefaultMimeTypeService() {
+ public DefaultMimeTypeService()
+ {
}
/**
@@ -101,7 +102,8 @@ public class DefaultMimeTypeService exte
*
* @param spec a MIME type extension specification to add.
*/
- public void setContentType(String spec) {
+ public void setContentType(String spec)
+ {
mimeTypeMap.setContentType(spec);
}
@@ -121,7 +123,8 @@ public class DefaultMimeTypeService exte
* @param fileName The name of the file to look up a MIME type mapping
for.
* @return the MIME type string.
*/
- public String getContentType(String fileName) {
+ public String getContentType(String fileName)
+ {
return mimeTypeMap.getContentType(fileName);
}
@@ -132,7 +135,8 @@ public class DefaultMimeTypeService exte
* @param def The default MIME type to use if no mapping exists.
* @return the MIME type string.
*/
- public String getContentType(String fileName, String def) {
+ public String getContentType(String fileName, String def)
+ {
return mimeTypeMap.getContentType(fileName, def);
}
@@ -142,7 +146,8 @@ public class DefaultMimeTypeService exte
* @param file the file.
* @return the MIME type.
*/
- public MimeType getMimeContentType(File file) {
+ public MimeType getMimeContentType(File file)
+ {
return mimeTypeMap.getMimeContentType(file);
}
@@ -152,7 +157,8 @@ public class DefaultMimeTypeService exte
* @param name the name of the file.
* @return the MIME type.
*/
- public MimeType getMimeContentType(String name) {
+ public MimeType getMimeContentType(String name)
+ {
return mimeTypeMap.getMimeContentType(name);
}
@@ -163,7 +169,8 @@ public class DefaultMimeTypeService exte
* @param def the default type if none is found.
* @return the MIME type.
*/
- public MimeType getMimeContentType(String ext, String def) {
+ public MimeType getMimeContentType(String ext, String def)
+ {
return mimeTypeMap.getMimeContentType(ext, def);
}
@@ -174,7 +181,8 @@ public class DefaultMimeTypeService exte
* @param type the MIME type as a string.
* @return the file name extension or null.
*/
- public String getDefaultExtension(String type) {
+ public String getDefaultExtension(String type)
+ {
return mimeTypeMap.getDefaultExtension(type);
}
@@ -185,7 +193,8 @@ public class DefaultMimeTypeService exte
* @param mime the MIME type.
* @return the file name extension or null.
*/
- public String getDefaultExtension(MimeType mime) {
+ public String getDefaultExtension(MimeType mime)
+ {
return mimeTypeMap.getDefaultExtension(mime);
}
@@ -195,7 +204,8 @@ public class DefaultMimeTypeService exte
* @param key the key for the charset.
* @param charset the corresponding charset.
*/
- public void setCharSet(String key, String charset) {
+ public void setCharSet(String key, String charset)
+ {
charSetMap.setCharSet(key, charset);
}
@@ -207,7 +217,8 @@ public class DefaultMimeTypeService exte
* @param locale the locale.
* @return the charset.
*/
- public String getCharSet(Locale locale) {
+ public String getCharSet(Locale locale)
+ {
return charSetMap.getCharSet(locale);
}
@@ -223,7 +234,8 @@ public class DefaultMimeTypeService exte
* @param variant a variant field.
* @return the charset.
*/
- public String getCharSet(Locale locale, String variant) {
+ public String getCharSet(Locale locale, String variant)
+ {
return charSetMap.getCharSet(locale, variant);
}
@@ -233,7 +245,8 @@ public class DefaultMimeTypeService exte
* @param key the key for the charset.
* @return the found charset or the default one.
*/
- public String getCharSet(String key) {
+ public String getCharSet(String key)
+ {
return charSetMap.getCharSet(key);
}
@@ -244,13 +257,16 @@ public class DefaultMimeTypeService exte
* @param def the default charset if none is found.
* @return the found charset or the given default.
*/
- public String getCharSet(String key, String def) {
+ public String getCharSet(String key, String def)
+ {
return charSetMap.getCharSet(key, def);
}
- private String getRealPath(String path) {
+ private String getRealPath(String path)
+ {
String absolutePath = null;
- if (applicationRoot == null) {
+ if (applicationRoot == null)
+ {
absolutePath = new File(path).getAbsolutePath();
} else {
absolutePath = new File(applicationRoot,
path).getAbsolutePath();
@@ -262,13 +278,16 @@ public class DefaultMimeTypeService exte
/**
* Avalon component lifecycle method
*/
- public void configure(Configuration conf) {
+ public void configure(Configuration conf)
+ {
mimetypePath = conf.getAttribute(MIME_TYPES, null);
charsetPath = conf.getAttribute(CHARSETS, null);
- if (mimetypePath != null) {
+ if (mimetypePath != null)
+ {
mimetypePath = getRealPath(mimetypePath);
}
- if (charsetPath != null) {
+ if (charsetPath != null)
+ {
charsetPath = getRealPath(charsetPath);
}
}
@@ -276,23 +295,38 @@ public class DefaultMimeTypeService exte
/**
* Avalon component lifecycle method
*/
- public void initialize() throws Exception {
- if (mimetypePath != null) {
- try {
+ public void initialize() throws Exception
+ {
+ if (mimetypePath != null)
+ {
+ try
+ {
mimeTypeMap = new MimeTypeMap(mimetypePath);
- } catch (IOException x) {
+ }
+ catch (IOException x)
+ {
throw new Exception(mimetypePath, x);
}
- } else {
+ }
+ else
+ {
mimeTypeMap = new MimeTypeMap();
}
- if (charsetPath != null) {
- try {
+
+ if (charsetPath != null)
+ {
+ try
+ {
charSetMap = new CharSetMap(charsetPath);
- } catch (IOException x) {
+ }
+ catch (IOException x)
+ {
throw new Exception(charsetPath, x);
}
- } else {
+
+ }
+ else
+ {
charSetMap = new CharSetMap();
}
}
@@ -305,7 +339,8 @@ public class DefaultMimeTypeService exte
* @param context the context to use
* @throws ContextException exception if context not found
*/
- public void contextualize(Context context) throws ContextException {
+ public void contextualize(Context context) throws ContextException
+ {
this.applicationRoot =
context.get("urn:avalon:home").toString();
}
Modified:
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/MimeTypeService.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/MimeTypeService.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/MimeTypeService.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/MimeTypeService.java
Tue Jan 15 15:53:18 2019
@@ -1,6 +1,5 @@
package org.apache.fulcrum.mimetype;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Modified:
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/CharSetMap.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/CharSetMap.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/CharSetMap.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/CharSetMap.java
Tue Jan 15 15:53:18 2019
@@ -1,6 +1,5 @@
package org.apache.fulcrum.mimetype.util;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -20,16 +19,16 @@ package org.apache.fulcrum.mimetype.util
* under the License.
*/
-
-import java.util.Locale;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Properties;
import java.io.File;
-import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map.Entry;
+import java.util.Properties;
+
+import org.apache.commons.lang3.StringUtils;
/**
* This class maintains a set of mappers defining mappings
@@ -68,54 +67,55 @@ public class CharSetMap
/**
* A common charset mapper for languages.
*/
- private static HashMap commonMapper = new HashMap();
+ private static HashMap<String, String> commonMapper = new HashMap<>();
+
static
{
- commonMapper.put("ar","ISO-8859-6");
- commonMapper.put("be","ISO-8859-5");
- commonMapper.put("bg","ISO-8859-5");
- commonMapper.put("ca","ISO-8859-1");
- commonMapper.put("cs","ISO-8859-2");
- commonMapper.put("da","ISO-8859-1");
- commonMapper.put("de","ISO-8859-1");
- commonMapper.put("el","ISO-8859-7");
- commonMapper.put("en","ISO-8859-1");
- commonMapper.put("es","ISO-8859-1");
- commonMapper.put("et","ISO-8859-1");
- commonMapper.put("fi","ISO-8859-1");
- commonMapper.put("fr","ISO-8859-1");
- commonMapper.put("hr","ISO-8859-2");
- commonMapper.put("hu","ISO-8859-2");
- commonMapper.put("is","ISO-8859-1");
- commonMapper.put("it","ISO-8859-1");
- commonMapper.put("iw","ISO-8859-8");
- commonMapper.put("ja","Shift_JIS");
- commonMapper.put("ko","EUC-KR");
- commonMapper.put("lt","ISO-8859-2");
- commonMapper.put("lv","ISO-8859-2");
- commonMapper.put("mk","ISO-8859-5");
- commonMapper.put("nl","ISO-8859-1");
- commonMapper.put("no","ISO-8859-1");
- commonMapper.put("pl","ISO-8859-2");
- commonMapper.put("pt","ISO-8859-1");
- commonMapper.put("ro","ISO-8859-2");
- commonMapper.put("ru","ISO-8859-5");
- commonMapper.put("sh","ISO-8859-5");
- commonMapper.put("sk","ISO-8859-2");
- commonMapper.put("sl","ISO-8859-2");
- commonMapper.put("sq","ISO-8859-2");
- commonMapper.put("sr","ISO-8859-5");
- commonMapper.put("sv","ISO-8859-1");
- commonMapper.put("tr","ISO-8859-9");
- commonMapper.put("uk","ISO-8859-5");
- commonMapper.put("zh","GB2312");
- commonMapper.put("zh_TW","Big5");
+ commonMapper.put("ar", "ISO-8859-6");
+ commonMapper.put("be", "ISO-8859-5");
+ commonMapper.put("bg", "ISO-8859-5");
+ commonMapper.put("ca", "ISO-8859-1");
+ commonMapper.put("cs", "ISO-8859-2");
+ commonMapper.put("da", "ISO-8859-1");
+ commonMapper.put("de", "ISO-8859-1");
+ commonMapper.put("el", "ISO-8859-7");
+ commonMapper.put("en", "ISO-8859-1");
+ commonMapper.put("es", "ISO-8859-1");
+ commonMapper.put("et", "ISO-8859-1");
+ commonMapper.put("fi", "ISO-8859-1");
+ commonMapper.put("fr", "ISO-8859-1");
+ commonMapper.put("hr", "ISO-8859-2");
+ commonMapper.put("hu", "ISO-8859-2");
+ commonMapper.put("is", "ISO-8859-1");
+ commonMapper.put("it", "ISO-8859-1");
+ commonMapper.put("iw", "ISO-8859-8");
+ commonMapper.put("ja", "Shift_JIS");
+ commonMapper.put("ko", "EUC-KR");
+ commonMapper.put("lt", "ISO-8859-2");
+ commonMapper.put("lv", "ISO-8859-2");
+ commonMapper.put("mk", "ISO-8859-5");
+ commonMapper.put("nl", "ISO-8859-1");
+ commonMapper.put("no", "ISO-8859-1");
+ commonMapper.put("pl", "ISO-8859-2");
+ commonMapper.put("pt", "ISO-8859-1");
+ commonMapper.put("ro", "ISO-8859-2");
+ commonMapper.put("ru", "ISO-8859-5");
+ commonMapper.put("sh", "ISO-8859-5");
+ commonMapper.put("sk", "ISO-8859-2");
+ commonMapper.put("sl", "ISO-8859-2");
+ commonMapper.put("sq", "ISO-8859-2");
+ commonMapper.put("sr", "ISO-8859-5");
+ commonMapper.put("sv", "ISO-8859-1");
+ commonMapper.put("tr", "ISO-8859-9");
+ commonMapper.put("uk", "ISO-8859-5");
+ commonMapper.put("zh", "GB2312");
+ commonMapper.put("zh_TW", "Big5");
}
/**
* An array of available charset mappers.
*/
- private Map mappers[] = new Map[6];
+ private HashMap<Integer, HashMap<String, String>> mappers = new
HashMap<Integer, HashMap<String, String>>();
/**
* Loads mappings from a stream.
@@ -124,12 +124,12 @@ public class CharSetMap
* @return the mappings.
* @throws IOException for an incorrect stream.
*/
- protected static Map loadStream(InputStream input)
+ protected static HashMap<String, String> loadStream(InputStream input)
throws IOException
{
Properties props = new Properties();
props.load(input);
- return new HashMap(props);
+ return convertPropertiesToHash(props);
}
/**
@@ -139,7 +139,7 @@ public class CharSetMap
* @return the mappings.
* @throws IOException for an incorrect file.
*/
- protected static Map loadFile(File file)
+ protected static HashMap<String, String> loadFile(File file)
throws IOException
{
return loadStream(new FileInputStream(file));
@@ -152,7 +152,7 @@ public class CharSetMap
* @return the mappings.
* @throws IOException for an incorrect file.
*/
- protected static Map loadPath(String path)
+ protected static HashMap<String, String> loadPath(String path)
throws IOException
{
return loadFile(new File(path));
@@ -164,7 +164,7 @@ public class CharSetMap
* @param name a resource name.
* @return the mappings.
*/
- protected static Map loadResource(String name)
+ protected static HashMap<String, String> loadResource(String name)
{
InputStream input = CharSetMap.class.getResourceAsStream(name);
if (input != null)
@@ -185,6 +185,19 @@ public class CharSetMap
}
/**
+ * Convert the properties obj to a hashmap
+ * @param props the properties
+ * @return HashMap of the properties
+ */
+ private static HashMap<String, String> convertPropertiesToHash(Properties
props)
+ {
+ HashMap<String, String> map = new HashMap<>();
+ for (final String name: props.stringPropertyNames())
+ map.put(name, props.getProperty(name));
+ return map;
+ }
+
+ /**
* Constructs a new charset map with default mappers.
*/
public CharSetMap()
@@ -194,10 +207,10 @@ public class CharSetMap
{
// Check whether the user directory contains mappings.
path = System.getProperty("user.home");
- if (path != null)
+ if ( StringUtils.isNotEmpty(path) )
{
path = path + File.separator + CHARSET_RESOURCE;
- mappers[MAP_HOME] = loadPath(path);
+ mappers.put(MAP_HOME, loadPath(path));
}
}
catch (IOException x)
@@ -210,7 +223,7 @@ public class CharSetMap
// Check whether the system directory contains mappings.
path = System.getProperty("java.home") +
File.separator + "lib" + File.separator + CHARSET_RESOURCE;
- mappers[MAP_SYS] = loadPath(path);
+ mappers.put(MAP_SYS, loadPath(path));
}
catch (IOException x)
{
@@ -218,13 +231,13 @@ public class CharSetMap
}
// Check whether the current class jar contains mappings.
- mappers[MAP_JAR] = loadResource("/META-INF/" + CHARSET_RESOURCE);
+ mappers.put(MAP_JAR, loadResource("/META-INF/" + CHARSET_RESOURCE));
// Set the common mapper to have the lowest priority.
- mappers[MAP_COM] = commonMapper;
+ mappers.put(MAP_COM, commonMapper);
// Set the cache mapper to have the highest priority.
- mappers[MAP_CACHE] = new Hashtable();
+ mappers.put(MAP_CACHE, new HashMap<String, String>());
}
/**
@@ -235,7 +248,7 @@ public class CharSetMap
public CharSetMap(Properties props)
{
this();
- mappers[MAP_PROG] = new HashMap(props);
+ mappers.put(MAP_PROG, convertPropertiesToHash(props));
}
/**
@@ -248,7 +261,7 @@ public class CharSetMap
throws IOException
{
this();
- mappers[MAP_PROG] = loadStream(input);
+ mappers.put(MAP_PROG, loadStream(input));
}
/**
@@ -261,7 +274,7 @@ public class CharSetMap
throws IOException
{
this();
- mappers[MAP_PROG] = loadFile(file);
+ mappers.put(MAP_PROG, loadFile(file));
}
/**
@@ -274,7 +287,7 @@ public class CharSetMap
throws IOException
{
this();
- mappers[MAP_PROG] = loadPath(path);
+ mappers.put(MAP_PROG, loadPath(path));
}
/**
@@ -283,15 +296,16 @@ public class CharSetMap
* @param key the key for the charset.
* @param charset the corresponding charset.
*/
- public synchronized void setCharSet(String key,
+ @SuppressWarnings("unchecked")
+ public synchronized void setCharSet(String key,
String charset)
{
- HashMap mapper = (HashMap) mappers[MAP_PROG];
+ HashMap<String, String> mapper = mappers.get(MAP_PROG);
mapper = mapper != null ?
- (HashMap) mapper.clone() : new HashMap();
+ (HashMap<String, String>) mapper.clone() : new HashMap<String,
String>();
mapper.put(key,charset);
- mappers[MAP_PROG] = mapper;
- mappers[MAP_CACHE].clear();
+ mappers.put(MAP_PROG, mapper);
+ mappers.get(MAP_CACHE).clear();
}
/**
@@ -302,7 +316,7 @@ public class CharSetMap
* @param locale the locale.
* @return the charset.
*/
- public String getCharSet(Locale locale)
+ public synchronized String getCharSet(Locale locale)
{
// Check the cache first.
String key = locale.toString();
@@ -314,6 +328,7 @@ public class CharSetMap
return DEFAULT_CHARSET;
}
}
+
String charset = searchCharSet(key);
if (charset.length() == 0)
{
@@ -327,7 +342,7 @@ public class CharSetMap
{
charset = DEFAULT_CHARSET;
}
- mappers[MAP_CACHE].put(key,charset);
+ mappers.get(MAP_CACHE).put(key,charset);
}
return charset;
}
@@ -348,12 +363,11 @@ public class CharSetMap
* @param variant a variant field.
* @return the charset.
*/
- public String getCharSet(Locale locale,
+ public synchronized String getCharSet(Locale locale,
String variant)
{
- // Check the cache first.
- if ((variant != null) &&
- (variant.length() > 0))
+ // Check the cache first
+ if ( StringUtils.isNotEmpty(variant) )
{
String key = locale.toString();
if (key.length() == 0)
@@ -390,7 +404,7 @@ public class CharSetMap
{
charset = DEFAULT_CHARSET;
}
- mappers[MAP_CACHE].put(key,charset);
+ mappers.get(MAP_CACHE).put(key,charset);
}
return charset;
}
@@ -406,7 +420,7 @@ public class CharSetMap
* @param key the key for the charset.
* @return the found charset or the default one.
*/
- public String getCharSet(String key)
+ public synchronized String getCharSet(String key)
{
String charset = searchCharSet(key);
return charset.length() > 0 ? charset : DEFAULT_CHARSET;
@@ -419,7 +433,7 @@ public class CharSetMap
* @param def the default charset if none is found.
* @return the found charset or the given default.
*/
- public String getCharSet(String key,
+ public synchronized String getCharSet(String key,
String def)
{
String charset = searchCharSet(key);
@@ -460,17 +474,15 @@ public class CharSetMap
StringBuilder base,
int count)
{
- if ((--count >= 0) &&
- (items[count] != null) &&
- (items[count].length() > 0))
+ if ( --count >= 0 && StringUtils.isNotEmpty(items[count]) )
{
String charset;
base.insert(0,items[count]);
int length = base.length();
for (int i = count; i > 0; i--)
{
- if ((i == count) ||
- (i <= 1))
+ if ( i == count ||
+ i <= 1 )
{
base.insert(0,'_');
length++;
@@ -498,32 +510,31 @@ public class CharSetMap
*/
private String searchCharSet(String key)
{
- if ((key != null) &&
- (key.length() > 0))
+ if ( StringUtils.isNotEmpty(key) == true )
{
// Go through mappers.
- Map mapper;
+ int mapKey;
+ HashMap<String, String> mapper;
String charset;
- for (int i = 0; i < mappers.length; i++)
+ for ( Entry<Integer, HashMap<String, String>> entry :
mappers.entrySet() )
{
- mapper = mappers[i];
- if (mapper != null)
+ mapKey = entry.getKey();
+ mapper = entry.getValue();
+ if (mapper != null && mapper.containsKey(key) )
{
- charset = (String) mapper.get(key);
- if (charset != null)
+ charset = mapper.get(key);
+
+ // Update the cache.
+ if (mapKey > MAP_CACHE)
{
- // Update the cache.
- if (i > MAP_CACHE)
- {
- mappers[MAP_CACHE].put(key,charset);
- }
- return charset;
+ mappers.get(MAP_CACHE).put(key, charset);
}
+ return charset;
}
}
// Not found, add an empty string to the cache.
- mappers[MAP_CACHE].put(key,"");
+ mappers.get(MAP_CACHE).put(key, "");
}
return "";
}
@@ -537,9 +548,10 @@ public class CharSetMap
protected synchronized void setCommonCharSet(String key,
String charset)
{
- HashMap mapper = (HashMap) ((HashMap) mappers[MAP_COM]).clone();
- mapper.put(key,charset);
- mappers[MAP_COM] = mapper;
- mappers[MAP_CACHE].clear();
+ @SuppressWarnings("unchecked")
+ HashMap<String, String> mapper = (HashMap<String, String>)
mappers.get(MAP_COM).clone();
+ mapper.put(key, charset);
+ mappers.put(MAP_COM, mapper);
+ mappers.get(MAP_CACHE).clear();
}
}
Modified:
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeType.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeType.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeType.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeType.java
Tue Jan 15 15:53:18 2019
@@ -1,6 +1,5 @@
package org.apache.fulcrum.mimetype.util;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -20,7 +19,6 @@ package org.apache.fulcrum.mimetype.util
* under the License.
*/
-
import java.util.ArrayList;
/**
@@ -37,24 +35,24 @@ public class MimeType
/**
* A list of well known MIME types.
*/
- public static MimeType TEXT_HTML;
- public static MimeType TEXT_WML;
- public static MimeType TEXT_HDML;
- public static MimeType TEXT_CHTML;
- public static MimeType TEXT_PLAIN;
- public static MimeType MULTIPART;
- public static MimeType MULTIPART_FORM_DATA;
- public static MimeType APPLICATION_POSTSCRIPT;
- public static MimeType APPLICATION_OCTET_STREAM;
- public static MimeType APPLICATION_X_JAVA_AGENT;
- public static MimeType APPLICATION_X_WWW_FORM_URLENCODED;
- public static MimeType MESSAGE_HTTP;
- public static MimeType TEXT_CSS;
- public static MimeType TEXT;
- public static MimeType IMAGE_PNG;
- public static MimeType IMAGE_GIF;
- public static MimeType IMAGE_JPEG;
- public static MimeType IMAGE_WBMP;
+ public static final MimeType TEXT_HTML;
+ public static final MimeType TEXT_WML;
+ public static final MimeType TEXT_HDML;
+ public static final MimeType TEXT_CHTML;
+ public static final MimeType TEXT_PLAIN;
+ public static final MimeType MULTIPART;
+ public static final MimeType MULTIPART_FORM_DATA;
+ public static final MimeType APPLICATION_POSTSCRIPT;
+ public static final MimeType APPLICATION_OCTET_STREAM;
+ public static final MimeType APPLICATION_X_JAVA_AGENT;
+ public static final MimeType APPLICATION_X_WWW_FORM_URLENCODED;
+ public static final MimeType MESSAGE_HTTP;
+ public static final MimeType TEXT_CSS;
+ public static final MimeType TEXT;
+ public static final MimeType IMAGE_PNG;
+ public static final MimeType IMAGE_GIF;
+ public static final MimeType IMAGE_JPEG;
+ public static final MimeType IMAGE_WBMP;
static
{
TEXT_HTML =
@@ -136,7 +134,7 @@ public class MimeType
*/
public MimeType(String spec)
{
- this(spec,true);
+ this(spec, true);
}
/**
@@ -154,12 +152,12 @@ public class MimeType
int length = spec.length();
// Skip leading/trailing blanks.
- while ((start < length) &&
+ while ( start < length &&
Character.isWhitespace(spec.charAt(start)))
{
start++;
}
- while ((length > start) &&
+ while ( length > start &&
Character.isWhitespace(spec.charAt(length - 1)))
{
length--;
@@ -167,8 +165,8 @@ public class MimeType
// Get the type.
StringBuilder sb = new StringBuilder();
- while ((start < length) &&
- ((look = spec.charAt(start)) != '/'))
+ while ( start < length &&
+ (look = spec.charAt(start)) != '/')
{
sb.append(look);
start++;
@@ -183,8 +181,8 @@ public class MimeType
// Get the subtype.
start++;
sb.setLength(0);
- while ((start < length) &&
- ((look = spec.charAt(start)) != ';') &&
+ while ( start < length &&
+ (look = spec.charAt(start)) != ';' &&
!Character.isWhitespace(look))
{
sb.append(look);
@@ -195,7 +193,7 @@ public class MimeType
if (parsep)
{
// Get parameters, if any.
- while ((start < length) &&
+ while ( start < length &&
Character.isWhitespace(spec.charAt(start)))
{
start++;
@@ -208,19 +206,19 @@ public class MimeType
"Syntax error in MIME type parameters " + spec);
}
start++;
- ArrayList na = new ArrayList(4);
- ArrayList va = new ArrayList(4);
+ ArrayList<String> na = new ArrayList<String>(4);
+ ArrayList<String> va = new ArrayList<String>(4);
while (start < length)
{
// Get the name.
- while ((start < length) &&
+ while ( start < length &&
Character.isWhitespace(spec.charAt(start)))
{
start++;
}
sb.setLength(0);
- while ((start < length) &&
- ((look=spec.charAt(start)) != '=') &&
+ while ( start < length &&
+ (look=spec.charAt(start)) != '=' &&
!Character.isWhitespace(look))
{
sb.append(Character.toLowerCase(look));
@@ -229,7 +227,7 @@ public class MimeType
String name = sb.toString();
// Get the value.
- while ((start < length) &&
+ while ( start < length &&
Character.isWhitespace(spec.charAt(start)))
{
start++;
@@ -240,7 +238,7 @@ public class MimeType
"Syntax error in MIME type parameters " + spec);
}
start++ ;
- while ((start < length) &&
+ while ( start < length &&
Character.isWhitespace(spec.charAt(start)))
{
start++;
@@ -252,16 +250,16 @@ public class MimeType
start++;
delim = '"';
}
- while ((start < length) &&
- ((look = spec.charAt(start)) != delim) &&
- ((delim == '"') ||
+ while ( start < length &&
+ ( look = spec.charAt(start) ) != delim &&
+ ( delim == '"' ||
!Character.isWhitespace(look)))
{
sb.append(look);
start++;
}
- while ((start < length) &&
- (spec.charAt(start) != ';'))
+ while ( start < length &&
+ spec.charAt(start) != ';')
{
start++;
}
@@ -304,8 +302,8 @@ public class MimeType
String names[],
String values[])
{
- if ((type == null) ||
- (subtype == null))
+ if (type == null ||
+ subtype == null)
{
throw new NullPointerException("MIME type or subtype missing");
}
@@ -389,7 +387,7 @@ public class MimeType
* @param param the name opf the parameter.
* @return true if the parameter found, otherwise false.
*/
- public boolean hasParameter(String param)
+ public synchronized boolean hasParameter(String param)
{
String[] na = parameterNames;
if (na != null)
@@ -412,7 +410,7 @@ public class MimeType
* @param param the name of the parameter.
* @return the value of the parameter, or null.
*/
- public String getParameter(String param)
+ public synchronized String getParameter(String param)
{
String[] na = parameterNames;
if (na != null)
@@ -475,9 +473,9 @@ public class MimeType
public synchronized void addParameters(String[] params,
String[] values)
{
- if ((params == null) ||
- (values == null) ||
- (params.length != values.length))
+ if (params == null ||
+ values == null ||
+ params.length != values.length)
throw new IllegalArgumentException("Incorrect MIME type
parameters");
if (parameterNames != null)
Modified:
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMap.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMap.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMap.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMap.java
Tue Jan 15 15:53:18 2019
@@ -1,6 +1,5 @@
package org.apache.fulcrum.mimetype.util;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -23,6 +22,9 @@ package org.apache.fulcrum.mimetype.util
import java.io.File;
import java.io.InputStream;
+
+import org.apache.commons.lang3.StringUtils;
+
import java.io.IOException;
/**
@@ -159,7 +161,7 @@ public class MimeTypeMap
{
// Check whether the user directory contains mappings.
path = System.getProperty("user.home");
- if (path != null)
+ if ( StringUtils.isNotEmpty(path) )
{
path = path + File.separator + MIMETYPE_RESOURCE;
mappers[MAP_HOME] = loadPath(path);
@@ -248,7 +250,7 @@ public class MimeTypeMap
* @param file The file to look up a MIME type mapping for.
* @return The MIME type, or {@link #DEFAULT_TYPE} if unmapped.
*/
- public String getContentType(File file)
+ public synchronized String getContentType(File file)
{
return getContentType(file.getName());
}
@@ -260,7 +262,7 @@ public class MimeTypeMap
* mapping for.
* @return The MIME type, or {@link #DEFAULT_TYPE} if unmapped.
*/
- public String getContentType(String fileName)
+ public synchronized String getContentType(String fileName)
{
return getContentType(fileName, DEFAULT_TYPE);
}
@@ -273,23 +275,18 @@ public class MimeTypeMap
* @param def The default MIME type to use if no mapping exists.
* @return The MIME type, or <code>def</code> if unmapped.
*/
- public String getContentType(String fileName, String def)
+ public synchronized String getContentType(String fileName, String def)
{
String ext = parseFileExtension(fileName);
- if (ext != null)
+ if ( StringUtils.isNotEmpty(ext) )
{
String mimeType;
- MimeTypeMapper mapper;
- for (int i = 0; i < mappers.length; i++)
+ for ( MimeTypeMapper mapper : mappers )
{
- mapper = mappers[i];
- if (mapper != null)
+ mimeType = mapper.getContentType(ext);
+ if (mimeType != null)
{
- mimeType = mapper.getContentType(ext);
- if (mimeType != null)
- {
- return mimeType;
- }
+ return mimeType;
}
}
}
Modified:
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMapper.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMapper.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMapper.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/java/org/apache/fulcrum/mimetype/util/MimeTypeMapper.java
Tue Jan 15 15:53:18 2019
@@ -1,6 +1,7 @@
package org.apache.fulcrum.mimetype.util;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -20,18 +21,19 @@ package org.apache.fulcrum.mimetype.util
* under the License.
*/
-
+import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
-import java.io.StringReader;
+import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.util.Map;
+import java.io.StringReader;
import java.util.HashMap;
+import java.util.Map;
import java.util.StringTokenizer;
+import org.apache.commons.lang3.StringUtils;
+
/**
* This class defines mappings between MIME types and the corresponding
* file name extensions. The mappings are defined as lines formed
@@ -47,8 +49,8 @@ public class MimeTypeMapper
/**
* Mappings between MIME types and file name extensions.
*/
- private HashMap mimeTypeExtensions = new HashMap();
- protected HashMap extensionMimeTypes = new HashMap();
+ private HashMap<String, String> mimeTypeExtensions = new HashMap<String,
String>();
+ protected HashMap<String, String> extensionMimeTypes = new HashMap<String,
String>();
/**
* Constructs an empty MIME type mapper.
@@ -80,21 +82,14 @@ public class MimeTypeMapper
public MimeTypeMapper(File file)
throws IOException
{
- FileReader freader = new FileReader(file);
- try
+ try (BufferedReader in = new BufferedReader(
+ new InputStreamReader(new FileInputStream(file), "UTF8")) )
{
- parse(new BufferedReader(freader));
+ parse(in);
}
- finally
+ catch (IOException x)
{
- try
- {
- freader.close();
- }
- catch (IOException x)
- {
- // ignore
- }
+ // ignore
}
}
@@ -129,26 +124,20 @@ public class MimeTypeMapper
/**
* Gets a MIME content type corresponding to a specified file name
- * extension. If a mapping is initially not found, tries a second
- * lookup using the provided extension in lower case.
+ * extension.
*
* @param ext The file name extension to resolve.
* @return The MIME type, or <code>null</code> if not found.
*/
- public String getContentType(String ext)
+ public synchronized String getContentType(String ext)
{
- String mimeType = (String) mimeTypeExtensions.get(ext);
- if (mimeType == null && ext != null)
- {
- String lcExt = ext.toLowerCase();
- if (!ext.equals(lcExt))
- {
- // Original file extension didn't resolve, but was
- // mixed case. Try it again with lower case chars.
- mimeType = (String) mimeTypeExtensions.get(lcExt);
- }
- }
- return mimeType;
+ if ( StringUtils.isNotEmpty(ext) &&
+
mimeTypeExtensions.containsKey(ext.toLowerCase()))
+ {
+ return mimeTypeExtensions.get(ext.toLowerCase());
+ } else {
+ return null;
+ }
}
/**
@@ -168,14 +157,15 @@ public class MimeTypeMapper
* @param reader a reader to parse.
* @throws IOException for an incorrect reader.
*/
- protected synchronized void parse(BufferedReader reader)
+ @SuppressWarnings("unchecked")
+ protected synchronized void parse(BufferedReader reader)
throws IOException
{
int l,count = 0;
String next;
String str = null;
- HashMap mimeTypes = (HashMap) extensionMimeTypes.clone();
- HashMap extensions = (HashMap) mimeTypeExtensions.clone();
+ HashMap<String, String> mimeTypes = (HashMap<String, String>)
extensionMimeTypes.clone();
+ HashMap<String, String> extensions = (HashMap<String, String>)
mimeTypeExtensions.clone();
while ((next = reader.readLine()) != null)
{
str = str == null ? next : str + next;
@@ -215,13 +205,13 @@ public class MimeTypeMapper
* @return the number of file name extensions parsed.
*/
protected int parseMimeTypeExtension(String spec,
- Map mimeTypes,
- Map extensions)
+ Map<String, String> mimeTypes,
+ Map<String, String> extensions)
{
int count = 0;
spec = spec.trim();
- if ((spec.length() > 0) &&
- (spec.charAt(0) != '#'))
+ if (spec.length() > 0 &&
+ spec.charAt(0) != '#')
{
StringTokenizer tokens = new StringTokenizer(spec);
String type = tokens.nextToken();
Modified:
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/MimetypeTest.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/MimetypeTest.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/MimetypeTest.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/MimetypeTest.java
Tue Jan 15 15:53:18 2019
@@ -1,3 +1,5 @@
+package org.apache.fulcrum.mimetype;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -17,14 +19,17 @@
* under the License.
*/
-package org.apache.fulcrum.mimetype;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.util.Locale;
-import org.apache.fulcrum.testcontainer.BaseUnitTest;
import org.apache.fulcrum.mimetype.util.MimeType;
import org.apache.fulcrum.mimetype.util.MimeTypeMapperTest;
+import org.apache.fulcrum.testcontainer.BaseUnit5Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
/**
* Tests for {@link DefaultMimeTypeService}.
@@ -35,29 +40,22 @@ import org.apache.fulcrum.mimetype.util.
* @author Daniel Rall
* @version $Id$
*/
-public class MimetypeTest extends BaseUnitTest
+public class MimetypeTest extends BaseUnit5Test
{
private MimeTypeService mimeTypeService = null;
- /**
- * Defines the testcase name for JUnit.
- *
- * @param name the testcase's name.
- */
- public MimetypeTest(String name)
- {
- super(name);
- }
-
+ @BeforeEach
public void setUp() throws Exception
{
- super.setUp();
- mimeTypeService = (MimeTypeService)
resolve(MimeTypeService.class.getName());
-
+ mimeTypeService = (MimeTypeService) lookup(MimeTypeService.ROLE);
mimeTypeService.setContentType(MimeTypeMapperTest.MIME_TYPE + ' ' +
MimeTypeMapperTest.KNOWN_EXTENSIONS);
}
+ /**
+ * @throws Exception generic exception
+ */
+ @Test
public void testGetCharSet() throws Exception
{
Locale locale = new Locale("en", "US");
@@ -65,6 +63,10 @@ public class MimetypeTest extends BaseUn
assertEquals("ISO-8859-1", s);
}
+ /**
+ * @throws Exception generic exception
+ */
+ @Test
public void testSetGetContentType() throws Exception
{
File f;
@@ -78,6 +80,10 @@ public class MimetypeTest extends BaseUn
}
}
+ /**
+ * @throws Exception generic exception
+ */
+ @Test
public void testGetDefaultExtensionForCrazy() throws Exception
{
String result =
mimeTypeService.getDefaultExtension(MimeTypeMapperTest.MIME_TYPE);
@@ -87,11 +93,19 @@ public class MimetypeTest extends BaseUn
assertEquals("crazy", result);
}
+ /**
+ * @throws Exception generic exception
+ */
+ @Test
public void testGetDefaultExtensionForPdf() throws Exception
{
assertEquals("pdf",
mimeTypeService.getDefaultExtension("application/pdf"));
}
+ /**
+ * @throws Exception generic exception
+ */
+ @Test
public void testGetContentTypeForPdf() throws Exception
{
assertEquals("application/pdf",
mimeTypeService.getContentType("foo.pdf"));
Modified:
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/util/MimeTypeMapperTest.java
URL:
http://svn.apache.org/viewvc/turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/util/MimeTypeMapperTest.java?rev=1851387&r1=1851386&r2=1851387&view=diff
==============================================================================
---
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/util/MimeTypeMapperTest.java
(original)
+++
turbine/fulcrum/trunk/mimetype/src/test/org/apache/fulcrum/mimetype/util/MimeTypeMapperTest.java
Tue Jan 15 15:53:18 2019
@@ -19,7 +19,12 @@ package org.apache.fulcrum.mimetype.util
* under the License.
*/
-import junit.framework.TestCase;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.fulcrum.testcontainer.BaseUnit5Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
/**
* Tests for the MimeTypeMapper
@@ -28,7 +33,7 @@ import junit.framework.TestCase;
* @author Daniel Rall
*
*/
-public class MimeTypeMapperTest extends TestCase
+public class MimeTypeMapperTest extends BaseUnit5Test
{
/**
* The MIME type used by most of our tests.
@@ -50,26 +55,14 @@ public class MimeTypeMapperTest extends
private MimeTypeMapper mtm;
- public static void main(String[] args)
- {
- junit.textui.TestRunner.run(MimeTypeMapperTest.class);
- }
-
- /**
- * Constructor for MimeTypeMapperTest.
- * @param arg0 default arguments
- */
- public MimeTypeMapperTest(String arg0)
- {
- super(arg0);
- }
-
+ @BeforeEach
public void setUp()
{
mtm = new MimeTypeMapper();
mtm.setContentType(MIME_TYPE + ' ' + KNOWN_EXTENSIONS);
}
+ @Test
public void testGetSetContentType()
{
for (int i = 0; i < INPUT_EXTENSIONS.length; i++)
@@ -78,13 +71,13 @@ public class MimeTypeMapperTest extends
}
}
- /* ### This is actually a test case for MimeTypeMap.
- public void testGetDefaultExtension() throws Exception
+ /**
+ * @throws Exception generic exception
+ */
+ @Test
+ public void testGetMimeTypeExtension() throws Exception
{
- String result = mtm.getDefaultExtension(MIME_TYPE);
- assertEquals("crazy", result);
- MimeType mt = new MimeType(MIME_TYPE);
- result = mtm.getDefaultExtension(mt);
+ String result = mtm.getExtension(MIME_TYPE);
assertEquals("crazy", result);
- }*/
+ }
}