Menu Chiudi

JjDom

JavajQueryDom

Is a technology, thanks to which we can create, manage and migrate an html document. We have the possibility to work on the DOM (Document Object Model). The peculiarity of JjDom is that allows us to use jQuery in the java programming language, obviously within the possible limits. JjDom is in the package cloud.jgo.jjdom.

Important message :

To use this technology you must also have the ftp4j library, as JjDom is possible via this API, which is that of the html or xml document it is.

Other code snapshots :


Let’s see how to use JjDom

First, include JjDom in your project :

MainTest.java
import static cloud.jgo.jjdom.JjDom.*;

Create the HTML document :

MainTest.java
package cloud.jgo;
import static cloud.jgo.jjdom.JjDom.*;
/**
 * 
@author Martire91  <https://github.com/wasp91>
 */

public class MainTest {
@SuppressWarnings
("static-access")
public static void main(String[] args) {
    
    
    newDocument
().useDoctype(true).setMinimalTags().home().jqueryInit();
    
    
    
}
}

Document elements are created :

MainTest.java
package cloud.jgo;
import static cloud.jgo.jjdom.JjDom.*;

import cloud.jgo.jjdom.dom.nodes.Element;
/**
 * 
@author Martire91  <https://github.com/wasp91>
 */

public class MainTest {
@SuppressWarnings
("static-access")
public static void main(String[] args) {
    
    
    newDocument
().useDoctype(true).setMinimalTags().home().jqueryInit();
    
    document
.getTitle().setTextContent("I’m a Test");
    
    Element h1
,h2 ;
    
    h1 
= document.createElement("h1");
    h2 
= document.createElement("h2");
    
    h1
.setTextContent("Test");
    h2
.setTextContent("By Wasp91");
    
    document
.getBody().appendChilds(h1,h2);
    
    
}
}

at this point, I print the document by calling the appropriate method :

MainTest.java
newDocument().useDoctype(true).setMinimalTags().home().jqueryInit();
    
    document
.getTitle().setTextContent("I’m a Test");
    
    Element h1
,h2 ;
    
    h1 
= document.createElement("h1");
    h2 
= document.createElement("h2");
    
    h1
.setTextContent("Test");
    h2
.setTextContent("By Wasp91");
    
    document
.getBody().appendChilds(h1,h2);
    
    document
.printMarkup();

now if I want, I can interact with jQuery :

MainTest.java
newDocument().useDoctype(true).setMinimalTags().home().jqueryInit();

        document
.getTitle().setTextContent("I’m a Test");

        Element h1
, h2;

        h1 
= document.createElement("h1");
        h2 
= document.createElement("h2");

        h1
.setTextContent("Test");
        h2
.setTextContent("By Wasp91");

        document
.getBody().appendChilds(h1, h2);

        document
.printMarkup();

        $
(DOCUMENT).ready(new jQueryfunction() {

            @Override
            
public void function(Event event) {

                $
("h1").hide(1000).show(1000);

                $
("h2").hide(jQueryEffect.SLOW).show(jQueryEffect.SLOW);

            
}
        
});
        
        preview
();

the last method is the preview of the html document.

In the next example we add a button, thanks to which we hide
and we show h1 and h2:

MainTest.java
newDocument().useDoctype(true).setMinimalTags().home().jqueryInit();

        document
.getTitle().setTextContent("I’m a Test");

        Element h1
, h2;

        HTMLElement button 
= document.createButton("click me", "btn");

        h1 
= document.createElement("h1");
        h2 
= document.createElement("h2");

        h1
.setTextContent("Test");
        h2
.setTextContent("By Wasp91");

        document
.getBody().appendChilds(h1, h2, button);

        document
.printMarkup();

        $
(DOCUMENT).ready(new jQueryfunction() {

            @Override
            
public void function(Event event) {

                $
("#btn").click(new jQueryfunction() {

                    @Override
                    
public void function(Event event) {

                        $
("h1,h2").fadeToggle("slow");

                    
}
                
});

            
}
        
});

        preview
();

example of iterative construct :

MainTest.java
$("a").each(new function() {
            
            @Override
            
public Object function(Object e) {
            
                
int index = (int) e ;
                
                HTMLElement element 
= element(index);
    
                System
.out.println("Link :"+element.getAttributeValue("href"));
                
                
return true ;
            
}
        
});

CSS Selector

However, the default selector css using JjDom does not include all types of selection. So let’s see how to access the default selector :

MainTest.java
        CSSSelector selector = JjDom.getSelector();

        CSSSelection selection 
= selector.select("h1");

To implement your own selector just extend the CSSSelector and CSSSelction classes. After that, just set the new selector :

MainTest.java
        CSSSelector selector = new MyCssSelector();
        
        JjDom
.setSelector(selector);

Document migration

Once the document is created, we can move on to its very simple migration. The first thing to do is to connect to the ftp server using this method :

MainTest.java
        JjDom.connect("localhost","wasp91","wasp91-passw");

then we just call the following method, specifying the url in which I want to place the document :

MainTest.java
        JjDom.migrate("index.html");

obviously I can call the two methods in an instruction. To retrieve the document instead I call the download method

MainTest.java
        Document document = JjDom.connect("host","user", "passw").download("index.html");

The next example shows how to download a document, make changes to it and then update it on the server.

MainTest.java
        HTMLDocument document = (HTMLDocument) JjDom.connect("host", "user", "passw").download("index.html");
        
        document
.appendChild(document.createComment("author: Wasp91"));
        
        JjDom
.update();

Take a look at the documentations involved

JjDom Documentation
JjQuery Documentation
Download

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *