Hi Steve, I guess you are 100% right with what you are saying. But the problem with this (well known) approach is that validation can only occur *after* the XML has been produced. Which -imho - is late, if not too late (in some cases).
Castor XML historically has always supported object validation *before* marshalling has taken place (if validators are being used). In other words, if you are starting with an XML schema and you are using the XML code generator to generate Java and descriptor classes, the latter ones will include code that validates the semantics defined in your XML schema at the Java object level. If you are populating your objects with incorrect values, why forcing our users to the complete round-trip (XML marshalling, validation at the XML level) when you can let them know earlier. For those users not using the XML code generator, though, it might be a nice option to add XML validation at the end of the marshalling cycle. Regards Werner [email protected] wrote: > Actually Werner, I discovered that it's pretty easy to validate an XML > document, using the javax.xml.validation classes. And in my particular > case, I just want to validate it after marshalling, not unmarshall it. > > FWIW, here is my validation code: > > import javax.xml.validation.Schema; > import javax.xml.validation.SchemaFactory; > import javax.xml.validation.Validator; > > … > > SchemaFactory factory = > SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); > > InputStream schemaInputStream = > getClass().getResourceAsStream(“myschema.xsd”); > Schema schema = factory.newSchema(new StreamSource(schemaInputStream)); > > Validator xmlValidator = schema.newValidator(); > > String xml = "xml document contents go here”; > StreamSource xmlSource = new StreamSource(new StringReader(xml)); > > try { > xmlValidator.validate(xmlSource); > > } catch (SAXException e) { > // handle validation errors > e.printStackTrace(); > } catch (Exception e) { > e.printStackTrace(); > } > > And if you want to get *all* validation errors, you can use your own > ErrorHandler implementation: > > xmlValidator.setErrorHandler(new MyErrorHandler()); > > Where MyErrorHandler is defined as: > > class MyErrorHandler extends org.xml.sax.helpers.DefaultHandler { > public List<SAXParseException> errors = new ArrayList(); > > public void error(SAXParseException e) throws SAXException { > // add the error to our list but don't throw it, so the validator can > continue > errors.add(e); > } > } > > You could also implement org.xml.sax.ErrorHandler directly, adding > warning() and fatalError() methods. > > -Steve > > > On Jun 4, 2009 2:40am, Werner Guttmann <[email protected]> wrote: >> Steve, > > > >> there's one (or more) existing Jira issues out there which is about > >> adding exactly this feature. As far as I remember, the code base holds a > >> 50% solution, but there's still some areas to be added/coded to enable > >> the described behaviour. > > > >> Fell free to have a look at the code base, the Jira issues and let us > >> know whether you'd like to contribute. > > > >> Cheers > >> Werner > > > >> Steve Kingsland wrote: > >> > I have configured my castor.properties file so that I'm now getting >> my XML > >> > documents validated against the Schema when I unmarshall, by adding >> these > >> > lines: > >> > > >> > org.exolab.castor.parser.namespaces=true > >> > > >> > org.exolab.castor.sax.features=http://xml.org/sax/features/validation,\ > >> > > >> > http://apache.org/xml/features/validation/schema,\ > >> > > >> > http://apache.org/xml/features/validation/schema-full-checking > >> > > >> > But I notice that I get a “MarshalException” when the document fails to > >> > validate, instead of a “ValidationException”. For example: > >> > > >> > *org.exolab.castor.xml.MarshalException*: cvc-enumeration-valid: Value > >> > 'PFPa' is not facet-valid with respect to enumeration '[PFP, MTBS]'. >> It must > >> > be a value from the enumeration.{File: [not available]; line: 6; >> column: 54} > >> > > >> > at > >> > >> org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(* >> > >> > Unmarshaller.java:794*) > >> > > >> > at org.exolab.castor.xml.Unmarshaller.unmarshal(*Unmarshaller.java:760 > >> > *) > >> > > >> > at org.exolab.castor.xml.Unmarshaller.unmarshal(*Unmarshaller.java:626 > >> > *) > >> > > >> > at org.CastorTest.validateXml(*CmhToStamps.java:378*) > >> > > >> > at org.CastorTest.testMarshallGeneric(*CmhToStamps.java:242*) > >> > > >> > at org.CastorTest.main(*CmhToStamps.java:55*) > >> > > >> > Caused by: *org.xml.sax.SAXParseException*: cvc-enumeration-valid: >> Value > >> > 'PFPa' is not facet-valid with respect to enumeration '[PFP, MTBS]'. >> It must > >> > be a value from the enumeration. > >> > > >> > at > >> > >> org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown >> > >> > Source) > >> > > >> > at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) > >> > > >> > ... > >> > > >> > (etc.) > >> > > >> > That's fine, but ideally I'd like the XML parser to inform me of >> **all** > >> > validation errors, not just the first one it comes across. And >> looking at > >> > the ValidationException class, I see a getNext() method which looks >> like it > >> > was meant to support this feature! So how do I get Castor to throw a > >> > ValidationException when my XML document fails validation? > >> > > >> > Or another way of putting it is, is there a way for me to validate XML > >> > documents with Castor so that I find out about **all** problems, and >> not > >> > just the first one? I'm ok changing XML parsers, if that's what's >> needed. > >> > > >> > -Steve > >> > > >> > ps – I'm using the latest version of Castor, and Xerces. > >> > > > > >> --------------------------------------------------------------------- > >> To unsubscribe from this list, please visit: > > > >> http://xircles.codehaus.org/manage_email > > > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

