Sorry, I forgot to post my working code. I'll attach it to this message. The zip file is a jar file renamed by adding .zip to its filename.
For the example, I have the class files in D:\Testing\ReadFile\
and the files to be read in E:\Testing\
The policy file was setup to allow files in D:\Testing\ and subfolders to read files from E:\Testing\ and subfolders.
The java.policy samples are in the program's source file as comments at the end.
I don't believe that Sun has changed the basic Permission granting logic. There is something about my program that Sun is NOT allowing in version 1.4.2 that it did allow in 1.3.1. I need to find that and fix it.
There is nothing wrong with my design. Your solution requires too many separate environments/programs as a solution. If you are using a browser to read HTML pages on your disk (for example the Java Tutorial) and you want to search the tutorial files for something, why exit the browser, find another tool, start the tool, find the folders for the tutorial and THEN be able to issue the search command. I prefer to issue the search command immediately from the browser while I'm looking at the files.
The AppletContext is a way for Java programs to communicate with a browser. I don't think it has to be only on the internet. Local use is fine. I think most computer users are used to using a browser, so putting an application in a browser environment seems to me an easy way to present an interface to a lot of none programming users.
Ok, but you don't need a browser to look at local (or remote) html files, you
can use a an application that contains a JEditorPane and implement a
HyperlinkListener for it.
As for struggling with the security management, I just don't have the time
for it.
But making a basic browser for displaying local (or remote) html files in java,
including images is no big deal.
This one is not perfect, it is not very good with fancy styling and it should
have a proper back button and perhaps some other nice stuff too.
A textfield for URL entry,a getURL-button and code for url-create and load
into the pane is all it needs to go on the web.
Btw, it reads/displays txt and rtf files also.
I agree this thread is long. Bye for now.
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.event.*;
/**
* Basic code for java browswr
*/
public class Browser extends JFrame implements ActionListener, HyperlinkListener {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton getFileBtn = new JButton();
JButton closeBtn = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JEditorPane edHTML = new JEditorPane();
public Browser() {
try {
jbInit();
getFileBtn.addActionListener(this);
closeBtn.addActionListener(this);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void loadFile(String fp) throws Exception {
if (fp==null) return;
String filePath=fp;
File f=new File(fp);
long av=f.length();
if (av==0) {
System.out.println("no data");
return;
}
URL url=new URL("file:"+fp);
edHTML.setPage(url);
this.validate();
}
public static void main(String[] args) {
Browser ed = new Browser();
ed.setBounds(20,20,800,600);
ed.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
getFileBtn.setText("Get FIle");
closeBtn.setText("Close");
edHTML.setEditable(false);
edHTML.addHyperlinkListener(this);
this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(getFileBtn, null);
this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(edHTML, null);
jPanel1.add(closeBtn, null);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.closeBtn)
System.exit(0);
if (e.getSource() == this.getFileBtn) {
FileDialog fd=new FileDialog(this,"Select File",FileDialog.LOAD);
fd.setVisible(true);
String fName=fd.getFile();
if (fName==null) return;
String fPath=fd.getDirectory()+fName;
try {
loadFile(fPath);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
URL url = e.getURL();
try {
edHTML.setPage(url);
}
catch (IOException ex) {
ex.printStackTrace();
return;
}
}
}
}
Thanks for the program. It works for simple HTML but not with Javascript.
I narrowed the Security problem to being when I called the Applet from Javascript within an HTML page in IE running jre 1.4.2
For example: document.myApplet.fromJS(str);
The applet in IE with 1.4.2 worked OK when called directly.
I tried several codeBase= entries in the .java.policy file and could not find any that would work. So I gave up and added the FilePermissions to the global entry at the beginning of the file.
For example: The bottom 4 entries allowed IE to work.
I'm not sure what javascript your talking about. Is it to do with the simple browser that you gave me? The javascript exists in the HTML pages that I'm searching thru.
What are the urls you mention? All the files are on my harddrive.
The javascript in my application calls the applet which has a MAYSCRIPT tag.
document.MyApplet.startSearch(str); // where str has the search parms
How else would you pass parameters from an HTML form to an applet? Then applet then uses a JSObject to write to a new Browser window with the results of the search.
If you load a local html file, like the javadoc index.html, then the urls returned in the hyperlink event are the local files on your harddrive.
If you have an html page w javascript that delivers a search parameter string to the
applet, then you should be able to create a near identically looking java
"clone" of that html page and its javascript logic and implement it as a JPanel
extension.
You then have a java application with three panels:
Browser: displays html content with working hyperlinks, or just plaint text/rtf
depending on what the url points to.
Search panel:handles serach parameter entry.
Search results panel:Generated during the search process by appending to
a StriingBuffer, not by using document.write() in javascript. This buffer is loaded into a new JEditorPane (html style), not by using
and clicking the links is this page opens the link in the browser panel.
The StingBuffers generated can be saved in a combobox as previous search
results.
With this setup you can search the contents two ways:
1; Search of all the hyperlinks on a displayed htmlPage , and the contents of all hyperlinks under each of those hyperlinks ... etc. to the level you set.
For your use you could restrict this follow-hyperlinks search to only
local files (but it would work for the web also). Note: you dont have to code any logic for getting all the urls (links)
contained in an HTML fle (or any other type of content you want). If you use the HTMLEditorKit.ParserCallback technique you get all the hyperlink urls
for free.
2: Use a user directory selection and get the files using standard File
methods with optional subdirectory search. If this method also uses the
"follow-hyperlinks" strategy it would be a broader search
The way I see it this is a far better solution. It has browser behaviour and
look and is coded in java only, not java + javascript + html. Maintenance
and enhancement is much easier.
If the javascript code && html are too complicated to stranslate to java, then
it would be very interesting to have a look
That depends... what do you want the javascript for ? Are you not searching for
content ?
Is is not enough to be able to extract all the urls and all the content ?
It's not what "I" want the javascript for. Some of the html pages I've downloaded contain javascript that build/control the gui for the page. The pages also have <STYLE tags. The simple browser example you posted doesn't seem to handle styles.
Sorry, I don't know how to extract the "content" from an html page. I haven't been keeping up with the latest releases of Java. I'm a couple of years behind. Could you give me the class(es) and method(s) that would do that?
Thanks,
Norm
You can parse for the text that falls between <body> and </body>, then strip out any text between script tags, then delete your other tags (anything between < and >) ... this will leave the "text" of the page? Take a look at the Scanner class and the ability to change your delimiter or to use the "findInLine" or "findWithinHorizon" methods to scan for the regex pattern ...