users@jax-ws.java.net

Re: Validating mandatory parameters of XML using JAXB 2.0

From: <breadfan_at_gmx.de>
Date: Thu, 19 Apr 2007 11:14:18 +0200

Hello,

we use the following code to check the parameters of a jaxws call/response against a schema, it's working quite fine, but is definitely slow.

        try
        {
            JAXBContext jaxbContext = JAXBContext.newInstance(packages);

            Marshaller marshaller = jaxbContext.createMarshaller();
            SchemaFactory schemaFactory = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            URL schemaURL = Thread.currentThread().getContextClassLoader().getResource(
                schemaLocation);

            if (schemaURL == null)
            {
                throw new RuntimeException("Schema for validation cannot be found in classpath: "
                    + schemaLocation);
            }

            Schema schema = schemaFactory.newSchema(schemaURL);
            marshaller.setSchema(schema);

            String className = ClassUtils.getShortName(object.getClass());
            String elementName = className.substring(0, 1).toLowerCase() + className.substring(1);

            JAXBElement jaxbElement = new JAXBElement(new QName(schemaNamespace, elementName),
                Object.class, object);

            if (LOGGER.isDebugEnabled())
            {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                marshaller.marshal(jaxbElement, os);
                LOGGER.debug("Marshalled Soap Transfer Object: " + os.toString());
            }
            else
            {
                OutputStream os = new NullOutputStream();
                marshaller.marshal(jaxbElement, os);
            }
        }
        catch (JAXBException e)
        {
            String errorMessage = buildValidationErrorMessage(pathInfo, object, e);
            LOGGER.debug(errorMessage);
            throw new RuntimeException(errorMessage);
        }
        catch (SAXException e)
        {
            String errorMessage = buildValidationErrorMessage(pathInfo, object, e);
            LOGGER.debug(errorMessage);
            throw new RuntimeException(errorMessage);
        }


Best regards,
Chris
-- 
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail