Search in sources :

Example 21 with JAXBContext

use of javax.xml.bind.JAXBContext 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 22 with JAXBContext

use of javax.xml.bind.JAXBContext 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 23 with JAXBContext

use of javax.xml.bind.JAXBContext 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)

Example 24 with JAXBContext

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

the class ResourceDoclet method getSerializedLinkFromTag.

private static String getSerializedLinkFromTag(final SeeTag linkTag) {
    final MemberDoc referencedMember = linkTag.referencedMember();
    if (referencedMember == null) {
        throw new NullPointerException("Referenced member of @link " + print(linkTag) + " cannot be resolved.");
    }
    if (!referencedMember.isStatic()) {
        LOG.warning("Referenced member of @link " + print(linkTag) + " is not static." + " Right now only references to static members are supported.");
        return null;
    }
    /* Get referenced example bean
         */
    final ClassDoc containingClass = referencedMember.containingClass();
    final Object object;
    try {
        final Field declaredField = Class.forName(containingClass.qualifiedName(), false, Thread.currentThread().getContextClassLoader()).getDeclaredField(referencedMember.name());
        if (referencedMember.isFinal()) {
            declaredField.setAccessible(true);
        }
        object = declaredField.get(null);
        LOG.log(Level.FINE, "Got object " + object);
    } catch (final Exception e) {
        LOG.info("Have classloader: " + ResourceDoclet.class.getClassLoader().getClass());
        LOG.info("Have thread classloader " + Thread.currentThread().getContextClassLoader().getClass());
        LOG.info("Have system classloader " + ClassLoader.getSystemClassLoader().getClass());
        LOG.log(Level.SEVERE, "Could not get field " + referencedMember.qualifiedName(), e);
        return null;
    }
    /* marshal the bean to xml
         */
    try {
        final JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        final StringWriter stringWriter = new StringWriter();
        final Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, stringWriter);
        final String result = stringWriter.getBuffer().toString();
        LOG.log(Level.FINE, "Got marshalled output:\n" + result);
        return result;
    } catch (final Exception e) {
        LOG.log(Level.SEVERE, "Could serialize bean to xml: " + object, e);
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) MemberDoc(com.sun.javadoc.MemberDoc) MalformedURLException(java.net.MalformedURLException) ClassDoc(com.sun.javadoc.ClassDoc)

Example 25 with JAXBContext

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

the class JaxbTransformationTest method testJaxbOrderAckToXML.

@Test
public void testJaxbOrderAckToXML() throws Exception {
    OrderAck orderAck = new OrderAck();
    orderAck.setAccepted(Boolean.TRUE);
    orderAck.setOrderId("PO-19838-XYZ");
    orderAck.setStatus("Order Accepted");
    JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { OrderAck.class });
    StringWriter resultWriter = new StringWriter();
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(orderAck, resultWriter);
    _testKit.compareXMLToResource(resultWriter.toString(), ORDER_ACK_XML);
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

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