Wednesday, November 10, 2010

JXPATH Uygulamalari

 1.Uygulama :
public class Employee {
    private String name;
    private String surname;
    private int age;
    public Employee() {
    }
    public Employee(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
}


public class Main {
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("Abdulkadir");
        e.setSurname("Selcukoglu");
        e.setAge(27);
        JXPathContext context = JXPathContext.newContext(e);
        Object n =  context.getValue("name");
        System.out.println(n);
    }
}


//Console Output
Abdulkadir




 2.Uygulama :
public class Employee {
    private String name;
    private String surname;
    private int age;
    public Employee() {
    }
    public Employee(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
}

public class EmployeeBean {
    List list =  new  ArrayList();
    public EmployeeBean() {
        list.add(new Employee("Abdulkadir","Selcukoglu", 27));
        list.add(new Employee("Ertugrul","Aslan", 26));
        list.add(new Employee("Rasim","Imanov", 35));
        list.add(new Employee("Nazim","Memedov", 25));
        list.add(new Employee("Murat","Tagiyev", 24));
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
}

public class Main {
    public static void main(String[] args) {
        EmployeeBean b = new EmployeeBean();
        for (Employee e : b.list) {
            JXPathContext context = JXPathContext.newContext(e);
            Object n =  context.getValue("name");
            Object s =  context.getValue("surname");
            Object a =  context.getValue("age");
            System.out.println(n + "  "+s+"  "+a);
        }
    }
 }

//Console Output
Abdulkadir  Selcukoglu  27
Ertugrul  Aslan  26
Rasim  Imanov  35
Nazim  Memedov  25
Murat  Tagiyev  24

 2.Uygulama : 

 public class Address {
    private String country;
    private String city;
    public Address() {
    }
    public Address(String country, String city) {
        this.country = country;
        this.city = city;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
}
public class Employee {
    private String name;
    private String surname;
    private int age;
    private Address address;
    public Employee() {
    }
    public Employee(String name, String surname, int age, Address address) {
        this.name = name;
        this.surname = surname;
        this.age = age;
        this.address = address;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
}

public class EmployeeBean {
    List list =  new  ArrayList();
    public EmployeeBean() {
        list.add(new Employee("Abdulkadir","Selcukoglu", 27,new Address("Turkiye","Mardin")));
        list.add(new Employee("Ertugrul","Aslan", 26,new Address("Turkiye","Gaziantep")));
        list.add(new Employee("Rasim","Imanov", 33,new Address("Gurcistan","Tiflis")));
        list.add(new Employee("Nazim","Memedov", 25,new Address("Gurcistan","Tiflis")));
        list.add(new Employee("Murat","Tagiyev", 26,new Address("Azerbaycan","Baku")));
    }
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
}

public class Main {
    public static void main(String[] args) {
        Address a =  new Address();
        a.setCountry("Turkiye");
        a.setCity("Mardin");
        Employee e = new Employee();
        e.setName("Abdulkadir");
        e.setSurname("Selcukoglu");
        e.setAge(27);
        e.setAddress(a);
        JXPathContext context = JXPathContext.newContext(e);
        Object ulke =  context.getValue("address/country");
        System.out.println(ulke);
    }
}

//Console Output
Turkiye

  3.Uygulama : 
public class Names {
    private String nameList[];
    public Names() {
    }
    public Names(String[] nameList) {
        this.nameList = nameList;
    }
    public String[] getNameList() {
        return nameList;
    }
    public void setNameList(String[] nameList) {
        this.nameList = nameList;
    }
}


public class Main {
    public static void main(String args[]){
        String list[]={"Abdulkadir","Ertugrul","Rasim","Nazim"};
        Names n=new Names();
        n.setNameList(list);
        JXPathContext context = JXPathContext.newContext(n);
        Object o[]= (Object[]) context.getValue("nameList");
        System.out.println(o[0]);
        System.out.println(o[1]);
        System.out.println(o[2]);
        System.out.println(o[3]);
    }
}

//Console Output
Abdulkadir
Ertugrul
Rasim
Nazim


  4.Uygulama :
 
public class Book {
    private String name;
    public Book() {
    }
    public Book(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


public class Author {
    Book books[];
    public Book[] getBooks() {
        return books;
    }
    public void setBooks(Book[] books) {
        this.books = books;
    }
}



public class Main {
    public static void main(String args[]) {
        Author author = new Author();
        Book bs[] = new Book[3];
        bs[0] = new Book();
        bs[0].setName("Hz Muhammed'in Hayati");
        bs[1] = new Book();
        bs[1].setName("Sahabalerin Hayati");
        bs[2] = new Book();
        bs[2].setName("Guzel Ahlak");
        author.setBooks(bs);
        JXPathContext context = JXPathContext.newContext(author);
        Book b[]= (Book[]) context.getValue("books");
        for (Book k : b) {
            System.out.println(k.getName());
        }
    }
}


//Console Output
Hz Muhammed'in Hayati
Sahabalerin Hayati
Guzel Ahlak



  5.Uygulama :
public class Book {
    private String name;
    public Book() {
    }
    public Book(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


public class Author {
    Book books[];
    public Book[] getBooks() {
        return books;
    }
    public void setBooks(Book[] books) {
        this.books = books;
    }
}
 
public class Main {
    public static void main(String args[]) {
        Author author = new Author();
        Book bs[] = new Book[3];
        bs[0] = new Book();
        bs[0].setName("Hz Muhammed'in Hayati");
        bs[1] = new Book();
        bs[1].setName("Sahabalerin Hayati");
        bs[2] = new Book();
        bs[2].setName("Guzel Ahlak");
        author.setBooks(bs);
        JXPathContext context = JXPathContext.newContext(author);
        Iterator iter = context.iterate("books[position() < 4]");
        while(iter.hasNext()){
            Book bb = (Book) iter.next();
            System.out.println(bb.getName());
        }
   }
}

//Console Output 
  Hz Muhammed'in Hayati
Sahabalerin Hayati
Guzel Ahlak


  6.Uygulama 

   
public class Address {
    private String country;

    private String city;

    public Address() {
    }

    public Address(String country, String city) {
        this.country = country;
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

public class Employee {
    private Map addresses = new HashMap();
    public Employee() {
        addresses.put("home", new Address("Turkiye","Mardin"));
        addresses.put("office", new Address("Azerbaycan","Baku"));
    }

    public Map getAddresses() {
        return addresses;
    }

    public void setAddresses(Map addresses) {
        this.addresses = addresses;
    }
 }

 public class Main {

    public static void main(String args[]) {
        Employee emp = new Employee();
        JXPathContext context = JXPathContext.newContext(emp);
        String cou = (String) context.getValue("addresses/home/country");
        String ci = (String) context.getValue("addresses/home/city");
        System.out.println(cou);
        System.out.println(ci);
        String cou2 = (String) context.getValue("addresses/office/country");
        String ci2 = (String) context.getValue("addresses/office/city");
        System.out.println(cou2);
        System.out.println(ci2);
    }
}
 

//Console Output
Turkiye
Mardin
Azerbaycan
Baku