I thought I may use date as an indicator, so the cached parsed template will
reset if this date changes (the code is below). It looks like I mistaken, and
the “toUrl” method should return null, right?
But this is not solving my problem. StringResource implementation is very
simply, I do not see where the issue can persist.
Regarding ClasspathResource, is this the same as using ordinary tapestry
mechanism? If yes, then it works.
/**
* The class is for use with DynamicTemplateParser
*
* Created with IntelliJ IDEA.
* User: r.makarov
* Date: 05.09.14
* Time: 17:40
*/
public class StringResource implements Resource {
private String tml;
private URL cacheKey;
/**
* @param tml - template to be rendered
* @param lastModificationTime - parameter to watch, to reset cache it
changes
*/
public StringResource(String tml, Date lastModificationTime) {
this.tml = tml;
try {
this.cacheKey = new URL("http://dynamic/" +
lastModificationTime.getTime());
} catch (MalformedURLException e) {
this.cacheKey = null;
}
}
@Override
public boolean exists() {
return false;
}
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream(tml.getBytes("UTF-8"));
}
@Override
public URL toURL() {
return cacheKey;
}
@Override
public Resource forLocale(Locale locale) {
return null;
}
@Override
public Resource forFile(String relativePath) {
return null;
}
@Override
public Resource withExtension(String extension) {
return null;
}
@Override
public String getFolder() {
return null;
}
@Override
public String getFile() {
return null;
}
@Override
public String getPath() {
return null;
}
}
From: Lance Java [mailto:[email protected]]
Sent: Monday, September 08, 2014 12:13 PM
To: Макаров Роман
Subject: Re: dynamic component example / question
Try getting it working first with ClasspathResource and a file on the
classpath. If that works, it's probably an issue with your StringResource.
What's the date for?
On 8 Sep 2014 07:40, "Макаров Роман"
<[email protected]<mailto:[email protected]>> wrote:
Hello all!
The dynamic component almost works for me. But in the output it is erroneously
giving me “container” tag. How can I get rid of it?
The output:
<container
xmlns="http://tapestry.apache.org/schema/tapestry_5_3.xsd">uuuuuuuu</container>
The code:
package ru.kupivip.ecommerce.components;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.services.dynamic.DynamicTemplate;
import org.apache.tapestry5.services.dynamic.DynamicTemplateParser;
import ru.kupivip.utils.StringResource;
import java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: r.makarov
* Date: 04.09.14
* Time: 15:03
*/
public class TrackerDynamic extends Tracker {
@Property
private DynamicTemplate dynamicTemplate;
public String getTestString() {
return "uuuuuuuu";
}
@InjectService("DynamicTemplateParser")
DynamicTemplateParser dynamicTemplateParser;
private String getTemplateString() {
StringBuilder sb = new StringBuilder(
"<t:container
xmlns:t=\"http://tapestry.apache.org/schema/tapestry_5_3.xsd\<http://tapestry.apache.org/schema/tapestry_5_3.xsd%5C>"
xmlns:p=\"tapestry:parameter\">");
sb.append("${getTestString()}");
sb.append("</t:container>");
return sb.toString();
}
@Override
void beginRender() {
super.beginRender();
dynamicTemplate = dynamicTemplateParser.parseTemplate(
new StringResource(getTemplateString(), new Date())
);
}
}