Fixes to loading template/script files from the classpath as UTF-8 by default.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6193 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2007-07-09 11:12:23 +00:00
parent cce11fa38b
commit 6b983aafec
3 changed files with 30 additions and 11 deletions

View File

@@ -163,7 +163,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
FileCopyUtils.copy(stream, os); // both streams are closed FileCopyUtils.copy(stream, os); // both streams are closed
byte[] bytes = os.toByteArray(); byte[] bytes = os.toByteArray();
return executeScriptImpl(resolveScriptImports(new String(bytes)), model); return executeScriptImpl(resolveScriptImports(new String(bytes, "UTF-8")), model);
} }
catch (Throwable err) catch (Throwable err)
{ {
@@ -379,7 +379,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
FileCopyUtils.copy(stream, os); // both streams are closed FileCopyUtils.copy(stream, os); // both streams are closed
byte[] bytes = os.toByteArray(); byte[] bytes = os.toByteArray();
// create the string from the byte[] using encoding if necessary // create the string from the byte[] using encoding if necessary
result = new String(bytes); result = new String(bytes, "UTF-8");
} }
catch (IOException err) catch (IOException err)
{ {

View File

@@ -54,8 +54,9 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
{ {
private NodeService nodeService; private NodeService nodeService;
private ContentService contentService; private ContentService contentService;
private String encoding;
public ClassPathRepoTemplateLoader(NodeService nodeService, ContentService contentService) public ClassPathRepoTemplateLoader(NodeService nodeService, ContentService contentService, String encoding)
{ {
if (nodeService == null) if (nodeService == null)
{ {
@@ -67,6 +68,7 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
} }
this.nodeService = nodeService; this.nodeService = nodeService;
this.contentService = contentService; this.contentService = contentService;
this.encoding = encoding;
} }
/** /**
@@ -90,7 +92,7 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
else else
{ {
URL url = this.getClass().getClassLoader().getResource(name); URL url = this.getClass().getClassLoader().getResource(name);
return url == null ? null : new ClassPathTemplateSource(url); return url == null ? null : new ClassPathTemplateSource(url, encoding);
} }
} }
@@ -101,7 +103,14 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
public Reader getReader(Object templateSource, String encoding) throws IOException public Reader getReader(Object templateSource, String encoding) throws IOException
{ {
return ((BaseTemplateSource)templateSource).getReader(); if (encoding != null)
{
return ((BaseTemplateSource)templateSource).getReader(encoding);
}
else
{
return ((BaseTemplateSource)templateSource).getReader(this.encoding);
}
} }
public void closeTemplateSource(Object templateSource) throws IOException public void closeTemplateSource(Object templateSource) throws IOException
@@ -115,7 +124,7 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
*/ */
abstract class BaseTemplateSource abstract class BaseTemplateSource
{ {
public abstract Reader getReader() throws IOException; public abstract Reader getReader(String encoding) throws IOException;
public abstract void close() throws IOException; public abstract void close() throws IOException;
@@ -131,11 +140,13 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
private final URL url; private final URL url;
private URLConnection conn; private URLConnection conn;
private InputStream inputStream; private InputStream inputStream;
private String encoding;
ClassPathTemplateSource(URL url) throws IOException ClassPathTemplateSource(URL url, String encoding) throws IOException
{ {
this.url = url; this.url = url;
this.conn = url.openConnection(); this.conn = url.openConnection();
this.encoding = encoding;
} }
public boolean equals(Object o) public boolean equals(Object o)
@@ -165,11 +176,18 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
return conn.getLastModified(); return conn.getLastModified();
} }
public Reader getReader() throws IOException public Reader getReader(String encoding) throws IOException
{ {
inputStream = conn.getInputStream(); inputStream = conn.getInputStream();
if (encoding != null)
{
return new InputStreamReader(inputStream, encoding);
}
else
{
return new InputStreamReader(inputStream); return new InputStreamReader(inputStream);
} }
}
public void close() throws IOException public void close() throws IOException
{ {
@@ -230,7 +248,7 @@ public class ClassPathRepoTemplateLoader implements TemplateLoader
return conn.getLastModified(); return conn.getLastModified();
} }
public Reader getReader() throws IOException public Reader getReader(String encoding) throws IOException
{ {
inputStream = conn.getContentInputStream(); inputStream = conn.getContentInputStream();
return new InputStreamReader(inputStream, conn.getEncoding()); return new InputStreamReader(inputStream, conn.getEncoding());

View File

@@ -100,7 +100,8 @@ public class FreeMarkerProcessor extends BaseProcessor implements TemplateProces
config.setCacheStorage(new MruCacheStorage(2, 0)); config.setCacheStorage(new MruCacheStorage(2, 0));
// use our custom loader to find templates on the ClassPath // use our custom loader to find templates on the ClassPath
config.setTemplateLoader(new ClassPathRepoTemplateLoader(this.services.getNodeService(), this.services.getContentService())); config.setTemplateLoader(new ClassPathRepoTemplateLoader(
this.services.getNodeService(), this.services.getContentService(), defaultEncoding));
// use our custom object wrapper that can deal with QNameMap objects directly // use our custom object wrapper that can deal with QNameMap objects directly
config.setObjectWrapper(new QNameAwareObjectWrapper()); config.setObjectWrapper(new QNameAwareObjectWrapper());