jmesa renders the html as text - java

I'm using jmesa in Java directly using the tableModel.render() to get the HTML directly. Some of my web objects in my result lists contain HTML - example:
class blah {
String email;
public String getEmailLink() {
return "<a href='" + email + "</a>"
}
}
In my Java code I would just do this:
htmlRow.addColumn(new HtmlColumn("emailLink"));
jmesa is rendering this as text. How can I tell jmesa to render the text as-is to be html in the document?
TIA

Looking at the JMesa soure code, HtmlCellEditor automatically escapes HTML.
I haven't tested it, but you should be able to override the default HtmlCellEditor with a different type... such as the bare-bones BasicCellEditor. It shouldn't be too much extra code:
HtmlColumn emailLinkColumn = new HtmlColumn("emailLink");
emailLinkColumn.setCellEditor(new BasicCellEditor());
htmlRow.addColumn(emailLinkColumn);
Another option to all of this is to create a custom CellEditor and have it create your <a> tag for you instead of doing it in your bean. This page should get you started with custom CellEditors if you want to go that route.
BTW, if you are messing with just a value inside of a cell, overriding/replacing CellEditor is probably all you need (CellEditor it is analogous to the body of a <td>). CellRenderer is concerned with the entire cell (analogous to the <td> as well as its contents).

Use a HtmlCellRenderer as shown in this tutorial.

Related

How to select text in HTML tag without a tag around it (JSoup)

I would like to select the text inside the strong-tag but without the div under it...
Is there a possibility to do this with jsoup directly?
My try for the selection (doesn't work, selects the full content inside the strong-tag):
Elements selection = htmlDocument.select("strong").select("*:not(.dontwantthatclass)");
HTML:
<strong>
I want that text
<div class="dontwantthatclass">
</div>
</strong>
You are looking for the ownText() method.
String txt = htmlDocument.select("strong").first().ownText();
Have a look at various methods jsoup have to deal with it https://jsoup.org/apidocs/org/jsoup/nodes/Element.html. You can use remove(), removeChild() etc.
One thing you can do is use regex.
Here is a sample regex that matches start and end tag also appended by </br> tag
https://www.debuggex.com/r/1gmcSdz9s3MSimVQ
So you can do it like
selection.replace(/<([^ >]+)[^>]*>.*?<\/\1>|<[^\/]+\/>/ig, "");
You can further modify this regex to match most of your cases.
Another thing you can do is, further process your variable using javascript or vbscript:-
Elements selection = htmlDocument.select("strong")
jquery code here:-
var removeHTML = function(text, selector) {
var wrapped = $("<div>" + text + "</div>");
wrapped.find(selector).remove();
return wrapped.html();
}
With regular expression you can use ownText() methods of jsoup to get and remove unwanted string.
I guess you're using jQuery, so you could use "innerText" property on your "strong" element:
var selection = htmlDocument.select("strong")[0].innerText;
https://jsfiddle.net/scratch_cf/8ds4uwLL/
PS: If you want to wrap the retrieved text into a "strong" tag, I think you'll have to build a new element like $('<strong>retrievedText</strong>');

Add a new string in between existing string

I have read the content of HTML into string, now I need to add a new attribute inside the body tag, I was thinking of using StringBuilder for this. But I am unable to frame the logic. Any help would be really appreciated.
Existing HTML
<body class="temporaryrevision">
HTML that I want to create
<body class="temporaryrevision" bgcolor="#FFFFFF">
String htmlString="<html>...<body class="temporaryrevision">..</body>...</html>";
String[] tempData=htmlString.split("<body class=/"temporaryrevision/"");
String data = tempData[0]+"bgcolor=/"#FFFFFF/""+tempData[1];
You can use jQUery for this:
$( ".temporaryrevision" ).attr( "bgcolor", "#FFFFFF" );
Ill suggets a way that will be generic:
Use a XML parser for the same.
Load the file into java and pass it to and instance of XML Parser(SAXParser or something like that)
Traverse the parsed Object tree to your required element tag name. in this example HTML
use Library method to add attribute to the element.
convert the object back to xml format(Basic HTML).
Write and replace the file contents with your updated content.
This way is complex but will be generic to all ur such needs..

How to remove <p>..</p> tag from ckEditor in java/GWT?

I have created ckeditor in GWT like:
CKEditor ck = new CKEditor();
and get the value of it like:
ck.getData().toString();
but I get the
<p> DISADAD </p>
Now, I want to remove it.
Basically replaceAll method from String class do the stuff.
I won't recommend it to do so. Because you are moving in a wrong direction.
Ck editor returns html markup in return. It's your responsibilty to show that html where ever you want. For ex: using safeHtml interface in GWT.
If you remove that html tags from string it's just visible like an normal string, which there is no meaning of using ck editor.

Which is the best Wicket component for rendering arbitrary HTML?

I am implementing a simple markdown wiki using Apache Wicket. The wiki would typically render any arbitrary HTML based on what the user has entered.
I am a bit confused about which Wicket component would be best suited to render such arbitrary HTML.
I tried the Label component but it does not render lists properly, neither does the MultilineLabel (which puts breaks instead of the regular list HTML).
Thanks for any help.
UPDATE: The Label component works perfectly. It was my mistake that I was not able to get it to work earlier. It was a combination of some bad stylesheets and late night coding. Thanks for the helpfull answers. As suggested, I am also going to check out some WYSIWYG editors, which actually might work out better than markdown. Visural Wicket seems especially promising.
If what you want to render is not big, or is already represented as a String, Label will work well, just call label.setEscapeModelStrings(false); to ensure it prints the string as is.
But, if your HTML content is generated dynamically, or read from an InputStream/Reader, and you don't want to keep it in memory, you could use WebComponent directly, and override the method onComponentTagBody(). This way, you write directly to the response, instead of filling a in-memory buffer, transform it to a String, and then write to the response (which happens if you use Label).
Sample code, for both cases:
HomePage.java
public class HomePage extends WebPage {
public HomePage() {
add(new Label("label", "<ul><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li></ul>")
.setEscapeModelStrings(false));
add(new WebComponent("html") {
#Override
protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
Response response = getRequestCycle().getResponse();
response.write("<ul>");
for (int i = 0; i < 5; i++)
response.write("<li>test</li>");
response.write("</ul>");
}
});
}
}
HomePage.html
<html xmlns:wicket="http://wicket.apache.org">
<body>
<h2>Label</h2>
<div wicket:id="label"></div>
<h2>WebComponent</h2>
<div wicket:id="html"></div>
</body>
</html>
It is Label, call Component.setEscapeModelStrings(false) though to render the raw html your model returns.

Java Swing JEditorPane: manipulating styled documents

I have model that is a queue of Strings associated with enum types.
I'm trying to display that model in a JEditorPane, with each element in the queue as a separate HTML paragraph that has attributes based based on the associated enum type.
However, my updating methods are not doing what I want. I tried writing the HTML strings directly to the document (e.g., I take the Strings, prepend <p style="color:red"> and append </p> and then insert them at the end of the document), but that gives me the html tags in the output (instead of as formatting) - which of course is inconsistent with the result of putting the tags on the string that I use construct the document with JEditorPane("text/html",String foo). I've also tried inserting with an AttributeSet, but apparently I'm doing that wrong as well.
Any suggestions?
I've never had much luck playing with HTML in a JEditorPane. I just use attributes in a JTextPane. Something like:
SimpleAttributSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
try
{
doc.insertString(doc.getLength(), "\nSome more text", keyWord );
}
catch(Exception e) {}

Categories