Can you make JEditorPanes understand advanced HTML and CSS? - java

Is it possible to make a JEditorPane understand more advanced HTML and CSS?
I mean, when you enter HTML or CSS such as <button>, <progress>, or most other tags, it does not understand them. Is there any way to make it understand and display more complex and advanced coding? If it is, how would I do it?
Thanks!

Yes. Java 7 EE has HTML5 functionality although I'm not sure about specific tags. Also, there are external packages, like one from Intel freely available and you can always extend the functionality yourself in some way or another.
Edit: addendum to my 2nd suggestion (implementing the topic in the linked question)
The Intel JWebEngine (which might be what you are already looking for) documentation says:
What are the advantages versus the built-in Java HTML editor?
The built-in Java HTML editor of Java is very limited. It only
supports HTML 3.2 with some features of HTML 4 and CSS. The support of
CSS is wrong in many cases, especially when it comes to cascading
selectors. The text is often too small and on many web sites Java only
shows an error instead of a incorrectly formatted page.
Here are some further differences:
JWebEngine can display the ACID 1 test correctly - Java does not
JWebEngine can display the ACID 2 test partly - Java only displays a
blank page because an error occurs
JWebEngine basically shows HTML pages like a browser with JavaScript disabled
In cases where the CSS
and HTML specifications are not explicit about implementation
JWebEngine is designed to mirror Mozilla FireFox's behavior.

No it just understands basic HTML 4.
You could use a JavaFx WebView inside a JFXPanel

Related

Generating Java GUI code from basic HTML/Javascript

I would like to know the best way and what it would require(languages/libraries) to generate Java GUI code from an HTML page with embedded Javascript code. It would emulate the HTML objects(like divs/tables) and their interactions. I don't need to emulate any complicated Javascript libraries, but just get basic functionality like handling mouse clicks/movements and key presses which Java can easily support.
I was thinking of representing all of the HTML objects using absolute positioning in Java without having to generate code for layout managers.
EDIT:
Regarding seand's comment: I meant that I don't need to convert code written in JQuery or other Javascript libraries into Java GUI code. I only want to deal with simple Javascript.
I was searching as well and it appears that nothing seems really meant to do that.
However one option is to embed a browser into your app using something like:
http://djproject.sourceforge.net/ns/
http://www.eclipse.org/articles/Article-SWT-browser-widget/browser.html
And then used it to load your HTML pages.

When writing a internet browser in java

I came across the problem of not having an editor kit that could not handle some parts of a webpage. Examples include javascript and css. Does anyone know where I can find an editor kit that is suitable for that?
Also I'm curious as to what programming language browsers like Google Chrome and IE use.
Try Aptana Studio
There are already miscellaneous browser projects using java. The java Scripting API supporting undermore JavaScript (JS sometime named ECMAScript) will be worth looking into. You should on reading the HTML, construct a DOM tree and interprete <script> blocks, which may operate on the DOM or write HTML you have to read from.
The EditorKit in swing builds a too simple non.tree StyledDocument, which you have to bridge.
The way to proceed would be to first not use swing output but immediately generate a DOM/generated HTML.

Java: How to send html content using java.awt.Desktop

I'm developing a console Java application(Not swing) using java.awt.Desktop class, which will launch the browser with something like this;
Desktop.getDesktop().browse(URI.create("http://www.google.com"));
This works, but actually what i want is not to give an absolute URL but to display a string with HTML content which I have builded in the code. Can I directly do do this without saving my content as a html page and then calling again?
String myHtmlstring="<body>.."
For this to work, you would need an browser, which can be started with the html-content as parameter.
firefox -code "<html><head><title>demo</title></head><body>..."
If you look at the manpages for firefox, lynx and opera, you won't find such an option (I didn't). But theoretically, it would be possible.
Since html-pages are normally some kb big, using the parameters would be very unhandy, because html often contains quotes and apostrophes, which would need masking. Therefore, if a passing of content would be possible, I would expect it as reading from stdin like so:
cat demo.html | browser
However, I don't know a browser which supports this.
Summary: No, it's not possible with today browsers, but in principle it would be possible. Going with temporary files would be the solution, you're already aware off.
Its not particularly clear what you're asking for though, what do you mean when you say, "display my own html string?" Do you want to generate html and then have the browser display that?
JavaFX 2.0 comes with a webkit component, and you can embed that in your swing application and do with it what you will. Its officially released for Windows and in [beta|preview|alpha] for max/linux. So if you don't require support for multiple platforms right now, its probably a good choice.

DOM implementation in java

In a web browser written in java different types of parser have been used to do the parsing and create a DOM document. In the process of rendering how the browser visualize the DOM into J-Component . Can anyone tell me about the whole process of implementing of DOM into J-Component to show the whole web-page in java ?
Here is a link where you can find how to display a DOM Hierarchy into JTree (subclass of JComponent) component:
http://download.oracle.com/docs/cd/E17802_01/j2ee/j2ee/1.4/docs/tutorial-update2/doc/JAXPDOM4.html#wp64186
Hope it will help you.
That is far too large a subject for this forum - Unless you restrict the browser to a specific version of HTML without CSS, without JavaScript (or other scripting languages) and without any embedded objects.
You could look at existing code if you can work within the GPL and other licences.
Well, basically you implement the HTML and CSS standards. Doing so completely and correctly is a HUGE amount of work, several man-years at least. There are some projects are are attempting this, but none have been very successful so far.

Is there a way to change or reskin an incoming website on the fly?

I have a project where they want me to embed a website into a java application and they want the website to have a similar color scheme as the rest of the application. I know a lot about CSS and building websites but I am not aware of a way to change the look of a website as it comes in on the fly. Is there someone who can help?
Update:
I don't have access to the header because it is not my website. To give more info about the project is we have a browser embedded in a java client application. The user needs to access a website that displays the contents of a database. I have no access to the original html or css from the site.
What i need is to change the background color and font sizes of the incoming webpage to match the look and feel of the java application.
One approach would be to replace their CSS with your own.
You could also take the approach used by the Stylish plugin, which involves a lot !important decelerations to override the site's CSS. Since this is a Java app, I assume the user will not have opportunity to supply their own CSS, so using !important here doesn't precisely go against the standard.
In your particular situation, I'd look into data scraping, all you need to do is scrape the website for the data, and then re-style it to present it how you want.
Good luck
The Greasemonkey add-on for Firefox does just this. You can write a bit of Javascript code and have it run when certain web pages load. One common thing to use it for is to make changes to the DOM to move page elements around, hide or resize elements, change colors, etc. There are a bunch of examples at userscripts.org if you want to get an idea of what I am talking about.
Your code would simply need to do something similar. Load the page (including the normal style sheets) and then use the DOM to make changes to style elements as desired. Browse through the source of the page to get the names/ids of important elements, and your code can key off of those. Loading an additional CSS file containing your changes is an option, but doing it programmatically will likely give you more flexibility in the event that the target website changes.
Depends on what do you use to show the pages in Java. Most browser implementations support dynamic changes to the DOM, so you can simply add a CSS file to header as a last element, and it will be applied.
you need to know the markup of the html / css so you can make the best skin.
you could theoretically do it by styling just the basic tags: h1...h6, p, etc... but it would not be as good and would probably fail to produce the best results at times and even produce horrible things at times.
if you KNOW the site markup then you can make a skin and simply use CSS/images to skin it as you wanted it.
just include your CSS markup LAST so that it overrides the one already present on the site that you want to skin differently.
should not be a difficult thing per se. the skin itself is probably the better (more effort required) part of the job.
On the fly, should mean changing the html fetched. So parsing and replacing tokens seems to be a/the way.
You could change the locations of the style sheet files by replacing the href value in a link that points to a css file, and set the value to your style sheet (a different URI).
<link type="text/css" href="mylocalURI" rel="stylesheet />
(this should be the result of a process/replacement)
I think you understand what should happend for inline styles.
I would use JTidy to normalize the original site HTML to XHTML, then use XSLT to filter only the interesting/relevant information, obtaining XML format; and finally (since I wouln't want to convert XML to objects), XSLT again to transform the "pure" XML into the HTML look & feel I need/want.
All of this can be assembled as streams, using more or less 4 Kb of buffer per filter (12 Kb total) per thread. Also meaning that it will run fast enough. And all built on standard, open-source available components.
Cheers.

Categories

Resources