Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement JAXBElement.

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:io.onedecision.engine.decisions.impl.DecisionModelFactory.java

@Override
public void write(Definitions dm, Writer out) throws IOException {
    JAXBContext context;//from  www.j a  va 2s.c  om
    try {
        context = JAXBContext.newInstance(Definitions.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Since no @XmlRootElement generated for Definitions need to create
        // element wrapper here. See
        // https://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html
        m.marshal(new JAXBElement<Definitions>(DEFINITIONS, Definitions.class, dm), out);
    } catch (JAXBException e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextFactoryTest.java

@Test
public void emptySetOfClasses() throws Exception {
    JAXBContext ctx = SweJaxbContextFactory.createContext(new Class[] {}, null);
    ctx.createMarshaller().marshal(/*w  w w  .  j  a  v a  2s  . co m*/
            new JAXBElement<String>(new QName("urn:test", "str"), String.class, "content"), System.out);
}

From source file:org.fcrepo.oai.AbstractOAIProviderIT.java

@PostConstruct
public void initTests() throws Exception {
    /* Check and/or add the default Identify response */
    if (!defaultIdentityResponseExists()) {
        IdentifyType id = oaiFactory.createIdentifyType();
        id.setRepositoryName("Fedora 4 Test Instance");
        id.setBaseURL(serverAddress);//from ww  w . j  ava 2  s  . c  o  m

        HttpPost post = new HttpPost(serverAddress + "/oai/identify/fcr:content");
        StringWriter data = new StringWriter();
        marshaller.marshal(new JAXBElement<IdentifyType>(new QName("Identify"), IdentifyType.class, id), data);
        post.setEntity(new StringEntity(data.toString()));
        try {
            HttpResponse resp = this.client.execute(post);
            assertEquals(201, resp.getStatusLine().getStatusCode());
        } finally {
            post.releaseConnection();
        }
    }
}

From source file:de.tudarmstadt.ukp.integration.alignment.xml.AlignmentXmlWriter.java

public void writeMetaData(XmlMeta meta) throws IOException {
    try {/*from w  ww.ja  va  2s  . c  om*/
        marshaller.marshal(new JAXBElement<XmlMeta>(new QName("metadata"), XmlMeta.class, meta),
                xmlEventWriter);
    } catch (JAXBException e) {
        throw new IOException(e);
    }

}

From source file:org.javelin.sws.ext.bind.SweJaxbContextFactoryTest.java

@Test
public void scanExceptionClassForJaxbRi() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(IllegalArgumentException.class);
    System.out.println(ctx.toString());
    ctx.createMarshaller().marshal(new JAXBElement<IllegalArgumentException>(new QName("urn:test", "e"),
            IllegalArgumentException.class, new IllegalArgumentException("exception")), System.out);
}

From source file:org.cleverbus.common.Tools.java

/**
 * Marshals object graph into XML.//from w w  w  . j av  a 2 s.co m
 *
 * @param obj   the object graph
 * @param qName the QName
 * @return XML as string
 * @see Marshaller
 */
@SuppressWarnings("unchecked")
public static <T> String marshalToXml(T obj, QName qName) {
    StringWriter stringWriter = new StringWriter();

    try {
        Marshaller marshaller = JAXBContext.newInstance(obj.getClass()).createMarshaller();
        Object element;
        if (qName != null) {
            element = new JAXBElement<T>(qName, (Class<T>) obj.getClass(), obj);
        } else {
            qName = new QName(obj.getClass().getPackage().getName(), obj.getClass().getSimpleName());
            element = new JAXBElement<T>(qName, (Class<T>) obj.getClass(), obj);
        }
        marshaller.marshal(element, stringWriter);
        return stringWriter.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.tangfan.test.UserServiceTest.java

/**
 * add jaxws-ri???//  ww w  . j  a v  a2  s . c om
 */
@Test
public void add() {
    try {
        //1.?xml
        JAXBContext jaxb = JAXBContext.newInstance(LicenseInfo.class);
        User lu = new User();
        lu.setId(0);
        lu.setNickname("");
        lu.setUsername("admin");
        lu.setPassword("123");
        LicenseInfo info = new LicenseInfo();
        info.setLoginUser(lu);

        QName qName = new QName(ns, "licenseInfo");
        JAXBElement<LicenseInfo> jele = new JAXBElement<LicenseInfo>(qName, LicenseInfo.class, info);

        Marshaller mars = jaxb.createMarshaller();
        mars.setProperty(Marshaller.JAXB_FRAGMENT, true);//xml
        mars.setProperty(Marshaller.JAXB_ENCODING, "utf-8");//?

        //2.?dom
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        mars.marshal(jele, doc);

        //3.Headers.create?header
        WSBindingProvider wsb = (WSBindingProvider) port;
        wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement()));

        User u = new User();
        u.setId(2);
        u.setUsername("master");
        u.setNickname("jboss?");
        u.setPassword("123456");
        port.add(u);
    } catch (UserException_Exception e) {
        System.out.println(e.getMessage());
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:de.tudarmstadt.ukp.integration.alignment.xml.AlignmentXmlWriter.java

public void writeAlignments(Alignments alignments) throws IOException {
    try {//from w ww.j a va 2 s  . c o  m
        marshaller.marshal(new JAXBElement<Alignments>(new QName("alignments"), Alignments.class, alignments),
                xmlEventWriter);
    } catch (JAXBException e) {
        throw new IOException(e);
    }

}

From source file:org.javelin.sws.ext.bind.internal.model.ClassHierarchyTest.java

@Test
public void handleXmlAccessTypeNONE() throws Exception {
    // f3, f4, p3, p4 - explicitely annotated
    System.out.println("\nC1");
    JAXBContext.newInstance(C1.class).createMarshaller()
            .marshal(new JAXBElement<C1>(new QName("", "r"), C1.class, new C1()), System.out);
    JAXBContext ctx = SweJaxbContextFactory.createContext(new Class[] { C1.class }, null);
    Map<Class<?>, TypedPattern<?>> patterns = (Map<Class<?>, TypedPattern<?>>) ReflectionTestUtils.getField(ctx,
            "patterns");
    ComplexTypePattern<C1> pattern = (ComplexTypePattern<C1>) patterns.get(C1.class);
    Map<QName, PropertyMetadata<C1, ?>> elements = (Map<QName, PropertyMetadata<C1, ?>>) ReflectionTestUtils
            .getField(pattern, "elements");
    assertThat(elements.size(), equalTo(4));
    assertTrue(elements.containsKey(new QName("", "f3")));
    assertTrue(elements.containsKey(new QName("", "f4")));
    assertTrue(elements.containsKey(new QName("", "p3")));
    assertTrue(elements.containsKey(new QName("", "p4")));
}

From source file:integration.report.ReportIT.java

@PostConstruct
public void initTests() throws Exception {
    /* Check and/or add the default Identify response */
    if (!defaultIdentityResponseExists()) {
        IdentifyType id = oaiFactory.createIdentifyType();
        id.setRepositoryName("Fedora 4 Test Instance");
        id.setBaseURL(serverAddress);/*www  . ja v  a  2s . c  o m*/

        HttpPost post = new HttpPost(serverAddress);
        StringWriter data = new StringWriter();
        marshaller.marshal(new JAXBElement<IdentifyType>(new QName("Identify"), IdentifyType.class, id), data);
        post.setEntity(new StringEntity(data.toString()));
        post.addHeader("Content-Type", "application/octet-stream");
        post.addHeader("Slug", "oai_identify");
        try {
            HttpResponse resp = this.client.execute(post);
            assertEquals(201, resp.getStatusLine().getStatusCode());
        } finally {
            post.releaseConnection();
        }
    }
}