Tuesday, 21 October 2014

How to “adapt” DOM (which read XML file) to a Jtree (which display parserized XML file) using Adapter Pattern

I'm making an exercize using Adapter Pattern and I would to adapt Jtree interface to a DOM class.
Here there is my DOM class (Target):

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;



    public class DOM {


    private Element root ;
    org.jdom2.Document document ;
    public void parserizza (String s) throws JDOMException, IOException
    {
    SAXBuilder builder = new SAXBuilder();
    document = builder.build(new File(s));
    root = document.getRootElement();
    }

    public Element getRoot ()
    {
    return root ;
    }


    public void showRootName ()
    {
    System.out.println("Nome radice : " + root.getName());
    }

    public void mostraAttributiRoot ()
    {
    System.out.println("Nome radice : " + root.getAttributes());
    }

    public List getChildren ()
    {
    List children = root.getChildren(); 
    return children ; 
    }

    public void showChildrenName ()
    {
    List children = ottieniFigli () ;
    Iterator iterator = children.iterator(); 
    while(iterator.hasNext())
    { 
    Element item = (Element)iterator.next(); 
    Element child = (Element) item.getChildren(); 
    System.out.println("Nome figlio : "+ child.getName()); 

    }

    }

    public void showChildrenAttributes ()
    {
    List children = ottieniFigli () ;
    Iterator iterator = children.iterator(); 
    while(iterator.hasNext())

    { 
    Element item = (Element)iterator.next(); 
    Element child = (Element) item.getChildren(); 
    System.out.println("Nome figlio : "+ child.getAttributes()); 

    }

    }

    public org.jdom2.Document getDocument ()
    {
        return document ;
    }

    }
This is my Adapter :
public class Adapter extends DOM{

    private Tree tree ;

    protected  void parserizza () 
    {
        tree.parserizza();
    }

}
How can I implement the Jtree according to Adapter's rules ? Should my Tree take the parserized Document by DOM as constructor's parameter ?

No comments:

Post a Comment