Topic: Technical Java Prog. Question
Mirage4279's photo
Sat 05/05/12 06:05 AM
I know quite a few of you know a little bit of technical computing so I will run this by you... Just as easy to find it on Google though specifics can be tough when it comes to coding even using a search engine...
SOmeone asked if I had any coding questions...so here they are...


I am learning Java Network programming..specifically HTML parsing (reading an input stream from a URL and converting to display a web-page).. using class JEditorPane on a JFrame we easily pull an HTML file and use method setPage( URL url) to display a simple graphic of the file. A more technical way is to use the HTMLDocument class as well as EditorKit class using JEditorPane's setEditorKit(String str) method

my code...

public static void main(String[] args)
{
final JEditorPane jep = new JEditorPane();
jep.setEditable(false);
EditorKit htmlKit = jep.getEditorKitForContentType("text/html");
HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument();

jep.setEditorKit(htmlKit);


final JFrame frame = new JFrame("Web-Browser");

jep.addHyperlinkListener( new GetThePage(jep,frame));
try
{

URL url = new URL("http://www.phoenix.edu");
InputStream in = url.openStream();
jep.read(in,doc);

}
catch(Exception e)
{
System.err.println(e);
}


Key Peaces here are...

EditorKit htmlKit = jep.getEditorKitForContentType("text/html");
HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument();

jep.setEditorKit(htmlKit);

and ...

URL url = new URL("http://www.phoenix.edu");
InputStream in = url.openStream();
jep.read(in,doc);


for parsing the HTML file...This method works great but has a couple of downsides to it...one the HyperlinkListener did not work properly and could not click links as a result...could be a fluke and possible my fault in coding...the other was is it possible to create an EditorKit object using "text/javascript" as the argument for getEditorKitForContentType(String contentType) method???? this way it will decode javascript's and not display it as text on the editor pane???

ALso other question is in this statment

HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument();

This type casts a Document object to HTMLDocument from a method that returns the Document object..this could be another way to display javascripts as they were intended to be displayed and not the text representation although I know of no JavaScriptDocument similar to the HTMLDocument class but I have not networked for very long.

Any programming suggestions are welcome


no photo
Sun 05/06/12 06:21 PM

one the HyperlinkListener did not work properly and could not click links as a result...could be a fluke and possible my fault in coding...


My gut says that when you are doing it this way, you are going to have to implement your own click handler. Even if that click handler just calls some generic method that already exists. It will probably be a generic event handler which then asks if the event is a click event, then handles the click.



the other was is it possible to create an EditorKit object using "text/javascript" as the argument for getEditorKitForContentType(String contentType) method????



I don't know. If you can't find the answer after serious googling, maybe let us know and if I have time I might research it.


ALso other question is in this statment

HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument();

This type casts a Document object to HTMLDocument from a method that returns the Document object..this could be another way to display javascripts as they were intended to be displayed and not the text representation although I know of no JavaScriptDocument similar to the HTMLDocument class but I have not networked for very long.




I believe your question is implied, but I don't see it.

Mirage4279's photo
Sun 05/06/12 07:28 PM
Edited by Mirage4279 on Sun 05/06/12 07:34 PM

My gut says that when you are doing it this way, you are going to have to implement your own click handler. Even if that click handler just calls some generic method that already exists. It will probably be a generic event handler which then asks if the event is a click event, then handles the click.



The statement:

jep.addHyperlinkListener( new GetThePage(jep,frame));

class GetThePage() is a custom class I designed that implements the HyperLinkListener interface... for some reason it did not work on this particular application...

A ClickHandler specifically for click handling will not work unless I wanted to write a class that I built from the ground up to handle click event which could actually work...i suppose ..is a ton of work though



I believe your question is implied, but I don't see it.



correct..the statement...
EditorKit htmlKit = jep.getEditorKitForContentType("text/html");

the String argument "text/html" implies that other String arguments such as "text/javascript" (found in a script header) and "text/css" that tells web - browsers to parse the data using these pre-defined formats... so therefore is there a way to create a Document object that is defined for JavaScript so that JS is executed instead of displaying the code on the editor pane. WIthout having to write my own code to parse javascripting in web pages

no photo
Sun 05/06/12 07:38 PM

other String arguments such as "text/javascript"


I saw that question in your post several lines above, when you said "is it possible to create an EditorKit object using "text/javascript"". I don't know the answer.


But I thought you were asking a new question when you said "also, other question is in this statement".


Mirage4279's photo
Sun 05/06/12 11:11 PM
Yes that may be true..honestly I just finished up a chapter on Java web services and kind of in auto pilot... I did look online though and got exactly what I expected...things that are similar in nature and but not as advanced as i need it to do any good (missing the specifics i need)...perhaps an online Java community is what i need.hmmmmwhoa

Parsing HTML is not an easy task..it is for designing classy web browsers.. parser callbacks and using the HTML.Tag class things as such... my code has difficulty and I do not completely understand it myself...but more complex topics just take practice and rarely understand the first time through...