1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
   | public class XmlEldoonHandler extends DefaultHandler{
    //résultats de notre parsing
    private List<XmlComponent> xmlCompList;
    private XmlComponent xmlComponent;
    //flags nous indiquant la position du parseur
    private boolean inEldoonPrj, inComponent, inPath, inGui, inPosition, inSize, inColor;
    //buffer nous permettant de récupérer les données 
    private StringBuffer buffer;
 
    // simple constructeur
    public XmlEldoonHandler(){
        super();
    }
 
    //détection d'ouverture de balise
    public void startElement(String uri, String localName,
            String qName, Attributes attributes) throws SAXException{
        if(qName.equals("eldoon-prj")){
            xmlCompList = new LinkedList<XmlComponent>();
            inEldoonPrj = true;
        }else if(qName.equals("component")){
            xmlComponent = new XmlComponent();
            String strName = attributes.getValue("name");
            xmlComponent.setName(strName);
            try{
                boolean val = Boolean.parseBoolean(attributes.getValue("enable"));
                xmlComponent.setEnable(val);
            }catch(Exception e){
                //erreur, le contenu de id n'est pas un boolean
                throw new SAXException(e);
            }
            inComponent = true;
        }else {
            buffer = new StringBuffer();
            if(qName.equals("path")){
                inPath = true;
            }else if(qName.equals("gui")){
                inGui = true;
            }else if(qName.equals("position")){
                inPosition = true;
            }else if(qName.equals("size")){
                inSize = true;
            }else if(qName.equals("color")){
                inColor = true;
            }else{
                //erreur, on peut lever une exception
                throw new SAXException("Balise <"+qName+"> inconnue.");
            }
        }
    }
    //détection fin de balise
    public void endElement(String uri, String localName, String qName)
            throws SAXException{
        if(qName.equals("eldoon-prj")){
            inEldoonPrj = false;
        }else if(qName.equals("component")){
            xmlCompList.add(xmlComponent);
            xmlComponent = null;
            inComponent = false;
        }else if(qName.equals("path")){
            xmlComponent.setPath(buffer.toString());
            buffer = null;
            inPath = false;
        }else if(qName.equals("gui")){
            //xmlComponent.setPrenom(buffer.toString());
            buffer = null;
            inGui = false;
        }else if(qName.equals("position")){
            //xmlComponent.setPrenom(buffer.toString());
            buffer = null;
            inPosition = false;
        }else if(qName.equals("color")){
            //xmlComponent.setPrenom(buffer.toString());
            buffer = null;
            inColor = false;
        }else if(qName.equals("size")){
            //xmlComponent.setPrenom(buffer.toString());
            buffer = null;
            inSize = false;
        }else{
            //erreur, on peut lever une exception
            throw new SAXException("Balise </"+qName+"> inconnue.");
        }          
    }
    //détection de caractères
    public void characters(char[] ch,int start, int length)
            throws SAXException{
        String lecture = new String(ch,start,length);
        if(buffer != null) buffer.append(lecture);       
    }
    //début du parsing
    public void startDocument() throws SAXException {
        System.out.println("Début du parsing");
    }
    //fin du parsing
    public void endDocument() throws SAXException {
        System.out.println("Fin du parsing");
        System.out.println("Resultats du parsing");
        for(XmlComponent p : xmlCompList){
            System.out.println(p);
        }
    }
 
    public List<XmlComponent> getComponents(){
        return xmlCompList;
    }
} | 
Partager