Using HTMLUnit to get javascript elements - html

I'm trying to use HTMLUnit to get the javascript elements on a webpage (https://www.coursera.org/courses), and it is only loading the html data. How do I get it to display the information shown in the javascript container?
Thanks!
My current code:
public String DownloadPage(String str){
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.getOptions().setTimeout(20000);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
try{
HtmlPage page = webClient.getPage(str);
XmlPage page2 = webClient.getPage(str);
int n = webClient.waitForBackgroundJavaScript(100000);
System.out.println("Executing " + n + " JavaSript jobs!");
System.out.println("OUTPUT: " + page2);
System.out.println("OUTPUT: " + page.asXml());
webClient.closeAllWindows();
}
catch(IOException e){
JOptionPane.showMessageDialog(null, "error");
}
webClient.closeAllWindows();
return "";
}

use
String theContent1 = webClient.getPage(theURL).getWebResponse().getContentAsString();
instead of
String theContent2 = webClient.getPage(theURL);
theContent1 should contain the actual page source including JavaScripts (if any).

Related

How to pass data from two textfield to Jeditorpane as html?

I try to retrieve data from database and populate it in the Jlist. After that, when I click the Jlist, the data will be populated to 3 textfields. Two of them will be populated to a Jeditorpane as a combination in the form of a html file. I have tried the following method but it creates an error.
String meaning1 = txtMeanings.getText();
String source1 = txtSources.getText();
String htmlText = "<html>"
+ "<body>"
+ "<div class='content'>{meaning1}</div>"
+ "<div class='footer'>{source1}</div>"
+"</body>"
+ "</html>";
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 40, 529, 387);
panel_2.add(scrollPane_1);
HTMLEditorKit hed = new HTMLEditorKit();
StyleSheet ss = hed.getStyleSheet();
ss.addRule("BODY {...}");
ss.addRule("h1{...}");
ss.addRule("p{...}");
ss.addRule("blockquote{...}");
ss.addRule("#title{...}");
ss.addRule("hr{...}");
ss.addRule("#content{ ...}");
ss.addRule("#footer{...}");
Document doc = hed.createDefaultDocument();
editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditorKit(hed);
editorPane.setDocument(doc);
editorPane.setEditable(false);
scrollPane_1.setViewportView(editorPane);
ListboxEntry.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
try
{
String query ="select EntryLists, Meanings, Sources from Entry where EntryLists like ? ";
PreparedStatement pst=Connection.prepareStatement(query);
pst.setString(1,(String)ListboxEntry.getSelectedValue());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
txtEntry.setText(rs.getString("EntryLists"));
txtMeanings.setText(rs.getString("Meanings"));
txtSources.setText(rs.getString("Sources"));
}
pst.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();}
editorPane.setText(htmlText);
}
});
What should I do? Is is possible to populate two data (meaning and source) directly to Jeditorpane without populating it first in the two textfield? In the VB.Net, I solve the problem simply by putting this simple code:
WebBrowser1.DocumentText = String.Format("<html><head><style><!--body{{....}}--></style></head></head><body><div id=content>{0}</div><div id=footer><b>Notes:<br></b>{1} </div></body></html>", txtMeanings.Text, txtSources.Text)
How to do that in Java?
I solve my own problem by changing to the following code:
ListboxEntry.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
try
{
String query ="select EntryLists, Meanings, Sources from Entry where EntryLists like ? ";
PreparedStatement pst=Connection.prepareStatement(query);
pst.setString(1,(String)ListboxEntry.getSelectedValue());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
txtEntry.setText(rs.getString("EntryLists"));
txtMeanings.setText(rs.getString("Meanings"));
txtSources.setText(rs.getString("Sources"));
}
pst.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();}
String htmlText = "<html>"
+ "<body>"
+ "<div class='content'>"+ txtMeanings.getText() +"</div>"
+ "<div class='footer'>"+ txtSources.getText() +"</div>"
+"</body>"
+ "</html>";
editorPane.setText(htmlText);
}
});
But, if anyone wants to improve it, your suggestion is welcome.

File download not working in Android using cordova-2.9.0

I am working on a hybird application and it seems very straightforward way to download a pdf file from server using HTML5 anchor download attribute and this is working exactly as expected using below code on desktop browsers.
Download
Challange: But when I am trying to run same code in my Hybird application, using cordova 2.9.0 , when debugging app on mobile; on clicking of Download nothing shows up and download does not start.
Am I missing something very basic here?
Please suggest.
This code is for Android platform.
First, open the file [appname].java in your platform folder:
appname\platforms\android\src\com\[appname]\app
Next, set downloadListener for the webview, right after super.init();
here is the complete code:
package com.[appname].app;
import android.os.Bundle;
import org.apache.cordova.*;
public class [appname] extends CordovaActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init();
super.appView.setDownloadListener(new android.webkit.DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
android.util.Log.d("Logger","url : " + url + " userAgent: " + userAgent + " contentDisposition: " + contentDisposition + " mimeType: " + mimetype + " contentLength " + contentLength);
android.net.Uri source = android.net.Uri.parse(url);
// Make a new request
android.app.DownloadManager.Request request = new android.app.DownloadManager.Request(source);
// appears the same in Notification bar while downloading
String filename = getFilename(contentDisposition);
request.setDescription("This file will be saved in your downloads folder.");
request.setTitle(filename);
//add cookie on request header (for authenticated web app)
String cookieContent = getCookieFromAppCookieManager(source.getHost());
request.addRequestHeader("Cookie", cookieContent);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(android.os.Environment.DIRECTORY_DOWNLOADS, filename);
// get download service and enqueue file
android.app.DownloadManager manager = (android.app.DownloadManager) getSystemService(android.content.Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
});
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html");
};
public String getFilename(String contentDisposition){
String filename[] = contentDisposition.split("filename=");
return filename[1].replace("filename=", "").replace("\"", "").trim();
};
public String getCookieFromAppCookieManager(String url){
android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
if (cookieManager == null)
return null;
String rawCookieHeader = null;
// Extract Set-Cookie header value from Android app CookieManager for this URL
rawCookieHeader = cookieManager.getCookie(url);
if (rawCookieHeader == null)
return null;
return rawCookieHeader;
};
}

Want to give professional looks to a email with ASP.NET?

I am trying to give the looks of email like this:
This is the looks of my email
Where Right hand side of the email subject is Ignou logo and it is a jpg image.
I have the following class for sending the email and it is working fine:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
namespace ProductManagementweb.HelperClasses
{
public class SendEmail
{
public static int SendMail(string ReceiverAddress, string Recsubject, string Recbody)
{
try
{
System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
System.Net.NetworkCredential credential = new System.Net.NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);
//Create the SMTP Client
SmtpClient client = new SmtpClient();
client.Host = settings.Smtp.Network.Host;
client.Credentials = credential;
client.Timeout = 30000;
client.EnableSsl = true;
MailMessage mm = new MailMessage();
mm.From = new MailAddress(settings.Smtp.Network.UserName, "Support Team (Clique City)");
mm.To.Add(ReceiverAddress);
mm.Priority = MailPriority.High;
// Assign the MailMessage's properties
mm.Subject = Recsubject;
mm.Body = Recbody;
mm.IsBodyHtml = false;
client.Send(mm);
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
How can i give the email Format like the image above it is sure that i have to mention the format using html tags but i am unknown where i have to mention and how. therefore any help will surely be appreciated.
You should put the html content from your email design in Recbody i.e. email body and change
mm.IsBodyHtml = false;
to
mm.IsBodyHtml = true;
In addition to vivekd's asnwer.
You just need to build the HTML in the code e.g
string messageContent = "";
messageContent += "<table><tr><td>" + telephoneNumber + "</td><td>" + locationAddress + "</td></tr></table>"
Then set the
mm.IsBodyHtml = true;
And sent the email content to be the messageContent
mm.Body = messageContent;
Obviously you will need to design the layout of the email yourself and build it up by appending to the messageContent string.

Vaadin LoginForm layout - HTML

I'm facing problem trying to set horizontal layout to LoginForm of Vaadin. The only solution for which I have an idea is overriding getLoginHTML() class so that it return the proper code.
I've added style="float:left" which worked for me in normal html.
The changed part of code looks like this:
(...) + "<div class='v-app v-app-loginpage' style=\"background:transparent;\">"
+ "<iframe name='logintarget' style='width:0;height:0;"
+ "border:0;margin:0;padding:0'></iframe>"
+ "<form id='loginf' target='logintarget' onkeypress=\"submitOnEnter(event)\" method=\"post\">"
+ "<div style=\"float: left;\">"
+ getUsernameCaption()
+ "</div><div style=\"float: left;\">"
+ "<input class='v-textfield' style=\"float: left;\" type='text' name='username'></div>"
+ "<div style=\"float: left;\">"
+ getPasswordCaption()
+ "</div>"
+ "<div style=\"float: left;\"><input class='v-textfield' style='display:inline; float:left;' type='password' name='password'></div>"
+ (...)
And that behaves in a weird way - UserField and PasswordLabel are in the same line - all others are in a separate lines. So it looks like that:
UsernameLabel
UserField PasswordLabel
PasswordField
Button
And as I said I would like it to be algined horizontally... any ideas?
The are any reason, why you should use LoginForm. Because if answer is no, you can use Vaadin simple Form, it allow you make your form layout more flexible. For example
public class FormBean{
private String username, password;
//getters and setters
}
HorizontalLayout newLayout = new HorizontalLayout();
Form form = new Form(newLayout){
#Override
protected void attachField(Object propertyId, Field field) {
String fieldName = (String) propertyId;
if(fieldName.equals("username")){
Label usernameLabel = new Label("username: ");
newLayout.addComponent(usernameLabel);
newLayout.addComponent(field);
}
}
};
form.setItemDataSource(new FormBean());
form.setFormFieldFactory(new FormFieldFactory() {
#Override
public Field createField(Item item, Object propertyId, Component uiContext) {
String fieldName = (String) propertyId;
if(fieldName.equals("username")){
TextField field = new TextField();
field.setImmediate(true);
//Here your can add some validations on "fly", for example is username allow only latin characters
field.addListener(new TextChangeListener() {
#Override
public void textChange(TextChangeEvent event) {
String currentText = event.getText();
//Here go validation
}
});
}
return null;
}
});
Inside formFieldFactory you can set if field required, set some validator and etc. If you want to set some sort of validation I recommended use Vaadin add-on BeanValidationForm, it allow using annotations inside POJO object with used to form creation set some rules, for example #NotNull, #Max, #Min and etc.
#NotNull(message = "Username required")
private String username = "";
#NotNull(message = "Password required")
private String userpass = "";

Ant task to extract image dimensions (height, width) from PNG and JPEG files?

Per the Google Page Speed recommendations, I want to Specify image dimensions to "Optimize browser rendering."
Specifying a width and height for all
images allows for faster rendering by
eliminating the need for unnecessary
reflows and repaints.
I am investigating ways to traverse through the image files (PNG, JPEG) in my static content project and output a file with the path and filename of each image file as well as the height and width in pixels. I would then use that to help me construct the tags by using the src attribute data to lookup the values to use for the height and width attributes.
\images\logo.png,100,25
My first ideas was looking for an ANT task, since our static content build uses Ant for other purposes (like using YUI Compressor on JavaScript and CSS files). I am open to other ideas as well, including other methods to solve this problem. I would prefer to not have to manually do this work.
You could try this https://github.com/mattwildig/image-size-report-task, which I've made just for this question.
Here was what I implemented so far (needs testing and clean up). Basically, used Tutorial: Tasks using Properties, Filesets & Paths to get me started in an Ant task and How to get image height and width using java? to extract the image dimensions. I'm going to compare against matt's answer before I deploy.
The test build script from my project:
<project name="ImagesTask" basedir="." default="test">
<target name="init">
<taskdef name="images" classname="ImageInfoTask" classpath="..\dist\ImageTask.jar"/>
</target>
<target name="test" depends="init">
<images outputFile="data/images.xml">
<fileset dir="data" includes="images/**/*.jpg"/>
<fileset dir="data" includes="images/**/*.gif"/>
<fileset dir="data" includes="images/**/*.png"/>
</images>
</target>
</project>
The Java source (without imports):
public class ImageInfoTask extends Task {
private String outputFile;
private List fileSetList = new ArrayList();
private PrintStream outputFileStream;
public void setOutputFile(String outputFile) {
this.outputFile = outputFile.replace("/", File.separator);
}
public void addFileset(FileSet fileset) {
fileSetList.add(fileset);
}
protected void validate() {
if (outputFile == null) {
throw new BuildException("file not set");
}
if (fileSetList.size() < 1) {
throw new BuildException("fileset not set");
}
}
protected void openOutputFile() throws IOException {
FileOutputStream out = new FileOutputStream(this.outputFile);
// Connect print stream to the output stream
this.outputFileStream = new PrintStream(out, true, "UTF-8");
this.outputFileStream.println("<images>");
}
protected void writeImgToOutputFile(String filename, Dimension dim) {
String imgTag = " <img src=\"/" + filename.replace("\\", "/")
+ "\" height=\"" + dim.height + "\" width=\"" + dim.width
+ "\" />";
this.outputFileStream.println(imgTag);
}
protected void closeOutputFile() {
this.outputFileStream.println("</images>");
this.outputFileStream.close();
}
#Override
public void execute() {
validate();
try {
openOutputFile();
for (Iterator itFSets = fileSetList.iterator(); itFSets.hasNext();) {
FileSet fs = (FileSet) itFSets.next();
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String[] includedFiles = ds.getIncludedFiles();
for (int i = 0; i < includedFiles.length; i++) {
String filename = includedFiles[i];
Dimension dim = getImageDim(ds.getBasedir() + File.separator + filename);
if (dim != null) {
writeImgToOutputFile(filename, dim);
}
}
}
closeOutputFile();
} catch (IOException ex) {
log(ex.getMessage());
}
}
private Dimension getImageDim(final String path) {
Dimension result = null;
String suffix = this.getFileSuffix(path);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(new File(path));
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
result = new Dimension(width, height);
} catch (IOException e) {
log(path + ": " + e.getMessage());
} finally {
reader.dispose();
}
}
return result;
}
private String getFileSuffix(final String path) {
String result = null;
if (path != null) {
result = "";
if (path.lastIndexOf('.') != -1) {
result = path.substring(path.lastIndexOf('.'));
if (result.startsWith(".")) {
result = result.substring(1);
}
}
}
return result;
}
}
I'm not aware of such ant task readily available but it should be relatively simple to write one. In PNG format image size is stored right at the beginning of the file in IHDR header. There are numerous samples of PNG parsers on Google - for example this. Wrap it up in ant task and you're done.