ws-jaxme-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joc...@apache.org
Subject cvs commit: ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl JMValidatorImpl.java
Date Mon, 21 Feb 2005 09:15:23 GMT
jochen      2005/02/21 01:15:23

  Modified:    src/jaxme/org/apache/ws/jaxme/impl JMValidatorImpl.java
  Log:
  Simple implementation of ValidationEventHandler.
  
  Revision  Changes    Path
  1.3       +43 -9     ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMValidatorImpl.java
  
  Index: JMValidatorImpl.java
  ===================================================================
  RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMValidatorImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JMValidatorImpl.java	16 Feb 2004 23:39:57 -0000	1.2
  +++ JMValidatorImpl.java	21 Feb 2005 09:15:23 -0000	1.3
  @@ -16,19 +16,53 @@
    */
   package org.apache.ws.jaxme.impl;
   
  +import javax.xml.bind.JAXBContext;
   import javax.xml.bind.JAXBException;
  +import javax.xml.bind.Marshaller;
  +import javax.xml.bind.Unmarshaller;
  +import javax.xml.bind.UnmarshallerHandler;
  +import javax.xml.bind.ValidationEvent;
  +import javax.xml.bind.ValidationEventHandler;
   
   import org.apache.ws.jaxme.JMValidator;
   
  -/**
  - * @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
  - * @version $Id$
  +
  +/** Simple validator implementation. Works by creating a
  + * {@link javax.xml.bind.Marshaller}, which generates SAX events.
  + * The SAX events are piped into an instance of
  + * {@link javax.xml.bind.UnmarshallerHandler}.
    */
   public class JMValidatorImpl extends JMControllerImpl implements JMValidator {
  -  public boolean validate(Object pObject) throws JAXBException {
  -    return true;
  -  }
  -  public boolean validateRoot(Object pObject) throws JAXBException {
  -    return true;
  -  }
  +    private class MyEventHandler implements ValidationEventHandler {
  +        private boolean invalid;
  +        public boolean handleEvent(ValidationEvent pEvent) {
  +            int severity = pEvent.getSeverity();
  +            boolean result = severity == ValidationEvent.ERROR
  +                         ||  severity == ValidationEvent.FATAL_ERROR;
  +            if (result) {
  +                invalid = true;
  +            }
  +            ValidationEventHandler eh = getEventHandler();
  +            if (eh == null) {
  +                return result;
  +            } else {
  +                return eh.handleEvent(pEvent);
  +            }
  +        }
  +    }
  +
  +    public boolean validate(Object pObject) throws JAXBException {
  +        JAXBContext jc = getJAXBContextImpl();
  +        Marshaller m = jc.createMarshaller();
  +        Unmarshaller u = jc.createUnmarshaller();
  +        MyEventHandler eh = new MyEventHandler();
  +        u.setEventHandler(eh);
  +        UnmarshallerHandler uh = u.getUnmarshallerHandler();
  +        m.marshal(pObject, uh);
  +        return !eh.invalid;
  +    }
  +
  +    public boolean validateRoot(Object pObject) throws JAXBException {
  +        throw new IllegalStateException("Not implemented, use validate(Object)");
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org


Mime
View raw message