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
   |    public int XMLValidation(String pXmlPath) {
        File lFichierXml;
        DocumentBuilderFactory lDbf;
        DocumentBuilder lDb;
 
        if (fichierXsd == null) {
            throw new NullPointerException();
        }
        else {
            lFichierXml = new File(pXmlPath);
            //Chargement du fichier d'instanciation en mémoire puis enregistrement temporaire sur le disque
            //apres suppression des URI relatives au fichier XSD, de maniere a n'utiliser que le schema donné dans le fichier de conf
            Document lDoc = XmlUtils.recupererDocument(pXmlPath);
            if (lFichierXml.exists()) {
                lDbf = DocumentBuilderFactory.newInstance();
                lDbf.setValidating(true);
                lDbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
                lDbf.setAttribute(JAXP_SCHEMA_SOURCE, fichierXsd);
                Node lSL = XmlUtils.getNode(lDoc, "/gpm").getAttributes().getNamedItem("xsi:schemaLocation");
//Je vide les valeur des attributs du tag gpm mais ca change rien.
                if(lSL != null){
                    lSL.setNodeValue("");
                }
                Node lX = XmlUtils.getNode(lDoc, "/gpm").getAttributes().getNamedItem("xmlns:xsi");
                if(lX != null){
                    lX.setNodeValue("");
                }
                String lFile = SaveDoc(lDoc);
                try {
                    lDb = lDbf.newDocumentBuilder();
                    lDb.setErrorHandler(errorHandler);
                    lDb.parse(lFile);
                    if (errorHandler.isError()) {
                        throw new AdmExceptionErreurValidationXSD(
                                        "Le fichier généré n'est pas un fichier gPM valide"
                                                        + System.getProperty("line.separator")
                                                        + "Vérifier que le fichier de transformation xsl est correct");
                }
 
                }
                catch (ParserConfigurationException lPce) {
                    log.message("XMLValidation : " + lPce);
                    return 2;
                }
                catch (IOException lIoe) {
                    log.message("XMLValisation : " + lIoe);
                    return 2;
                }
                catch (SAXException lSae) {
                    log.message("XMLVAlidation : " + lSae);
                    return 2;
                }
                log.message("Le fichier XML a été validé par la XSD avec succes");
 
                File lF = new File(lFile);
                if(lF.exists()){
                    lF.delete();
                }
            }
            else {
                throw new FichierManquantException("Le fichier xml "
                        + pXmlPath + " n'existe pas");
            }
        }
        return 0;
    } | 
Partager