Good morning Claus!

I had the same thoughts, but because this class comes from
jaxb-impl-2.1.13, I assume it will not be a problem with an IBM JDK. I use
the Apple JDK (Java version: 1.6.0_31, vendor: Apple Inc.) and it works
well.

NamespacePrefixMapper is an abstract class which doesn't implement any
interface.
Another possibility is to use the java.lang.Object as type for the instance
variable and the getter/setter and not
com.sun.xml.bind.marshaller.NamespacePrefixMapper. This is possible,
because this instance is passed thru the marshaller.setProperty(String,
Object) method. Than we don't have any imports on
com.sun.xml.bind.marshaller.NamespacePrefixMapper but may a runtime
exception if users set Objects from a wrong type (I didn't test whether
JAXB RI ignore it or throw an exception).

Thoughts?

Best,
Christian

On Fri, May 11, 2012 at 5:57 AM, Claus Ibsen <claus.ib...@gmail.com> wrote:

> Morning guys
>
> Christian I noticed that you import a com.sun package.
> +import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
>
> Will this not be problems for people using IBM JVMs etc?
> I would normally discourage against using com.sun packages, especially
> in camel-core.
>
> Maybe there is an interface for the mapper? Or we could create an
> interface, and allow people to plugin the SUN if they configure that
> themselves etc.
>
> Or maybe we can use reflection to load the mapper class or something?
> Just so at least the code can compile on IBM JDKs and run there.
>
> And I wonder about OpenJDK as well? Do they carry the com.sun classes as
> well?
>
>
> On Thu, May 10, 2012 at 9:15 PM,  <cmuel...@apache.org> wrote:
> > Author: cmueller
> > Date: Thu May 10 19:15:26 2012
> > New Revision: 1336836
> >
> > URL: http://svn.apache.org/viewvc?rev=1336836&view=rev
> > Log:
> > CAMEL-5267: Improve camel-jaxb to be able to set a custom
> NameSpacePrefixMapper
> >
> > Modified:
> >
>  
> 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
> >
> > 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=1336836&r1=1336835&r2=1336836&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 19:15:26 2012
> > @@ -43,6 +43,8 @@ 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
> > @@ -64,6 +66,7 @@ public class JaxbDataFormat extends Serv
> >     private QName partNamespace;
> >     private String partClass;
> >     private Class<Object> partialClass;
> > +    private NamespacePrefixMapper nameSpacePrefixMapper;
> >
> >     private TypeConverter typeConverter;
> >
> > @@ -82,6 +85,9 @@ public class JaxbDataFormat extends Serv
> >         try {
> >             // must create a new instance of marshaller as its not
> thread safe
> >             Marshaller marshaller = getContext().createMarshaller();
> > +            if (nameSpacePrefixMapper != null) {
> > +
>  marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
> nameSpacePrefixMapper);
> > +            }
> >             if (isPrettyPrint()) {
> >                 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
> Boolean.TRUE);
> >             }
> > @@ -244,6 +250,14 @@ public class JaxbDataFormat extends Serv
> >     public void setCamelContext(CamelContext camelContext) {
> >         this.camelContext = camelContext;
> >     }
> > +
> > +    public NamespacePrefixMapper getNameSpacePrefixMapper() {
> > +        return nameSpacePrefixMapper;
> > +    }
> > +
> > +    public void setNameSpacePrefixMapper(NamespacePrefixMapper
> nameSpacePrefixMapper) {
> > +        this.nameSpacePrefixMapper = nameSpacePrefixMapper;
> > +    }
> >
> >     @Override
> >     protected void doStart() throws Exception {
> > @@ -287,5 +301,4 @@ public class JaxbDataFormat extends Serv
> >     protected Unmarshaller createUnmarshaller() throws JAXBException {
> >         return getContext().createUnmarshaller();
> >     }
> > -
> >  }
> >
> > 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=1336836&r1=1336835&r2=1336836&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 19:15:26 2012
> > @@ -26,6 +26,8 @@ import org.apache.camel.example.Order;
> >  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")
> > @@ -59,6 +61,35 @@ public class JaxbDataFormatMultipleNames
> >         assertTrue(payload.contains("city>Sulzbach</"));
> >         assertTrue(payload.contains("order>"));
> >     }
> > +
> > +    @Test
> > +    public void testMarshallWithNamespacePrefixMapper() 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("direct:marshallWithNamespacePrefixMapper",
> 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>"));
> > +    }
> >
> >     @Test
> >     public void testUnarshallMultipleNamespaces() throws Exception {
> > @@ -87,9 +118,25 @@ public class JaxbDataFormatMultipleNames
> >             public void configure() throws Exception {
> >                 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";
> > +                    }
> > +                });
> > +
> >                 from("direct:marshall")
> >                         .marshal(jaxbDataFormat)
> >                         .to("mock:marshall");
> > +
> > +                from("direct:marshallWithNamespacePrefixMapper")
> > +                    .marshal(jaxbDataFormatWithNamespacePrefixMapper)
> > +                    .to("mock:marshall");
> >
> >                 from("direct:unmarshall")
> >                         .unmarshal(jaxbDataFormat)
> >
> >
>
>
>
> --
> Claus Ibsen
> -----------------
> CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
> FuseSource
> Email: cib...@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Reply via email to