Search in sources :

Example 11 with TemplateException

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);
    }
}
Also used : TemplateException(org.alfresco.service.cmr.repository.TemplateException) IOException(java.io.IOException)

Example 12 with TemplateException

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);
    }
}
Also used : TemplateException(org.alfresco.service.cmr.repository.TemplateException) Environment(freemarker.core.Environment) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 13 with TemplateException

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);
    }
}
Also used : TemplateException(org.alfresco.service.cmr.repository.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 14 with TemplateException

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);
    }
}
Also used : TemplateException(org.alfresco.service.cmr.repository.TemplateException) Environment(freemarker.core.Environment) IOException(java.io.IOException) Template(freemarker.template.Template)

Aggregations

TemplateException (org.alfresco.service.cmr.repository.TemplateException)14 IOException (java.io.IOException)7 NodeRef (org.alfresco.service.cmr.repository.NodeRef)4 Template (freemarker.template.Template)3 TemplateProcessor (org.alfresco.service.cmr.repository.TemplateProcessor)3 Environment (freemarker.core.Environment)2 HashMap (java.util.HashMap)2 StringTokenizer (java.util.StringTokenizer)2 NodeService (org.alfresco.service.cmr.repository.NodeService)2 TemplateService (org.alfresco.service.cmr.repository.TemplateService)2 Document (org.w3c.dom.Document)2 SimpleDate (freemarker.template.SimpleDate)1 SimpleHash (freemarker.template.SimpleHash)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketException (java.net.SocketException)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1