سؤال

I 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?

EDIT: @Octopus Here's the code.

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();
    }
هل كانت مفيدة؟

المحلول 2

"\r\n" is a relict from Windows & Unix. Using Confluence, you want HTML tags for line breaks instead, because html treats other line breaks as a single space.

So a simple result = ((String) result).replace("\r\n", "<br>"); did the trick.

نصائح أخرى

You shouldn't have to worry about this. JSON is perfectly capable of representing any UTF-8 character. Once you receive the JSON string, you are definitely doing something in your code, which get rids of the \r\n characters. You should be able to convert the \r\n to new lines like adding <br> html tag by writing your logic. Please add to the post how you get the data from JSON to print it in your page

This line removes all `\r\n' from your String

 result = ((String) result).replaceAll("\\W", " ");

that's why the String literal didn't have any \r and \n characters. Please remove that line and test. Please note that \W means "any non-word character". i.e characters excluding these [a-zA-Z0-9_] characters. Hope this helps

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top