Using JEditorPane and its setPage method - java

I have an editor pane which displays an HTML file. When I ask it to do it once, it displays the HTML file fine. I then make changes to my HTML file and use the setPage method so that it prints the updated HTML file but it doesn't do this even though the HTML file is changed.
Is this a problem with the Editor pane or is there something wrong with my code?

Looking at the JavaDoc for setPage, I see the following:
If the desired URL is the one currently being displayed, the document will not be
reloaded. To force a document reload it is necessary to clear the stream description
property of the document. The following code shows how this can be done:
Document doc = jEditorPane.getDocument();
doc.putProperty(Document.StreamDescriptionProperty, null);

I've done some tests and I've reproduced this behaviour. When I am calling setPage and passing as argument the same URL that was passed previously in the same method, the JEditorPane seems to ignore the call.
A simple work around: test if the new Url is the same as the previous one. If yes, format the Url a bit differently. For example add an %20 at the end. The new Url actually refers to the same page, but the JEditorPane will consider it as a new one and it will properly reload it.

Related

I dont understand this output produced by selenium webdriver java

I'm trying to write an application for the game Path of Exile, that lists the items in my stash on trading websites automatically.
For this I have to retrieve the items in my stash from their website. For some reason the ".getText()" functions is behaving very weird on the website. I really can't see any mistakes I did with the x-path Expressions.
Example:
Here you can see a snippet from the HTML file I am working on
screenshot of browser debugg tool
In the screenshot you can see that the x-Path I am using is selecting a element with a text element, however when I iterate over the elements and get the text with the getText() function, it returns a empty String... I really have no clue what I am doing wrong, is it the website, that is denying me to access the field?
In case it helps I add here a screenshot of the source code for outputting the text fields
printing the text of the elements(SourceCode)
5 empty Strings as output
On your place I would try to get value instead of text.
try to replace
e.getText()
with
e.getAttribute("value")
or you can also try to play with .getCssValue()

Retrieve an SVG dynamically rendered on a web page from Java

Is there a way for me to render and parse the SVG element from a html page rendered by javascript in Java,
for example: http://bl.ocks.org/mbostock/raw/4063269/, which in the case is using d3.js.
If I understood you question correctly, your problem is that tools you used (HtmlUnit) cannot handle complex JS (d3.js).
In this case there is nothing better than using an actual browser. You can use Selenium to open your page with a remote controlled browser instance and get JS rendered html from there.
This tutorial contains pretty much all you need. Except the getting html part, you can find it in this SO answer.
After that you can feed the html to any parser you want.
EDIT
Just thought of another way, you can try WebKit Html2Pdf. Its purpose is to create PDF files but it uses WebKit under the hood and you can inject custom script (like document.onload callback) that will post SVG contents to you service after page is loaded.
But I wouldnt go that road, it has many limitations (basically only works for direct urls) and overall is pretty messed up.
If what you're trying to do is get the SVG content as a String, Selenium is your best choice, like #chimmi said. But, you might get away without a real browser window opening by using PhantomJS instead.
In theory, it should work like this:
System.setProperty("phantomjs.binary.path", "/path/to/phantomjs");
WebDriver driver = new PhantomJSDriver();
// Open your page with SVG
driver.get("http://localhost:8080/svgpage");
// Find the SVG
WebElement svg = driver.findElement(By.tageName("svg"));
// Get its XML content
String xml = svg.getAttribute("outerHTML");
From here, you could use Batik if you want to actually render the SVG on screen in your non-web app.
Or, if all you wanted was to make assertions on the SVG contents for testing purposes, remember you can select sub-elements using normal CSS or XPath selectors:
//Select all <path> elements within the SVG
Lis<WebElement> pathElementsInSVG = svg.findElements(By.tagName("path"));
//Assert there is 4 <path>s
assert pathElementsInSVG.size() == 4

jquery: load/include html code into html file without delay?

I'm working on a mobile-app that uses cordova, so it's basically a html-website runing in an app. And I have a lot of elements and html-code that has to be present on all pages, e.g. navigation but also popups and so on and so on.
While working on larger webprojects I usually wrap these kind of code-segments in php-files and use "php include" to create my html file. Here however I can't work with php since there is no server. So since I would like to avoid having countless copies of the same code in every html-file, I'm looking for a way to include html code into an html file using jquery maybe?
I did try it with:
$("#includeContent1").load( "mod_navigation.html" );
and
$.get('mod_navigation.html', function(data) { $('body').append(data);});
Both worked, not as great as php, but did the trick. Problem is, the additional content (in this case the navigation) is being loaded "after" the parent html file is shown, making the navigation just pop up with a slight delay. This looks just horrible, because the navigation at the bottom of the screen just keeps flickering while using the app.
Is there a way to avoid these delays? Maybe by jumping to the new page AFTER everything in the html file has been loaded ... or any other way?
could you just try to create the whole DOM in a variable, and when all the HTML code is saved in the variable append it to the body? Say
$("body").empty(); // clear all
var content = "";
/* start to create content here */
$("body").append(content);
Althoug I have no idea how fast this is...
Cheers
You cannot use JavaScript that's on your page to preprocess that page's HTML as it is part of the document and therefore by definition executed after the document has been loaded.
You could however use a callback function to only display the new page after it has fully been loaded.

Find invisible text in iText

I am creating a PDF document of multiple pages using iText. I am adding some unique text on one of the pages in the middle of this document but making it invisible as-
Chunk chunk = new Chunk("invisible text here");
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE, 0f, null);
com.lowagie.text.Document iTextDoc.add(new Paragraph(Element.ALIGN_JUSTIFIED, chunk));
The reason for adding this invisible text is to identify this particular page at the time of onEndPage(). But it is failing.
To achieve in the onEndPage(), I have the following code -
boolean b = (pdfWriter.getDirectContent().toString()).contains("invisible text here");
I get the value of b as false.
If I compare any other text on that page(which is visible) results b as true.
I tried to manually search the invisible text in the PDF reader and it finds the text.
What could I modify to achieve this?
It is never a good idea to assume you can recognize text in the content without elaborate parsing. The text may be split into multiple segments, encoding might not be platform's default character encoding, etc... Thus don't try something like
boolean b = (pdfWriter.getDirectContent().toString()).contains("invisible text here");
You can achieve your goal
The reason for adding this invisible text is to identify this particular page at the time of onEndPage().
much more easily. Simply add a member to your PdfPageEvent implementation, i.e. the class with your onEndPage() method, and set it where you used to add the invisible page content to the text you used to add to the page.
Now you can test that member variable directly in your onEndPage(). Don't forget to reset the variable afterwards, preferably in onEndPage() itself!

Print Multi page JEditorPane html contents in Java

I have developed a code in Java where I have embedded HTML content into a JEditorPane by setting its content type to text / html.
However this content spills over to the second page and so on. I know that the JEditorPane has a print() method that prints the contents of the JEditorPane and I know that the print() method of the JEditorPane prints all the contents of the JEditorPane and even prints multiple pages.
What I want is that the top, left, right and bottom margins should be set to 0.
If I would simply leave the work of setting the margins to 0 at run-time by changing the print settings before printing, it would just not work.
Due to this I had to implement the Printable interface. But when I used the Printable interface it allows to print only the first page of the JEditorPane.
When I searched a lot for this solution I got this link on Google;
http://download.oracle.com/javase/tutorial/displayCode.html?code=http://download.oracle.com/javase/tutorial/2d/printing/examples/PaginationExample.java
But the problem here is that I am using html contents in the JEditorPane. I dont know how to integrate the above solution provided in the link with a JEditorPane that consists of HTML Code
If you need real WYSIWYG use articles about paginated printing from here
http://java-sl.com/articles.html
If you need just print JEditorPane's content paginated use this
http://java-sl.com/JEditorPanePrinter.html

Categories