Java Code Examples for freemarker.template.SimpleScalar#getAsString()

The following examples show how to use freemarker.template.SimpleScalar#getAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormatDescriptionMethod.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public Object exec(List args) throws TemplateModelException {
    if (args.size() != 1) {
        throw new TemplateModelException("There should be a single argument of type string " +
                "passed to format description method");
    }

    SimpleScalar arg1 = (SimpleScalar) args.get(0);
    String inputString = arg1.getAsString();

    // Replacing spaces that should not be considered in text wrapping with non breaking spaces
    inputString = replaceLeadingSpaces(inputString);

    inputString = inputString.replaceAll("<", "&lt;");
    inputString = inputString.replaceAll(">", "&gt;");

    // Replacing new line characters
    inputString = replaceNewLines(inputString);

    inputString = inputString.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
    inputString = inputString.replaceAll("```([^```]*)```",
            "</p><pre>$1</pre><p style=\"word-wrap: break-word;margin: 0;\">");
    inputString = inputString.replaceAll("`([^`]*)`", "<code>$1</code>");

    return "<p style=\"word-wrap: break-word;margin: 0;\">" + inputString + "</p>";
}
 
Example 2
Source File: RenderLinkDirective.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
private LayoutType resolveLayoutType(Map params) throws TemplateException
{
    LayoutType layoutType = LayoutType.HORIZONTAL;
    SimpleScalar layoutModel = (SimpleScalar) params.get("layout");
    if (layoutModel != null)
    {
        String lt = layoutModel.getAsString();
        try
        {
            layoutType = LayoutType.valueOf(lt.toUpperCase());
        }
        catch (IllegalArgumentException e)
        {
            throw new TemplateException("Layout: " + lt + " is not supported.", e, null);
        }
    }
    return layoutType;
}
 
Example 3
Source File: FindSourceFilesByClassNameMethod.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (String)");
    }
    SimpleScalar arg = (SimpleScalar) arguments.get(0);
    String qualifedClassName = arg.getAsString();
    JavaClassModel classModel = javaClassService.getByName(qualifedClassName);
    List<AbstractJavaSourceModel> results = new ArrayList<>();
    if (classModel instanceof AmbiguousJavaClassModel)
    {
        AmbiguousJavaClassModel ambiguousJavaClassModel = (AmbiguousJavaClassModel) classModel;
        for (JavaClassModel referencedClass : ambiguousJavaClassModel.getReferences())
        {
            addSourceFilesToResult(results, referencedClass);
        }
    }
    else
    {
        addSourceFilesToResult(results, classModel);
    }
    return results;
}
 
Example 4
Source File: BlockDirectiveUtils.java    From freemarker-template-inheritance with Apache License 2.0 5 votes vote down vote up
public static String getBlockName(Environment env, Map params, String paramName) throws TemplateException {

        SimpleScalar blockNameScalar = (SimpleScalar) params.get(paramName);
        if (blockNameScalar == null) {
            throw new TemplateException("This directive must have '" + paramName + "' attribute.", env);
        }
        return blockNameScalar.getAsString();
    }
 
Example 5
Source File: RenderRuleLinkDirective.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException
{
    final Writer writer = env.getOut();
    SimpleScalar ruleIDStringModel = (SimpleScalar) params.get("ruleID");
    if (ruleIDStringModel == null || ruleIDStringModel.getAsString() == null)
        return;

    SimpleScalar renderTypeScalar = (SimpleScalar) params.get("renderType");
    final String renderType;
    if (renderTypeScalar == null)
        renderType = RENDER_TYPE_TAG;
    else
        renderType = renderTypeScalar.getAsString();

    SimpleScalar cssClassScalar = (SimpleScalar) params.get("class");
    String cssClass;
    if (cssClassScalar == null || StringUtils.isBlank(cssClassScalar.getAsString())) {
        cssClass = "";
    }
    else {
        cssClass = cssClassScalar.getAsString();
    }

    String ruleID = ruleIDStringModel.getAsString();

    writer.append("<a title='View Rule: " + ruleID + "' href='" + RenderRuleProviderReportRuleProvider.OUTPUT_FILENAME + "#" + ruleID + "'>");
    if (RENDER_TYPE_GLYPH.equals(renderType))
        writer.append("<span class='glyphicon glyphicon-link "+cssClass+"'></span>");
    else if (RENDER_TYPE_TAG.equals(renderType))
        writer.append("<span class='tag "+cssClass+"'>&lt;rule/></span>");
    else
        writer.append("<span class='plain "+cssClass+"'>"+ ruleID +"</span>");
    writer.append("</a>");
}
 
Example 6
Source File: GetTagModelByNameMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    if (arguments.size() != 1)
        throw new TemplateModelException("Expected one String argument - the name of the tag.");

    SimpleScalar stringModel = (SimpleScalar) arguments.get(0);
    String tagName = stringModel.getAsString();

    return new TagGraphService(graphContext).getTagByName(tagName);
}
 
Example 7
Source File: RenderLinkDirective.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
private String resolveCssClass(Map params)
{
    SimpleScalar css = (SimpleScalar) params.get("class");
    if (css == null)
        return "";
    else
        return css.getAsString();
}
 
Example 8
Source File: MarkdownToHtmlMethod.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException
{
    if (arguments.size() != 1)
    {
        throw new TemplateModelException("Error, method expects one argument (String)");
    }
    SimpleScalar freemarkerArg = (SimpleScalar) arguments.get(0);
    String markdownSource = freemarkerArg.getAsString();

    SoftReference<String> cachedResult = cache.get(markdownSource);
    String cachedResultString;
    if (cachedResult != null && (cachedResultString = cachedResult.get()) != null)
    {
        return cachedResultString;
    }

    try
    {
        // build the plugins object with our extensions
        PegDownPlugins plugins = PegDownPlugins.builder().build();
        PegDownProcessor processor = new PegDownProcessor(Extensions.FENCED_CODE_BLOCKS, MAX_PARSING_TIME_MILLIS, plugins);

        // build the node and then serialize it so that we can make sure the serializer uses our plugins
        RootNode outputNode = processor.parseMarkdown(markdownSource.toCharArray());

        // Our plugin is also a serializer, so build a plugins list for serialization as well
        List<ToHtmlSerializerPlugin> serializerPlugins = new ArrayList<>(1);

        ToHtmlSerializer serializer = new ToHtmlSerializerExtended(new LinkRenderer(), Collections.<String, VerbatimSerializer> emptyMap(),
                    serializerPlugins);
        String result = serializer.toHtml(outputNode);
        cache.put(markdownSource, new SoftReference<>(result));
        return result;
    }
    catch (Throwable t)
    {
        LOG.log(Level.WARNING, "Failed to parse markdown due to: " + t.getMessage() + " markdown source: " + markdownSource, t);
        // Return the unformatted markdown, as this is better than failing the report completely.
        return markdownSource;
    }
}
 
Example 9
Source File: RenderApplicationPieChartDirective.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,
            TemplateDirectiveBody body)
            throws TemplateException, IOException
{
    StringModel projectStringModel = (StringModel) params.get("project");
    ProjectModel projectModel = null;
    if (projectStringModel != null)
        projectModel = (ProjectModel) projectStringModel.getWrappedObject();

    StringModel projectTraversalStringModel = (StringModel) params.get("projectTraversal");
    ProjectModelTraversal projectTraversal = null;
    if (projectTraversalStringModel != null)
        projectTraversal = (ProjectModelTraversal) projectTraversalStringModel.getWrappedObject();

    if (projectModel != null && projectTraversal != null)
        throw new WindupException(NAME + " both 'project' and 'projectTraversal' were specified. Only one of these values should be specified.");
    else if (projectModel == null && projectTraversal == null)
        throw new WindupException(NAME + " neither 'project' nor 'projectTraversal' were specified. At least one of these must be specified.");

    TemplateBooleanModel recursiveBooleanModel = (TemplateBooleanModel) params.get("recursive");
    boolean recursive = recursiveBooleanModel.getAsBoolean();
    SimpleScalar elementIDStringModel = (SimpleScalar) params.get("elementID");
    String elementID = elementIDStringModel.getAsString();

    Set<String> includeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) params.get("includeTags"));
    Set<String> excludeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) params.get("excludeTags"));

    TypeReferenceService typeReferenceService = new TypeReferenceService(context);
    Map<String, Integer> data;
    if (projectModel != null)
        data = typeReferenceService.getPackageUseFrequencies(projectModel, includeTags, excludeTags, 2, recursive);
    else
        data = typeReferenceService.getPackageUseFrequencies(projectTraversal, includeTags, excludeTags, 2, recursive);

    if (data.keySet().size() > 0)
    {
        drawPie(env.getOut(), data, elementID);
    }
    else
    {
        // if we aren't drawing a pie, remove the element that would have held it
        Writer writer = env.getOut();
        writer.append("<script type='text/javascript'>");
        writer.append("$('#" + elementID + "').parent().remove()");
        writer.append("</script>");
    }
}
 
Example 10
Source File: RenderLinkDirective.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException
{
    final Writer writer = env.getOut();
    StringModel stringModel = (StringModel) params.get(MODEL);

    SimpleScalar defaultTextModel = (SimpleScalar) params.get(TEXT);
    String defaultText = defaultTextModel == null ? null : defaultTextModel.getAsString();

    if (stringModel == null || stringModel.getWrappedObject() == null)
    {
        if (StringUtils.isNotBlank(defaultText))
            writer.append(defaultText);
        else
        {
            writer.append("unknown");
            LOG.warning("Failed to resolve name or text for " + getClass().getName() + ". " + env);
        }
        return;
    }

    StringModel projectStringModel = (StringModel) params.get(PROJECT);
    ProjectModel project = null;
    if (projectStringModel != null && projectStringModel.getWrappedObject() instanceof ProjectModel)
        project = (ProjectModel)projectStringModel.getWrappedObject();

    Object model = stringModel.getWrappedObject();

    LayoutType layoutType = resolveLayoutType(params);
    String cssClass = resolveCssClass(params);

    if (model instanceof FileLocationModel)
    {
        processFileLocationModel(writer, cssClass, project, (FileLocationModel) model, defaultText);
    }
    else if (model instanceof FileModel)
    {
        processFileModel(writer, cssClass, project, (FileModel) model, defaultText);
    }
    else if (model instanceof JavaClassModel)
    {
        processJavaClassModel(writer, cssClass, project, (JavaClassModel) model, defaultText);
    }
    else if (model instanceof LinkableModel)
    {
        processLinkableModel(writer, layoutType, cssClass, project, (LinkableModel) model, defaultText);
    }
    else
    {
        throw new TemplateException("Object type not permitted: " + model.getClass().getSimpleName(), env);
    }
}