Processing newline character in function jquery.i18n.prop() - java

JQuery code is as follows:
alert(jQuery.i18n.prop('message.key'));
The value is specified in the properties file as:
message.key=value is after newline\nValue here
Following output is expected from javascript alert():
value is after newline
Value here
The actual output is:
value is after newline\nValue here
I tried different methods by changing value stored in properties file to:
message.key=value is after newline\\nValue here
message.key=value is after newline\u000DValue here
But it doesn't work. It displays "\\n" instead
What changes are required to be made to get the desired output?
EDIT: Following code gives desired output in javascript:
alert('value is after newline\nValue here')
But I need to use jquery.i18n.properties for localization

I'm pretty sure you can just go like this (the plugin supports multi-line properties):
message.key1=value is after newline
Value here
message.key2=next value

Related

How can I write a _FillValue parameter in a NetCDF CHAR Variable using Java?

I am trying to create a NetCDF file using java (unidata library). One of the requirements is to include the _FillValue attribute in all the Variables. I have one of type CHAR, and I can not do it.
The Attribute constructor only accepts Strings or numbers (or arrays of them), not chars. I have tried both of them anyway but the final netcdf does not show the attribute.
Other languages let you do it (we have seen this working in matlab), but I don't know how to do it using java.
I see in the documentation that the _FillValue should be of the same type of the Variable itself but Attribute values does not accept Chars, only String or Numbers
For example: When I try
Nc4Chunking chunker = Nc4ChunkingStrategy.factory(Nc4Chunking.Strategy.standard, 6, true);
NetcdfFileWriter dataFile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4_classic, fileName, chunker);
....
Variable varid_scdr = dataFile.addVariable(null, "SCDR", DataType.CHAR, dimsTMS15);
varid_scdr.addAttribute(new Attribute("_FillValue", " "));
....
dataFile.write(varid_scdr, scodData);
dataFile.close();
The resulting netcdf file has no _FillValue, it is not written in the file.
But if I change the attribute name and do this
varid_scdr.addAttribute(new Attribute("FillValue", " "));
the parameter is present in the output file
I have no problems with other data types or other attribute names. I am prety sure that the problem is about the attribute _FillValue for the variable of type Char. I dont know how to write it and I need the _FillValue attribute to be explicity present in the variable attribute list.
********* 5th July 2019 ***********
I realized that the problem is only related to netcdf4 and netcdf4_classic files. So perhaps is about chunking or something like that. If I try it creating netcdf3 files it workis.
Any help about this issue? what am I missing?
I think this is due to bug that has been addressed in the latest version of netcdf-java (v5.0.0). v5.0.0 has been released and is available for download; my hope is that the announcement will go out today.
If you want to be explicit about writing a CHAR valued attribute, one way to to it would be:
String fillValue = " ";
Array charArrayFillValue = ArrayChar.makeFromString(fillValue, 1);
charAttrFillValue = new Attribute("_FillValue", charArrayFillValue);
varid_scdr.addAttribute(charAttrFillValue)
another way would be:
String fillValue = " ";
Array charArrayFillValue = ArrayChar.makeFromString(fillValue, 1);
charAttrFillValue = new Attribute("_FillValue", DataType.CHAR);
charAttrFillValue.setValues(charArrayFillValue);
varid_scdr.addAttribute(charAttrFillValue)
Both of those are a bit verbose, though. I just checked using version 5, and your one liner works:
varid_scdr.addAttribute(new Attribute("_FillValue", " "));
However, if you try to pass in a value for _FillValue that isn't a string of length 1, the netCDF-C library will throw an error. So this:
varid_scdr.addAttribute(new Attribute("_FillValue", "ab"));
will result in:
-36 (NetCDF: Invalid argument) on attribute ':_FillValue = "ab"' on var varid_scdr
netCDF-Java will make sure the string you pass in gets converted to CHARs, but it won't truncate the resulting set of CHARs to fit into the single character limit on the _FillValue attribute.

How to replace a query string in an Apache Velocity template?

In my web application I'm trying to prevent users from inserting JavaScript in the freeText parameter when they're running a search.
To do this, I've written code in the header Velocity file to check whether the query string contains a parameter called freeText, and if so, use the replace method to replace the characters within the parameter value. However, when you load the page, it still displays the original query string - I'm unsure on how to replace the original query string with my new one which has the replaced characters.
This is my code:
#set($freeTextParameter = "$request.getParameter('freeText')")
freeTextParameter: $freeTextParameter
#if($freeTextParameter)
##Do the replacement:
#set($replacedQueryString = "$freeTextParameter.replace('confirm','replaced')")
replacedQueryString after doing the replace: $replacedQueryString
The query string now: $request.getQueryString()
The freeText parameter now: $request.getParameter('freeText')
#end
In the code above, the replacedQueryString variable has changed as expected (ie the replacement has been carried out as expected), but the $request.getQueryString() and $request.getParameter('freeText') are still the same as before, as if the replacement had never happened.
Seeing as there is a request.getParameter method which works fine for getting the parameters, I assumed there would be a request.setParameter method to do the same thing in reverse, but there isn't.
The Java String is an immutable object, which means that the replace() method will return an altered string, without changing the original one.
Since the parameters map given by the HttpServletRequest object cannot be modified, this approach doesn't work well if your templates rely on $request.getParameter('freeText').
Instead, if you rely on VelocityTools, then you can rather rely on $params.freeText in your templates. Then, you can tune your WEB-INF/tools.xml file to make this parameters map alterable:
<?xml version="1.0">
<tools>
<toolbox scope="request">
<tool key="params" readOnly="false"/>
...
</toolbox>
...
</tools>
(Version 2.0+ of the tools is required).
Then, in your header, you can do:
#set($params.freeText = params.freeText.replace('confirm','replaced'))
I managed to fix the issue myself - it turned out that there was another file (which gets called on every page) in which the $!request.getParameter('freeText')" variable is used. I have updated that file so that it uses the new $!replacedQueryString variable (ie the one with the JavaScript stripped out) instead of the existing "$!request.getParameter('freeText')" variable. This now prevents the JavaScript from being executed on every page.
So, this is the final working code in the header Velocity file:
#set($freeTextParameter = "$!m.request.httpRequest.getParameter('freeText')")
#if($freeTextParameter)
#set($replacedQueryString = "$freeTextParameter.replace('confirm','').replace('<','').replace('>','').replace('(','').replace(')','').replace(';','').replace('/','').replace('\"','').replace('&','').replace('+','').replace('script','').replace('prompt','').replace('*','').replace('.','')")
#end

get all values using get paremeters in java

I'm passing the some values url from flex to java example:
URL format:
../mahesh/initUser.do?method=fwdAccDetails&securityId=mUuB3/p/ky5JhZPY5T8Znf01YCcIarIalQiGEXPMMsOkWDX+KtT4fx2gMML+uup8
After I'm tiring to get "securityId" values in java like
request.getParameter("securityId")
But I'm getting following values only
mUuB3/p/ky5JhZPY5T8Znf01YCcIarIalQiGEXPMMsOkWDX KtT4fx2gMML uup8
symbol getting empty space in java side..
Here is my Flex code:
navigateToURL(new URLRequest('../mahesh/initUser.do?method=fwdAccDetails&securityId='+value+'),'_s‌​elf');
I didn't get full values.. any one can help me how I will get correct values in Java..
You should use the encodeURIComponent()-Function to properly encode your securityId.
value = encodeURIComponent(value);
navigateToURL(new URLRequest('../mahesh/initUser.do?method=fwdAccDetails&securityId='+value+'),'_s‌​elf');
That way your String will be correct on the Java side.
If you want to read more about proper escaping, have a look at When are you supposed to use escape instead of encodeURI / encodeURIComponent? (Same arguments apply for Flex and JavaScript).
i just resolve my issue for following code in a javURLDecoder.decode(param1AfterEncoding.replace("+", "%2B"), "UTF-8").replace("%2B", "+")
Now its working fine only.. i dint other special character will work fine.. i will check it later..

Remove unnecessary linebreaks on template output?

Using Play 2 I am realising a simple REST API, the output is plain text. My template looks like this:
#(items: Map[String,String])
#for((key, value) <- items) {
#value
#key
}
In the controller:
return ok(views.html.bla.render(itemsMap)).as("text/plain");
This gives the following output:
(empty line)
(empty line)
value
key
(empty line)
value
key
I want to get rid of the first 2 empty lines - is that possible?
Putting the for in the first line removes one of the empty lines at the top, however one still remains and for in the first line makes the template hard to read ): Thanks for any hint!
First off, if you use plain text, you should use txt templates (bla.scala.txt). They also automatically set text/plain; charset=utf-8 content type.
To trim the content, you can return the rendered content directly:
return ok(views.txt.bla.render(itemsMap).body().trim());
In case you want to render HTML content you'd need to change this manually:
return ok(views.html.ble.render().body().trim()).as("text/html; charset=utf-8");
If you are generating plain text output from a map, why do you use views at all? They don't provide any benefit in your case.
You can write the render function in pure Scala. Something like
items.map{ case (k,v) => v + '\n' + k}.mkString('\n')

How to handle newlines in a Struts form that relies on JavaScript

I have a Struts form which contains a Map:
private Map<Long, String> questionAnswers = new TreeMap<Long, String>();
I have the normal getter and setter for this variable (not shown here), and I also have the getter and setter required for Struts to work (using String/Object):
public Object getQuestionAnswer(String questionId) {
return getQuestionAnswers().get(questionId);
}
public void setQuestionAnswer(String questionId, Object answerText) {
String answer = (answerText == null) ? "" : answerText.toString();
getQuestionAnswers().put(Long.valueOf(questionId), answer);
}
In my JSP, I am dynamically generating the textareas that are used to enter the values for the map. This is all working fine. However, when the form is invalid, I need to dynamically generate the textareas again, and put the user text back into the textareas. I am currently repopulating the textareas like so:
<c:forEach items="${myForm.questionAnswers}" var="questionAnswer">
var textareaBoxName = "questionAnswer(" + '${questionAnswer.key}' + ")";
var textareaBox = document.getElementsByName(textareaBoxName)[0];
if (textareaBox) {
$('textarea[name=' + textareaBoxName + ']').val('${questionAnswer.value}');
}
</c:forEach>
This works fine, except if you enter a newline in the textarea. Then a JavaScript error complains about an "Unterminated string constant". I am guessing the newlines are being performed instead of just read.
In the setQuestionAnswer method, I put in some debugging and found that a newline entered in the textarea is being read as 2 characters, which converted into ints are 13 and 10 (which I believe are \r and \n). I tried replacing the the "\r\n" with just "\n" in the setQuestionAnswer method (using the String replaceAll method), but the same error occurred. I then tried replacing the "\r\n" with "%0A" (which I believe is the JavaScript newline). While this got rid of the JavaScript error, the textareas now have the "%0A" displayed instead of a newline. I tried all sorts of escaping and unescaping with no luck (note, I also want special characters to be preserved).
Does anyone have any idea on how to preserve newlines and special characters in the textarea boxes on invalid submits? I need this to work in IE. And I would like to avoid anything hacky like using some special character/string to "represent" a newline which I then replace in JavaScript, etc.
Since ${questionAnswer.value} is put inside a JavaScript String literal, you need to escape it as you would do if you wanted a newline in a JavaScript literal: the lines Hello and World must be written as 'Hello\nWorld'. Look at commons-lang StringEscapeUtils escapeECMAScript method. In iddition to escaping the newlines, it will also escape tabs, apostrophes, etc.
Make this method an EL method, and use it directly into your JSP:
$('textarea[name=' + textareaBoxName + ']').val('${myFn:escapeJs(questionAnswer.value)}');
You might also generate the text areas statically instead of generating them using JavaScript:

Categories

Resources