package common.template.sample;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.etouch.contmgmt.common.CMUtils;
import net.etouch.contmgmt.wiki.TemplateTagResolver;
/**
* This is a template of the class to interpret the value of the plugin defined. You need to override the method defined
* translate(String regex, String content, String projectName, String title, String family, String ctx, String sessionId) in interface
* WikiContentTranslator. The plugin tag must start with a '#' (e.g #TEMP{zip="94555"})
*
* Additionally, entries are required to make the plugin work in an application.
* 1) Create a jar file for this class (e.g jar -cf :\samepage\tomcat\common\lib\custom_plugin.jar :\temp\SampleTagResolver.class) provided
* you have JDK 1.4.2
* 2) Add the entry at the last in the file :\samepage\cm\WEB-INF\lib\conf\wiki.plugins.
* e.g.
* ... , common.template.sample.SampleTagResolver
* 3) Restart the samepage application and use the newely constructed plugin #TEMP{zip="94555"}
*/
public class SampleTagResolver extends TemplateTagResolver
{
/**
* Used to create multiple instances of plugin tag. It is used in method resolve(String content).
*/
int counter = 0;
/**
* Default constructor of the class to instantiate the counter variable.
*
*/
public SampleTagResolver()
{
counter = 0;
}
/**
* Overloaded method
* @param regex
* @param content
* @param projectName
* @param title
* @param family
* @param ctx
* @param sessionId
* @return
*/
public String translate(String regex, String content, String projectName, String title, String family, String ctx, String sessionId)
{
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
StringBuffer sb = new StringBuffer();
while(matcher.find())
{
String tmp = matcher.group(2);
if( tmp != null && tmp.indexOf("#TEMP") != -1 )
{
tmp = resolveTag(tmp);
matcher.appendReplacement(sb, CMUtils.escapeDollar(tmp));
}
else
{
matcher.appendReplacement(sb, "$1");
}
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* This method enables to create multiple instances of the plugin tag.
* @param content
* @return
*/
private String resolveTag(String content)
{
String ret = content;
String matchPat = "\\#TEMP\\{.*?(\\})";
Pattern pattern = Pattern.compile(matchPat, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
StringBuffer sb = new StringBuffer();
String id = null;
while(matcher.find())
{
counter++;
id="temp"+counter;
String tmp = matcher.group();
tmp = displayText(tmp,id,counter-1);
matcher.appendReplacement(sb, CMUtils.escapeDollar(tmp));
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* This method contains the logic for the interpretation of the tag and returns the output to the screen. It call's the generic method
* getAttributeValue(String str, String attributeName) which return's the value of parameter passed to the plugin tag and the returned
* value can be used to add some additional logic for validation's.
* @param tag
* @param id
* @param counter
* @return
*/
private String displayText(String tag,String id,int counter)
{
String ret = tag;
try
{
String zip = getAttributeValue(tag, "zip");
if(CMUtils.isBlankNull(zip))
{
ret = "Invalid zip code.";
}
ret = "Your current local temperature is " + 32 + " degrees Fahrenheit." ;
}
catch(Exception e)
{
e.printStackTrace();
ret = tag;
}
return ret;
}
}