StringEscapeUtils is unescaping everything, except newlines (\n) - java

I try to escape and unescape large text using the StringEscapeUtils from Apache Commons library (v. 1.7) that will be stored and retrieved from a database, in this case a H2 database. Almost every special character is escaped and unescaped successfully except for the new line. I am using Spring Boot (v2.1.3.) with thymeleaf.
So for instance, if I try to store the following text:
He didn't say, "Stop!"
This is a new line!
It will store the text as:
He didn't say, \"Stop!\"\r\n\r\nThis is a new line!
Which is good. But when I unescape it with the unescapeJava method the new line character is not working correctly. I get
He didn't say, "Stop!" This is a new line!
Edit:
The unescapeJava method works when the text is displayed in a html textarea. But when it is rendered as plain html, the linebreak is not working.

The unescapeJava method works when the text is displayed in a html textarea. But when it is rendered as plain html, the linebreak is not working.
Please check your HTML source, it most likely is there
In HTML a newline in source does not introduce a newline on screen, if you want that you should replace newlines with for example <br/> tags.
See more at:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br

What #Martin van Wingerden says is correct. A newline in source does not introduce / render it on screen. I found this thread
Render a string in HTML and preserve spaces and linebreaks
which explains that you can use CSS white-space: pre-wrap that will preserve white spaces and line breaks in formatting.

Related

Forward Slashes in Strings Aren't Allowing HTML To Work Properly For JLabels

I'm trying to create a JLabel with a mixed fraction. This is the code in HTML for the JLabel.
<html>70 <span class="f"><div class="n">3</div><div class="d">5</div></span></html>
This HTML code prints out the following, (sorry i cant post images)
Although you cannot put this code into a string because strings don't allow for the " character without a forward slash in front of them.
So this makes me change it with the forward slashes in front of the " characters like this.
But now, the HTML code is interpreted with the forward slashes, and so the output of the HTML code now looks like this.

how can i use tabulator\whitespace in JTextPane using html?

I wish to use some tabspace in my JTextPane using html, how can i do it ?
I tried the Java way with putting \t, however that seems not to work.
this is my code:
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
String ausgabe = "hello world";
HTMLDocument doc=(HTMLDocument) pane.getStyledDocument();
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),"<b>"+ausgabe"+"<br></b>");
Generally, HTML does not display formatting such as indentation, tabs, etc. You can check that in a plain HTML file in a browser.
However, there is one circumstance where it does - in a preformatted block. That is, with the tag <pre>.
So try changing that last line to:
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),"<pre>"+ausgabe+"</pre>");
Note that the font used in preformatted blocks is usually fixed-width (e.g. Courier or a similar font).
As an alternative solution, you can also insert 4 times the character

Displaying tab in JSP page

I am trying to display a text file content in Body of the JSP Page.I used BufferedReader and StringBuilder for it. The file contents contains tab instead of spaces between words.While displaying ,the tab between words were displaying as single space between words in browser.
Thanks
Use one of following
1. <PRE></PRE> blocks.
2. CSS - using padding
3. four times
After reading from file and while generating html you can use one of these options.
for more options refer this -
HTML: Tab space instead of multiple non-breaking spaces ("nbsp")?

Android text to speech reads html tags

I have a strange one, I am using text to speech in my app which works perfectly apart from the fact that it reads out some of the Html code from my formatted string.
Example:
<string name="Aggression">
<![CDATA[
<p><b>Identifying Obsessive Behaviours</b></p>
]]>
</string>
When reading out the string it ignores "< p >" and "< / p >" but reads out the bold tags!
So my question is, any ideas to stop it reading out some html tags?
P.S Im using CDATA due to the length of some of the strings used and formatting issues.
Ok so I found a pretty amazing workaround. My goal was to still display perfectly formatted html style text which was easy to maintain yet have a text-to-speech engine read out the string for accessibility.
My TextView still used this to display the html formatted text:
contentTextView.setText(Html.fromHtml(content));
My text-to-speech function now uses this which strips all of the tags and headers out and only reads the bare text:
String editedTextReadable = android.text.Html.fromHtml(content).toString();

Remove last character from JTextPane

I'm writing a vt100 emulator and I'm using a JTextPane with a DefaultStyledDocument to display the formatted text. Now I want to implement the backspace, so i need to be able to remove the last character.
I tried the following:
doc.remove(doc.getEndPosition().getOffset()-1, doc.getEndPosition().getOffset());
But I keep getting a 'javax.swing.text.BadLocationException: Invalid remove'
How should this be done?
You're using the API wrong. The last parameter is the number of characters to remove which in your case should be 1.
Here is the API for Document.remove(int, int).

Categories

Resources