Skip to content

Commit

Permalink
Adds ThemeConfig class to make Spring MVC integration easier.
Browse files Browse the repository at this point in the history
Improves loop option for place tags: first_last="first,last,place"
Adds new filters rpad and lpad.
  • Loading branch information
Tom McClure committed Jun 17, 2015
1 parent b355aa7 commit 0ca75ba
Show file tree
Hide file tree
Showing 19 changed files with 720 additions and 89 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/bin/
/target/
pom.xml.asc
self-release.sh
/.settings/
6 changes: 6 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Release History

v2.6.4 - 2015-06-17 tmcclure

Improves loop option for place tags: first_last="first,last,place"
Adds ThemeConfig class to make Spring MVC integration easier.
Adds new filters rpad and lpad.

v2.6.3 - 2015-01-01 tmcclure

New {% data %}...{% enddata %} section markers for {% exec %}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>com.x5dev</groupId>
<artifactId>chunk-templates</artifactId>
<packaging>jar</packaging>
<version>2.6.3</version>
<version>2.6.4</version>
<name>Chunk Templates</name>
<description>Chunk Template Engine for Java</description>
<url>http://www.x5software.com/chunk/</url>
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/com/x5/template/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Vector;
import java.util.Map;
Expand Down Expand Up @@ -244,15 +245,15 @@
* Updates: <A href="http://www.x5software.com/chunk/">Chunk Documentation</A><BR>
*
* @author Tom McClure
* @version 2.6.3
* @version 2.6.4
*/

public class Chunk implements Map<String,Object>
{
public static final int HASH_THRESH = 8;
public static final int DEPTH_LIMIT = 17;

public static final String VERSION = "2.6.3";
public static final String VERSION = "2.6.4";

private static final String TRUE = "TRUE";

Expand Down Expand Up @@ -1480,11 +1481,31 @@ public void setLocale(String localeCode)
this.localeCode = localeCode;
}

public void setLocale(Locale javaLocale)
{
if (javaLocale == null) {
this.localeCode = null;
} else {
String localeCode = javaLocale.toString();
localeCode = localeCode.replace('-', '_');
this.setLocale(localeCode);
}
}

public void setLocale(ChunkLocale chunkLocale)
{
if (chunkLocale == null) {
this.localeCode = null;
} else {
this.setLocale(chunkLocale.toString());
}
}

public ChunkLocale getLocale()
{
if (localeCode == null) return null;
if (locale == null) {
locale = ChunkLocale.getInstance(localeCode,this);
locale = ChunkLocale.getInstance(localeCode, this);
}
return locale;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/x5/template/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public static Object applyFilter(Chunk context, String filter, Object input)
filterName = filter.substring(0,parenPos);
filterArgs = parseArgs(filter.substring(parenPos+1));
} else {
// this is awful, need to change ChunkFilter interface to
// give filterName a dedicated argument in the apply call.
filterArgs = new String[]{filterName};
}

Expand Down
43 changes: 28 additions & 15 deletions src/main/java/com/x5/template/LoopTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public class LoopTag extends BlockTag
// for speed
private Chunk rowX;

//private static final String ON_EMPTY_MARKER = "{~.onEmpty}";
//private static final String DIVIDER_MARKER = "{~.divider}";
private static final String FIRST_MARKER = "is_first";
private static final String LAST_MARKER = "is_last";
private static final String FIRST_MARKER = "first";
private static final String LAST_MARKER = "last";
private static final String PLACE_TAG = "place";

public static void main(String[] args)
{
Expand Down Expand Up @@ -66,7 +65,7 @@ public LoopTag(String params, Chunk ch, String origin)
parseParams(params);

// this constructor is only called when the loop tag has no body
// eg {^loop data="~mydata" template="#test_row" no_data="#test_empty" divider="<hr/>"}
// eg {% loop data="$mydata" template="#test_row" no_data="#test_empty" divider="<hr/>" %}
initWithoutBlock(origin);
}

Expand Down Expand Up @@ -310,6 +309,7 @@ public void cookLoopToPrinter(Writer out, Chunk context, String origin,
String counterTag = null;
String firstRunTag = null;
String lastRunTag = null;
String placeTag = null;
String objectKeyLabel = null;
String objectValueLabel = null;

Expand Down Expand Up @@ -356,13 +356,19 @@ public void cookLoopToPrinter(Writer out, Chunk context, String origin,
String[] userFirstLast = tagNames.split(",");
firstRunTag = eatTagSymbol(userFirstLast[0]);
lastRunTag = eatTagSymbol(userFirstLast[1]);
if (userFirstLast.length > 2) {
placeTag = eatTagSymbol(userFirstLast[2]);
}
}
if (firstRunTag == null || firstRunTag.length() == 0) {
firstRunTag = FIRST_MARKER;
}
if (lastRunTag == null || lastRunTag.length() == 0) {
lastRunTag = LAST_MARKER;
}
if (placeTag == null || placeTag.length() == 0) {
placeTag = PLACE_TAG;
}
}
if (options.containsKey("valname")) {
objectValueLabel = (String)options.get("valname");
Expand All @@ -379,13 +385,7 @@ public void cookLoopToPrinter(Writer out, Chunk context, String origin,
this.rowX.append( rowSnippet );
}
// make sure cached rowX chunk matches context locale
if (context.getLocale() == null) {
if (rowX.getLocale() != null) rowX.setLocale(null);
} else {
if (rowX.getLocale() == null || rowX.getLocale() != context.getLocale()) {
rowX.setLocale(context.getLocale().toString());
}
}
rowX.setLocale(context.getLocale());

String prefix = null;
if (options != null && options.containsKey("name")) {
Expand Down Expand Up @@ -499,16 +499,29 @@ public void cookLoopToPrinter(Writer out, Chunk context, String origin,
if (firstRunTag != null) {
if (counter == 0) {
rowX.set(firstRunTag, "TRUE");
if (prefix != null) rowX.set(prefix + "." + firstRunTag, "TRUE");
rowX.set(placeTag, firstRunTag);
if (prefix != null) {
rowX.set(prefix + "." + firstRunTag, "TRUE");
rowX.set(prefix + "." + placeTag, firstRunTag);
}
} else if (counter == 1) {
rowX.unset(firstRunTag);
if (prefix != null) rowX.unset(prefix + "." + firstRunTag);
rowX.set(placeTag, "");
if (prefix != null) {
rowX.unset(prefix + "." + firstRunTag);
rowX.set(prefix + "." + placeTag, "");
}
}
}
if (lastRunTag != null) {
if (!data.hasNext()) {
String place = counter == 0 ? (firstRunTag + " " + lastRunTag) : lastRunTag;
rowX.set(lastRunTag, "TRUE");
if (prefix != null) rowX.set(prefix + "." + lastRunTag, "TRUE");
rowX.set(placeTag, place);
if (prefix != null) {
rowX.set(prefix + "." + lastRunTag, "TRUE");
rowX.set(prefix + "." + placeTag, place);
}
}
}

Expand Down
55 changes: 44 additions & 11 deletions src/main/java/com/x5/template/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Theme implements ContentSource, ChunkFactory
private String themesFolder;
private String themeLayerNames;
private String fileExtension;
private int cacheMins = 0;

private static final String DEFAULT_THEMES_FOLDER = "themes";

Expand All @@ -27,6 +28,28 @@ public Theme()
this(null, null, null);
}

public Theme(ThemeConfig config)
{
this(config.getThemeFolder(), config.getLayerNames(), config.getDefaultExtension());
this.setDirtyInterval(config.getCacheMinutes());
this.localeCode = config.getLocaleCode();
if (config.hideErrors()) {
this.setErrorHandling(false, config.getErrorLog());
}

ChunkFilter[] filters = config.getFilters();
if (filters != null) {
for (ChunkFilter filter : filters) {
this.registerFilter(filter);
}
}

String encoding = config.getEncoding();
if (encoding != null) {
this.setEncoding(encoding);
}
}

public Theme(ContentSource templates)
{
themeLayers.add(templates);
Expand Down Expand Up @@ -94,15 +117,17 @@ private void init()

String[] layerNames = parseLayerNames(themeLayerNames);
if (layerNames == null) {
TemplateSet simple = new TemplateSet(themesFolder,fileExtension,0);
TemplateSet simple = new TemplateSet(themesFolder, fileExtension, cacheMins);
if (!renderErrs) simple.signalFailureWithNull();
themeLayers.add(simple);
} else {
for (int i=0; i<layerNames.length; i++) {
TemplateSet x = new TemplateSet(this.themesFolder + layerNames[i],fileExtension,0);
TemplateSet x = new TemplateSet(this.themesFolder + layerNames[i], fileExtension, cacheMins);
x.setLayerName(layerNames[i]);
// important: do not return pretty HTML-formatted error strings
// when template can not be located.
// do not return pretty HTML-formatted error strings
// when template can not be located -- with multiple
// layers, a null response is required to search the
// next layer in the stack for the missing template.
x.signalFailureWithNull();
themeLayers.add(x);
}
Expand All @@ -123,18 +148,25 @@ private ArrayList<ContentSource> getThemeLayers()

private String[] parseLayerNames(String themeLayerNames)
{
if (themeLayerNames == null) return null;
if (themeLayerNames == null || themeLayerNames.trim().length() == 0) {
return null;
}

return themeLayerNames.split(" *, *");
}

public void setDirtyInterval(int minutes)
{
// propagate setting down to each layer
ArrayList<TemplateSet> templateSets = getTemplateSets();
if (templateSets != null) {
for (TemplateSet layer : templateSets) {
layer.setDirtyInterval(minutes);
if (this.themeLayers.size() == 0) {
// lazy init has not happened yet
this.cacheMins = minutes;
} else {
// propagate setting down to each layer
ArrayList<TemplateSet> templateSets = getTemplateSets();
if (templateSets != null) {
for (TemplateSet layer : templateSets) {
layer.setDirtyInterval(minutes);
}
}
}
}
Expand Down Expand Up @@ -259,7 +291,8 @@ public String getProtocol()
public Chunk makeChunk()
{
Chunk c = new Chunk();
c.setMacroLibrary(this,this);
// provide theme path context and user-provided filters
c.setMacroLibrary(this, this);
// make sure chunk inherits theme settings
shareContentSources(c);
c.setLocale(localeCode);
Expand Down
Loading

0 comments on commit 0ca75ba

Please sign in to comment.