Search in sources :

Example 16 with JAXBContext

use of javax.xml.bind.JAXBContext in project jOOQ by jOOQ.

the class GenerationTool method load.

/**
     * Load a jOOQ codegen configuration file from an input stream
     */
public static Configuration load(InputStream in) throws IOException {
    // [#1149] If there is no namespace defined, add the default one
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    copyLarge(in, out);
    String xml = out.toString();
    // TODO [#1201] Add better error handling here
    xml = xml.replaceAll("<(\\w+:)?configuration xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-codegen-\\d+\\.\\d+\\.\\d+.xsd\">", "<$1configuration xmlns$2=\"" + Constants.NS_CODEGEN + "\">");
    xml = xml.replace("<configuration>", "<configuration xmlns=\"" + Constants.NS_CODEGEN + "\">");
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        javax.xml.validation.Schema schema = sf.newSchema(GenerationTool.class.getResource("/xsd/" + Constants.XSD_CODEGEN));
        JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                log.warn("Unmarshal warning", event.getMessage());
                return true;
            }
        });
        return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new GeneratorException("Error while reading XML configuration", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) Configuration(org.jooq.util.jaxb.Configuration) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) SQLException(java.sql.SQLException) IOException(java.io.IOException) ValidationEvent(javax.xml.bind.ValidationEvent) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 17 with JAXBContext

use of javax.xml.bind.JAXBContext in project jersey by jersey.

the class AbstractJaxbProvider method getUnmarshaller.

/**
     * Get the JAXB unmarshaller for the given class and media type.
     * <p>
     * In case this provider instance has been {@link #AbstractJaxbProvider(Providers, MediaType)
     * created with a fixed resolver media type}, the supplied media type argument will be ignored.
     * </p>
     *
     * @param type      Java type to be unmarshalled.
     * @param mediaType entity media type.
     * @return JAXB unmarshaller for the requested Java type, media type combination.
     * @throws JAXBException in case retrieving the unmarshaller fails with a JAXB exception.
     */
protected final Unmarshaller getUnmarshaller(Class type, MediaType mediaType) throws JAXBException {
    if (fixedResolverMediaType) {
        return getUnmarshaller(type);
    }
    final ContextResolver<Unmarshaller> unmarshallerResolver = jaxrsProviders.getContextResolver(Unmarshaller.class, mediaType);
    if (unmarshallerResolver != null) {
        Unmarshaller u = unmarshallerResolver.getContext(type);
        if (u != null) {
            return u;
        }
    }
    final JAXBContext ctx = getJAXBContext(type, mediaType);
    return (ctx == null) ? null : ctx.createUnmarshaller();
}
Also used : JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 18 with JAXBContext

use of javax.xml.bind.JAXBContext in project jersey by jersey.

the class AbstractJaxbProvider method getMarshaller.

/**
     * Get the JAXB marshaller for the given class and media type.
     * <p>
     * In case this provider instance has been {@link #AbstractJaxbProvider(Providers, MediaType)
     * created with a fixed resolver media type}, the supplied media type argument will be ignored.
     * </p>
     *
     * @param type      Java type to be marshalled.
     * @param mediaType entity media type.
     * @return JAXB marshaller for the requested Java type, media type combination.
     * @throws JAXBException in case retrieving the marshaller fails with a JAXB exception.
     */
protected final Marshaller getMarshaller(Class type, MediaType mediaType) throws JAXBException {
    if (fixedResolverMediaType) {
        return getMarshaller(type);
    }
    final ContextResolver<Marshaller> mcr = jaxrsProviders.getContextResolver(Marshaller.class, mediaType);
    if (mcr != null) {
        Marshaller m = mcr.getContext(type);
        if (m != null) {
            return m;
        }
    }
    final JAXBContext ctx = getJAXBContext(type, mediaType);
    if (ctx == null) {
        return null;
    }
    Marshaller m = ctx.createMarshaller();
    if (formattedOutput.get()) {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput.get());
    }
    return m;
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext)

Example 19 with JAXBContext

use of javax.xml.bind.JAXBContext in project jersey by jersey.

the class AbstractJaxbProvider method getMarshaller.

private Marshaller getMarshaller(Class type) throws JAXBException {
    final ContextResolver<Marshaller> resolver = mtMarshaller.get();
    if (resolver != null) {
        Marshaller u = resolver.getContext(type);
        if (u != null) {
            return u;
        }
    }
    final JAXBContext ctx = getJAXBContext(type);
    if (ctx == null) {
        return null;
    }
    Marshaller m = ctx.createMarshaller();
    if (formattedOutput.get()) {
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput.get());
    }
    return m;
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext)

Example 20 with JAXBContext

use of javax.xml.bind.JAXBContext in project jersey by jersey.

the class JaxbStringReaderProvider method getStoredJAXBContext.

/**
     * Get the stored JAXB context supporting the Java type.
     *
     * @param type Java type supported by the stored JAXB context.
     * @return stored JAXB context supporting the Java type.
     * @throws JAXBException in case JAXB context retrieval fails.
     */
protected JAXBContext getStoredJAXBContext(Class type) throws JAXBException {
    synchronized (jaxbContexts) {
        JAXBContext c = jaxbContexts.get(type);
        if (c == null) {
            c = JAXBContext.newInstance(type);
            jaxbContexts.put(type, c);
        }
        return c;
    }
}
Also used : JAXBContext(javax.xml.bind.JAXBContext)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)442 Unmarshaller (javax.xml.bind.Unmarshaller)240 Marshaller (javax.xml.bind.Marshaller)129 JAXBException (javax.xml.bind.JAXBException)128 Test (org.junit.Test)92 InputStream (java.io.InputStream)84 File (java.io.File)50 StringWriter (java.io.StringWriter)47 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 StringReader (java.io.StringReader)27 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)21 ArrayList (java.util.ArrayList)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 JAXBElement (javax.xml.bind.JAXBElement)19 FileOutputStream (java.io.FileOutputStream)18 Schema (javax.xml.validation.Schema)18 SchemaFactory (javax.xml.validation.SchemaFactory)17 SAXSource (javax.xml.transform.sax.SAXSource)14