Decode html parameter in cocoa - cocoa

I get the name of a file from a notification between one pluggin and my cocoa application. My problem is that I am receiving the file name like this: "My+file+name.png" instead of "My file name.png" (with spaces). I don't know how to decode this parameter in order to get the correct file name.
Any ideas? Thanks

Assuming that this is encoded as for a URL query string, you'll want to replace any plus signs with spaces and unescape the percent escape sequences.

I solved it in the javascript code from my pluggin. I added this function:
function decode(str) {
return unescape(str.replace(/\+/g, " "));
}
I called before passing the parameters to my cocoa application.

Related

Certain characters make moveItemAtURL:toURL:error: crash. how do I avoid them?

First, I'm using Swift. Second this line works fine in my code:
let didIt = fileManager.moveItemAtURL(originalFilePath, toURL: newFilePath, error: nil)
...as long as there are no special characters in the newFilePath. if the newFilePath has a dollar sign or an ampersand ($, & ) in it, the line fails. My issue is that the newFilePath comes from a text field in a window where the user can type any old thing. How do I escape special characters, or encode them so they will pass the test and be included in the new filename?
thanks in advance for any pointers.
My issue is that the newFilePath comes from a text field in a window where the user can type any old thing.
Right there is your problem. Why are you not using an NSSavePanel for letting the user select a name under which to save a file?
If you insist on taking input from a text field, the docs for -URLByAppendingPathComponent: specifically say that the path component string should be "in its original form (not URL encoded)" (emphasis mine).
How did you originally create newFilePath, before appending the path component? For example, you should have used one of the methods with "[fF]ileURL" in the name.

Jmeter - How Can I Replace a string and resend it?

I'm trying to create a script that will take a URL out of a response and send it out again.
Using the regular expression extractor I've succeeded in taking the wanted URL, but it holds "&" so naturally when sending it out the request fails.
Example:
GET http://[ia-test01.inner-active.mobi:8080/simpleM2M/ClientUpdateStatus?cn=WS2006&v=2_1_0-iOS-2_0_3_7&ci=99999&s=3852719769860497476&cip=113-170-93-111&po=642&re=1&lt=0&cc=VN&acp=&pcp=]/
I'm trying to replace the "&" with a "&".
I've tried: ${__javaScript(${url}.replace("&","&"))}
But it did not work. I've tried the regex function as well- the same.
I'm not sure the IP field in the request supports the us e of functions.
I'm currently trying to use the beanshell post-processor. But I'm pretty sure there is a simpler solution I'm missing.
Not sure what you're trying to get by replacing & with & however will try to respond.
First of all: given multiple & instances you need to use replaceall function, not replace
Second: replace / replaceall functions take a RegEx as parameter, so you'll need to escape your &
If you're trying to substitute URL Path in realtime, you'll need Beanshell Pre Processor, not the Post Processor
Sample Beanshell Pre-Processor code
import java.net.URL;
URL myURL = sampler.getUrl();
String path = myURL.getPath();
String path_replaced = path.replaceAll("\\&", "&");
vars.put("NEW_PATH", path_replaced);
After that put ${NEW_PATH} to "Path:" section of your HTTP Request.
Hope this helps.
Solution with less code:
Install the Custom JMeter Functions plugin
Use the following syntax
${__strReplace(ImAGoodBoy,Good,Bad,replaceVar)}
‘ImAGoodBoy’ is a string in which replacement will take place
‘Good’ is a substring to be replaced
‘Bad’ is the replacement string
‘replaceVar’ is a variable to save result string
Refer this URL for more info!
Thank a lot. However, i see from a recent experience that to replace a character that is actually a RegExp special character, like \ " ( ) etc, you need to put 3 backslashes and not 1, not 2. This is weird.
so you write
var res = str.replaceAll("\\\\u003c", "<");
to replace \u003c with <

C# MVC3 and non-latin characters

I have my database results (áéíóúàâêô...) and when I display any of this characters I get codes like:
á
My controller is like this:
ViewBag.EstadosDeAlma = (from e in db.EstadosDeAlma select e.Title).ToList();
My cshtml page is like this:
var data = '#foreach (dynamic item in ViewBag.EstadosDeAlma){ #(item + " ") }';
In addition, if I use any rich text editor as Tiny MCE all non-latin characters are like this too.
What should I do to avoid this problem?
What output encoding are you using on your web pages? I would suggest using UTF-8 since you want a lot of non-ascii characters to work.
I think you should HTML encode/decode the values before comparing them.
Since you are using jQuery you can take advantage of the encoding functions built-in into it. For example:
$('<div/>').html('& #225;gil').html()
gives you "ágil" (notice that I added an extra space between the & and the # so that stackoverflow does not encode it, you won't need it)
This other question has more information about this.
HTML-encoding lost when attribute read from input field

How do I replace carriage returns with <br /> using freemarker and spring?

I've got an internationalised app that uses spring and freemarker. I'm getting content from localised property files using.
${rc.getMessage("help.headings.frequently_asked_questions")}
For some of the content there are carriage returns in the property values. Because I'm displaying in a web page I'd like to replace these with .
What is the best way to do this?
Edit: looking closer it seems that I don't actually have carriage returns in the property files. The properties are coming back as single line strings.
Is there a better way to declare the properties so they know they are multi-line?
help.faq.answer.new_users=If you have not yet set a PIN, please enter your username and passcode (from your token) in the boxes provided and leave the PIN field blank.\
You will be taken through the steps to create a PIN the first time you log in.
Cheers,
Pete
${springMacroRequestContext.getMessage("help.headings.frequently_asked_questions", [], "", false)?html?replace("\n", "<br>")}
To handle CR + LF (carriage return + line feed) line endings, as well as just LF do this:
<#escape x as x?html?replace("\\r?\\n","<br />",'r')>...</#escape>
<#escape x as x?html?replace('\n', '<br>')>...</#escape>
works just fine.
If you want this to be the default behaviour, consider writing a custom TemplateLoader as suggested in this blog: http://watchitlater.com/blog/2011/10/default-html-escape-using-freemarker/.
As to the
Is there a better way to declare the properties so they know they are multi-line?
part of your question, maybe this helps: you can include line terminator characters in your property values by using the \r and \n escape sequences, like it is explained in the API documentation of java.util.Properties#load(java.io.Reader).
I would recommend writing a custom directive for it (see freemarker.template.TemplateDirectiveModel), so in your templates you can write something like <#my.textAsHtml springMacroRequestContext.getMessage(...) />. It's important that this is a directive, not function, so it works properly inside <#escape x as x?html>...</#escape>. Otherwise it would be double-escaped. Using a directive can also give the highest performance, as you can directly send the output to the output Writer, rather than building a String first.

Printing printing variable in single quoted string Ruby

I am attempting to send a tweet to twitter using the twitter_oauth gem with the following code:
client.update('.# #{tweeter}, have a nice day!')
Because of the single quotes I cannot get the variable to display but the tweet will not send if single quote are not used. Does anyone have any suggestions as to how to get this to work? thanks
Just replace the ' with ", single quoted strings don't do variable substitution and the other neat things of double quoted strings. They exist because of those missing features they are faster to parse.
If the tweet doesn't work despite using " then the problem is likely that the variable tweeter contains characters that are not allowed or in some other way invalid (maybe requiring some sort of escaping, e.g. URL or XML escaping).
Have you tried the old, java-esque way:
client.update('.# ' + tweeter + ', have a nice day!')
Or using a temporary variable:
message = ".# #{tweeter}, have a nice day!"
client.update(message)

Resources