You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2013/06/13 10:47:03 UTC

git commit: WICKET-4579 PackageTextTemplate misses locale, style and variation

Updated Branches:
  refs/heads/master 0c8d63542 -> b5d53c526


WICKET-4579 PackageTextTemplate misses locale, style and variation


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b5d53c52
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b5d53c52
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b5d53c52

Branch: refs/heads/master
Commit: b5d53c526003863598dfdc0173ff9d8d65282565
Parents: 0c8d635
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Thu Jun 13 11:46:13 2013 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Thu Jun 13 11:46:13 2013 +0300

----------------------------------------------------------------------
 .../util/template/PackageTextTemplate.java      | 128 ++++++++++++++-----
 1 file changed, 96 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/b5d53c52/wicket-core/src/main/java/org/apache/wicket/util/template/PackageTextTemplate.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/util/template/PackageTextTemplate.java b/wicket-core/src/main/java/org/apache/wicket/util/template/PackageTextTemplate.java
index ca5db0f..ae50ff1 100644
--- a/wicket-core/src/main/java/org/apache/wicket/util/template/PackageTextTemplate.java
+++ b/wicket-core/src/main/java/org/apache/wicket/util/template/PackageTextTemplate.java
@@ -19,6 +19,7 @@ package org.apache.wicket.util.template;
 import java.io.IOException;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.util.io.Streams;
@@ -53,6 +54,12 @@ public class PackageTextTemplate extends TextTemplate
 	/** contents */
 	private final StringBuilder buffer = new StringBuilder();
 
+	private final Class<?> scope;
+
+	private final String fileName;
+
+	private String encoding;
+
 	/**
 	 * Constructor.
 	 * 
@@ -129,57 +136,112 @@ public class PackageTextTemplate extends TextTemplate
 	{
 		super(contentType);
 
-		String path = Packages.absolutePath(clazz, fileName);
+		this.scope = clazz;
+		this.fileName = fileName;
+		this.encoding = encoding;
 
-		Application app = Application.get();
+		setStyle(style);
+		setVariation(variation);
+		setLocale(locale);
+	}
 
-		// first try default class loading locator to find the resource
-		IResourceStream stream = app.getResourceSettings()
-			.getResourceStreamLocator()
-			.locate(clazz, path, style, variation, locale, null, false);
+	@Override
+	public void setStyle(String style)
+	{
+		if (Objects.equals(style, getStyle()) == false)
+		{
+			buffer.setLength(0);
+		}
+		super.setStyle(style);
+	}
 
-		if (stream == null)
+	@Override
+	public void setLocale(Locale locale)
+	{
+		if (Objects.equals(locale, getLocale()) == false)
 		{
-			// if the default locator didn't find the resource then fallback
-			stream = new ResourceStreamLocator().locate(clazz, path, style, variation, locale, null, false);
+			buffer.setLength(0);
 		}
+		super.setLocale(locale);
+	}
 
-		if (stream == null)
+	@Override
+	public void setVariation(String variation)
+	{
+		if (Objects.equals(variation, getVariation()) == false)
 		{
-			throw new IllegalArgumentException("resource " + fileName + " not found for scope " +
-				clazz + " (path = " + path + ")");
+			buffer.setLength(0);
 		}
+		super.setVariation(variation);
+	}
 
-		setLastModified(stream.lastModifiedTime());
+	public void setEncoding(String encoding)
+	{
+		if (Objects.equals(encoding, this.encoding) == false)
+		{
+			buffer.setLength(0);
+		}
+		this.encoding = encoding == null ? DEFAULT_ENCODING : encoding;
+	}
 
-		try
+	/**
+	 * Loads the template if it is not loaded yet
+	 */
+	private void load() {
+		if (buffer.length() == 0)
 		{
-			if (encoding != null)
+			String path = Packages.absolutePath(scope, fileName);
+
+			Application app = Application.get();
+
+			// first try default class loading locator to find the resource
+			IResourceStream stream = app.getResourceSettings()
+				.getResourceStreamLocator()
+				.locate(scope, path, getStyle(), getVariation(), getLocale(), null, false);
+
+			if (stream == null)
 			{
-				buffer.append(Streams.readString(stream.getInputStream(), encoding));
+				// if the default locator didn't find the resource then fallback
+				stream = new ResourceStreamLocator().locate(scope, path, getStyle(), getVariation(), getLocale(), null, false);
 			}
-			else
+
+			if (stream == null)
 			{
-				buffer.append(Streams.readString(stream.getInputStream()));
+				throw new IllegalArgumentException("resource " + fileName + " not found for scope " +
+					scope + " (path = " + path + ")");
 			}
-		}
-		catch (IOException e)
-		{
-			throw new RuntimeException(e);
-		}
-		catch (ResourceStreamNotFoundException e)
-		{
-			throw new RuntimeException(e);
-		}
-		finally
-		{
+
+			setLastModified(stream.lastModifiedTime());
+
 			try
 			{
-				stream.close();
+				if (encoding != null)
+				{
+					buffer.append(Streams.readString(stream.getInputStream(), encoding));
+				}
+				else
+				{
+					buffer.append(Streams.readString(stream.getInputStream()));
+				}
 			}
 			catch (IOException e)
 			{
-				log.error(e.getMessage(), e);
+				throw new RuntimeException(e);
+			}
+			catch (ResourceStreamNotFoundException e)
+			{
+				throw new RuntimeException(e);
+			}
+			finally
+			{
+				try
+				{
+					stream.close();
+				}
+				catch (IOException e)
+				{
+					log.error(e.getMessage(), e);
+				}
 			}
 		}
 	}
@@ -190,6 +252,7 @@ public class PackageTextTemplate extends TextTemplate
 	@Override
 	public String getString()
 	{
+		load();
 		return buffer.toString();
 	}
 
@@ -214,8 +277,9 @@ public class PackageTextTemplate extends TextTemplate
 	{
 		if (variables != null)
 		{
+			load();
 			String result = new MapVariableInterpolator(buffer.toString(), variables).toString();
-			buffer.delete(0, buffer.length());
+			buffer.setLength(0);
 			buffer.append(result);
 		}
 		return this;