Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity Template and Java String

I develop jira report plugin.Velocity template shows ascii code single or double quote in java string.

Java Code:

Main:

    Map velocityParams = new HashMap();
    String horizontalAxis = convertKeysToWeekCategories(bugtrends.keySet());

    velocityParams.put("horizontalAxis", horizontalAxis);

convertKeysToWeekCategories method:

    StringBuilder build = new StringBuilder();

    for (Integer integer : keySet) {
        build.append("\'");
        build.append("W");
        build.append(integer.toString());
        build.append("\'");
        build.append(",");
    }

    String result = build.toString();

    result = result.substring(0, result.length() - 1);

    return result;

}

And I draw with HighChart api.

bugstrendview.vm:

<head >
    <meta charset="utf-8">
</head>

<script>AJS.$(document).ready(function(){ 


    chart = new Highcharts.Chart({

        chart: {
            type: 'spline',
            renderTo: 'container'
        },
        exporting: {
            enabled: true,
            filename: "open-bugs-trend-chart"
        },
        title: {
            text: 'Open Bugs Trend',
            x: -20 //center
        },
        subtitle: {
            text: 'results are displayed in weeks',
            x: -20 //center
        },
        xAxis: {
            categories: [$horizontalAxis],
            labels: {
                rotation: -90,
                align: 'right',
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },

        tooltip: {

        formatter: function() {
            var myDates = $tooltipDates;
            return "<b>W"+(this.point.x+1)+"</b><br/>"+
            myDates[this.point.x]+" - "+myDates[this.point.x + 1]+
            "<br/>"+"Total Bugs : "+this.point.y ;

        },
    },


        yAxis: {
            gridLineWidth: 1,
            minorGridLineWidth: 1,
            title: {
                text: 'Number of Open Bugs'
            },
            min: 0,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        plotOptions: {
            spline: {
                dataLabels: {
                    enabled: true
                },
                marker: {
                    enable: true
                }
            }
        },
        series: [
        {
            showInLegend: false,
            data: [$verticalAxis]
        }]
    });        });

</script>

Then shows in the bugstrendview.vm categories:[&#39;W1&#39;] as error 'Uncaught SyntaxError: Unexpected token &' and of course dont draw.

Why shows ascii code of single quote?

like image 300
Anıl Sevici Avatar asked Feb 15 '26 19:02

Anıl Sevici


1 Answers

Obviously the ' gets encoded to &#39;

See this thread:

https://answers.atlassian.com/questions/24351/how-to-prevent-velocity-escape-html

appending WithHtml to a variable name should prevent HTML escaping

You can do:

#set($horizontalAxisWithHtml = $horizontalAxis)

and then use $horizontalAxisWithHtml

like image 178
Robert Avatar answered Feb 18 '26 07:02

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!