How do you replace htmlspecialchars simply? - java

input.replace("&", "&").replace("<", "<").replace(">", ">").replace(""", "\"");
im using this line of code as a sudo htmlspecialchars converter. with my application, it doesnt need to be too elaborate.
for some reason the above code does not replace("&", "&"), it doesnt seem to do anything.
any advice?
input = input.replaceAll("[^\\x20-\\x7e]", "");
i also tried this.

In android there is one class Html inside android.text.Html which you can use like this as below:
Html.fromHtml(any_html_text);
But this function will return Spanned object so use
Spanned spn=Html.fromHtml(any_html_text);
You can set this spanned text to any button or textview text Hope it will help you.
See this link also for your reference.

Instead of doing this manually, check out the Html class. Calling Html.fromHtml() should do the trick.

Trying to escape/unescape html by replacing strings yourself you might introduce security issues in your application (depending on what it does with the output). Either use Html.FromHtml or from Apache libs org.apache.commons.lang.StringEscapeUtils.unescapeHtml

This does the work -
public String cleanString(String dirty) {
return Html.fromHtml(dirty).toString();
}

Related

Convert Android view to PDF of A4 size

I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.
I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.
Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.
try this:
create a WebView and copy the text of your edittext in it:
webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");
and convert webview to pdf by below function:
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
if (printManager != null) {
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
after that user can select page size for example A4
There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.
The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.
- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).
check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2
and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591
- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.
They may be other solutions, but these are the popular ones.
Let Me know if it works for you and also if you're stuck. Thank you!

Get following sibling on android using Appium

driver.findElementByXPath("//android.widget.TextView[#text='a1492']/../following-sibling::android.view.ViewGroup").click();
This does not work. Am I missing anything?
#text is looking for an attribute text. If you're actually after the text content, it's [text()='a1492'].
You can try below, this will solve your problem:
driver.findElementByXPath("//android.view.ViewGroup[preceding-sibling::android.view.ViewGroup/*[#text='a1492']]").click();

Console. Write text with "link" to class/line

Whenever you display a stack trace, you can get a "url-like" text which if you click it opens the appropriate class at the appropriate line.
Is there a possibility to output a text in a way that the console recognizes it and make it clickable like that?
But how would you format the output so it would recognize it as a "link" ?
You can't and you don't.
AndroidStudio supports that feature. You just need to call
exception.printStackTrace() and you should be able to click it in the console tab of IDE.
You will see something like
java.lang.Exception
at whatever.Test.main(Test.java:22)
The text inside the bracket will be highlighted and you can click it.
Based on #JEeemy's answer I managed to do this
public static void printLinkToThisLine() {
System.out.println(Thread.currentThread().getStackTrace()[3]);
}
I works for me ...
I don't know if you're using Eclipse, but the Eclipse console parses based on a pattern: FileName.java:lineNumber.
MyFile.java:3
Would link you to the 3rd line of whatever class you specified as MyFile.
You could use:
.getClass().getName()
to the the name of the file programmatically.

Want to access attribute value

i need help regarding html parser i want to get the first attribute"href" value of tag "a" please resolve my problem.I want to fetch this link from the code http://myneta.info/gujarat2012/candidate.php?candidate_id=1591,
i am attaching the snap please see and provide some solution,
i have tried this code but not works for me -
String temp = source.getElementById("main").getFirstElementByClass("grid_9").getAttributeValue("a");
here is image
Please try the following:
source.getElementById("main").getFirstElementByClass("grid_9").getFirstElement("a").getAttributeValue("href")

jquery-mobile spinner loading

i tried to do that while js is creating a timetable... it shows spinner loading..
i tried to do this $.mobile.pageLoading(); $.mobile.pageLoading(true); and also tried to use this plugin http://contextllc.com/tools/jQuery-showLoading
The result is the same.. it shows spinner after he generate time table.. and i don't know why...here is where i tried to use it...
$('#timeDropList').change(function() {
$.mobile.pageLoading(false);
$('div.addedEntry').remove();
drawTemplate();
});
Are you using the right version of jQuery Mobile? Looks like they took away the pageLoading method in 1.0b1 in favor of two separate methods: showPageLoadingMsg and hidePageLoadingMsg. The last reference I see to pageLoading is in 1.0a4.1.
$('#home').live('pagebeforecreate',function(event){
alert('This page was just enhanced by jQuery Mobile!');
$.mobile.loadingMessage = "pagebeforeshow Page...";
$.mobile.showPageLoadingMsg();
});

Categories