users@jersey.java.net

Re: JAXB prefix Mapper

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 05 Feb 2008 13:34:55 +0100

Hi Guilhem,

This is not possible in the 0.5 version :-)

It is possible in the latest build of 0.6, but does require a little
plumbing to get right. You can create a ContextResolver<JAXBContext>
(see the StorageServer example, and a previous email i sent about this)
that returns a JAXBContext for a set of JAXB objects. That JAXBContext
can be adapted as follows:

public final JAXBContextAdapter extends JAXBContext {
   private final JAXBContext c;

   public JAXBContextAdapter(JAXBContext c) {
     this.c = c;
   }

   // wrap all methods

   // Implement the abstract method createMarshaller
   public Marshaller createMarshaller() {
     Marshaller m = c.createMarshaller();
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
     m.setProperty("com.sun.xml.bind.namespacePrefixMapper",
       new NamespacePrefixMapperImpl());
     return m;
   }
}

@Provider
public final class JAXBContextResolver implements
   ContextResolver<JAXBContext> {

   private final JAXBContext context;

   private final Set<Class> types;

   private final Class[] cTypes = {...};

   public JAXBContextResolver() throws Exception {
       this.types = new HashSet(Arrays.asList(cTypes));
       this.context = new
         JAXBContextAdapter(JAXBContext.newInstance(cTypes));
   }

   public JAXBContext getContext(Class<?> objectType) {
       return (types.contains(objectType)) ? context : null;
   }
}

We could probably make this easier by providing some helper classes for
adapting a JAXBContext and using a ContextResolver<JAXBContext>
instance from a set of JAXB classes or a package name.

Another way we could probably do this is to allow developers to extend
the JAXB message body writer to implement a createMashaller method.
However, this would apply to all JAXB contexts.

Hope that helps,
Paul.

guilhem legal wrote:
> hi!
>
> I'm using jersey to return xml using JAXB, I would know if it possible
> to specify a NamespacePrefixMapper to jersey.
>
> I explain :
> - if i use jersey to build my xml like this :
>
> Object obj = .....;
> return Response.ok(obj, "text/xml").build();
>
> my xml looks like that :
> <ns1:Object>
> <ns1:property1/>
> <ns2:property2/>
> ............................
>
> - So i build my xml and return a string :
> JAXBContext jbcontext =
> JAXBContext.newInstance("net.seagis.sos");
> marshaller = jbcontext.createMarshaller();
> marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
>
> marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new
> NamespacePrefixMapperImpl());
> Object obj = .....;
> StringWriter sw = new StringWriter();
> marshaller.marshal(Object, sw);
> return Response.ok(sw.toString(), "text/xml").build();
>
> and my xml looks like that:
> <sos:Object>
> <sos:property1>
> <gml:property2>
> .............................
>
> So my question is how to pass a namespacePrefixMapper to jersey?
>
> Guilhem Legal
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109