users@jaxb.java.net

validationEventCollector problem

From: bruno guedes-prestataire <bruno.guedes-prestataire_at_cncc.fr>
Date: Fri, 22 Feb 2008 17:32:53 +0100

Hi

I've coded this kind of method :

      public Object unMarshall(
                  InputStream fileToUnmarshall,
                  String packageClass) throws JAXBException,
                                              GestionMandatsMetierException,
                                              SAXException,
                                              ParserConfigurationException {

        JAXBContext jaxbContext = JAXBContext.newInstance(packageClass);
        Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL urlFichierProp = Thread.currentThread().getContextClassLoader().getResource("importDeclaration2008.xsd");
            Schema schema = schemaFactory.newSchema(new File(urlFichierProp.getFile()));
            unMarshaller.setSchema(schema);

            ValidationEventCollector validationEventCollector = new ValidationEventCollector();
            unMarshaller.setEventHandler(validationEventCollector);

            Object objectUnmarshalled = null;

        try {
            objectUnmarshalled =
                unMarshaller.unmarshal(fileToUnmarshall);
        }
        catch (JAXBException jaxbe) {

            String events = "\n";

            if (validationEventCollector.hasEvents()) {
                  for (ValidationEvent event : validationEventCollector.getEvents()) {
                        events = events + "\n" + "message d'erreur : " +
                        event.getMessage() +
                        " - ligne : " +
                        event.getLocator().getLineNumber() +
                        " colonne : " +
                        event.getLocator().getColumnNumber() +
                        " de l'erreur.";
                  }
                  System.out.println(events);
            }

                  throw new GestionMandatsMetierException(
                             "Le fichier d'import " +
                             " n'est pas valide ! " +
                             "liste des erreur : " + events);
        }

        return objectUnmarshalled;
      }

I've a XML instance not passing validation for two lind of thing :

One a date error
A number pattern not valid

But in the events array, I've only one error at a time, not the array with the two errors !!

Could someone tell me what I've done wrong please ..

Thank's for any help

Have fun