I18NUtil support for Locale

ContentData support for Locale
 - Full property string has "|locale=" addition
 - getLocale() added to ContentData (may be null)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4581 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-12-12 14:42:01 +00:00
parent f2c5192a7f
commit ac61e7fd21
2 changed files with 90 additions and 53 deletions

View File

@@ -17,8 +17,10 @@
package org.alfresco.service.cmr.repository; package org.alfresco.service.cmr.repository;
import java.io.Serializable; import java.io.Serializable;
import java.util.Locale;
import java.util.StringTokenizer;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.i18n.I18NUtil;
import org.alfresco.util.EqualsHelper; import org.alfresco.util.EqualsHelper;
/** /**
@@ -36,6 +38,7 @@ public class ContentData implements Serializable
private final String mimetype; private final String mimetype;
private final long size; private final long size;
private final String encoding; private final String encoding;
private final Locale locale;
/** /**
* Construct a content property from a string * Construct a content property from a string
@@ -45,52 +48,59 @@ public class ContentData implements Serializable
*/ */
public static ContentData createContentProperty(String contentPropertyStr) public static ContentData createContentProperty(String contentPropertyStr)
{ {
// get the content url String contentUrl = null;
int contentUrlIndex = contentPropertyStr.indexOf("contentUrl="); String mimetype = null;
if (contentUrlIndex == -1) long size = 0L;
String encoding = null;
Locale locale = null;
// now parse the string
StringTokenizer tokenizer = new StringTokenizer(contentPropertyStr, "|");
while (tokenizer.hasMoreTokens())
{ {
throw new AlfrescoRuntimeException( String token = tokenizer.nextToken();
"ContentData string does not have a content URL: " + if (token.startsWith("contentUrl="))
contentPropertyStr);
}
int mimetypeIndex = contentPropertyStr.indexOf("|mimetype=", contentUrlIndex + 11);
if (mimetypeIndex == -1)
{ {
throw new AlfrescoRuntimeException( contentUrl = token.substring(11);
"ContentData string does not have a mimetype: " +
contentPropertyStr);
}
int sizeIndex = contentPropertyStr.indexOf("|size=", mimetypeIndex + 10);
if (sizeIndex == -1)
{
throw new AlfrescoRuntimeException(
"ContentData string does not have a size: " +
contentPropertyStr);
}
int encodingIndex = contentPropertyStr.indexOf("|encoding=", sizeIndex + 6);
if (encodingIndex == -1)
{
throw new AlfrescoRuntimeException(
"ContentData string does not have an encoding: " +
contentPropertyStr);
}
String contentUrl = contentPropertyStr.substring(contentUrlIndex + 11, mimetypeIndex);
if (contentUrl.length() == 0) if (contentUrl.length() == 0)
{
contentUrl = null; contentUrl = null;
String mimetype = contentPropertyStr.substring(mimetypeIndex + 10, sizeIndex); }
}
else if (token.startsWith("mimetype="))
{
mimetype = token.substring(9);
if (mimetype.length() == 0) if (mimetype.length() == 0)
{
mimetype = null; mimetype = null;
String sizeStr = contentPropertyStr.substring(sizeIndex + 6, encodingIndex); }
if (sizeStr.length() == 0) }
sizeStr = "0"; else if (token.startsWith("size="))
String encoding = contentPropertyStr.substring(encodingIndex + 10); {
String sizeStr = token.substring(5);
if (sizeStr.length() > 0)
{
size = Long.parseLong(sizeStr);
}
}
else if (token.startsWith("encoding="))
{
encoding = token.substring(9);
if (encoding.length() == 0) if (encoding.length() == 0)
{
encoding = null; encoding = null;
}
}
else if (token.startsWith("locale="))
{
String localeStr = token.substring(7);
if (localeStr.length() > 0)
{
locale = I18NUtil.parseLocale(localeStr);
}
}
}
long size = Long.valueOf(sizeStr); ContentData property = new ContentData(contentUrl, mimetype, size, encoding, locale);
ContentData property = new ContentData(contentUrl, mimetype, size, encoding);
// done // done
return property; return property;
} }
@@ -109,11 +119,22 @@ public class ContentData implements Serializable
existing == null ? null : existing.contentUrl, existing == null ? null : existing.contentUrl,
mimetype, mimetype,
existing == null ? 0L : existing.size, existing == null ? 0L : existing.size,
existing == null ? "UTF-8" : existing.encoding); existing == null ? "UTF-8" : existing.encoding,
existing == null ? null : existing.locale);
// done // done
return ret; return ret;
} }
/**
* Create a content data without a locale.
*
* @see #ContentData(String, String, long, String, Locale)
*/
public ContentData(String contentUrl, String mimetype, long size, String encoding)
{
this(contentUrl, mimetype, size, encoding, null);
}
/** /**
* Create a compound set of data representing a single instance of <i>content</i>. * Create a compound set of data representing a single instance of <i>content</i>.
* <p> * <p>
@@ -124,16 +145,17 @@ public class ContentData implements Serializable
* <b>mimetype</b> must be supplied. * <b>mimetype</b> must be supplied.
* @param mimetype the content mimetype. This is mandatory if the <b>contentUrl</b> is specified. * @param mimetype the content mimetype. This is mandatory if the <b>contentUrl</b> is specified.
* @param size the content size. * @param size the content size.
* @param encoding the content encoding. * @param encoding the content encoding (may be <tt>null</tt>).
* @param locale the locale of the content (may be <tt>null</tt>).
*/ */
public ContentData(String contentUrl, String mimetype, long size, String encoding) public ContentData(String contentUrl, String mimetype, long size, String encoding, Locale locale)
{ {
checkContentUrl(contentUrl, mimetype); checkContentUrl(contentUrl, mimetype);
this.contentUrl = contentUrl; this.contentUrl = contentUrl;
this.mimetype = mimetype; this.mimetype = mimetype;
this.size = size; this.size = size;
this.encoding = encoding; this.encoding = encoding;
this.locale = locale;
} }
public boolean equals(Object obj) public boolean equals(Object obj)
@@ -148,7 +170,8 @@ public class ContentData implements Serializable
return (EqualsHelper.nullSafeEquals(this.contentUrl, that.contentUrl) && return (EqualsHelper.nullSafeEquals(this.contentUrl, that.contentUrl) &&
EqualsHelper.nullSafeEquals(this.mimetype, that.mimetype) && EqualsHelper.nullSafeEquals(this.mimetype, that.mimetype) &&
this.size == that.size && this.size == that.size &&
EqualsHelper.nullSafeEquals(this.encoding, that.encoding)); EqualsHelper.nullSafeEquals(this.encoding, that.encoding) &&
EqualsHelper.nullSafeEquals(this.locale, that.locale));
} }
/** /**
@@ -160,7 +183,8 @@ public class ContentData implements Serializable
sb.append("contentUrl=").append(contentUrl == null ? "" : contentUrl) sb.append("contentUrl=").append(contentUrl == null ? "" : contentUrl)
.append("|mimetype=").append(mimetype == null ? "" : mimetype) .append("|mimetype=").append(mimetype == null ? "" : mimetype)
.append("|size=").append(size) .append("|size=").append(size)
.append("|encoding=").append(encoding == null ? "" : encoding); .append("|encoding=").append(encoding == null ? "" : encoding)
.append("|locale=").append(locale == null ? "" : locale);
return sb.toString(); return sb.toString();
} }
@@ -243,4 +267,14 @@ public class ContentData implements Serializable
{ {
return encoding; return encoding;
} }
/**
* Get the content's locale.
*
* @return Returns a locale, or null if the locale is unknown
*/
public Locale getLocale()
{
return locale;
}
} }

View File

@@ -16,6 +16,8 @@
*/ */
package org.alfresco.service.cmr.repository; package org.alfresco.service.cmr.repository;
import org.alfresco.i18n.I18NUtil;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@@ -38,18 +40,19 @@ public class ContentDataTest extends TestCase
// check null string // check null string
String propertyStr = property.toString(); String propertyStr = property.toString();
assertEquals("Null values not converted correctly", assertEquals("Null values not converted correctly",
"contentUrl=|mimetype=|size=0|encoding=", propertyStr); "contentUrl=|mimetype=|size=0|encoding=|locale=", propertyStr);
// convert back // convert back
ContentData checkProperty = ContentData.createContentProperty(propertyStr); ContentData checkProperty = ContentData.createContentProperty(propertyStr);
assertEquals("Conversion from string failed", property, checkProperty); assertEquals("Conversion from string failed", property, checkProperty);
property = new ContentData("uuu", "mmm", 123L, "eee"); String localeStr = I18NUtil.getLocale().toString();
property = new ContentData("uuu", "mmm", 123L, "eee", I18NUtil.getLocale());
// convert to a string // convert to a string
propertyStr = property.toString(); propertyStr = property.toString();
assertEquals("Incorrect property string representation", assertEquals("Incorrect property string representation",
"contentUrl=uuu|mimetype=mmm|size=123|encoding=eee", propertyStr); "contentUrl=uuu|mimetype=mmm|size=123|encoding=eee|locale=" + localeStr, propertyStr);
// convert back // convert back
checkProperty = ContentData.createContentProperty(propertyStr); checkProperty = ContentData.createContentProperty(propertyStr);