is there a way i can open a pdf embedded into a component and send to a new browser tab?
The idea is to set the resource to this component and open in a new tab. I will need to in more buttons later to navigate between different documents.
public class EmbeddedPdfDocument extends Component implements HasSize {
public EmbeddedPdfDocument(StreamResource resource) {
this();
getElement().setAttribute("data", resource);
}
public EmbeddedPdfDocument(String url) {
this();
getElement().setAttribute("data", url);
}
protected EmbeddedPdfDocument() {
getElement().setAttribute("type", "application/pdf");
setSizeFull();
}
}
This thread in the Vaadin.com forum discusses your issue.
Anchor::setTarget ➙ "_blank"
Use the Anchor widget for your link. See demo page.
➥ The key is to set the “target” to the string _blank.
String url = "…" ;
Anchor anchor = new Anchor( url , "Open a PDF document" ) ;
anchor.setTarget( "_blank" ) ; // Specify `_blank` to open in a new browser tab/window.
Here is a complete example app in Vaadin 14.1.19 based on a starter project of the Plain Java Servlet variety.
Run this example app. Click the link to see another web browser tab open and display the PDF document.
package work.basil.example;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
/**
* The main view contains a button and a click listener.
*/
#Route ( "" )
// #PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
#CssImport ( "./styles/shared-styles.css" )
#CssImport ( value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field" )
public class MainView extends VerticalLayout
{
public MainView ( )
{
// Widgets
H1 heading = new H1( "Download PDF in browser tab" );
String url = "https://www.fda.gov/media/76797/download";
Anchor anchor = new Anchor( url , "Open a PDF document" );
anchor.setTarget( "_blank" ); // Specify `_blank` to open in a new browser tab/window.
// Arrange
this.add( heading , anchor );
}
}
Related
-I’m writing a confluence server plugin for page events listener module that sync with custom system.
Whenever page copy events happened, I needed info about original copied page.
-EventListener triggers when copying page with children or hierarchy.
-However, when copying page without children or hierarchy, it falls into the category of page creation events and unable to know where this page originally from. I needed to differentiate between create and copy though technically, they are the same.
Code Sample Below:
package com.linn.aung;
import com.atlassian.confluence.event.events.content.ContentEvent;
import com.atlassian.confluence.event.events.content.page.PageCopyEvent;
import com.atlassian.confluence.event.events.content.page.PageCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageViewEvent;
import com.atlassian.confluence.event.events.content.pagehierarchy.CopyPageHierarchyFinishEvent;
import com.atlassian.confluence.event.events.content.pagehierarchy.CopyPageHierarchyStartEvent;
import com.atlassian.confluence.pages.Page;
import com.atlassian.event.Event;
import com.atlassian.event.EventListener;
import org.apache.log4j.Logger;
public class PageListener implements EventListener{
private static final Logger log = Logger.getLogger(PageListener.class);
private Class[] handledClasses = new Class[]{
ContentEvent.class,
PageViewEvent.class,
PageCreateEvent.class,
PageCopyEvent.class,
CopyPageHierarchyStartEvent.class,
CopyPageHierarchyFinishEvent.class
};
public void handleEvent(Event event) {
if (event instanceof PageCreateEvent) {
PageCreateEvent pageCreateEvent = (PageCreateEvent) event;
Page currentPage = pageCreateEvent.getPage();
String pageTitle = currentPage.getTitle();
log.warn("-----here page created-----");
log.warn(pageTitle);
}
else if(event instanceof CopyPageHierarchyStartEvent) {
CopyPageHierarchyStartEvent copyStart = (CopyPageHierarchyStartEvent) event;
Page destinationPage = copyStart.getDestination();
String pageTitle = destinationPage.getTitle();
log.warn("-----here page copy start-----");
log.warn(pageTitle);
}
else if(event instanceof PageCopyEvent) {
PageCopyEvent pageCopyEvent = (PageCopyEvent) event;
Page currentPage = pageCopyEvent.getPage();
String pageTitle = currentPage.getTitle();
log.warn("-----here page copied----");
log.warn(pageTitle);
}
else if(event instanceof CopyPageHierarchyFinishEvent) {
CopyPageHierarchyFinishEvent copyFinish = (CopyPageHierarchyFinishEvent) event;
Page destinationPage = copyFinish.getDestination();
String pageTitle = destinationPage.getTitle();
log.warn("-----here page copy finish-----");
log.warn(pageTitle);
}
}
How can I get a button without having a name, an ID or a type like
button?
This is the HTML-Code I try to manage:
<a class="btnv6_blue_hoverfade btn_small" href="#"
onclick="DoAgeGateSubmit(); return false;">
<span>Fortfahren</span>
</a>
And this is my Code I have at this moment:
package htmlParser;
import java.io.IOException;
import java.net.URL;
import org.jsoup.nodes.Element;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class HitTheDamnButton
{
public static void main(String[] args) throws Exception
{
String url = "http://store.steampowered.com/agecheck/app/72850/? snr=1_7_7_230_150_2";
WebClient webClient = new WebClient();
HtmlPage startPage = webClient.getPage(url);
HtmlForm form = (HtmlForm) startPage.getElementById("agecheck_form");
HtmlSelect dropDown1 = form.getSelectByName("ageDay");
HtmlSelect dropDown2 = form.getSelectByName("ageMonth");
HtmlSelect dropDown3 = form.getSelectByName("ageYear");
dropDown1.setSelectedAttribute("2", true);
dropDown2.setSelectedAttribute("February", true);
dropDown3.setSelectedAttribute("1970", true);
webClient.close();
}
}
How can I get this button to click? I tried everything
HTMLButton button = form.getButtonByName("a.btnv6_blue_hoverfade.btn_small");
... form.hasAttribute(), ... getSelectByName("name");
But nothing worked.
Thanks for any help in advance!
What you are looking for is an anchor not a button.
Try something like startPage.getAnchorByText or startPage.getAnchors and than iterate and compare the class and/or text to get the right one.
Ok, the advice to search for an Anchor led to some results.
For testing-purposes I switched to another site where I just have to click on a button (no formular has to be filled, I first wanted to solve the simply-Click-on-the-button-problem). I choosed this site:
http://store.steampowered.com/app/324800/?snr=1_7_...
and it leads to the agecheck of "Shadow Warriors 2".
The mentioned button in HTML-code is:
<a class="btn_grey_white_innerfade btn_medium" href="#" onclick="HideAgeGate( 324800 )">ev<span>Weiter</span></a>
Now I made it, to identify the button and clicked on it. But I'm not sure "on what I clicked at last", cause I wasn't redirected to the site behind the agecheck, but to "Shadow Warrior Classics"....
The new URL to which I was directed is:
http://store.steampowered.com/widget/238070/?dynamiclink=1
I don't get it.
Here is my programmCode:
List<HtmlAnchor> anchor = startPage.getAnchors();
// for(HtmlAnchor out : anchor)
// {
// System.out.println(out);
// }
HtmlAnchor anchorButton = anchor.get(143);
System.out.println(anchor.get(143));
// anchorButton.dblClick();
anchorButton.click();
document = Jsoup.connect(anchorButton.click().getUrl().toString()).timeout(0).get();
currentLink = startPage.getBaseURL();
url = currentLink.toString();
document = Jsoup.connect(url).timeout(0).get();
Element parentNode = document.getElementById("app_reviews_hash");
Elements childNodes = parentNode.getElementsByClass("user_reviews_filter_section");
for(Element out2 : childNodes)
{
String all = out2.getElementsByClass("user_reviews_count").text();
String steamPurchasers = out2.getElementsByClass("user_reviews_count").text();
System.out.println(all);
}
System.out.println(anchor.get(143));
shows the right button:
HtmlAnchor[<a class="btn_grey_white_innerfade btn_medium" href="#" onclick="HideAgeGate( 324800 )">]
but after I clicked on it (by "anchorButton.click();") I wont be directed to the right site. The agecheck is still active....
And I still got a NullPointer Exception at
line:
Elements childNodes = parentNode.getElementsByClass("user_reviews_filter_section");
cause on the mislinked site isn't such Element for
Element parentNode = document.getElementById("app_reviews_hash");
so parentNode remains "null".
What have I done wrong?
Ok, I solved the problem. In short terms: I switched to Selenium WebDriver (for JavaCode) and Selenium IDE (FireFox Plugin).
________ Elaborately Description (step by step):
1. Install Selenium IDE for FireFox-Browser:
Go to:
!!!FUCK: I'm not allowed to post Links cause of my low reputation. Just want to do some good deeds, but was hindered (Fuck this world!) !!!
h**ps://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
(note: replace the two * with t)
and click on "+ Add to Firefox"-Button. After rebooting Firefox, the
installation will be done.
ATTENTION: It could be, that some errors will occure at this point (the "Selenium IDE"-entry doesn't appear in the menu of Firefox. If that so,
try to install Selenium IDE by Firefox-> Add-ons->Plug ins: search for
Selenium and select:
Selenium IDE 2.9.1.1-signed"
"Highlight Elements (Selenium IDE)"
"Selenium IDE Button 1.2.0.1-signed.1-signed"
Navigate in FireFoxMenu to :
Tools-> Web-DevelopmentExtras-> add new tools:
(don't Know the exact term, cause I'm using german version of Firefox:
-> Web-Entwickler->Weitere Tools laden)
search for Selenium and choose:
"SeleniumX"
After the Installation the "Selenium IDE"-entry appears in the firefoxMenu under: Tools-> Selenium IDE (german: Extras).
2. Install Selenium WebDriver for Eclipse / Dynamic WebProjects:
Got to:
!!!FUCK: I'm not allowed to post Links cause of my low reputation. Just want to do some good deeds, but was hindered (Fuck this world!) !!!
h**p://www.seleniumhq.org/download/#selenium_ide
(note: replace the two * with t)
and download (first section on site): Selenium Standalone Server
=> version 3.0.1 (date: 11.5.16 [month-day-year])
After downloading the .jar-file, copy it to your in
Eclipse into the following folder:
NameofProject\WebContent\WEB-INF\lib
Note: you could import this by "Build Path-> Configure Build Path", but I
prefer this faster way.
Note: For creating a new "Dynamic Web Project" you have to install some
new software in Eclipse: Help-> Install new Software: In the first line
"Work with" choose:
"Luna - FORBITTEN LINK for low REPUTATION-people"
(for Eclipse Luna version, modify it to your Eclipse version!).
WAIT, til Pending... is done and then choose (last entry):
"Web, XML, Java EE and OSGI Enterprise Development)
3. Using Selenium IDE to identify WebElement in HTML-Code by creating "Test cases" and export them as Java-Code to Eclipse:
Detailed Tutorial:
!!!FUCK: I'm not allowed to post Links cause of my low reputation. Just want to do some good deeds, but was hindered (Fuck this world!) !!!
h**p://docs.seleniumhq.org/docs/02_selenium_ide.jsp
(note: replace the two * with t)
3.1. Open FireFox-Browser: Go to WebSite you want to inspect / crawl / parse HTML-Code. Then (after page was loaded) open Selenium IDE (Tools-> Selenium IDE). Assure that the red button (looks like record-button in some Video-Tools)
on the right most position in the menuBar (over "Table / Source"-Tabs) is
activated (you can read a message by MouseOver). While recording, each
CLICK on the Website you want to inspect creates automatically an entry
into the "Table"-tab (a sort of simple Script-Command). Try to execute as
many actions as you can / need on the website you want to crawl, cause
each action gives you the element in the HTML-code and helps you later to
identify it by Java-Code!
3.2. After finishing your "inspectation" by simply MouseClicks, you have
to save your "Test case" you created right now.
File (F) ->Save Test Case: Choose a name you wish and confirm the save-
Process.
Note: the default StoreLocation for your Test cases is the "Mozilla-
FireFox"-folder on your PC (common path: C:\Programs\Mozilla Firefox).
3.3. Export the Test case as JAVA-CODE to Eclipse:
!!!!! This is the most awsome feature of Selenium IDE !!!!!
Now - after saving your Test case - go again in Selenium IDE to:
File (F)-> Export Test Case As:
choose: Java/JUnit 4/WebDriver: again FileChooser opens (default:
FireFox-folder) and now you can save this "Export-File" as a Java-file.
IMPORTANT: the file ending has to be ".java" (e.g.: "IHateLowReputation.java").
Then copy / import it into your Eclipse-Project. Now you can open this
.java-file and inspect the outwritten Java code for the rigth WebElements
you want to find / choose / manipulate.
You can use this to get a feeling, how Selenium Webdriver commands in
Java has to be coded. Copy the required Code-Lines to your Class.
_____________ And here is my SolutionCode for my Problem above:
package fixWrongEntries;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.support.ui.Select;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import data.DB_Steam_Spiele;
import data.Spiel;
public class SolveButtonClick_FormSubmitt
{
public static void main(String[] args)
{
String agecheckButton = "Content in this product may not be appropriate for all ages, or may not be appropriate for viewing at work.";
String agecheckKonkret = "Please enter your birth date to continue:";
String noReviews = "There are no reviews for this product";
try
{
// turn off annoying htmlunit warnings
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
// Enabling JavaScript => true in brackets
HtmlUnitDriver driver = new HtmlUnitDriver(true);
// Link for agecheck Typ 1 (simply Button click)
String url = "http://store.steampowered.com/app/324800/?snr=1_7_...";
// Link for agecheck Typ 2 (fill out formular and submitt)
//Stng url = "http://store.steampowered.com/agecheck/app/72850/";
driver.get(url);
// System.out.println(driver.findElement(By.cssSelector("h2")).getText());
System.out.println(driver.getCurrentUrl());
/*********************************************************************
*
* Agecheck Typ 2
*
*********************************************************************/
if(driver.findElement(By.cssSelector("h2")).getText().equals(agecheckKonkret))
{
System.out.println("Achtung: Agecheck konkret!");
// Fill out form with age-specifications:
new Select(driver.findElement(By.name("ageDay"))).selectByVisibleText("18");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
new Select(driver.findElement(By.name("ageMonth"))).selectByVisibleText("April");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
new Select(driver.findElement(By.id("ageYear"))).selectByVisibleText("1970");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Click AgeCheck Formular Button: Fortfahren
driver.findElement(By.cssSelector("a.btnv6_blue_hoverfade.btn_small > span")).click();
if(driver.findElement(By.id("app_reviews_hash")).getText().contains(noReviews))
{
System.out.println("Keine Reviews vorhanden!");
continue;
}
else if(!driver.findElement(By.id("app_reviews_hash")).getText().contains(noReviews))
{
String all = driver.findElement(By.xpath("//div[#id='app_reviews_hash']/div[3]/div[2]/label"))
.getText();
String steamPurchaser = driver.findElement(By
.xpath("//div[#id='app_reviews_hash']/div[3]/div[2]/label[2]")).getText();
String communityURL = driver.findElement(By.cssSelector("a.btnv6_blue_hoverfade.btn_medium"))
.getAttribute("href");
}
}
/*********************************************************************
*
* AgeChecck Type 1
*
*********************************************************************/
else if(driver.findElement(By.cssSelector("h2")).getText().equals(agecheckButton))
{
System.out.println("Achtung: Agecheck Button!");
driver.findElement(By.cssSelector("a.btn_grey_white_innerfade.btn_medium > span")).click();
if(driver.findElement(By.id("app_reviews_hash")).getText().contains(noReviews))
{
System.out.println("Keine Reviews vorhanden!");
continue;
}
else if(!driver.findElement(By.id("app_reviews_hash")).getText().contains(noReviews))
{
String all = driver.findElement(By.xpath("//div[#id='app_reviews_hash']/div[3]/div[2]/label"))
.getText();
String steamPurchaser = driver.findElement(By
.xpath("//div[#id='app_reviews_hash']/div[3]/div[2]/label[2]")).getText();
String communityURL = driver.findElement(By.cssSelector("a.btnv6_blue_hoverfade.btn_medium"))
.getAttribute("href");
}
}
/*********************************************************************
*
* No Agecheck
*
*********************************************************************/
else
{
if(driver.findElement(By.id("app_reviews_hash")).getText().contains(noReviews))
{
System.out.println("Keine Reviews vorhanden!");
continue;
}
else if(!driver.findElement(By.id("app_reviews_hash")).getText().contains(noReviews))
{
String all = driver.findElement(By.xpath("//div[#id='app_reviews_hash']/div[3]/div[2]/label"))
.getText();
String steamPurchaser = driver.findElement(By
.xpath("//div[#id='app_reviews_hash']/div[3]/div[2]/label[2]")).getText();
String communityURL = driver.findElement(By.cssSelector("a.btnv6_blue_hoverfade.btn_medium"))
.getAttribute("href");
}
}
}
catch(Throwable t)
{
System.out.println("Fehlermeldung aufgefangen");
t.printStackTrace();
}
}
private static boolean isElementPresent(WebDriver driver, By by)
{
try
{
driver.findElement(by);
return true;
}
catch(NoSuchElementException e)
{
return false;
}
}
}
I hope this will help people with a simular problem.
I'm making JavaFX application which will open pdf files. I found free library for PDF Viewer but it's made in Swing. So I need to add JPanel to ScrollPane(JavaFX). I tried but without success.
I got this error:
Aug 13, 2016 9:59:09 PM org.icepdf.core.pobjects.Document
WARNING: PDF write support was not found on the class path.
I found here on stackoverflow how to add swing component to javafx pane and I do that but I got this error.
Any suggestion is welcome.
package application;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.icepdf.ri.common.ComponentKeyBinding;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import javafx.embed.swing.SwingNode;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
public class PDFView{
public JPanel viewerComponentPanel;
public static Node showPDF(File sFiles) throws MalformedURLException {
String filePath = sFiles.toURI().toURL().toString();
// build a controller
SwingController controller = new SwingController();
// Build a SwingViewFactory configured with the controller
SwingViewBuilder factory = new SwingViewBuilder(controller);
// Use the factory to build a JPanel that is pre-configured
//with a complete, active Viewer UI.
JPanel viewerComponentPanel = factory.buildViewerPanel();
// add copy keyboard command
ComponentKeyBinding.install(controller, viewerComponentPanel);
// add interactive mouse link annotation support via callback
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode, viewerComponentPanel);
// Open a PDF document to view
controller.openDocument(filePath);
return swingNode;
}
private static void createAndSetSwingContent(final SwingNode swingNode, JPanel viewerComponentPanel) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
swingNode.setContent(viewerComponentPanel);
}
});
}
}
This is main class where I call the method from PDFView class
for(int i=0;i<fileNumber;i++){
choosedName=sFiles[i].getName();
String ext=choosedName.substring(choosedName.lastIndexOf(".") + 1);
switch (ext) {
case "doc":
break;
case "docx":
break;
case "pdf":
tab = new Tab();
tab.setText(choosedName);
s1=new ScrollPane();
tab.setContent(s1);
s1.setContent(PDFView.showPDF(sFiles[i]));
tpane.getTabs().add(tab);
I downloaded icepdf viewer and core jars.
and a minimal change in your code:
//String filePath = sFiles.toURI().toURL().toString();
String filePath = sFiles.getAbsolutePath();
then, it worked for me, hope also works for you...
I'm trying to write my own crawljax 3.6 plugin in Java. It should tell crawljax which is a very famous web-crawler to also download files, which he finds on webpages. (PDF, Images, and so on). I don't want only the HTML or actual DOM-Tree. I would like to get access to the files (PDF, jpg) he finds.
How can I tell crawljax to download PDF files, images and so on?
Thanks for any help!
This is what I have so far -a new Class using the default plugin (CrawlOverview):
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import com.crawljax.browser.EmbeddedBrowser.BrowserType;
import com.crawljax.condition.NotXPathCondition;
import com.crawljax.core.CrawlSession;
import com.crawljax.core.CrawljaxRunner;
import com.crawljax.core.configuration.BrowserConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration.CrawljaxConfigurationBuilder;
import com.crawljax.core.configuration.Form;
import com.crawljax.core.configuration.InputSpecification;
import com.crawljax.plugins.crawloverview.CrawlOverview;
/**
* Example of running Crawljax with the CrawlOverview plugin on a single-page
* web app. The crawl will produce output using the {#link CrawlOverview}
* plugin.
*/
public final class Main {
private static final long WAIT_TIME_AFTER_EVENT = 200;
private static final long WAIT_TIME_AFTER_RELOAD = 20;
private static final String URL = "http://demo.crawljax.com";
/**
* Run this method to start the crawl.
*
* #throws IOException
* when the output folder cannot be created or emptied.
*/
public static void main(String[] args) throws IOException {
CrawljaxConfigurationBuilder builder = CrawljaxConfiguration
.builderFor(URL);
builder.addPlugin(new CrawlOverview());
builder.crawlRules().insertRandomDataInInputForms(false);
// click these elements
builder.crawlRules().clickDefaultElements();
builder.crawlRules().click("div");
builder.crawlRules().click("a");
builder.setMaximumStates(10);
builder.setMaximumDepth(3);
// Set timeouts
builder.crawlRules().waitAfterReloadUrl(WAIT_TIME_AFTER_RELOAD,
TimeUnit.MILLISECONDS);
builder.crawlRules().waitAfterEvent(WAIT_TIME_AFTER_EVENT,
TimeUnit.MILLISECONDS);
// We want to use two browsers simultaneously.
builder.setBrowserConfig(new BrowserConfiguration(BrowserType.FIREFOX,
1));
CrawljaxRunner crawljax = new CrawljaxRunner(builder.build());
crawljax.call();
}
}
As images are concerned - I don't see any problem, Crawljax loads these just fine for me.
On the PDF topic:
Unfortunately Crawljax is hardcoded to skip links to PDF files.
See com.crawljax.core.CandidateElementExtractor:342:
/**
* #param href
* the string to check
* #return true if href has the pdf or ps pattern.
*/
private boolean isFileForDownloading(String href) {
final Pattern p = Pattern.compile(".+.pdf|.+.ps|.+.zip|.+.mp3");
Matcher m = p.matcher(href);
if (m.matches()) {
return true;
}
return false;
}
This could be solved by modifying Crawljax source and introducing a configuration option for pattern above.
After that limitations of Selenium regarding non-HTML files apply: PDF is either viewed in Firefox JavaScript PDF viewer, a download pop-up appears or the file is downloaded. It is somewhat possible to interact with the JavaScript viewer, it is not possible to interact with the download popup but if autodownload is enabled then the file is downloaded to disk.
If you would like to set Firefox to automatically download file without popping up a download dialog:
import javax.inject.Provider;
static class MyFirefoxProvider implements Provider<EmbeddedBrowser> {
#Override
public EmbeddedBrowser get() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "/tmp");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream,application/pdf,application/x-gzip");
// disable Firefox's built-in PDF viewer
profile.setPreference("pdfjs.disabled", true);
// disable Adobe Acrobat PDF preview plugin
profile.setPreference("plugin.scan.plid.all", false);
profile.setPreference("plugin.scan.Acrobat", "99.0");
FirefoxDriver driver = new FirefoxDriver(profile);
return WebDriverBackedEmbeddedBrowser.withDriver(driver);
}
}
And use the newly created FirefoxProvider:
BrowserConfiguration bc =
new BrowserConfiguration(BrowserType.FIREFOX, 1, new MyFirefoxProvider());
Obtain the links manually using Jsoup by using the CSS selector a[href] on getStrippedDom(), iterate through the elements and use a HttpURLConnection / HttpsURLConnection to download them.
I am trying to create a help panel for an application I am working on. The help file as already been created using html technology and I would like it to be rendered in a pane and shown. All the code I have seen shows how to render a site e.g. "http://google.com". I want to render a file from my pc e.g. "file://c:\tutorial.html"
This is the code i have, but it doesn't seem to be working.
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.io.IOException;
import static java.lang.System.err;
import static java.lang.System.out;
final class TestHTMLRendering
{
// ------------------------------ CONSTANTS ------------------------------
/**
* height of frame in pixels
*/
private static final int height = 1000;
/**
* width of frame in pixels
*/
private static final int width = 1000;
private static final String RELEASE_DATE = "2007-10-04";
/**
* title for frame
*/
private static final String TITLE_STRING = "HTML Rendering";
/**
* URL of page we want to display
*/
private static final String URL = "file://C:\\print.html";
/**
* program version
*/
private static final String VERSION_STRING = "1.0";
// --------------------------- main() method ---------------------------
/**
* Debugging harness for a JFrame
*
* #param args command line arguments are ignored.
*/
#SuppressWarnings( { "UnusedParameters" } )
public static void main( String args[] )
{
// Invoke the run method on the Swing event dispatch thread
// Sun now recommends you call ALL your GUI methods on the Swing
// event thread, even the initial setup.
// Could also use invokeAndWait and catch exceptions
SwingUtilities.invokeLater( new Runnable()
{
/**
* } fire up a JFrame on the Swing thread
*/
public void run()
{
out.println( "Starting" );
final JFrame jframe =
new JFrame( TITLE_STRING + " " + VERSION_STRING );
Container contentPane = jframe.getContentPane();
jframe.setSize( width, height );
contentPane.setBackground( Color.YELLOW );
contentPane.setForeground( Color.BLUE );
jframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
try
{
out.println( "acquiring URL" );
JEditorPane jep = new JEditorPane( URL );
out.println( "URL acquired" );
JScrollPane jsp =
new JScrollPane( jep,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
contentPane.add( jsp );
}
catch ( IOException e )
{
err.println( "can't find URL" );
contentPane.add( new JLabel( "can't find URL" ) );
}
jframe.validate();
jframe.setVisible( true );
// Shows page, with HTML comments erroneously displayed.
// The links are not clickable.
}
} );
}// end main
}// end TestHTMLRendering
Rendering HTML in Swing is problematic. Swing components have some native support for HTML but it's not even HTML4. It's (limited!) HTML 3.2. If you use a different desktop library API you'll have much better options
WebKit for SWT (ver. 0.6) for the Eclipse Standard Widget Tookit is an excellent option. As the name suggests, it plugs the WebKit rendering engine (which powers Chrome and Safari) into SWT;
Netbeans Platform may have some options too.
You may also want to look at Flying Saucer, which is:
An XML/XHTML/CSS 2.1 Renderer
(in 100% Java)
You forgot to set the content type of the JEditorPane.
jep.setContentType("text/html");
What #cletus says is all true. If you want to get your current app going though with a file-based URL, try setting:
URL = "file:///C://print.html"