Search in sources :

Example 11 with Marshaller

use of javax.xml.bind.Marshaller in project feign by OpenFeign.

the class JAXBContextFactoryTest method buildsMarshallerWithSchemaLocationProperty.

@Test
public void buildsMarshallerWithSchemaLocationProperty() throws Exception {
    JAXBContextFactory factory = new JAXBContextFactory.Builder().withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd").build();
    Marshaller marshaller = factory.createMarshaller(Object.class);
    assertEquals("http://apihost http://apihost/schema.xsd", marshaller.getProperty(Marshaller.JAXB_SCHEMA_LOCATION));
}
Also used : Marshaller(javax.xml.bind.Marshaller) Test(org.junit.Test)

Example 12 with Marshaller

use of javax.xml.bind.Marshaller in project feign by OpenFeign.

the class JAXBContextFactoryTest method buildsMarshallerWithNoNamespaceSchemaLocationProperty.

@Test
public void buildsMarshallerWithNoNamespaceSchemaLocationProperty() throws Exception {
    JAXBContextFactory factory = new JAXBContextFactory.Builder().withMarshallerNoNamespaceSchemaLocation("http://apihost/schema.xsd").build();
    Marshaller marshaller = factory.createMarshaller(Object.class);
    assertEquals("http://apihost/schema.xsd", marshaller.getProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION));
}
Also used : Marshaller(javax.xml.bind.Marshaller) Test(org.junit.Test)

Example 13 with Marshaller

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

the class JAXBUtil method marshal.

/**
     * Returns string representation of {@link GreetingRequest}.
     * 
     * @param rq Request.
     * @return XML representation.
     * @throws JAXBException
     */
public static String marshal(GreetingRequest rq) throws JAXBException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Marshaller marshaller = CONTEXT.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(rq, bos);
    return new String(bos.toByteArray());
}
Also used : Marshaller(javax.xml.bind.Marshaller) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 14 with Marshaller

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

the class WadlResource method getWadl.

@Produces({ "application/vnd.sun.wadl+xml", "application/xml" })
@GET
public synchronized Response getWadl(@Context UriInfo uriInfo) {
    try {
        if (!wadlContext.isWadlGenerationEnabled()) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        final boolean detailedWadl = WadlUtils.isDetailedWadlRequested(uriInfo);
        if ((wadlXmlRepresentation == null) || (!isCached(uriInfo, detailedWadl))) {
            this.lastBaseUri = uriInfo.getBaseUri();
            lastDetailedWadl = detailedWadl;
            this.lastModified = new SimpleDateFormat(HTTPDATEFORMAT).format(new Date());
            ApplicationDescription applicationDescription = wadlContext.getApplication(uriInfo, detailedWadl);
            Application application = applicationDescription.getApplication();
            try {
                final Marshaller marshaller = wadlContext.getJAXBContext().createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                marshaller.marshal(application, os);
                wadlXmlRepresentation = os.toByteArray();
                os.close();
            } catch (Exception e) {
                throw new ProcessingException("Could not marshal the wadl Application.", e);
            }
        }
        return Response.ok(new ByteArrayInputStream(wadlXmlRepresentation)).header("Last-modified", lastModified).build();
    } catch (Exception e) {
        throw new ProcessingException("Error generating /application.wadl.", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) Application(com.sun.research.ws.wadl.Application) Date(java.util.Date) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with Marshaller

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

the class AbstractCollectionJaxbProvider method writeTo.

@Override
public final void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
    try {
        final Collection c = (type.isArray()) ? Arrays.asList((Object[]) t) : (Collection) t;
        final Class elementType = getElementClass(type, genericType);
        final Charset charset = getCharset(mediaType);
        final String charsetName = charset.name();
        final Marshaller m = getMarshaller(elementType, mediaType);
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);
        if (charset != UTF8) {
            m.setProperty(Marshaller.JAXB_ENCODING, charsetName);
        }
        setHeader(m, annotations);
        writeCollection(elementType, c, mediaType, charset, m, entityStream);
    } catch (JAXBException ex) {
        throw new InternalServerErrorException(ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) Collection(java.util.Collection) Charset(java.nio.charset.Charset) InternalServerErrorException(javax.ws.rs.InternalServerErrorException)

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