Search in sources :

Example 21 with Context

use of org.apache.velocity.context.Context in project camel by apache.

the class VelocityEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    String path = getResourceUri();
    ObjectHelper.notNull(path, "resourceUri");
    String newResourceUri = exchange.getIn().getHeader(VelocityConstants.VELOCITY_RESOURCE_URI, String.class);
    if (newResourceUri != null) {
        exchange.getIn().removeHeader(VelocityConstants.VELOCITY_RESOURCE_URI);
        log.debug("{} set to {} creating new endpoint to handle exchange", VelocityConstants.VELOCITY_RESOURCE_URI, newResourceUri);
        VelocityEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
        newEndpoint.onExchange(exchange);
        return;
    }
    Reader reader;
    String content = exchange.getIn().getHeader(VelocityConstants.VELOCITY_TEMPLATE, String.class);
    if (content != null) {
        // use content from header
        reader = new StringReader(content);
        if (log.isDebugEnabled()) {
            log.debug("Velocity content read from header {} for endpoint {}", VelocityConstants.VELOCITY_TEMPLATE, getEndpointUri());
        }
        // remove the header to avoid it being propagated in the routing
        exchange.getIn().removeHeader(VelocityConstants.VELOCITY_TEMPLATE);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Velocity content read from resource {} with resourceUri: {} for endpoint {}", new Object[] { getResourceUri(), path, getEndpointUri() });
        }
        reader = getEncoding() != null ? new InputStreamReader(getResourceAsInputStream(), getEncoding()) : new InputStreamReader(getResourceAsInputStream());
    }
    // getResourceAsInputStream also considers the content cache
    StringWriter buffer = new StringWriter();
    String logTag = getClass().getName();
    Context velocityContext = exchange.getIn().getHeader(VelocityConstants.VELOCITY_CONTEXT, Context.class);
    if (velocityContext == null) {
        Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
        @SuppressWarnings("unchecked") Map<String, Object> supplementalMap = exchange.getIn().getHeader(VelocityConstants.VELOCITY_SUPPLEMENTAL_CONTEXT, Map.class);
        if (supplementalMap != null) {
            variableMap.putAll(supplementalMap);
        }
        velocityContext = new VelocityContext(variableMap);
    }
    // let velocity parse and generate the result in buffer
    VelocityEngine engine = getVelocityEngine();
    log.debug("Velocity is evaluating using velocity context: {}", velocityContext);
    engine.evaluate(velocityContext, buffer, logTag, reader);
    // now lets output the results to the exchange
    Message out = exchange.getOut();
    out.setBody(buffer.toString());
    out.setHeaders(exchange.getIn().getHeaders());
    out.setAttachments(exchange.getIn().getAttachments());
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) VelocityEngine(org.apache.velocity.app.VelocityEngine) InputStreamReader(java.io.InputStreamReader) Message(org.apache.camel.Message) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader)

Example 22 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class LocalizedXSLTransformer method initTransformer.

/**
 * Helper to create XSLT transformer for this instance
 */
private void initTransformer() {
    // translate xsl with velocity
    Context vcContext = new VelocityContext();
    vcContext.put("t", pT);
    vcContext.put("staticPath", StaticMediaDispatcher.createStaticURIFor(""));
    String xslAsString = "";
    try (InputStream xslin = getClass().getResourceAsStream("/org/olat/ims/resources/xsl/" + XSLFILENAME)) {
        xslAsString = slurp(xslin);
    } catch (IOException e) {
        log.error("Could not convert xsl to string!", e);
    }
    String replacedOutput = evaluateValue(xslAsString, vcContext);
    TransformerFactory tfactory = TransformerFactory.newInstance();
    XMLReader reader;
    try {
        reader = XMLReaderFactory.createXMLReader();
        reader.setEntityResolver(er);
        Source xsltsource = new SAXSource(reader, new InputSource(new StringReader(replacedOutput)));
        templates = tfactory.newTemplates(xsltsource);
    } catch (SAXException e) {
        throw new OLATRuntimeException("Could not initialize transformer!", e);
    } catch (TransformerConfigurationException e) {
        throw new OLATRuntimeException("Could not initialize transformer (wrong config)!", e);
    }
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) VelocityContext(org.apache.velocity.VelocityContext) InputStream(java.io.InputStream) IOException(java.io.IOException) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) DocumentSource(org.dom4j.io.DocumentSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader)

Example 23 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class CourseCreationConfiguration method getSinglePageText.

/**
 * @param translator
 * @return single page content
 */
public String getSinglePageText(Translator translator) {
    VelocityContainer vc = new VelocityContainer("singlePageTemplate", CourseCreationHelper.class, "singlePageTemplate", translator, null);
    vc.contextPut("coursetitle", courseTitle);
    // prepare rendering of velocity page for the content of the single page node
    GlobalSettings globalSettings = new GlobalSettings() {

        public int getFontSize() {
            return 100;
        }

        public AJAXFlags getAjaxFlags() {
            return new EmptyAJAXFlags();
        }

        public boolean isIdDivsForced() {
            return false;
        }
    };
    Context context = vc.getContext();
    Renderer fr = Renderer.getInstance(vc, translator, null, new RenderResult(), globalSettings);
    StringOutput wOut = new StringOutput(10000);
    VelocityRenderDecorator vrdec = new VelocityRenderDecorator(fr, vc, wOut);
    context.put("r", vrdec);
    VelocityHelper.getInstance().mergeContent(vc.getPage(), context, wOut, null);
    // free the decorator
    context.remove("r");
    IOUtils.closeQuietly(vrdec);
    return WysiwygFactory.createXHtmlFileContent(wOut.toString(), courseTitle);
}
Also used : Context(org.apache.velocity.context.Context) Renderer(org.olat.core.gui.render.Renderer) RenderResult(org.olat.core.gui.render.RenderResult) GlobalSettings(org.olat.core.gui.GlobalSettings) StringOutput(org.olat.core.gui.render.StringOutput) VelocityContainer(org.olat.core.gui.components.velocity.VelocityContainer) VelocityRenderDecorator(org.olat.core.gui.render.velocity.VelocityRenderDecorator)

Example 24 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class TaskFolderCallback method getTaskDeletedMailBody.

private String getTaskDeletedMailBody(UserRequest ureq, String fileName, String courseName, String courseLink) {
    // grab standard text
    String confirmation = translate("task.deleted.body");
    Context c = new VelocityContext();
    Identity identity = ureq.getIdentity();
    c.put("first", identity.getUser().getProperty(UserConstants.FIRSTNAME, getLocale()));
    c.put("last", identity.getUser().getProperty(UserConstants.LASTNAME, getLocale()));
    c.put("email", UserManager.getInstance().getUserDisplayEmail(identity, ureq.getLocale()));
    c.put("filename", fileName);
    c.put("coursename", courseName);
    c.put("courselink", courseLink);
    return VelocityHelper.getInstance().evaluateVTL(confirmation, c);
}
Also used : Context(org.apache.velocity.context.Context) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) VelocityContext(org.apache.velocity.VelocityContext) MailContext(org.olat.core.util.mail.MailContext) VelocityContext(org.apache.velocity.VelocityContext) Identity(org.olat.core.id.Identity)

Example 25 with Context

use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.

the class AssessmentObjectComponentRenderer method renderInteraction.

/**
 * Render the interaction or the PositionStageObject
 * @param renderer
 * @param sb
 * @param interaction
 * @param assessmentItem
 * @param itemSessionState
 * @param component
 * @param ubu
 * @param translator
 */
private void renderInteraction(AssessmentRenderer renderer, StringOutput sb, FlowInteraction interaction, ResolvedAssessmentItem resolvedAssessmentItem, ItemSessionState itemSessionState, AssessmentObjectComponent component, URLBuilder ubu, Translator translator) {
    Context ctx = new VelocityContext();
    ctx.put("interaction", interaction);
    String page = getInteractionTemplate(interaction);
    renderVelocity(renderer, sb, interaction, ctx, page, resolvedAssessmentItem, itemSessionState, component, ubu, translator);
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) VelocityContext(org.apache.velocity.VelocityContext) AssessmentRenderFunctions.contentAsString(org.olat.ims.qti21.ui.components.AssessmentRenderFunctions.contentAsString)

Aggregations

Context (org.apache.velocity.context.Context)41 VelocityContext (org.apache.velocity.VelocityContext)33 StringWriter (java.io.StringWriter)9 IOException (java.io.IOException)8 Identity (org.olat.core.id.Identity)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 AssessmentRenderFunctions.contentAsString (org.olat.ims.qti21.ui.components.AssessmentRenderFunctions.contentAsString)6 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)4 StringReader (java.io.StringReader)4 Locale (java.util.Locale)4 Map (java.util.Map)4 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)4 ValidationError (org.olat.core.gui.components.form.ValidationError)4 MailContext (org.olat.core.util.mail.MailContext)4 I18nManagerWrapper (com.agiletec.aps.system.services.i18n.I18nManagerWrapper)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 Renderer (org.olat.core.gui.render.Renderer)3