How can I sanitize text from a codename one textarea? - java

I have a text area in a form that is filled by the user. There are no constraints on the text area.
Because of the prepondernce of differnt types of mobile keyboards, I want to make sure that the text I get from the text area is sanitized. i.e. It should be stripped clean of any emoticons or hidden characters. It should only contain alphaumeric and punctuation characters.
What is the best way to do this in codename one? Thank you for your help.

This can be done using a regexp and there is no cleaner way. However, I believe you are approaching the problem sub-optimally.
You can detect the browser used by the user from the user agent string and based on that you can determine whether emoticons should be shown or not. Before you render the content, check whether emoticons should be shown. If not, then filter out unneeded characters. If yes, then show those emoticons.
Finally, I must mention that you must protect your database against SQL injection attempts or accidental bugs and you should make sure that XSS is not possible either.

Related

accessing androids auto correct facilities?

Is there a way of using Android's auto correct / predictive text capabilities with a bespoke input method? I'd like to be able to access a list of the nearest words to the word entered, similar to what happens when we send a text. For example if I entered the string "hapy" I would get a list containing "happy", harpy", "hazy" ...
Looks like a yes.
And the place to start: http://developer.android.com/guide/topics/text/spell-checker-framework.html
You wouldn't want to. First off, predictive text was only turned into a service with 4.0. Before then it was just part of the keyboard, and most keyboards still implement their own I suspect. Secondly, it would be optimized for typing mistakes, not voice mistakes. Typing g instead of f is common (they're next to each other), doing it by voice is not. It wouldn't work well.
But the built in voice to text behavior does return alternatives- it returns an array list of possible texts. That is your auto-correct.

Linebreaks and Spaces appearing in TextAreas

In our Cocoon environment we have a few forms with textareas. Once the user submits a form, an overview is displayed before the final submit is done.
Therefor, each form-object's data is stored in POJOs.
If the user is on that overview page and decides to go back to the form, the form is filled with the already submitted data read from the POJOs. However, when filling the textarea with data from the JavaObject, some linebreaks and whitespaces are added to the data.
I checked the POJO's data for these linebreaks but the String looks clean. Each whitespace entered by the user is of Character 32, which is a simple space.
I also checked the Serializer (we use a custom one that extends Cocoon's AbstractSerializer) but no linebreaks/whitespaces added by accident here.
When using Javascript to output the current content of that Textarea though, it contains linebreak characters ('\n') as well as the aforementioned additional whitespaces.
My suspicion is that the conversion from Java's Space-Character to HTML's space characters somehow fails.
These linebreaks appear instead of spaces, not inside a single word. They also change position depending on the textarea's size. They are not at the end of a line, so they can't be forced by wrap or something.
Example:
User input "test test test test test" becomes "test\n [36x Space] test test test test"
Here's a thought... What do you use to actually output the page to the client? I'm not entirely familiar with the Cocoon environment but I assume you're using some sort of a "templating" engine (JSP? Velocity?). I'm talking about the actual file, on the server side, that has the textarea element; paste here the snippet of code that involves the textarea element and we'll see.
These extra linebreaks and whitespaces are typical from XSL transformations (that were developed unaware of such linebreaks/whitespace issues).
It is likely that you use XSLT in your cocoon application, and maybe they should be checked on that matter.
There are a number of well-known cautions you can take. You can start on SO (XSLT - remove whitespace from template) to get an idea of these.

Showing Suggestions below text field taking values from database

I am having a form. I want to show user suggestions about the name of the security he can enter. For the same I want to use the values of securities that I have installed in my Oracle Database.
I want to do something like this, if user enters a, he should get all security names below the text field which starts from a, when user selects anyone of them, then the text field must show the selected values.
Thanks in Advance.
You need autocomplete enabled text field. There's a tutorial how to build one. Even better, use jQuery java script library for this and many other features.
I'd suggest a trie. They are great for things like predicive text and auto complete.
Get all the valid security names and put them into the trie, then query the trie with whatever prefix the use has entered and it will give you all the matching names.
Note that Java doesn't come with a Trie implementation itself buy it would be pretty easy to write your own or borrow one, for eg from here; http://forums.sun.com/thread.jspa?threadID=5295936
Edit: You didn't say if this was going to be on the web or desktop (eg Swing)....

list option in html from text files

I want to list options in drop down menu in HTML form but i need to make some options to be multiline .and the options i need to read them from text file.any ideas how to do that?
In order to have your HTML display values from a text file, you're going to need to use some kind of server-side generation (ASP.NET, JSP, CGI, whatever). The exact solution will depend on what server-side technology you use/choose, but in all cases it should be fairly straightforward as this is a common requirement for all of these technologies.
As for your first part, what exactly do you mean by "some options to be multiline"? Do you mean that some values are quite long and have spaces, so you want them to wrap? Do you want to have some kind of drop-down box that shows multiple values at once? Do you want them not to be a dropdown at all? How will you decide between the values that should be multiline and those that should not? If you can express your intention more clearly (which may involve some additional thinking on your end), it will be possible to help, and you may even end up working out the solution yourself.

Long text input from user and PDF generation

I have built a web application that can be seen as an overcomplicated application form. There are bunch of text areas with a given character limit. After the form submission various things happen and one of them is PDF generation.
The text is queried from the DB and inserted in the PDF template created in iReports. This works fine but the major pain is overflowing text.
The maximum number of characters is set based on 'average' text. But sometimes people prefer to write with CAPS or add plenty of linefeeds to format their text. These then cause user's text to overflow the space given in PDF. Unfortunately the PDF document must look like a real application form so I cannot allow unlimited space.
What kind of approaches you have used to tackle this?
Clean/restrict user input?
Calculate the space requirement of the text based on font metrics?
Provide preview of the PDF? (too bad users are not allowed to change their input after submission...)
Ideally, calculate the requirement based on metrics. I don't know how iReports handles text, but with iText, it lays everything out itself, you just present the data as a streaming document, so we don't worry about overflowing text.
However, iReport may not support that, or you may need to have the PDF layout fit within certain bounds. I'd try to clean the input (ie: if it's all caps, lowercase/sentence case/proper case it), strip extra whitespace. If cleaning the input can't be reliably done, or people are still getting past that, I'd also restrict it.
As a last resort, I'd present the PDF for the user to authorize. Really, users shouldn't be given more work to do, and they're not going to do it anyways.
Your own suggested solutions to your problem are all good. Probably the most important question to have answered is what should your PDF look like when the data to be displayed in a field won't fit? Do you ever need the "full answer" for anything else? When you know the answer to these, you'll have your options reduced.
For example if a field must be limited to 1/2 a page, and users sometimes enter more than 1/2 a page of text you can either
1) limit the user input - on submission calculate the size (using font-metrics as you said) and reject the submission until corrected. This assumes you can legitimately force the user to reduce their data entry.
2) accept the user input and truncate in the display of this report. Some systems use "..." to indicate data has been truncated, and can provide a hyperlink (even within the PDF) to get more information.
Providing a preview would work really well, but only if the users are good at checking and correcting and your system can handle the extra load this will generate.
Do you have control of the font that is used when generating the PDF? If so, I would look for a font in the Monospace family. This will give you consistent length for a given number of chars, regardless of puncuation, capitalization, etc.

Categories