Search in sources :

Example 1 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class CamelContextAddRouteDefinitionsFromXmlTest method parseUri.

protected Object parseUri(String uri) throws JAXBException {
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    URL resource = getClass().getResource(uri);
    assertNotNull("Cannot find resource on the classpath: " + uri, resource);
    Object value = unmarshaller.unmarshal(resource);
    return value;
}
Also used : Unmarshaller(javax.xml.bind.Unmarshaller) URL(java.net.URL)

Example 2 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class RestContextRefDefinitionHelper method cloneRestDefinition.

private static RestDefinition cloneRestDefinition(JAXBContext jaxbContext, RestDefinition def) throws JAXBException {
    Marshaller marshal = jaxbContext.createMarshaller();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    marshal.marshal(def, bos);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object clone = unmarshaller.unmarshal(bis);
    if (clone != null && clone instanceof RestDefinition) {
        RestDefinition def2 = (RestDefinition) clone;
        Iterator<VerbDefinition> verbit1 = def.getVerbs().iterator();
        Iterator<VerbDefinition> verbit2 = def2.getVerbs().iterator();
        while (verbit1.hasNext() && verbit2.hasNext()) {
            VerbDefinition verb1 = verbit1.next();
            VerbDefinition verb2 = verbit2.next();
            if (verb1.getToOrRoute() instanceof RouteDefinition && verb2.getToOrRoute() instanceof RouteDefinition) {
                RouteDefinition route1 = (RouteDefinition) verb1.getToOrRoute();
                RouteDefinition route2 = (RouteDefinition) verb2.getToOrRoute();
                // need to clone the namespaces also as they are not JAXB marshalled (as they are transient)
                Iterator<ExpressionNode> it = ProcessorDefinitionHelper.filterTypeInOutputs(route1.getOutputs(), ExpressionNode.class);
                Iterator<ExpressionNode> it2 = ProcessorDefinitionHelper.filterTypeInOutputs(route2.getOutputs(), ExpressionNode.class);
                while (it.hasNext() && it2.hasNext()) {
                    ExpressionNode node = it.next();
                    ExpressionNode node2 = it2.next();
                    NamespaceAwareExpression name = null;
                    NamespaceAwareExpression name2 = null;
                    if (node.getExpression() instanceof NamespaceAwareExpression) {
                        name = (NamespaceAwareExpression) node.getExpression();
                    }
                    if (node2.getExpression() instanceof NamespaceAwareExpression) {
                        name2 = (NamespaceAwareExpression) node2.getExpression();
                    }
                    if (name != null && name2 != null && name.getNamespaces() != null && !name.getNamespaces().isEmpty()) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.putAll(name.getNamespaces());
                        name2.setNamespaces(map);
                    }
                }
            }
        }
        return def2;
    }
    return null;
}
Also used : Marshaller(javax.xml.bind.Marshaller) RestDefinition(org.apache.camel.model.rest.RestDefinition) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) NamespaceAwareExpression(org.apache.camel.model.language.NamespaceAwareExpression)

Example 3 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class DefaultCamelContext method loadRestsDefinition.

public synchronized RestsDefinition loadRestsDefinition(InputStream is) throws Exception {
    // load routes using JAXB
    if (jaxbContext == null) {
        // must use classloader from CamelContext to have JAXB working
        jaxbContext = getModelJAXBContextFactory().newJAXBContext();
    }
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object result = unmarshaller.unmarshal(is);
    if (result == null) {
        throw new IOException("Cannot unmarshal to rests using JAXB from input stream: " + is);
    }
    // can either be routes or a single route
    RestsDefinition answer;
    if (result instanceof RestDefinition) {
        RestDefinition rest = (RestDefinition) result;
        answer = new RestsDefinition();
        answer.getRests().add(rest);
    } else if (result instanceof RestsDefinition) {
        answer = (RestsDefinition) result;
    } else {
        throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
    }
    return answer;
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class MessageHelperTest method testMessageDump.

public void testMessageDump() throws Exception {
    JAXBContext jaxb = JAXBContext.newInstance(MessageDump.class);
    Unmarshaller unmarshaller = jaxb.createUnmarshaller();
    CamelContext context = new DefaultCamelContext();
    context.start();
    message = new DefaultExchange(context).getIn();
    // xml message body
    message.setBody("Hello World");
    message.setHeader("foo", 123);
    String out = MessageHelper.dumpAsXml(message, true);
    MessageDump dump = (MessageDump) unmarshaller.unmarshal(new StringReader(out));
    assertNotNull(dump);
    assertEquals("java.lang.String", dump.getBody().getType());
    assertEquals("Hello World", dump.getBody().getValue());
    assertEquals(1, dump.getHeaders().size());
    assertEquals("foo", dump.getHeaders().get(0).getKey());
    assertEquals("java.lang.Integer", dump.getHeaders().get(0).getType());
    assertEquals("123", dump.getHeaders().get(0).getValue());
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 5 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class JaxbDataFormat method createUnmarshaller.

protected Unmarshaller createUnmarshaller() throws JAXBException, SAXException, FileNotFoundException, MalformedURLException {
    Unmarshaller unmarshaller = getContext().createUnmarshaller();
    if (schema != null) {
        unmarshaller.setSchema(cachedSchema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            public boolean handleEvent(ValidationEvent event) {
                // ERROR
                return event.getSeverity() == ValidationEvent.WARNING;
            }
        });
    }
    return unmarshaller;
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) ValidationEvent(javax.xml.bind.ValidationEvent) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

Unmarshaller (javax.xml.bind.Unmarshaller)292 JAXBContext (javax.xml.bind.JAXBContext)240 JAXBException (javax.xml.bind.JAXBException)97 InputStream (java.io.InputStream)91 Test (org.junit.Test)79 StringReader (java.io.StringReader)40 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 File (java.io.File)33 InputSource (org.xml.sax.InputSource)22 IOException (java.io.IOException)21 JAXBElement (javax.xml.bind.JAXBElement)18 Marshaller (javax.xml.bind.Marshaller)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParserFactory (javax.xml.parsers.SAXParserFactory)13 XMLInputFactory (javax.xml.stream.XMLInputFactory)13 XMLStreamException (javax.xml.stream.XMLStreamException)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Schema (javax.xml.validation.Schema)13