[stringtemplate-interest] String manipulations

John Snyders jjsnyders at rcn.com
Sat Sep 23 07:11:27 PDT 2006


Here is the change I made to StringTemplate.java

Before:
	protected void dup(StringTemplate from, StringTemplate to) {
		to.pattern = from.pattern;
		to.chunks = from.chunks;
		to.formalArguments = from.formalArguments;
		to.numberOfDefaultArgumentValues = from.numberOfDefaultArgumentValues;
		to.name = from.name;
		to.group = from.group;
		to.nativeGroup = from.nativeGroup;
		to.listener = from.listener;
		to.regions = from.regions;
		to.isRegion = from.isRegion;
		to.regionDefType = from.regionDefType;
	}

After:
	protected void dup(StringTemplate from, StringTemplate to) {
		to.pattern = from.pattern;
		to.chunks = from.chunks;
		to.formalArguments = from.formalArguments;
		to.numberOfDefaultArgumentValues = from.numberOfDefaultArgumentValues;
		to.name = from.name;
		to.group = from.group;
		to.nativeGroup = from.nativeGroup;
		to.listener = from.listener;
		to.regions = from.regions;
		to.isRegion = from.isRegion;
		to.regionDefType = from.regionDefType;
		to.attributeRenderers = from.attributeRenderers;
	}

It is just the one line added to the end:
		to.attributeRenderers = from.attributeRenderers;

This will allow using templates to do specific rendering such as Uppercase:
    StringTemplateGroup templates = new StringTemplateGroup("html");
    StringTemplateGroup builtinTemplates = new
StringTemplateGroup("builtins");
    StringTemplate uc = builtinTemplates.defineTemplate("upperCase",
"x$it$x");
    uc.registerRenderer(String.class, new UpperCaseRenderer());
    templates.setSuperGroup(builtinTemplates);

Where UpperCaseRenderer is:
    public class UpperCaseRenderer implements AttributeRenderer
    {

        public String toString(Object o)
        {
            if (o instanceof String)
            {
                String s = (String)o;
                return s.toUpperCase();
            }
            return o.toString();
        }
    }

Then in your templates you can use
$upperCase(it=name)$
or
$name:upperCase()$

John

-----Original Message-----
From: Kay Roepke [mailto:kroepke at classdump.org]
Sent: Friday, September 22, 2006 3:47 PM
To: John Snyders
Cc: Oliver Flege; stringtemplate-interest at antlr.org
Subject: Re: [stringtemplate-interest] String manipulations


Hi *,

On 22. Sep 2006, at 16:12 Uhr, John Snyders wrote:

> I can't think of any reason why dup wouldn't copy the renderers. As
> it is
> now it means that the only  time assigning a specific renderer to a
> StringTemplate would work is if it is the top level template (from
> your
> code's perspective the one you are calling toString on).

Correct.

> I assume you made the change to dup. How do we go about giving this
> code
> back so that it can be included as a bug fix?

Simply post it to the list, possibly CC'ing Terence to get his
attention ;)

> Or can someone explain why dup should not copy the renderers?

I think it should and consider that a bug. Normally individual
templates don't have their own renderers and instead rely on the
group's renderers to do the work. Still, when dup'ing them they
should retain everything except the enclosing instance.

Ter is away until Sunday, so we'd have to talk it through with him
again then.


More information about the stringtemplate-interest mailing list