Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Losing new line symbols (\r\n)

DanielM
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

have a macro that takes the whole issue information from JIRA (as a JSON Object) and prints it in Confluence. But in Confluence the new lines are lost and the formatting goes to hell.

This is the text in Jira (and how it looks):

Reports are afterwards sent via ftp to Aviva Risk Management team.

Aviva risk team use the daily reports to extract some of the numbers and for a report on the risk performance of the portfolio.

This is how it looks in the JSON:

Reports are afterwards sent via ftp to Aviva Risk Management team.\r\n\r\nAviva risk team use the daily reports to extract some of the numbers and for a report on the risk performance of the portfolio.

And this is what gets printed afterwards:

Reports are afterwards sent via ftp to Aviva Risk Management team. Aviva risk team use the daily reports to extract some of the numbers and for a report on the risk performance of the portfolio.

So basically, what I get from this, is that the \n\r symbols are lost or don't translate the way they should in Confluence. How can I fix that?

public class FieldinfoMacro implements Macro {

    @Override
    @SuppressWarnings("unchecked")
    // This macro gets the issue's field values from the page's context so you don't have to
    // make new requests to Jira for each field seperately
    public String execute(Map<String, String> par, String s, ConversionContext conversionContext)
            throws MacroExecutionException {

        // parameters
        String add = (String) par.get("getfield").toLowerCase();
        HashMap<String, String> fieldMap = (HashMap<String, String>) conversionContext.getProperty("fieldMap");
        JSONObject issue = (JSONObject) conversionContext.getProperty("issueJson");
        Object output = null;
        Object result = null;
        StringBuilder temp = new StringBuilder();

       /* if (add.equals("reporter") || add.equals("assignee") || add.equals("account manager")) {
            try {
                output = issue.getJSONObject("fields").get(fieldMap.get(add));
                result = ((JSONObject) output).get("emailAddress");
                result = ((String) result).replaceAll("@.*", "");
                result = ((String) result).replaceAll("\\W", " ");
            } catch (JSONException je) {
                je.printStackTrace();
            }
        } else { */
        // get the requested field value from the issue
        // there's a handler for the different types of returned data
        // i.e. JSONObject, JSONArray, String
            try {
                output = issue.getJSONObject("fields").get(fieldMap.get(add));
                // String handler
                if (output instanceof String) {
                    result = output;
                    // JSONArray handler (covered different cases)
                } else if (output instanceof JSONArray) {
                    for (int i = 0; i < ((JSONArray) output).length(); i++) {
                        if (((JSONArray) output).get(i) instanceof JSONObject) {
                            result = ((JSONArray) output).getJSONObject(i).getString("value");
                        } else {
                            if (((JSONArray) output).length() < 2) {
                                temp.append(((JSONArray) output).getString(i));
                            } else if (i < ((JSONArray) output).length() - 1) {
                                temp.append(((JSONArray) output).getString(i) + ", ");
                            } else temp.append(((JSONArray) output).getString(i));
                            result = temp;
                        }
                    }
                    // JSONObject handler
                } else if (output instanceof JSONObject) {
                    result = ((JSONObject) output).getString("value");
                }
            } catch (JSONException je) {
                je.printStackTrace();
         }

        return result.toString();
    }

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Josch Bencke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

Hi, "\r\n" is a relict from windows & unix - using confluence, you probably want HTMl tags for linebreaks instead. So how about youtry to searchreplace and insert some "<br>"s ?

DanielM
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

So I should try something like result.replaceAll("\r\n", "<br>"); ?

Josch Bencke
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

Sounds reasonable. Check what confluence does with it :)

DanielM
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

I just noticed, that the \r\n symbols only appeared when I was viewing the JSON in my browser, the text I get from the JSON in Java has the new lines, but I don't see any \r\n symbols to replace with <br>, and those new lines don't translate to Confluence afterwards.

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2014

Typically what I do is take any "\r\n" and turn it to "\n" then I take all "\r" and turn it to "\n" and then I take all "\n" and turn it to "<br>". That way if someone pastes content from windows/old mac/or any *nix system then all of the variant EOL sequences will be covered.

TAGS
AUG Leaders

Atlassian Community Events