It's all about the answers!

Ask a question

setHTMLDescription with line breaks


Susan Hanson (1.6k2201194) | asked Feb 03 '11, 2:19 p.m.
I am programmatically creating work items, and in my HTMLSummary, I want to have multiple lines/paragraphs. I am loading the information from a file into an ArrayList with 1 row in the array per line of text. Then I want to put all of the rows together with "line breaks" to preserve the formatting of the original Summary.

I have tried to string them together:
String allText = "";
for (int i=0;i<myArray.size();i++) {
allText = allText + "\\n" + myArray.get(i);
}

Trying to put a \n new line into the plain text, and then use
XMLString description = XMLSTring.createFrom PlainText(allText);

but that, as you can imagine, just uses \n in the text itself.

Is there a way to do this using createFromPlainText? Or does createfromXMLText(String xmlText) understand HTML-type items like <p>?

Thanks,
Susan

3 answers



permanent link
Eduardo Bello (4401922) | answered Feb 09 '11, 1:47 p.m.
Investigating the code we see:


public static XMLString createFromPlainText(String plainText) {
if (plainText == null)
plainText= "null"; //$NON-NLS-1$
HTMLOutputHandler handler= new HTMLOutputHandler();
handler.beginDocument();
handler.text(plainText);
handler.endDocument();
return handler.getHTML();
}


A little deeper, we can see this:


private static class LineDelimiterRule implements IInternalRule {

private static final String BREAK_TAG= "<br>"; //$NON-NLS-1$

public String evaluate(InternalScanner scanner) {
int ch= scanner.read();
if (ch == '\n')
return BREAK_TAG;
if (ch == '\r') {
ch= scanner.read();
if (ch != '\n')
scanner.unread();
return BREAK_TAG;
}
scanner.unread();
return null;
}
}


So, I guess if you change your code, replacing "\\n" with just "\n", it might works.

String allText = "";
for (int i=0;i<myArray.size();i++) {
allText = allText + "\n" + myArray.get(i);
}

permanent link
SEC Servizi (97123860) | answered May 23 '12, 10:56 a.m.
A little deeper, we can see this:


private static class LineDelimiterRule implements IInternalRule {

private static final String BREAK_TAG= "<br>"; //$NON-NLS-1$

public String evaluate(InternalScanner scanner) {
int ch= scanner.read();
if (ch == '\n')
return BREAK_TAG;
if (ch == '\r') {
ch= scanner.read();
if (ch != '\n')
scanner.unread();
return BREAK_TAG;
}
scanner.unread();
return null;
}
}


So, I guess if you change your code, replacing "\\n" with just "\n", it might works.

Thanks bello, your post was precious!

P.S. Where could we find the source code of those classes?

permanent link
Michele Pegoraro (1.8k14119103) | answered May 24 '12, 11:16 a.m.
Davide,
you can find them on SDK. In version 3.x they are provided by zip files but in version 2.x was provided by jar (search for those with .source.). You only have to unzip them.

Michele.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.