Search in sources :

Example 16 with Marshaller

use of javax.xml.bind.Marshaller 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 17 with Marshaller

use of javax.xml.bind.Marshaller 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 18 with Marshaller

use of javax.xml.bind.Marshaller in project quickstarts by jboss-switchyard.

the class Library method toString.

public String toString(boolean detailed) {
    StringWriter sw = new StringWriter();
    synchronized (librarian) {
        try {
            if (detailed) {
                JAXBContext ctx = JAXBContext.newInstance("org.switchyard.quickstarts.demos.library.types");
                Marshaller m = ctx.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
                ObjectFactory of = new ObjectFactory();
                for (Book book : isbns_to_books.values()) {
                    int quantity = isbns_to_quantities.get(book.getIsbn());
                    sw.write("\nBook (quantity=" + quantity + ")\n");
                    m.marshal(of.createBook(book), sw);
                    sw.write('\n');
                }
            } else {
                for (Book book : isbns_to_books.values()) {
                    int quantity = isbns_to_quantities.get(book.getIsbn());
                    sw.write(book.getTitle() + " (" + quantity + ")\n");
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    return sw.toString().trim();
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) ObjectFactory(org.switchyard.quickstarts.demos.library.types.ObjectFactory) Book(org.switchyard.quickstarts.demos.library.types.Book) JAXBContext(javax.xml.bind.JAXBContext)

Example 19 with Marshaller

use of javax.xml.bind.Marshaller in project quickstarts by jboss-switchyard.

the class LibraryClient method wrapRequest.

private <T> String wrapRequest(QName name, Class<T> declaredType, T value, String pid) throws Exception {
    JAXBElement<T> e = new JAXBElement<T>(name, declaredType, null, value);
    JAXBContext ctx = JAXBContext.newInstance("org.switchyard.quickstarts.demos.library.types");
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    StringWriter sw = new StringWriter();
    String processInstanceId = pid != null ? "<bpm:processInstanceId xmlns:bpm='urn:switchyard-component-bpm:bpm:1.0'>" + pid + "</bpm:processInstanceId>" : "";
    sw.write(SOAP_REQUEST_PREFIX.replaceFirst("PID", processInstanceId));
    m.marshal(e, sw);
    sw.write(SOAP_REQUEST_SUFFIX);
    return sw.toString();
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement)

Example 20 with Marshaller

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

the class WadlGeneratorResourceDocSupportTest method wadlIsGeneratedWithUnknownCustomParameterAnnotation.

@Test
public void wadlIsGeneratedWithUnknownCustomParameterAnnotation() throws JAXBException {
    /* Set up a ClassDocType that has something for a custom-annotated parameter */
    ClassDocType cdt = new ClassDocType();
    cdt.setClassName(TestResource.class.getName());
    MethodDocType mdt = new MethodDocType();
    mdt.setMethodName("method");
    cdt.getMethodDocs().add(mdt);
    ParamDocType pdt = new ParamDocType("x", "comment about x");
    mdt.getParamDocs().add(pdt);
    AnnotationDocType adt = new AnnotationDocType();
    adt.setAnnotationTypeName(CustomParam.class.getName());
    adt.getAttributeDocs().add(new NamedValueType("value", "x"));
    pdt.getAnnotationDocs().add(adt);
    ResourceDocType rdt = new ResourceDocType();
    rdt.getDocs().add(cdt);
    /* Generate WADL for that class */
    WadlGenerator wg = new WadlGeneratorResourceDocSupport(new WadlGeneratorImpl(), rdt);
    WadlBuilder wb = new WadlBuilder(wg, false, null);
    Resource resource = Resource.from(TestResource.class);
    ApplicationDescription app = wb.generate(Collections.singletonList(resource));
    /* Confirm that it can be marshalled without error */
    StringWriter sw = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(Application.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(app.getApplication(), sw);
}
Also used : WadlGeneratorImpl(org.glassfish.jersey.server.wadl.internal.WadlGeneratorImpl) Marshaller(javax.xml.bind.Marshaller) ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) NamedValueType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.NamedValueType) Resource(org.glassfish.jersey.server.model.Resource) AnnotationDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.AnnotationDocType) WadlBuilder(org.glassfish.jersey.server.wadl.internal.WadlBuilder) JAXBContext(javax.xml.bind.JAXBContext) ApplicationDescription(org.glassfish.jersey.server.wadl.internal.ApplicationDescription) WadlGenerator(org.glassfish.jersey.server.wadl.WadlGenerator) StringWriter(java.io.StringWriter) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) WadlGeneratorResourceDocSupport(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.WadlGeneratorResourceDocSupport) ParamDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ParamDocType) Test(org.junit.Test)

Aggregations

Marshaller (javax.xml.bind.Marshaller)214 JAXBContext (javax.xml.bind.JAXBContext)129 JAXBException (javax.xml.bind.JAXBException)75 StringWriter (java.io.StringWriter)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 File (java.io.File)22 Test (org.junit.Test)21 IOException (java.io.IOException)19 FileOutputStream (java.io.FileOutputStream)18 Unmarshaller (javax.xml.bind.Unmarshaller)18 JAXBElement (javax.xml.bind.JAXBElement)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)8 InputStream (java.io.InputStream)7 QName (javax.xml.namespace.QName)7 Schema (javax.xml.validation.Schema)7 Diff (org.custommonkey.xmlunit.Diff)7 ArrayList (java.util.ArrayList)6 SchemaFactory (javax.xml.validation.SchemaFactory)6