How does Experts Exchange format their text?

Meaning that, right now I am entering text into a textbox (textarea) and I put in some simple formatting in here ... nothing rich text though.

Just some line breaks ....

    - And some spaces for maybe code or example text

All I want to do is get the same simple HTML formatting into my pages ... I am using JSP to do the dynamic input and output for my pages and so far I am saving the results to the database without any problem. I am just looking for the best way to format it on the way back out ... or is it best to save the initial input as the formatted HTML that I want so I don't need to perform an action on the text every time I pull it out from the DB?

So far I am thinking of just doing a simple replace of a carriage return (character 32?) with a "<br>" and I think that I will look for instaces of 2 spaces in a row and replace that with a "&nbsp;" ... regular expression ... I think.

What are your thoughts? Code snippets in Java?
LVL 14
dfu23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tdisessaCommented:
Do you want save multiple spaces as sequential &nbsp;s?

Also, how do you have your textarea's configured?  Some paramters make a big difference in perserving CRLFs.
0
dfu23Author Commented:
Moving the question to the Community Support to be answered by the Engineering team would be fine ...

tdisessa:

This input it being saved into an Oracle DB from a web form/textarea. Right now I only really have to worry about IE as the primary browser but I would like it to work fine with any browsers input. And to your first question my answer is yes. I want to find the places where there are two or more spaces together and replace them with the same number of &nbsp;'s. At least that is the thought for now ... any better suggestions are welcome.
0
tdisessaCommented:
1 to many spaces in HTML text will always be displayed one space (duh!).  A problem with regular expression are that they are "single-pass".

So, consider the following line:

Beginning onespace onespaceagain  twospaces    fourspaces     fivespaces

We can't search for "  "(two spaces) and recursively consume and match each space until we get something like this:

Beginning onespace onespaceagain&nbsp; twospaces&nbsp;&nbsp;&nbsp; fourspaces&nbsp;&nbsp;&nbsp;&nbsp; fivespaces

But, we can be tricky with one pass and still retain the spacing.  Consider this substitution phrase:

/  / \&nbsp;/g

on the original line, we would get:

Beginning onespace onespaceagain &nbsp;twospaces &nbsp; &nbsp;fourspaces &nbsp; &nbsp; fivespaces

Which would exactly retain the initial spacing used in our original example(and would save on some space over the recursively
consuming example) when displayed in HTML.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

dfu23Author Commented:
ok, so let's say that I have the input from the textarea stored in a String:

String strInput = request.getParameter("txtMessage");

How would I then run the regular expression you suggested against this string?
0
tdisessaCommented:
(The following exmaple is in java 1.4)

      String strInput = request.getParameter("txtMessage");
        java.util.regex.Pattern p = java.util.regex.Pattern.compile("  ");  // Two spaces
        java.util.regex.Matcher m = p.matcher(strInput);  // Apply regular expression to string
        String newReplacedString = m.replaceAll(" &nbsp;");  //  Replace all applicable matches
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dfu23Author Commented:
Very cool,

I really like the thinking on saving space and doing:

 &nbsp; &nbsp;

Instead of:

&nbsp;&nbsp;&nbsp;&nbsp;

I have had success now with getting the basic formatting of what I wanted with the following:

java.util.regex.Pattern p1 = java.util.regex.Pattern.compile("  ");
java.util.regex.Matcher m1 = p1.matcher(message.getMessage());
String strFormattedMessage = m1.replaceAll(" &nbsp;");
java.util.regex.Pattern p2 = java.util.regex.Pattern.compile("[\n]");
java.util.regex.Matcher m2 = p2.matcher(strFormattedMessage);

Where message.getMessage() retrieves the message from the DB for the matcher in the first instance and then a second pattern matcher is used to find new lines. I then spit out the results later in the page like this:

m2.replaceAll("<br/>");

Any suggestions on improving this? Also, I'm not handling characters yet that could mess with the HTML code ... do you know of any easy ways to replace these instances too?

BTW - I'm increasing the points because you have been a great help to me and feel that you deserve more :)
0
tdisessaCommented:
Your solution for newlines is pretty much right on.  My only critique is you can use the pattern "\n" instead of
"[\n]".  The brackets match any character in a specified range.  In your code, your specified range is just "\n",
so it will only match "\n".

Check out: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

Now, for handling characters that would mess up HTML, first of all, don't forget characters that will mess up SQL.
Second, I can't think of a way to do all the HTML special character substitions in regular expression.

But, in regards to SQL special characters, spaces and HTML special characters, you probably don't want to save
the "&nbsp;"s and other HTML special characters in your database (like I said before about saving space).  Just
do the translations after you pull the data out of your database.  But, beware of special characters that might mess
up your SQL statements.  Escape special SQL characters and try to save all of the data from the page in the same
format of as what was typed in.  That way, if a comment includes "&quot;", you will save "&quot" in the database
and then when you output it to HTML, you'll output "&amp;quot;".

(Check the HTML that EE created for my comment).

Here is a summary in pseudo-code:

1.  Read input and save as string
2.  Escape all SQL characters
3.  Save to database

Then, when requested:

1.  Read string from database (assuming you escaped the SQL chars correctly, this string should match from the
step 1 above)
2.  Escape all HTML characters, with a series of regular expressions
   a.  You might even want to encapsulate those three statements into one method like:
          String substitute(String inputString, String expressionString, String replacementString);
3.  Spit it out into HTML
0
dfu23Author Commented:
I think that I have the trouble covered of handling special SQL characters because I am using a stored procedure to interact with Oracle on saving and retrieving the data. Let me know if you know differently on that subject ...

From past experience with server-side web dev tools I seen methods built in, like HTMLEncode, which will take a string and replace the special characters with what is needed to display appropriately on a web page. Do you know if such a thing is available in JSP?
0
tdisessaCommented:
Stored procedures work fine.  I was just trying to be precautious (I've spent a few nights tracking down
little problems like this).

Try this for HTML Encoding:

http://www.mmbase.org/development/api/1.7/org/mmbase/util/Encode.html



0
dfu23Author Commented:
tdisessa,

Thanks very much for all of your help. With a little further searching I found a nice little peice of code from an earlier post here in the Java Topic Area that will replace any special character in a string with its equivilant. If you are interested here is a link:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21114291.html

So basically I do that first ... replace special characters.
Then I do another sweep with the regular expression to replace two spaces with a space and a &nbsp;
Then I do another sweep with the regular expression to replace newlines with a <br/>
Then I do another sweep with the regular expression to replace tabs with a space a &nbsp; a space a &nbsp; and a space (5 spaces)

I am very pleased with the results.
0
tdisessaCommented:
Cool!  I'm glad I could help.

I was thinking about the tab replacement.  I'm glad you found a solution to that, also.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.