use of org.alfresco.service.cmr.repository.TemplateException in project alfresco-repository by Alfresco.
the class XSLTProcessor method process.
@Override
public void process(String template, Object model, Writer out, Locale locale) {
if (template.indexOf(StoreRef.URI_FILLER) != -1) {
// If template is a node ref, ignore locale
process(template, model, out);
} else {
// Otherwise try and locate a locale specific resource.
TemplateSource templateSource = null;
int lastDot = template.lastIndexOf('.');
String prefix = lastDot == -1 ? template : template.substring(0, lastDot);
String suffix = lastDot == -1 ? "" : template.substring(lastDot);
String localeName = LOCALE_SEPARATOR + locale.toString();
StringBuffer buf = new StringBuffer(template.length() + localeName.length());
buf.append(prefix);
for (; ; ) {
buf.setLength(prefix.length());
String path = buf.append(localeName).append(suffix).toString();
try {
templateSource = (TemplateSource) templateLoader.findTemplateSource(path);
} catch (IOException ex) {
throw new TemplateException(MSG_UNABLE_TO_READ_TEMPLATE, new Object[] { ex.getMessage() }, ex);
}
if (templateSource != null) {
break;
}
int lastUnderscore = localeName.lastIndexOf('_');
if (lastUnderscore == -1) {
break;
}
localeName = localeName.substring(0, lastUnderscore);
}
if (templateSource == null) {
throw new TemplateException(MSG_ERROR_NO_TEMPLATE, new Object[] { template });
}
process(templateSource, model, out);
}
}
use of org.alfresco.service.cmr.repository.TemplateException in project alfresco-repository by Alfresco.
the class FreeMarkerProcessor method process.
/**
* @see org.alfresco.service.cmr.repository.TemplateProcessor#process(java.lang.String, java.lang.Object, java.io.Writer, java.util.Locale)
*/
public void process(String template, Object model, Writer out, Locale locale) {
if (template == null || template.length() == 0) {
throw new IllegalArgumentException("Template name is mandatory.");
}
if (model == null) {
throw new IllegalArgumentException("Model is mandatory.");
}
if (out == null) {
throw new IllegalArgumentException("Output Writer is mandatory.");
}
if (locale == null) {
throw new IllegalArgumentException("Locale is mandatory.");
}
try {
long startTime = 0;
if (logger.isDebugEnabled()) {
// + " on model: " + model);
logger.debug("Executing template: " + template);
startTime = System.currentTimeMillis();
}
Template t = getConfig().getTemplate(template, locale);
if (t != null) {
try {
// perform the template processing against supplied data model
Object freeMarkerModel = convertToFreeMarkerModel(model);
Environment env = t.createProcessingEnvironment(freeMarkerModel, out);
// set the locale to ensure dates etc. are appropriate localised
env.setLocale(locale);
env.process();
} catch (Throwable err) {
throw new TemplateException(MSG_ERROR_TEMPLATE_FAIL, new Object[] { err.getMessage() }, err);
}
} else {
throw new TemplateException(MSG_ERROR_NO_TEMPLATE, new Object[] { template });
}
if (logger.isDebugEnabled()) {
long endTime = System.currentTimeMillis();
logger.debug("Time to execute template: " + (endTime - startTime) + "ms");
}
} catch (IOException ioerr) {
throw new TemplateException(MSG_ERROR_TEMPLATE_IO, new Object[] { template }, ioerr);
}
}
use of org.alfresco.service.cmr.repository.TemplateException in project alfresco-repository by Alfresco.
the class FreeMarkerProcessor method processString.
/**
* @see org.alfresco.service.cmr.repository.TemplateProcessor#processString(java.lang.String, java.lang.Object, java.io.Writer)
*/
public void processString(String template, Object model, Writer out) {
if (template == null || template.length() == 0) {
throw new IllegalArgumentException("Template is mandatory.");
}
if (model == null) {
throw new IllegalArgumentException("Model is mandatory.");
}
if (out == null) {
throw new IllegalArgumentException("Output Writer is mandatory.");
}
try {
long startTime = 0;
if (logger.isDebugEnabled()) {
// + " on model: " + model);
logger.debug("Executing template: " + template);
startTime = System.currentTimeMillis();
}
Template t = getStringConfig(PATH, template).getTemplate(PATH);
if (t != null) {
try {
// perform the template processing against supplied data model
Object freeMarkerModel = convertToFreeMarkerModel(model);
t.process(freeMarkerModel, out);
if (logger.isDebugEnabled()) {
long endTime = System.currentTimeMillis();
logger.debug("Time to execute template: " + (endTime - startTime) + "ms");
}
} catch (Throwable err) {
throw new TemplateException(MSG_ERROR_TEMPLATE_FAIL, new Object[] { err.getMessage() }, err);
}
} else {
throw new TemplateException(MSG_ERROR_NO_TEMPLATE, new Object[] { template });
}
} catch (IOException ioerr) {
throw new TemplateException(MSG_ERROR_TEMPLATE_IO, new Object[] { template }, ioerr);
}
}
use of org.alfresco.service.cmr.repository.TemplateException in project alfresco-repository by Alfresco.
the class FreeMarkerProcessor method process.
/**
* @see org.alfresco.service.cmr.repository.TemplateProcessor#process(java.lang.String, java.lang.Object, java.io.Writer)
*/
public void process(String template, Object model, Writer out) {
if (template == null || template.length() == 0) {
throw new IllegalArgumentException("Template name is mandatory.");
}
if (model == null) {
throw new IllegalArgumentException("Model is mandatory.");
}
if (out == null) {
throw new IllegalArgumentException("Output Writer is mandatory.");
}
try {
long startTime = 0;
if (logger.isDebugEnabled()) {
// + " on model: " + model);
logger.debug("Executing template: " + template);
startTime = System.currentTimeMillis();
}
Template t = getConfig().getTemplate(template);
if (t != null) {
try {
// perform the template processing against supplied data model
Object freeMarkerModel = convertToFreeMarkerModel(model);
Environment env = t.createProcessingEnvironment(freeMarkerModel, out);
// set the locale to ensure dates etc. are appropriate localised
env.setLocale(I18NUtil.getLocale());
env.process();
} catch (Throwable err) {
throw new TemplateException(MSG_ERROR_TEMPLATE_FAIL, new Object[] { err.getMessage() }, err);
}
} else {
throw new TemplateException(MSG_ERROR_NO_TEMPLATE, new Object[] { template });
}
if (logger.isDebugEnabled()) {
long endTime = System.currentTimeMillis();
logger.debug("Time to execute template: " + (endTime - startTime) + "ms");
}
} catch (IOException ioerr) {
throw new TemplateException(MSG_ERROR_TEMPLATE_IO, new Object[] { template }, ioerr);
}
}
Aggregations