users@jaxb.java.net

namespace prefix mapping.

From: <david_at_davidkarlsen.com>
Date: Tue, 5 Jan 2010 16:55:51 +0100 (CET)

attached is my testclass.

the test usePrefixNamespaceMapper gives me what I want (because the prefix
value actually matters and *has to be exactly "Sw").

What puzzles me is why rootElementHasControlledSwPrefix() doesn't use the
requested prefix "Sw" ?



private InitRequest createInitRequest()
     {
         InitRequest initRequest = new InitRequest();
         initRequest.setExplicitNamespaceAttributeMode( "TRUE" );
         initRequest.setCryptoMode( "Whatever" );
         initRequest.setFACryptoMode( "see if I care" );

         return initRequest;
     }

     private Marshaller getMarshallerForClass( Class<?> clazz,
NamespacePrefixMapper namespacePrefixMapper )
         throws Exception
     {
         JAXBContext jaxbContext = JAXBContext.newInstance( clazz );
         Marshaller marshaller = jaxbContext.createMarshaller();
         // format it nicely to be light on your eyes
         marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE );
         // controls the <? xml header e.g. if fragment or standalone
document
         marshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE );

         if ( namespacePrefixMapper != null )
         {
             marshaller.setProperty(
"com.sun.xml.bind.namespacePrefixMapper", namespacePrefixMapper );
         }

         return marshaller;
     }

     /**
      * Nothing special - std. usage.
      * @throws Exception
      */
     @Test
     public void rootElementHasDefaultNamespace()
         throws Exception
     {
         InitRequest initRequest = createInitRequest();

         QName qName = new QName( "urn:swift:snl:ns.Sw", "InitRequest" );
         Class<InitRequest> clazz = InitRequest.class;
         JAXBElement<InitRequest> jaxbElement = new
JAXBElement<InitRequest>( qName, clazz, initRequest );
         Marshaller marshaller = getMarshallerForClass( clazz, null );

         marshaller.marshal( jaxbElement, System.out );
     }

     /**
      * Now with asking to use "Sw" as prefix - puzzled that this does not
seem to be taken into consideration.
      */
     @Test
     public void rootElementHasControlledSwPrefix()
         throws Exception
     {
         InitRequest initRequest = createInitRequest();

         QName qName = new QName( "urn:swift:snl:ns.Sw", "InitRequest",
"Sw" );
         Class<InitRequest> clazz = InitRequest.class;
         JAXBElement<InitRequest> jaxbElement = new
JAXBElement<InitRequest>( qName, clazz, initRequest );
         Marshaller marshaller = getMarshallerForClass( clazz, null );

         marshaller.marshal( jaxbElement, System.out );
     }

     /**
      * This uses a {_at_linkplain NamespacePrefixMapper}
      * 3rd attempt and output like we want it according to Matthew.
      */
     @Test
     public void usePrefixNamespaceMapper()
         throws Exception
     {
         InitRequest initRequest = createInitRequest();
         Class<InitRequest> clazz = InitRequest.class;
         QName qName = new QName( "urn:swift:snl:ns.Sw", "InitRequest" );
         JAXBElement<InitRequest> jaxbElement = new
JAXBElement<InitRequest>( qName, clazz, initRequest );
         Marshaller marshaller = getMarshallerForClass( clazz, new
IdioticSwiftFormatNamespacePrefixMapper() );

         marshaller.marshal( jaxbElement, System.out );
     }



--
David J. M. Karlsen - +47 90 68 22 43
http://www.davidkarlsen.com
http://mp3.davidkarlsen.com
Sent from my bog-standard SMTP client