1 17 package org.apache.ws.jaxme.impl; 18 19 import javax.xml.bind.JAXBContext; 20 import javax.xml.bind.JAXBException; 21 import javax.xml.bind.Marshaller; 22 import javax.xml.bind.Unmarshaller; 23 import javax.xml.bind.UnmarshallerHandler; 24 import javax.xml.bind.ValidationEvent; 25 import javax.xml.bind.ValidationEventHandler; 26 27 import org.apache.ws.jaxme.JMValidator; 28 29 30 35 public class JMValidatorImpl extends JMControllerImpl implements JMValidator { 36 private class MyEventHandler implements ValidationEventHandler { 37 private boolean invalid; 38 public boolean handleEvent(ValidationEvent pEvent) { 39 invalid = true; 40 ValidationEventHandler eh = getEventHandler(); 41 if (eh == null) { 42 return true; 43 } else { 44 return eh.handleEvent(pEvent); 45 } 46 } 47 } 48 49 public boolean validate(Object pObject) throws JAXBException { 50 JAXBContext jc = getJAXBContextImpl(); 51 Marshaller m = jc.createMarshaller(); 52 Unmarshaller u = jc.createUnmarshaller(); 53 MyEventHandler eh = new MyEventHandler(); 54 u.setEventHandler(eh); 55 UnmarshallerHandler uh = u.getUnmarshallerHandler(); 56 m.marshal(pObject, uh); 57 return !eh.invalid; 58 } 59 60 public boolean validateRoot(Object pObject) throws JAXBException { 61 return validate(pObject); 62 } 63 } 64 | Popular Tags |