Thymeleaf java template engine is changing some special characters - java

final Context ctx = new Context();
context.setVariable("data", data);
templateEngine.process(template, context).trim();
here are the imports:
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
template variable points to "content_completed". Where this content_completed is a html file that exists in the project classpath.
contents of this html file:
<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
[[${data.fileName}]][[${T(abc.composer.NoteData).COMPLETED_NO_ERRORS}]]
</html>
where NoteData is a java class
If this data.fileName has something like "sample&.text", the Thymeleaf template engine is changing it to "sample&.text".
Any thoughts on how to avoid this?

Solved it myself. Here is the solution:
<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
<th:block th:utext="${data.fileName}"/>[[${T(abc.composer.NoteData).COMPLETED_NO_ERRORS}]]
</html>

Thymeleaf is escaping the filename so that a browser does not interpret the & as a special HTML character (it's used for HTML entities).
So, the answer, I think, is that it's doing the right thing. When you view the HTML output in a browser it will render as you are expecting.

Related

Java sax parsing replacing a custom tag with the resolved value

I have an XML String which is actually an HTML. It contains few custom tags that should be read and replaced with actual value. I am unable to figure out how to do this using SAX parsing
<html>
<body>
<p>The joiner report for today</p>
<p><APP:FT value="THIS_WEEKDAY"/></p>
<p> </p>
</body>
</html>
This template would be evaluated using a SAX parsing and java code, where the value of the custom tag
<APP:FT>
would be evaluated using java code. For example
<APP:FT value="THIS_WEEKDAY"/>
should be replaced by TUESDAY considering today is 13-Dec-2016. It is easy to find the value, but I am unable to figure out a way to replace this in the HTML string. The final HTML should look like
<html>
<body>
<p>The joiner report for today</p>
<p>TUESDAY</p>
<p> </p>
</body>
</html>
Thank you folks for reading through. i solved the problem not by XML but by using freemarker template API - http://freemarker.org/

how to implement build a selector for HTML DOM elements by its class name using regexp

I have a question here. If I have a html file here.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<h1>Welcome to My Homepage</h1>
<p class="intro">My name is Donald.</p>
<h1 class="intro"><p class="important">Note that this is an important paragraph.</p>
</h1>
<div class="intro important"><p class="apple">I live in apple.</p></div>
<div class="intro important">I like apple.</p></div>
<p>I live in Duckburg.</p>
</body>
</html>
Right now I want to get html element by class name.
If the class name is ".intro", it should return:
My name is Donald.
<p class="important">Note that this is an important paragraph.</p>
If the class name is ".intro.important" it should return:
Note that this is an important paragraph.
If the class name is ".intro.important>.apple", it should return:
I live in apple.
I know jquery has class selector this function, but now I want to implement this function.
Can I use java regexp to do this? It seems like that the class name is single string is ok. But if the class name has a child class name, it will make it hard.
One more question, can java get the dom structure of the html?
You can't parse [x]HTML with RegEx
It's that simple, RegExp was not built to cover the full grammar of XML and different tools need to be used for different jobs.
CSS Selectors not readily available
Unfortunately CSS selector parsers are not yet (afaik) a part of DOM parsers so you would need to use an XPath parser to achieve the same things as with CSS selectors.
There are however some projects such as jquery4j.org which port jQuery (+ widgets) to Java, but they don't bring CSS selectors to the table, the bring a lot more and I'm not sure if you really need all that.
XPath Selectors as an alternative to CSS Selectors
DOM parser + XPath parser for Java are the best approach. The DOM parser reads and load the HTML structure as DOM objects while the XPath parser uses (its own different type of selectors) to find objects within the DOM.
But be careful, don't feed the DOM parser huge amounts of HTML code (entire pages) unless you really need it to sift through it all. If you have a smaller piece of string that isolates the targeted area in the HTML where your info is present then it's better to use DOM with that. This is because DOM parsers are memory hungry beasts.
Can I use java regexp to do this?
You can create regex that selects nested content within tag with specific class name.
I can give you regex that finds content within a tag but it doesn't care of class name:
<([a-z][a-z0-9]*+)[^>]*>.*?</\\1>
But if the class name has a child class name, it will make it hard.
In such case it is easier to use java string.
can java get the dom structure of the html?
Yes, it can be done with jsoup at jsoup.org.

Extract HTML from xml

I want to extract html page from an xml file. Any ideas please ?
<?xml ....>
<first>
</first>
<second>
</second>
<xhtml>
<html>
.....some html code here
</html>
</xhtml>
I want to extract html page as it is from the above.
because xml and html markup is similar any xml parser might have issues with it. I would suggest when you save the html data in the xml file, you encode it to prevent the xml parser from having issues. Then when you recall the data from the xml you just need to decode it for use.
<?xml ....?
<first></first>
<second></second>
<markup>
<html>
code here
</html>
</markup>
when you decode the markup section it will look like this
<html>
code here
</html>
You might find this of some use:
http://www.w3schools.com/xml/xml_parser.asp
You can extract the HTML from the XML using JavaScript. You can then create an element on your HTML page in JavaScript and dump the HTML in there. The only issue with this is that it seems that the XML data you're receiving has a HTML tag.
If you want to add the content to an existing page, then you would have to strip the html and body tags.
If you use python, extraction can be very easy.
from simplified_scrapy.simplified_doc import SimplifiedDoc
html='''
<?xml >
<first>
</first>
<second>
</second>
<xhtml>
<html>
.....some html code here
</html>
</xhtml>
'''
doc = SimplifiedDoc(html)
html = doc.xhtml.html
print (html)
First you need to install simplified_scrapy using pip.
pip install simplified_scrapy

How can I reuse code in JSP?

For example, a user will be rendered throughout my application as
<div class="user">
<p class="username">${user.name}</p>
<p class="karma">${user.karma}</p>
<img src="/users/${user.id}"/>
</div>
How can I reuse this code block?
Note - my code is running within a tag, so I can't use tags for this (or any JSP) otherwise I get a Scripting elements are disallowed here error.
Edit
I'm trying to use a tag file, but getting PropertyNotFoundException.
This is my tag file called 'user.tag':
<%#tag description="User" pageEncoding="UTF-8" %>
<a href="../user/showUser.do?userId=${user.id}">
<p>${user.name}</p>
<img class='avatar' src='${user.avatarUrl}' alt=""/>
</a>
And usage inside a jsp:
Where job.poster is a java bean with id, name, and avatarUrl properties.
If I add
<%#attribute name="user" %>
to the tag file, then I get an exception
Property 'id' not found on type java.lang.String
Since JSP 2.0, there is yet another kind of tags: Tag files. Tag files are JSP custom tags written as a JSP template itself, which seems to be what you want.
http://fforw.de/post/creating-jsp-layouts-with-page-tags/ shows how to use such a tag file as general layout solution. Using them as component should be even easier.
You should be able to use tag files within tag files; this works for me in a JSP 2.2 container:
<%-- mytag.tag --%>
<%#tag description="demo code" pageEncoding="UTF-8"%>
<%#taglib prefix="cust" tagdir="/WEB-INF/tags" %>
<%#attribute name="message"%>
<cust:mytag2 message="${message}" /><%-- uses mytag2.tag --%>
If that fails, you can use the include directive: <%#include file="/WEB-INF/jspf/fragment.jspf" %>
Note that the spec says about tags:
Directive Available? Interpretation/Restrictions
======================================================================
page no A tag file is not a page. The tag directive must
be used instead. If this directive is used in a
tag file, a translation error must result.
So, fragment.jspf must not have a any elements that are not supported in tags, including a page directive.
For the example you have given it sounds like some templating framework is needed, to display the user badge on each screen. At its simplest level this may just be a jsp:include which always includes your "UserBadge.jsp".
If you are running on a web framework e.g. JSF you may use Facelet templates or write a custom component for this. So the answer depends on what framework you have. Breaking it down to just JSP and JSTL - the included JSP or a javax.servlet.jsp.tagext.Tag would certainly reduce the duplication.
Always be careful to follow the DRY Principle... Don't Repeat Yourself!
I feel sometimes creating a .tag file is overkill (especially if it's only for one page), and I've wanted what you describe for years, so I wrote my own, simple solution. See here:
https://stackoverflow.com/a/25575120/1607642
Why don't you use a custom tag or jsp functions.
Thanks,

Java: help me convert a working .jsp to some XML notation

I've got a .jsp file that is working fine. It's maybe a bit special in that it is calling a factory (ArticlesFactory) that returns a singleton (but that is a detail) of class Articles (it does so by automatically fetching shared Google Docs that are transformed to html and then stored into ".../text/en" but that is a detail too).
The following is working fine: it does exactly what I need, it fetches the articles automatically and I can access my Articles instance fine.
<%# page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%# page import="com.domain.projectname.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head></head>
<body>
<% Articles articles = ArticlesFactory.create( getServletContext().getRealPath( "text/en" )); %>
We have <%= articles.getNbItems()%>
</body>
</html>
However, I must transform it to some notation I don't know nor understand, I'm not even sure what the name for that is and obviously I've got some issue.
I don't know if it's a namespace issue or if there's a problem with the ArticlesFactory factory's static factory method creating the Articles singleton:
<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core">
<jsp:directive.page import="com.domain.project.ArticlesFactory"/>
<jsp:directive.page contentType="text/html; charset=UTF-8" />
We have ${variable.nbItems} <!-- What to put here !? -->
</jsp:root>
I tried many things and couldn't figure it out.
Basically I need to:
- call the static create method from the ArticlesFactory class
- by passing it the result of getServletContext().getRealPath( "text/en" ))
(which should give back an Articles instance)
then I want to put the result of getNbItems() in a variable that I want to display
Note that I don't want to have to call getServletContext from any servlet/dispatcher: I want to do it just like in the first working example (ie directly from inside the .jsp).
You're basically looking for "JSP in XML syntax". Most is already explained in this (old) tutorial. You yet have to replace <% %> by <jsp:scriptlet> and <%= %> by <jsp:expression>.
The xmlns:c namespace is by the way unnecessary here, unless you'd like to use any of the JSTL core tags.
The Expression Language (those ${} things) which is explained in this (also old) tutorial is by the way a separate subject. It only acts on objects in the page, request, session or application scope. In scriptlets however, variables are only defined in local scopes (methodlocal actually), those aren't available in EL. You would need to do the following in the scriptlet to make it available in EL:
pageContext.setAttribute("articles", articles); // Put in page scope (recommended).
request.setAttribute("articles", articles); // Or in request scope. Also accessible by any include files.
session.setAttribute("articles", articles); // Or in session scope. Accessible by all requests in same session.
application.setAttribute("articles", articles); // Or in application scope. Accessible by all sessions.
This way it's available by ${articles} in EL.

Categories