Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > build a browser in java

Reply
Thread Tools

build a browser in java

 
 
isha
Guest
Posts: n/a
 
      09-24-2006
does ne1 hav any clue as to how to go about building a browser using
java..im lost

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      09-24-2006
On Sun, 24 Sep 2006 22:57:02 +0100, isha <> wrote:

> does ne1 hav any clue as to how to go about building a browser using
> java..im lost


As with any software project, start with the requirements. Until you know
exactly what you want to achieve, you can't really plan to achieve it.

Are you talking about a fully-featured web-browser for veiwing any page on
the public Internet with full JavaScript and CSS support? That's a huge
task (look how long it took the Mozilla Foundation with hundreds of
volunteers). If it's just for viewing specific pages that you have some
control over, then things start to become more manageable since you can
restrict yourself to well-formed XHTML and not worry about having to parse
the tag soup. Even so, just the layout engine for that is a big project.

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      09-24-2006
isha schrieb:
> does ne1 hav any clue as to how to go about building a browser using
> java..im lost
>


Here's one:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

public class Browser {

private JTextField addressField;
private JEditorPane editorPane;

private void initComponents() {
addressField = new JTextField(50);
editorPane = new JEditorPane();
editorPane.setEditable( false );

addressField.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
setPage( addressField.getText().trim() );
}
});

editorPane.addHyperlinkListener( new HyperlinkListener() {
public void hyperlinkUpdate( HyperlinkEvent e ) {
if ( e.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED ) {
if ( e instanceof HTMLFrameHyperlinkEvent ) {
HTMLFrameHyperlinkEvent evt =
(HTMLFrameHyperlinkEvent)e;
HTMLDocument doc =
(HTMLDocument)editorPane.getDocument();
doc.processHTMLFrameHyperlinkEvent( evt );
} else {
setPage( e.getURL().toString() );
}
}
}
});
}

public void setPage( String page ) {
try {
editorPane.setPage( page );
addressField.setText( page );
} catch ( Exception ex ) {
JOptionPane.showMessageDialog( null, ex.getMessage() );
}
}

private JComponent createAddressPanel() {
JLabel addressLabel = new JLabel("Address" );
addressLabel.setDisplayedMnemonic( KeyEvent.VK_A );
addressLabel.setLabelFor( addressField );

Box addressPanel = new Box( BoxLayout.X_AXIS );
addressPanel.add( addressLabel );
addressPanel.add( Box.createHorizontalStrut(5) );
addressPanel.add( addressField );

return addressPanel;
}

private void createAndShowGUI() {
initComponents();

JPanel contentPanel = new JPanel( new BorderLayout() );
JComponent addressPanel = createAddressPanel();
addressPanel.setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );

contentPanel.add( addressPanel, BorderLayout.NORTH );
contentPanel.add( new JScrollPane(editorPane) );

JFrame frame = new JFrame("Browser");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( contentPanel );
frame.pack();
frame.setVisible(true);
}

public static final void main( String args[] ) {
EventQueue.invokeLater( new Runnable() {
public void run() {
new Browser().createAndShowGUI();
}
});
}
}

Bye
Michael
 
Reply With Quote
 
charles hottel
Guest
Posts: n/a
 
      09-25-2006

"isha" <> wrote in message
news: oups.com...
> does ne1 hav any clue as to how to go about building a browser using
> java..im lost
>


See the book "Just Java" 5th edition by Peter van der Linden. It has code
for a small browser. IIRC it is around 200 lines.


 
Reply With Quote
 
onetitfemme
Guest
Posts: n/a
 
      09-25-2006
I just did a little tweaking with your code, but there is something I
still don't get right, namely:
..
Why is it you can not display "text/html; charset=UTF-8" or
"text/html; charset=GB2312" encoded pages, even if per javadocs:
..
// __
http://java.sun.com/j2se/1.4.2/docs/...ditorPane.html
// __
http://java.sun.com/j2se/1.4.2/docs/...e.html#setPage
// __
http://java.sun.com/j2se/1.4.2/docs/...setContentType
..
text/html: HTML text. The kit used in this case is the class
javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support.
..
Weren't "text/html; charset=utf-8" encoded pages supported by HTML
3.2? Something like:
..
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>HTML 3.2 and text/html; charset=utf-8 test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
</head>
....
</html>
..
also I don't quite understand what is meant in the specification with:
..
setContentType: public final void setContentType(String type)
..
Sets the type of content that this editor handles. This calls
getEditorKitForContentType, and then setEditorKit if an editor kit can
be successfully located. This is mostly convenience method that can be
used as an alternative to calling setEditorKit directly.
If there is a charset definition specified as a parameter of the
content type specification, it will be used when loading input streams
using the associated EditorKit. For example if the type is specified as
text/html; charset=EUC-JP the content will be loaded using the
EditorKit registered for text/html and the Reader provided to the
EditorKit to load unicode into the document will use the EUC-JP charset
for translating to unicode. If the type is not recognized, the content
will be loaded using the EditorKit registered for plain text,
text/plain.
..
How can you display non-ASCII characters using a basic java-based
browser like this one?
..
// - - - - - - - - - - - THE CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

// __
public class Browser00{
private JTextField addressField;
private JTextPane JTxtPn;
// __
private void initComponents(){
addressField = new JTextField(50);
JTxtPn = new JTextPane();
// __
String aCntntEnc;
aCntntEnc = "text/html; charset=utf-8";
aCntntEnc = "text/html; charset=GB2312";
JTxtPn.setContentType(aCntntEnc);
JTxtPn.setEditable( false );
// __
addressField.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent e ){
setPage( addressField.getText().trim() );
}
});
// __
JTxtPn.addHyperlinkListener( new HyperlinkListener(){
public void hyperlinkUpdate( HyperlinkEvent e ){
if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED ){
if ( e instanceof HTMLFrameHyperlinkEvent ){
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
HTMLDocument doc = (HTMLDocument)JTxtPn.getDocument();
doc.processHTMLFrameHyperlinkEvent( evt );
}
else{ setPage( e.getURL().toString() ); }
}
}
});
}
// __
public void setPage( String page ){
try{
System.out.println("// - - - - - - - - - - - - - - - - - - ");
System.out.println("// __ page: " + page);
System.out.println("// __ Before JTxtPn.setPage(): " +
JTxtPn.getEditorKit().getContentType());
JTxtPn.setPage( page );
System.out.println("// __ After JTxtPn.setPage(): " +
JTxtPn.getEditorKit().getContentType());
addressField.setText( page );
}catch ( Exception ex ){ JOptionPane.showMessageDialog( null,
ex.getMessage()); }
}
// __
private JComponent createAddressPanel(){
JLabel addressLabel = new JLabel("Address" );
addressLabel.setDisplayedMnemonic( KeyEvent.VK_A );
addressLabel.setLabelFor( addressField );

Box addressPanel = new Box( BoxLayout.X_AXIS );
addressPanel.add( addressLabel );
addressPanel.add( Box.createHorizontalStrut(5) );
addressPanel.add( addressField );

return addressPanel;
}
// __
private void createAndShowGUI(){
initComponents();

JPanel contentPanel = new JPanel( new BorderLayout() );
JComponent addressPanel = createAddressPanel();
addressPanel.setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );

contentPanel.add( addressPanel, BorderLayout.NORTH );
contentPanel.add( new JScrollPane(JTxtPn), BorderLayout.CENTER );

JFrame frame = new JFrame("Browser00");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.setContentPane( contentPanel );
frame.pack();
frame.setVisible(true);
}
// __
public static final void main( String args[] ){
EventQueue.invokeLater( new Runnable(){
public void run(){ new Browser00().createAndShowGUI(); }
});
}
}

 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      09-25-2006
onetitfemme schrieb:
> I just did a little tweaking with your code, but there is something I
> still don't get right, namely:
> .
> Why is it you can not display "text/html; charset=UTF-8" or
> "text/html; charset=GB2312" encoded pages, even if per javadocs:


It *can* display such pages. You don't have to specify the encoding.

E. g. point your "browser" to

http://www.ibm.com/gr/el

You'll see a page containing greek glyphs. If not, either your fonts
don't support it or it might be a bug in your java version

> .
> also I don't quite understand what is meant in the specification with:


If you e.g. use setContentType("text/plain") this method will set the
right EdiorKit (not HTMLEditorKit).

<api>
There are multiple ways to get a character set mapping to happen with
JEditorPane.

1. One way is to specify the character set as a parameter of the
MIME type. This will be established by a call to the setContentType
method. If the content is loaded by the setPage method the content type
will have been set according to the specification of the URL. It the
file is loaded directly, the content type would be expected to have been
set prior to loading.
2. Another way the character set can be specified is in the document
itself. This requires reading the document prior to determining the
character set that is desired. To handle this, it is expected that the
EditorKit.read operation throw a ChangedCharSetException which will be
caught. The read is then restarted with a new Reader that uses the
character set specified in the ChangedCharSetException (which is an
IOException).
</api>

> .
> How can you display non-ASCII characters using a basic java-based
> browser like this one?


It is encoding-aware already.

> .
> // - - - - - - - - - - - THE CODE


Please follow the conventions and don't capitalize variable names (it's
jTxtPn not JTxtPn).

Bye
Michael
 
Reply With Quote
 
bassel
Guest
Posts: n/a
 
      09-25-2006

isha wrote:
> does ne1 hav any clue as to how to go about building a browser using
> java..im lost


I think sun had a java browser called "Hot Java" it's now obsolete but
the source code is available for research. you can go there and
download it.

Bassel

 
Reply With Quote
 
onetitfemme
Guest
Posts: n/a
 
      09-25-2006
OK, I went to http://www.ibm.com/gr/el and as you stated I could see
the greek characters.
..
this page's heading + Content-Type stanzas looks like:
..
<!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" lang="el" xml:lang="el">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
....
..
But have you gone to sites like, say,
http://juyou.linktone.com/default.lt using your browser?
..
I even started up the application passing the system properties at
start time:
..
java -Dfile.encoding=UTF-8 Browser02
..
but could not still see the page right even if it is encoded as UTF-8
..
> If not, either your fonts don't support it or it might be a bug in your java version

..
"your fonts" ... How come I can see without any problems
http://www.xoops.cn/ on both Internet Explorer and Firefox, but not
the:
..
java -version
java version "1.4.2_12"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_12-b03)
Java HotSpot(TM) Client VM (build 1.4.2_12-b03, mixed mode)
..
international edition of the JDK I am using?
..
I need such java-based basic code to display i18n pages and Math
formulas
..
I gave a first quick try to Hotjava, but had problems also:
// __
C:\cmllpz_dir\dev\java\JBrowser\HotJava\hjb115-generic\HotJava1.1.5\bin>hotjava_batch.bat

C:\cmllpz_dir\dev\java\JBrowser\HotJava\hjb115-generic\HotJava1.1.5\bin>echo
off
..
" CLASSPATH:" ~\HotJava\hjb115-generic\HotJava1.1.5\lib\classes;.
" HOTJAVA_HOME:" ~\HotJava\hjb115-generic\HotJava1.1.5
" JAVA_HOME:" ~\j2sdk1.4.2_12
..
[Starting HotJava]
[Initializing globals]
java.util.MissingResourceException: Can't find bundle for base name
hjResourceBundle, locale en_US
at
java.util.ResourceBundle.throwMissingResourceExcep tion(ResourceBundle.java:83
at
java.util.ResourceBundle.getBundleImpl(ResourceBun dle.java:807)
at java.util.ResourceBundle.getBundle(ResourceBundle. java:551)
at sunw.hotjava.misc.Globals.initProperties(Globals.j ava:312)
at sunw.hotjava.Main.main(Main.java:79)
java.lang.Exception: Failed to load localized properties:
java.util.MissingResourceException: Can't find bundle for base name
hjResourceBundle, locale en_US
at sunw.hotjava.misc.Globals.initProperties(Globals.j ava:316)
at sunw.hotjava.Main.main(Main.java:79)
error: Failed to load localized properties:
java.util.MissingResourceException: Can't find bundle for base name
hjResourceBundle, locale en_US
// __
..
otf

 
Reply With Quote
 
onetitfemme
Guest
Posts: n/a
 
      09-25-2006
I installed the latest version of the hotjava browser 3.0 (which by
the way must use jre 1.1.6)
..
http://java.sun.com/products/archive...3.0/index.html
..
and it could not render CHinese characters either, even thogh under
VIew > CHaracter Encoding, it included UTF-8 as one of the options.
which is the same char enc. of the opages I was testing.
..
otf

 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      09-27-2006
onetitfemme schrieb:
> I installed the latest version of the hotjava browser 3.0 (which by
> the way must use jre 1.1.6)
> .
> http://java.sun.com/products/archive...3.0/index.html
> .
> and it could not render CHinese characters either, even thogh under
> VIew > CHaracter Encoding, it included UTF-8 as one of the options.
> which is the same char enc. of the opages I was testing.
> .


If you've problems with reading Chinese characters from a stream, the
character encoding used for reading is wrong.

If you've problems rendering Chinese characters, the used font doesn't
include these.

E.g. the following

import javax.swing.*;

public class Unicode {
public static final void main( String args[] ) {
JFrame frame = new JFrame("Unicode");
frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
frame.getContentPane().add( new JLabel("\u4e10 \u2704") );
frame.pack();
frame.setVisible( true );
}
}

can be used to display a chinese symbol [1] and a dingbat symbol [2]
(scissors).

Since the fonts I use don't include chinese characters (but dingbat
symbols) I get a small rectangle followed by the scissors.

[1] http://www.unicode.org/charts/PDF/U4E00.pdf
[2] http://www.unicode.org/charts/PDF/U2700.pdf

Bye
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not Buid errors when build, yes build erros when deploy, why? Pedro Mir ASP .Net 2 06-20-2006 02:26 AM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM
How to build ASP.NET projects on a separate build machine? Vagif Abilov ASP .Net 2 07-07-2004 04:34 PM
Python 2.3.3 : Win32 build vs Cygwin build performance ? Nicolas Lehuen Python 3 01-28-2004 07:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57