KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > impl > JMValidatorImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

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 /** Simple validator implementation. Works by creating a
31  * {@link javax.xml.bind.Marshaller}, which generates SAX events.
32  * The SAX events are piped into an instance of
33  * {@link javax.xml.bind.UnmarshallerHandler}.
34  */

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 JavaDoc 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 JavaDoc pObject) throws JAXBException {
61         return validate(pObject);
62     }
63 }
64
Popular Tags