HTML structure varying on input - java

I have a string which contains dynamic HTML. The HTML can contain static image, maps, texts, links, etc. You can take a look at this link.
The answer to this question is working when I am having text and links (a href). But, if the html contains images or maps, its malfunctioning and the html is not getting generated as expected.
The methods which I have created to do the job are:
private void createHtmlWeb(){
String listOfElements = "null"; // normally found if
// webTextcontains.maps.google.com
Toast.makeText(getApplicationContext(), "" + mainEditText.getHeight(), Toast.LENGTH_SHORT).show();
ParseObject postObject = new ParseObject("Post");
Spannable s = mainEditText.getText();
String webText = Html.toHtml(s);
webText = webText.replaceAll("(</?(?:b|i|u)>)\\1+", "$1").replaceAll("</(b|i|u)><\\1>", "");
// Logic to add center tag before image
// Document doc = Jsoup.parse(webText);
// Elements imgs = doc.select("img");
// for (Element img : imgs) {
// img.attr("src", "images/" + img.attr("src")); // or whatever
// }
//
// doc.outerHtml(); // returns the modified HTML
//Determine link and favourite types to add favourite a class around it.
// Determine link and favourite types to add favourite a class around
// it.
if (webText.contains("a href")) {
String favourite = "favourite";
// Parse it into jsoup
Document doc = Jsoup.parse(webText);
// Create an array to tackle every type individually as wrap can
// affect whole body types otherwises.
Element[] array = new Element[doc.select("a").size()];
for (int i = 0; i < doc.select("a").size(); i++) {
if (doc.select("a").get(i) != null) {
array[i] = doc.select("a").get(i);
}
}
for (int i = 0; i < array.length; i++) {
// we don't want to wrap link types. Common part links have is
// http. Should update for somethng more secure.
if (array[i].toString().contains("http") == false) {
// wrapping inner href with a tag attributes
Elements link = doc.select("a");
String linkHref = link.attr("href");
Log.e("linkHref",linkHref);
array[i] = array[i].wrap("<a class=" + favourite + " href='"+linkHref+"'></a>");
}
}
// Log.e("From doc.body html *************** ", " " + doc.body());
Element element = doc.body();
Log.e("From element html *************** ", " " + element.html());
//changes to update html ahref
String currentHtml = element.html();
String newHtml = currentHtml.substring(0,currentHtml.indexOf("<a href")+1)+currentHtml.substring(currentHtml.indexOf("font"),currentHtml.indexOf("</a>"))+currentHtml.substring(currentHtml.indexOf("</a>")+4,currentHtml.length());
listOfElements = newHtml;
//refactoring html
listOfElements = wrapImgWithCenter(listOfElements);
//listOfElements = element.html();
}
// First need to do a check of the code if iti s a google maps image
if (webText.contains("maps.google.com")) {
Document doc = Jsoup.parse(webText); // Parse it into jsoup
for (int i = 0; i < doc.select("img").size(); i++) {
if (doc.select("img").get(i).toString().contains("maps.google.com")) {
// Get all numbers + full stops + get all numbers
Pattern noImage = Pattern.compile("(\\-?\\d+(\\.\\d+)?),(\\-?\\d+(\\.\\d+))+%7C(\\-?\\d+(\\.\\d+)?),(\\-?\\d+(\\.\\d+))");
// Gets the URL SRC basically.. almost.. lets try it
Matcher matcherer = noImage.matcher(doc.select("img").get(i).toString());
// Have two options - multi route or single route
if (matcherer.find() == true) {
for (int j = 0; j < matcherer.groupCount(); j++) {
latitude_to = Double.parseDouble(matcherer.group(1));
longitude_to = Double.parseDouble(matcherer.group(3));
latitude_from = Double.parseDouble(matcherer.group(5));
longitude_from = Double.parseDouble(matcherer.group(7));
}
String coOrds = "" + latitude_to + "," + longitude_to + "," + latitude_from + "," + longitude_from;
Element ele = doc.body();
ele.select("img").get(i).wrap("");
listOfElements = ele.html();
listOfElements = listOfElements.replace("&", "&");
} else if (matcherer.find() == false) {
noImage = Pattern.compile("(\\-?\\d+(\\.\\d+)?),\\s*(\\-?\\d+(\\.\\d+)?)");
matcherer = noImage.matcher(doc.select("img").get(i).toString());
Toast.makeText(getApplicationContext(), "Regex Count:" + matcherer.groupCount(), Toast.LENGTH_LONG).show();
if (matcherer.find()) {
for (int j = 0; j < matcherer.groupCount(); j++) {
latitude = Double.parseDouble(matcherer.group(1));
parseGeoPoint.setLatitude(latitude);
longitude = Double.parseDouble(matcherer.group(3));
parseGeoPoint.setLongitude(longitude);
}
}
String coOrds = "" + latitude + "," + longitude;
Element ele = doc.body();
ele.select("img").get(i).wrap("");
listOfElements = ele.html();
listOfElements = listOfElements.replace("&", "&");
}
} else {
// standard photo
Element ele = doc.body();
ele.select("img").get(i);
listOfElements = ele.html();
}
}
Log.e("listOfElements", listOfElements);
//refactoring html
listOfElements = wrapImgWithCenter(listOfElements);
// Put new value in htmlContent
postObject.put("htmlContent", listOfElements);
} else {
//refactoring html
webText = wrapImgWithCenter(webText);
postObject.put("htmlContent", webText);
}
mainEditText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout(){
// TODO Auto-generated method stub
Rect r = new Rect();
mainEditText.getWindowVisibleDisplayFrame(r);
// int screenHeight = mainEditText.getRootView().getHeight();
// int heightDifference = screenHeight - (r.bottom - r.top);
}
});
// See if a trip exists
if (finalTrip != null) {
}
// Want to put the location in the location section
// if parsegeoPoint != null -- old information
if (latitude != -10000 && longitude != -10000) {
// Toast.makeText(getApplicationContext(),
// "Adding in location co-ods: " + latitude + " : " + longitude ,
// Toast.LENGTH_SHORT).show();
postObject.put("location", parseGeoPoint);
}
postObject.put("type", Post.PostType.HTML.getPostVal());
postObject.put("user", ParseObject.createWithoutData("_User", user.getObjectId()));
// Transfer these details
Intent i = new Intent(getApplicationContext(), WriteStoryAnimation.class);
i.putExtra("listOfElements", listOfElements);
i.putExtra("webText", webText);
i.putExtra("finalTrip", finalTrip);
i.putExtra("latitude", latitude);
i.putExtra("longitude", longitude);
if(mainEditText.length() > 0){
finish();
//Conflict was here from html merge.
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Your story is empty", Toast.LENGTH_SHORT).show();
}
// finish();
// Toast.makeText(getApplicationContext(), "EditText Sie: " + height +
// " : " + desiredHeight, Toast.LENGTH_LONG).show();
}
// method to refactor html
public String wrapImgWithCenter(String html){
Document doc = Jsoup.parse(html);
//adding center tag before images
doc.select("img").wrap("<center></center>");
//adding gap after last p tag
for (int i =0; i<= 1; i++) {
doc.select("p").last().after("<br>");
}
Log.e("Wrapping", doc.html());
return doc.html();
}
You have to read the question in the link to understand the input and the output.
Other output with image and links for your reference:
<html>
<head></head>
<body>
<p dir="ltr">
<center>
<img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-9fca384a-2f7b-4632-a585-65c78f40842a-file" />
</center><br /> <font color="#009a49">Rohit Lalwani</font><br /> <a href="45.5033204,-99.8865083">
<center>
<img src="http://maps.google.com/maps/api/staticmap?center=45.5033204,-99.8865083&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C45.5033204,-99.8865083" />
</center></a><br /> </p>
<br />
<br />
</body>
</html>
There you can see that the class="favourite" in the href tag is missing. This is what I need to rectify. Please suggest me what to do.

Reading your original question I see that you can achieve what you want this way:
You have an anchor (a.favorite)
You have to pick his grandchild (font in this particular case, but it could be an img or whatever)
You delete the children of the original anchor
and then you append the grandchildren as a new child!.
This may sound complicated but it is very easy, here you are a code example:
String html ="<a class=\"favourite\" href=\"LixWQfueLU\"><font color=\"#009a49\">Rohit Lalwani</font></a>";
Document doc = Jsoup.parse(html);
//The original anchor
Element afav = doc.select(".favourite").first();
//The grandchild
Element select = doc.select("font").first();
afav.remove();
afav.appendChild(select);
System.out.println(afav);
Output:
<a class="favourite" href="LixWQfueLU"><font color="#009a49">Rohit Lalwani</font></a>
Hope it helps!

Related

Going to next activity once the list is complete

So I am working on a project and I don't know much about the android studio. So what I am doing is, I am using jsoup as my web parser and I am creating a list of products from e-commerce websites. So I want that once I get all the details from the parser, then my next activity starts. Till now I am using handler and setting some delay time in it. But that is not helping because sometimes it takes more than 3-4 seconds.
Below is the code by which I am sending the allproduct ArrayList and producturl ArrayList
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST", (Serializable) allproducts);
args.putSerializable("URLLINKS", (Serializable) producturl);
intent.putExtra("BUNDLE", args);
intent.putExtra(EXTRA_TEXT, searchtext);
startActivity(intent);
}
}, 6500);
Sometimes the listview shows only 2 websites result and sometimes it shows every website.
private class Snapdeal extends AsyncTask<String, Void, ArrayList<String>> {
ArrayList<String> tempurlstore = new ArrayList<>();
String link;
#Override
protected void onPostExecute(ArrayList<String> s) {
String product;
String urlstore;
super.onPostExecute(s);
for (int j = 0; j < 6; j++) {
product = s.get(j);
urlstore = tempurlstore.get(j);
allproducts.add(product);
producturl.add(urlstore);
}
String seemore = "See more products on website....";
allproducts.add(seemore);
producturl.add(link);
}
#Override
protected ArrayList<String> doInBackground(String... strings) {
try {
Document doc = Jsoup.connect(strings[0]).get();
Elements links = doc.getElementsByClass("col-xs-6 favDp product-tuple-listing js-tuple ");
ArrayList<String> mainlist = new ArrayList<String>();
mainlist.add("SNAPDEAL");
link = strings[0];
tempurlstore.add("https://www.snapdeal.com");
for (Element link : links) {
String temp1 = null, temp2 = null, temp3 = null, temp4 = null, temp5 = null;
String permanent1 = null;
Elements elLink = link.getElementsByTag("a");
Elements eltitle = link.getElementsByClass("product-title"); //for product title
Elements elpricebefore = link.getElementsByClass("lfloat product-desc-price strike ");
Elements elpriceafter = link.getElementsByClass("lfloat product-price");
Elements discount = link.getElementsByClass("product-discount");
//product title loop
for (Element titleOfProduct : eltitle) {
temp1 = "Title: " + titleOfProduct.text();
}
//product original price loop
for (Element priceOfProductBefore : elpricebefore) {
temp2 = "Price before: " + priceOfProductBefore.text();
}
//product discounted price loop
for (Element priceOfProductAfter : elpriceafter) {
temp3 = "Discounted price: " + priceOfProductAfter.text();
}
//discount in number loop
for (Element productdiscount : discount) {
temp4 = "Discount: " + productdiscount.text();
}
ArrayList<String> linkArray = new ArrayList<String>();
for (Element elementLink : elLink) {
String MainLink = elementLink.attr("href");
linkArray.add(MainLink);
}
for (int j = 0; j < 1; j++) {
temp5 = linkArray.get(0);
}
if (elpricebefore.text()==null)
{
permanent1 = "\n" + temp1 + "\n" + "Price :" + elpriceafter.text() + "\n" + temp4 + "\n";
}
else
{
permanent1 ="\n" + temp1 + "\n" + temp2 + "\n" + temp3 + "\n" + temp4 + "\n";
}
mainlist.add(permanent1);
tempurlstore.add(temp5);
}
return mainlist;
} catch (Exception e) {
ArrayList<String> exception = new ArrayList<String>();
String ex = e.toString();
exception.add(ex);
return exception;
}
}
}
Above is some part of my project. So this is how I am taking all the products and all from the parser.
So how can I do this that once all the products are ready in the Arraylist then only intent gets started?
Please help. Thank you.
Why are you using a timer instead of a callback from your parser? This is creating a race condition which is at the heart of your issue. The only way around it is to fire your intent only after your parser is complete.
Without seeing the parser code it is tough to know the exact place to add your hook.
You shouldn't use a handler for that. What you could do is use an Observer or use Android's Live Data so the one you choose can let you know when the list is ready so you can start the activity.

How to put a string with HTML tags into an array or list?

I have a string that has some html tags in it and has multiple data in one string. I need to check if an element on the UI is present in that string.
I was able to remove the tags but I am not sure how convert the string into an array or list so that it'll be easier to compare.
For example, the string coming from the database is:
<dl style="float: left; text-align: left; width: 50%;">
<dt>
Note1amp;M
</dt>
<dd>
- This is an example
</dd>
<dt>
Note2
</dt>
<dd>
- Example 2
</dd>
<dt>
Note 3
</dt>
<dd>
- This is example 3
</dd>
The text from the ui is
Note1 - This is an example
where Note1 is one element
This is an example is another element
So far what I have to remove tags and try to put in a list
public String[] verifyData(Strint txtFromDB) {
String[] txt = new String[3];
boolean compareValue1 = false, compareValue2 = false;
boolean boolBack = false;
WebElement abbreviation = driver.findElement(By.xpath(itemLocatorP1));
WebElement fullName = driver.findElement(By.xpath(itemLocatorP2));
String p1, p2;
if((abbreviation.isDisplayed()) && (fullName.isDisplayed())) {
try {
getMenu().scroll_To_View_Web_Element(itemLocatorP1);
p1 = getUITxt(itemLocatorP1); // getting a text from the UI;
getMenu().scroll_To_View_Web_Element(itemLocatorP2);
p2 = getUITxt(itemLocatorP2); // getting the second part text from the UI:
txt[0] = p1; // Note 1
txt[1] = p2; // - This is an example
System.out.println("Array txt -> " + txt[0]);
}
catch(Exception e) {
txt[0] = "Blank";
System.out.println("Array txt Exception-> " + txt[0]);
}
// removing some html txt from the txtFromDB so that it can match with the UI
txtFromDB = txtFromDB.replaceAll("<dt>", "");
txtFromDB = txtFromDB.replaceAll("</dt>", "");
txtFromDB = txtFromDB.replaceAll("<dd>", "");
txtFromDB = txtFromDB.replaceAll("</dd>", "");
txtFromDB = txtFromDB.replaceAll(">", "");
txtFromDB = txtFromDB.replaceAll("</dl>", "");
txtFromDB = txtFromDB.replaceAll("</dl", "");
txtFromDB = txtFromDB.replaceAll("<dl style=", "");
txtFromDB = txtFromDB.replaceAll("float: left; text-align: left; width: 50%;", "");
txtFromDB = txtFromDB.replaceAll("\"\"", "");
txtFromDB = txtFromDB.replaceAll(" ", " ");
txtFromDB = txtFromDB.replaceAll("amp;", "");
txtFromDB = txtFromDB.replaceAll(" ", " ");
txtFromDB = txtFromDB.replaceAll("’s", "’s");
txtFromDB = txtFromDB.replaceAll("–", "–");
txtFromDB = txtFromDB.replaceAll("(?m)^[ \t]*\r?\n", "");
System.out.println("DB Txt -> " + txtFromDB);
String[] temp = txtFromDB.split("\\n");
for(String x : temp) {
System.out.println(x);
if((x.contains(txt[0])) && (x.contains(txt[1]))) {
System.out.println(x + " from DB matches the UI -> " + txt[0] + txt[1]);
compareValue1 = true;
break;
}
else {
System.out.println("Still Searching.....");
}
}
if(compareValue1 )
boolBack = true;
else
boolBack = false;
}
else {
System.out.println("No such element was found in the page");
txt[0] = "Blank";
boolBack = false;
}
txt[2] = Boolean.toString(boolBack);
return txt;
}
So what I want to do is put <dt>Note1</dt> and <dd>-This is an example</dd> as one string like: Note 1 - This is an example in a list or array so that I can compare with any data on the UI.
With JSoup
Instead of parsing it yourself you could look into using JSoup.
https://en.wikipedia.org/wiki/Jsoup
With JSoup you can remove all the html and get the text by:
String html = "<p>example</p>";
Document doc = Jsoup.parse(html);
System.out.println(doc.text()); // doc.text() returns the text only from the html
This would output:
example
With JSoup you can also find elements with a certain id to separate them easier.
String html = "<dt>example</dt>";
Document doc = Jsoup.parse(html);
Elements dts = doc.getElementsByClass("dt");
Kind of figured it out, split the string based on double lines, then stored that in a list, and checked if the element I need to verify contains in that list
public void verifyEachCommonAcronymsAndAbbreviationsForAPB(String itemLocatorP1, String itemLocatorP2, String txtFromDB) {
String[] txt = new String[3];
boolean compareValue1 = false, compareValue2 = false;
WebElement abbreviation = driver.findElement(By.xpath(itemLocatorP1));
WebElement fullName = driver.findElement(By.xpath(itemLocatorP2));
String p1, p2;
if((abbreviation.isDisplayed()) && (fullName.isDisplayed())) {
try {
getMenu().scroll_To_View_Web_Element(itemLocatorP1);
p1 = getUITxt(itemLocatorP1);
getMenu().scroll_To_View_Web_Element(itemLocatorP2);
p2 = getUITxt(itemLocatorP2);
txt[0] = p1;
txt[1] = p2;
}
catch(Exception e) {
txt[0] = "Blank";
System.out.println("Array txt Exception-> " + txt[0]);
}
// removing some html txt from the txtFromDB so that it can match with the UI
txtFromDB = txtFromDB.replaceAll("<dt>", "");
txtFromDB = txtFromDB.replaceAll("</dt>", "");
txtFromDB = txtFromDB.replaceAll("<dd>", "");
txtFromDB = txtFromDB.replaceAll("</dd>", "");
txtFromDB = txtFromDB.replaceAll(">", "");
txtFromDB = txtFromDB.replaceAll("</dl>", "");
txtFromDB = txtFromDB.replaceAll("</dl", "");
txtFromDB = txtFromDB.replaceAll("<dl style=", "");
txtFromDB = txtFromDB.replaceAll("float: left; text-align: left; width: 50%;", "");
txtFromDB = txtFromDB.replaceAll("\"\"", "");
txtFromDB = txtFromDB.replaceAll(" ", " ");
txtFromDB = txtFromDB.replaceAll("amp;", "");
txtFromDB = txtFromDB.replaceAll(" ", " ");
txtFromDB = txtFromDB.replaceAll("’s", "’s");
txtFromDB = txtFromDB.replaceAll("–", "–");
txtFromDB = txtFromDB.replaceAll("(?m)^[ \t]*\r?\n", "");
//System.out.println("DB Txt -> " + txtFromDB);
String[] splitArrDB = txtFromDB.split("\\n");
List<String> acronymsList = new ArrayList<>();
for(int i = 0 ; i < splitArrDB.length; i++) {
acronymsList.add(splitArrDB[i] + splitArrDB[i]);
}
for(String temp : acronymsList) {
if((temp.contains(txt[0]))) {
System.out.println("Found " + txt[0] + " in the list");
compareValue1 = true;
break;
}
//System.out.println("still searching.....");
}
for(String x : acronymsList) {
if((x.contains(txt[1]))) {
System.out.println("Found " + txt[1] + " in the list");
compareValue1 = true;
break;
}
//System.out.println("still searching.....");
}

How to split URL?

This is my code to split URL, but that code have problem. All link appear with double word, example www.utem.edu.my/portal/portal . the words /portal/portal always double in any link appear. Any suggestion to me extract links in the webpage?
public String crawlURL(String strUrl) {
String results = ""; // For return
String protocol = "http://";
// Assigns the input to the inURL variable and checks to add http
String inURL = strUrl;
if (!inURL.toLowerCase().contains("http://".toLowerCase()) &&
!inURL.toLowerCase().contains("https://".toLowerCase())) {
inURL = protocol + inURL;
}
// Pulls URL contents from the web
String contectURL = pullURL(inURL);
if (contectURL == "") { // If it fails, then try with https
protocol = "https://";
inURL = protocol + inURL.split("http://")[1];
contectURL = pullURL(inURL);
}
// Declares some variables to be used inside loop
String aTagAttr = "";
String href = "";
String msg = "";
// Finds A tag and stores its href value into output var
String bodyTag = contectURL.split("<body")[1]; // Find 1st <body>
String[] aTags = bodyTag.split(">"); // Splits on every tag
//To show link different from one another
int index = 0;
for (String s: aTags) {
// Process only if it is A tag and contains href
if (s.toLowerCase().contains("<a") && s.toLowerCase().contains("href")) {
aTagAttr = s.split("href")[1]; // Split on href
// Split on space if it contains it
if (aTagAttr.toLowerCase().contains("\\s"))
aTagAttr = aTagAttr.split("\\s")[2];
// Splits on the link and deals with " or ' quotes
href = aTagAttr.split(((aTagAttr.toLowerCase().contains("\""))? "\"" : "\'"))[1];
if (!results.toLowerCase().contains(href))
//results += "~~~ " + href + "\r\n";
/*
* Last touches to URl before display
* Adds http(s):// if not exist
* Adds base url if not exist
*/
if(results.toLowerCase().indexOf("about") != -1) {
//Contains 'about'
}
if (!href.toLowerCase().contains("http://") && !href.toLowerCase().contains("https://")) {
// http:// + baseURL + href
if (!href.toLowerCase().contains(inURL.split("://")[1]))
href = protocol + inURL.split("://")[1] + href;
else
href = protocol + href;
}
System.out.println(href);//debug
consider to use the URL class...
Use it as suggested by the documentation :
)
public static void main(String[] args) throws Exception {
URL aURL = new URL("http://example.com:80/docs/books/tutorial"
+ "/index.html?name=networking#DOWNLOADING");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
}
the output:
protocol = http
authority = example.com:80
host = example.com
port = 80
etc
after this you can take the elements you need an create a new one string/URL :)

Changing my own tags to data from XML

I have a text editor where user can put text like this:
[[PAYMENTS_N]] You have to pay [[amount]]$ before [[date]].[[/PAYMENTS_N]]
[[PAYMENTS_Z]] You didn't pay [[debt]]$ on time: [[ddate]].[[/PAYMENTS_Z]]
And there are XML files that look like this:
<DATA_NZ><USER><ID>12345</ID><COMP_NZ><COMP>
<STATUS>Z</STATUS>
<AMOUNT_NZ>128.01</AMOUNT_NZ>
<DATE_NZ>28.05.2015</DATE_NZ>
<STATUS>N</STATUS>
<AMOUNT_NZ>12.32</AMOUNT_NZ>
<DATE_NZ>21.09.2015</DATE_NZ>
<STATUS>N</STATUS>
<AMOUNT_NZ>12.32</AMOUNT_NZ>
<DATE_NZ>20.10.2015</DATE_NZ>
</COMP></COMP_NZ></USER></DATA_NZ>
Now I want to change the text from editor into something like this:
Remember about your next payments:
You have to pay 12.32$ before 21.09.2015.
You have to pay 12.32$ before 20.10.2015.
You didn't pay 128.01$ on time: 28.05.2015.
And I have this code:
public void parseTags(String content) {
String nOpenTag, nCloseTag;
String zOpenTag, zCloseTag;
String dnTag, knTag, dzTag, kzTag;
nOpenTag = "[[PAYMENTS_N]]";
nCloseTag = "[[/PAYMENTS_N]]";
zOpenTag = "[[PAYMENTS_Z]]";
zCloseTag = "[[/PAYMENTS_Z]]";
dnTag = "[[amount]]";
knTag = "[[date]]";
dzTag = "[[debt]]";
kzTag = "[[ddate]]";
String textToExtract = "";
String textToExtract = "";
String textToReplace = "";
String parsedContent = content;//; = content;
if (content.contains(nOpenTag) || content.contains(zOpenTag)) {
for (int i = 0; i < getXMLContainer("SZUK_XM4.XML").getUsers().size(); i++) {
if (getXMLContainer("SZUK_XM4.XML").getUsers().get(i) != null) { //
contact = consentService.findByContactTypeAndIdNumber(ContactType.EMAIL, getXMLContainer("SZUK_XM4.XML").getUsers().get(i).getIdNumber());
payments = getXMLContainer("SZUK_XM4.XML").getUsers().get(i).getPaymentContainer().getPayments();
if (contact.size() > 0) {
StringBuilder strB = new StringBuilder();
StringBuilder strB2 = new StringBuilder();
String textToReplace;
for (int c = 0; c < contact.size(); c++) {
strB.setLength(0);
for (int p = 0; p < payments.size(); p++) {
if (payments.get(p).getStatus().contains("N")) {
textToExtract = content.substring(content.indexOf(nOpenTag) + nOpenTag.length(), content.indexOf(nCloseTag));
textToReplace = textToExtract.replace(dnTag, "Day " + payments.get(p).getPaymentDate());
textToReplace = textToReplace.replace(knTag, "Amount " + payments.get(p).getPaymentAmount());
strB.append(textToReplace);
parsedContent = content.replace(textToExtract, strB.toString());
}
if (payments.get(p).getStatus().contains("Z")) {
textToExtract = parsedContent.substring(parsedContent.indexOf(zOpenTag) + zOpenTag.length(), parsedContent.indexOf(zCloseTag));
textToReplace = textToExtract.replace(dzTag, "Day " + payments.get(p).getPaymentDate());
textToReplace = textToReplace.replace(kzTag, "Amount " + payments.get(p).getPaymentAmount());
strB2.append(textToReplace);
parsedContent = parsedContent.replace(textToExtract, strB2.toString());
}
}
mailService.sendTemplateMail(contact.get(c).getContact(), parsedContent);
}
}
}
}
}
}
This code is supposed to find in the database user with given ID from XML.
If user exists then it reads all the data between and replaces [[TAGS]] from the text editor. After everything's done, it should send an email with replaced text.
The problem is that the text it sends isn't correctly replaced. It gives
Remember about your next payments: You have to pay 12.32$ before
21.09.2015. You have to pay 12.32$ before 20.10.2015.
You didn't pay [[debt]]$ on time: [[ddate]].
See the difference in the last line?
I'm using JAXB to read XML, if it matters.
Hope you can help me! Thanks.
replace:
parsedContent = content.replace(textToExtract, strB.toString());
with:
parsedContent = parsedContent.replace(textToExtract, strB.toString());

Replacing url in a CSS with CSS Parser and Regex (Java)

I have this requirement that I need to replace URL in CSS, so far I have this code that display the rules of a css file:
#Override
public void parse(String document) {
log.info("Parsing CSS: " + document);
this.document = document;
InputSource source = new InputSource(new StringReader(this.document));
try {
CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
CSSRuleList ruleList = stylesheet.getCssRules();
log.info("Number of rules: " + ruleList.getLength());
// lets examine the stylesheet contents
for (int i = 0; i < ruleList.getLength(); i++)
{
CSSRule rule = ruleList.item(i);
if (rule instanceof CSSStyleRule) {
CSSStyleRule styleRule=(CSSStyleRule)rule;
log.info("selector: " + styleRule.getSelectorText());
CSSStyleDeclaration styleDeclaration = styleRule.getStyle();
//assertEquals(1, styleDeclaration.getLength());
for (int j = 0; j < styleDeclaration.getLength(); j++) {
String property = styleDeclaration.item(j);
log.info("property: " + property);
log.info("value: " + styleDeclaration.getPropertyCSSValue(property).getCssText());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
However, I am not sure whether how to actually replace the URL since there is not much a documentation about CSS Parser
Here is the modified for loop:
//Only images can be there in CSS.
Pattern URL_PATTERN = Pattern.compile("http://.*?jpg|jpeg|png|gif");
for (int j = 0; j < styleDeclaration.getLength(); j++) {
String property = styleDeclaration.item(j);
String value = styleDeclaration.getPropertyCSSValue(property).getCssText();
Matcher m = URL_PATTERN.matcher(value);
//CSS property can have multiple URL. Hence do it in while loop.
while(m.find()) {
String originalUrl = m.group(0);
//Now you've the original URL here. Change it however ou want.
}
}

Categories

Resources