ParserXMLHandler.java :  » App » libtool » org » hirodana » libtool » dataFournisseur » Android Open Source

Android Open Source » App » libtool 
libtool » org » hirodana » libtool » dataFournisseur » ParserXMLHandler.java
package org.hirodana.libtool.dataFournisseur;

import org.hirodana.libtool.objects.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class ParserXMLHandler extends DefaultHandler {
    // nom des tags XML
    private final String LIVRE = "livre";
    private final String DVD = "dvd";
    private final String CD = "cd";
    private final String REVUE = "revue";
    private final String ISBN = "isbn";
    private final String ISSN = "issn";
    private final String EAN = "ean";
    private final String NOMBRE_EXEMPLAIRE_DISPOS = "nombre_exemplaires_dispos";
    private final String ID = "id";
    private final String TITRE = "titre";
    private final String DESCRIPTION = "description";
    private final String EDITEUR = "editeur";
    private final String COLLECTION = "collection";
    private final String AUTEUR = "auteur";
    private final String REALISATEUR = "realisateur";
    private final String PERIODE = "periode";
    private final String INTERPRETE = "interprete";
    private final String MINIATURE = "miniature";
    private final String IMAGE = "image";
    
      
    // Variables temporaires pour stocker les infos de chaque oeuvre 
    private  String tempTitre;
    private  int tempNbExDispos;
    private  String tempDescription;
    private  String tempEditeur;
    private  String tempCollection;
    private  String tempAuteur;
    private  String tempRealisateur;
    private  String tempIsbn;
    private  String tempIssn;
    private  String tempEan;
    private  String tempPeriode;
    private  String tempInterprete;
    private  String tempMiniature;
    private  String tempImage;
    private  int tempId;
    
    // Instanciation d'un nouveau Catalogue
    private Catalogue catalogue= new Catalogue();
    
    // Boolean permettant de savoir si nous sommes  l'intrieur d'un item
    private boolean inItem;
    
    
    // Buffer permettant de contenir les donnes d'un tag XML
    private StringBuffer buffer;
    @Override
    public void processingInstruction(String target, String data) throws SAXException {
      super.processingInstruction(target, data);
    }
    
    public ParserXMLHandler() {
      super();
    }
    
    @Override
    public void startDocument() throws SAXException {
      super.startDocument();
      //Ajout de cette partie si besoin d'un traitement au dbut du document, inutile pour le moment
    }
    
    
    /*
     * Fonction tant dclenche lorsque le parseur trouve un tag XML
     * C'est cette mthode que nous allons utiliser pour instancier une nouvelle Oeuvre
     */
    
    @Override
    public void startElement(String uri, String localName, String name,  Attributes attributes) throws SAXException {
      // Nous rinitialisons le buffer a chaque fois qu'il rencontre une Oeuvre
      buffer = new StringBuffer();
      
      // Ci dessous, localName contient le nom du tag rencontr
      
      if (localName.equalsIgnoreCase(LIVRE)){
        inItem = true;
      }
      
      if (localName.equalsIgnoreCase(CD)){
        inItem = true;
      }
      if (localName.equalsIgnoreCase(DVD)){
        inItem = true;
      }
      if(localName.equalsIgnoreCase(REVUE)){
        inItem = true;
      }
    }
    
    // * Fonction  dclenche lorsque le parseur a rcupr
    // * l'intrieur de la balise XML .Tous les caractre inclus
    // * dans la "ligne" en cours sont copis dans le buffer par
    // * la mthode Characters. On peut donc  les stocker 
    
    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
      
      if (localName.equalsIgnoreCase(ID)){
        if(inItem){
          tempId=Integer.parseInt((buffer.toString()));
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(TITRE)){
        if(inItem){
          // Les caractres sont dans l'objet buffer
          tempTitre=(buffer.toString());
          buffer = null;
        }
      }
      if (localName.equalsIgnoreCase(DESCRIPTION)){
        if(inItem){
          tempDescription=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(EDITEUR)){
        if(inItem){
          tempEditeur=(buffer.toString());
          buffer = null;
        }
      }
      
      
      if (localName.equalsIgnoreCase(NOMBRE_EXEMPLAIRE_DISPOS)){
        if(inItem){
          tempNbExDispos=Integer.parseInt((buffer.toString()));
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(COLLECTION)){
        if(inItem){
          tempCollection=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(AUTEUR)){
        if(inItem){
          tempAuteur=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(PERIODE)){
        if(inItem){
          tempPeriode=(buffer.toString());
          buffer = null;
        }
      }
      
      
      if (localName.equalsIgnoreCase(REALISATEUR)){
        if(inItem){
          tempRealisateur=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(INTERPRETE)){
        if(inItem){
          tempInterprete=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(ISSN)){
        if(inItem){
          tempIssn=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(ISBN)){
        if(inItem){
          tempIsbn=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(EAN)){
        if(inItem){
          tempEan=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(MINIATURE)){
        if(inItem){
          tempMiniature=(buffer.toString());
          buffer = null;
        }
      }
      
      if (localName.equalsIgnoreCase(IMAGE)){
        if(inItem){
          tempImage=(buffer.toString());
          buffer = null;
        }
      }
      
      // On instancie les objets de type Oeuvre grce aux informationss rcupres
      if (localName.equalsIgnoreCase(LIVRE)){
        if(inItem){
          Livre book = new Livre(tempId,tempTitre,tempDescription,tempEditeur,tempNbExDispos,tempAuteur,tempCollection,tempIsbn,tempMiniature,tempImage);
          this.catalogue.addLivre(book);  
          
        }
      }
      
      if (localName.equalsIgnoreCase(REVUE)){
        if(inItem){
          Revue revue= new Revue(tempId,tempTitre,tempDescription,tempEditeur,tempNbExDispos,tempIssn,tempPeriode);
          this.catalogue.addRevue(revue);  
          
        }
      }
      
      if (localName.equalsIgnoreCase(CD)){
        if(inItem){
           CD cd= new CD (tempId,tempTitre,tempDescription,tempEditeur,tempNbExDispos,tempInterprete,tempEan,tempMiniature,tempImage);
          this.catalogue.addCD(cd);  
          
        }
      }
      
      if (localName.equalsIgnoreCase(DVD)){
        if(inItem){
           Dvd dvd= new Dvd (tempId,tempTitre,tempDescription,tempEditeur,tempNbExDispos,tempRealisateur,tempEan,tempMiniature,tempImage);
          this.catalogue.addDvd(dvd);  
          
        }
      }

    }
    
    // * Tout ce qui est dans l'arborescence mais n'est pas partie
    // * intgrante d'un tag, dclenche la leve de cet vnement.
    // * Cet vnement est donc lev tout simplement
    // * par la prsence de texte entre la balise d'ouverture et
    // * la balise de fermeture
    @Override
    public void characters(char[] ch,int start, int length)  throws SAXException{
      String lecture = new String(ch,start,length);
      if(buffer != null) buffer.append(lecture);
    }
    // cette mthode nous permettra de rcuprer les donnes
    public Catalogue getData(){
      
      return catalogue;
      
    }
  }
  
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.