You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2012/05/10 23:48:08 UTC
svn commit: r1336912 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/model/dataformat/
components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/
components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/
components/cam...
Author: cmueller
Date: Thu May 10 21:48:07 2012
New Revision: 1336912
URL: http://svn.apache.org/viewvc?rev=1336912&view=rev
Log:
CAMEL-5267: Improve camel-jaxb to be able to set a custom NameSpacePrefixMapper
Added:
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MarshalWithNamespacePrefixMapperTest.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNameSpacePrefixMapper.java
camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java
camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatMultipleNamespacesTest.java
camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/jaxb.index
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java?rev=1336912&r1=1336911&r2=1336912&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/JaxbDataFormat.java Thu May 10 21:48:07 2012
@@ -20,10 +20,12 @@ import javax.xml.bind.annotation.XmlAcce
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
import javax.xml.namespace.QName;
import org.apache.camel.model.DataFormatDefinition;
import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.RouteContext;
import org.apache.camel.util.ObjectHelper;
/**
@@ -51,6 +53,10 @@ public class JaxbDataFormat extends Data
private String partClass;
@XmlAttribute
private String partNamespace;
+ @XmlAttribute
+ private String nameSpacePrefixMapper;
+ @XmlTransient
+ private Object nameSpacePrefixMapperInstance;
public JaxbDataFormat() {
super("jaxb");
@@ -125,6 +131,27 @@ public class JaxbDataFormat extends Data
this.partNamespace = partNamespace;
}
+ public String getNameSpacePrefixMapper() {
+ return nameSpacePrefixMapper;
+ }
+
+ public void setNameSpacePrefixMapper(String nameSpacePrefixMapper) {
+ this.nameSpacePrefixMapper = nameSpacePrefixMapper;
+ }
+
+ protected DataFormat createDataFormat(RouteContext routeContext) {
+ if (nameSpacePrefixMapper != null) {
+ try {
+ Class<?> clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(nameSpacePrefixMapper);
+ nameSpacePrefixMapperInstance = clazz.newInstance();
+ } catch (Exception e) {
+ throw ObjectHelper.wrapRuntimeCamelException(e);
+ }
+ }
+
+ return super.createDataFormat(routeContext);
+ }
+
@Override
protected void configureDataFormat(DataFormat dataFormat) {
Boolean answer = ObjectHelper.toBoolean(getPrettyPrint());
@@ -161,6 +188,8 @@ public class JaxbDataFormat extends Data
setProperty(dataFormat, "encoding", encoding);
}
setProperty(dataFormat, "contextPath", contextPath);
+ if (nameSpacePrefixMapperInstance != null) {
+ setProperty(dataFormat, "nameSpacePrefixMapper", nameSpacePrefixMapperInstance);
+ }
}
-
}
\ No newline at end of file
Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java?rev=1336912&r1=1336911&r2=1336912&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java (original)
+++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java Thu May 10 21:48:07 2012
@@ -32,6 +32,8 @@ import javax.xml.stream.XMLStreamExcepti
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
+import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
+
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Exchange;
@@ -43,8 +45,6 @@ import org.apache.camel.util.ObjectHelpe
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
-
/**
* A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat})
* using JAXB2 to marshal to and from XML
Modified: camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatMultipleNamespacesTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatMultipleNamespacesTest.java?rev=1336912&r1=1336911&r2=1336912&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatMultipleNamespacesTest.java (original)
+++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatMultipleNamespacesTest.java Thu May 10 21:48:07 2012
@@ -23,11 +23,10 @@ import org.apache.camel.builder.RouteBui
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.example.Address;
import org.apache.camel.example.Order;
+import org.apache.camel.jaxb.MyNameSpacePrefixMapper;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
-import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
-
public class JaxbDataFormatMultipleNamespacesTest extends CamelTestSupport {
@EndpointInject(uri = "mock:marshall")
@@ -119,28 +118,19 @@ public class JaxbDataFormatMultipleNames
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(JAXBContext.newInstance(Order.class, Address.class));
JaxbDataFormat jaxbDataFormatWithNamespacePrefixMapper = new JaxbDataFormat(JAXBContext.newInstance(Order.class, Address.class));
- jaxbDataFormatWithNamespacePrefixMapper.setNameSpacePrefixMapper(new NamespacePrefixMapper() {
- public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
- if (namespaceUri.equals("http://www.camel.apache.org/jaxb/example/order/1")) {
- return "order";
- } else if (namespaceUri.equals("http://www.camel.apache.org/jaxb/example/address/1")) {
- return "address";
- }
- return "ns";
- }
- });
+ jaxbDataFormatWithNamespacePrefixMapper.setNameSpacePrefixMapper(new MyNameSpacePrefixMapper());
from("direct:marshall")
- .marshal(jaxbDataFormat)
- .to("mock:marshall");
+ .marshal(jaxbDataFormat)
+ .to("mock:marshall");
from("direct:marshallWithNamespacePrefixMapper")
.marshal(jaxbDataFormatWithNamespacePrefixMapper)
.to("mock:marshall");
from("direct:unmarshall")
- .unmarshal(jaxbDataFormat)
- .to("mock:unmarshall");
+ .unmarshal(jaxbDataFormat)
+ .to("mock:unmarshall");
}
};
}
Added: camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MarshalWithNamespacePrefixMapperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MarshalWithNamespacePrefixMapperTest.java?rev=1336912&view=auto
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MarshalWithNamespacePrefixMapperTest.java (added)
+++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MarshalWithNamespacePrefixMapperTest.java Thu May 10 21:48:07 2012
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.jaxb;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.example.Address;
+import org.apache.camel.example.Order;
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version
+ */
+public class MarshalWithNamespacePrefixMapperTest extends CamelSpringTestSupport {
+
+ @EndpointInject(uri = "mock:marshall")
+ private MockEndpoint mockMarshall;
+
+ @Test
+ public void testMarshallByDataFormats() throws Exception {
+ send("direct:marshall1");
+ }
+
+ @Test
+ public void testMarshallBySpringBeanRef() throws Exception {
+ send("direct:marshall2");
+ }
+
+ private void send(String endpointUri) throws Exception {
+ mockMarshall.expectedMessageCount(1);
+
+ Order order = new Order();
+ order.setId("1");
+ Address address = new Address();
+ address.setStreet("Main Street");
+ address.setStreetNumber("3a");
+ address.setZip("65843");
+ address.setCity("Sulzbach");
+ order.setAddress(address);
+ template.sendBody(endpointUri, order);
+
+ assertMockEndpointsSatisfied();
+
+ String payload = mockMarshall.getExchanges().get(0).getIn().getBody(String.class);
+ assertTrue(payload.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"));
+ assertTrue(payload.contains("<order:order xmlns:order=\"http://www.camel.apache.org/jaxb/example/order/1\" xmlns:address=\"http://www.camel.apache.org/jaxb/example/address/1\">"));
+ assertTrue(payload.contains("<order:id>1</order:id>"));
+ assertTrue(payload.contains("<address:address>"));
+ assertTrue(payload.contains("<address:street>Main Street</address:street>"));
+ assertTrue(payload.contains("<address:streetNumber>3a</address:streetNumber>"));
+ assertTrue(payload.contains("<address:zip>65843</address:zip>"));
+ assertTrue(payload.contains("<address:city>Sulzbach</address:city>"));
+ assertTrue(payload.contains("</address:address>"));
+ assertTrue(payload.contains("</order:order>"));
+ }
+
+ @Override
+ protected ClassPathXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml");
+ }
+}
\ No newline at end of file
Added: camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNameSpacePrefixMapper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNameSpacePrefixMapper.java?rev=1336912&view=auto
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNameSpacePrefixMapper.java (added)
+++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/MyNameSpacePrefixMapper.java Thu May 10 21:48:07 2012
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.jaxb;
+
+import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
+
+public class MyNameSpacePrefixMapper extends NamespacePrefixMapper {
+
+ @Override
+ public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
+ if (namespaceUri.equals("http://www.camel.apache.org/jaxb/example/order/1")) {
+ return "order";
+ } else if (namespaceUri.equals("http://www.camel.apache.org/jaxb/example/address/1")) {
+ return "address";
+ }
+ return "ns";
+ }
+}
\ No newline at end of file
Modified: camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/jaxb.index
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/jaxb.index?rev=1336912&r1=1336911&r2=1336912&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/jaxb.index (original)
+++ camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/example/jaxb.index Thu May 10 21:48:07 2012
@@ -14,6 +14,7 @@
## See the License for the specific language governing permissions and
## limitations under the License.
## ------------------------------------------------------------------------
+Order
PurchaseOrder
Partial
Foo
Added: camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml?rev=1336912&view=auto
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml (added)
+++ camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml Thu May 10 21:48:07 2012
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+ ">
+
+ <!-- START SNIPPET: example -->
+ <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+ <route>
+ <from uri="direct:marshall1"/>
+ <marshal>
+ <jaxb prettyPrint="true" contextPath="org.apache.camel.example" nameSpacePrefixMapper="org.apache.camel.jaxb.MyNameSpacePrefixMapper"/>
+ </marshal>
+ <to uri="mock:marshall"/>
+ </route>
+
+ <route>
+ <from uri="direct:marshall2"/>
+ <marshal ref="myJaxb"/>
+ <to uri="mock:marshall"/>
+ </route>
+ </camelContext>
+
+ <bean id="myJaxb" class="org.apache.camel.converter.jaxb.JaxbDataFormat">
+ <property name="contextPath" value="org.apache.camel.example"/>
+ <property name="nameSpacePrefixMapper">
+ <bean class="org.apache.camel.jaxb.MyNameSpacePrefixMapper" />
+ </property>
+ </bean>
+ <!-- END SNIPPET: example -->
+</beans>
\ No newline at end of file