Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How does Experts Exchange format their text?

Posted on 2004-11-01
13
Medium Priority
?
225 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:dfu23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
13 Comments
 
LVL 2

Expert Comment

by:tdisessa
ID: 12467984
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
 
LVL 14

Author Comment

by:dfu23
ID: 12472646
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
 
LVL 2

Expert Comment

by:tdisessa
ID: 12472870
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Author Comment

by:dfu23
ID: 12473340
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
 
LVL 2

Accepted Solution

by:
tdisessa earned 1000 total points
ID: 12473542
(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
 
LVL 14

Author Comment

by:dfu23
ID: 12475622
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
 
LVL 2

Expert Comment

by:tdisessa
ID: 12476076
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
 
LVL 14

Author Comment

by:dfu23
ID: 12476438
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
 
LVL 2

Expert Comment

by:tdisessa
ID: 12476720
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
 
LVL 14

Author Comment

by:dfu23
ID: 12494319
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
 
LVL 2

Expert Comment

by:tdisessa
ID: 12494371
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question