Return-Path: Delivered-To: apmail-incubator-cxf-commits-archive@locus.apache.org Received: (qmail 44944 invoked from network); 28 Nov 2007 01:07:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Nov 2007 01:07:22 -0000 Received: (qmail 55782 invoked by uid 500); 28 Nov 2007 01:07:10 -0000 Delivered-To: apmail-incubator-cxf-commits-archive@incubator.apache.org Received: (qmail 55725 invoked by uid 500); 28 Nov 2007 01:07:10 -0000 Mailing-List: contact cxf-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cxf-dev@incubator.apache.org Delivered-To: mailing list cxf-commits@incubator.apache.org Received: (qmail 55716 invoked by uid 99); 28 Nov 2007 01:07:10 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Nov 2007 17:07:10 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Nov 2007 01:06:47 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 1C0251A9832; Tue, 27 Nov 2007 17:06:50 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r598839 - in /incubator/cxf/trunk: rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/ rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/ rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/ rt/databinding/jaxb/src/t... Date: Wed, 28 Nov 2007 01:06:48 -0000 To: cxf-commits@incubator.apache.org From: bimargulies@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071128010650.1C0251A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bimargulies Date: Tue Nov 27 17:06:45 2007 New Revision: 598839 URL: http://svn.apache.org/viewvc?rev=598839&view=rev Log: Allow applications to supply JAXB context and Marshaller properties. This allows complete control of prefixes on the wire for types managed by JAXB, aside from other sneaky tricks. Added: incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/ incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/unqualified/ incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/unqualified/UnqualifiedBean.java Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/DynamicClientFactory.java incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBDataBindingTest.java incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/DynamicClientFactory.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/DynamicClientFactory.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/DynamicClientFactory.java (original) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/endpoint/dynamic/DynamicClientFactory.java Tue Nov 27 17:06:45 2007 @@ -26,7 +26,9 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; +import java.util.Map; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; @@ -84,6 +86,8 @@ private boolean simpleBindingEnabled = true; + private Map jaxbContextProperties; + private DynamicClientFactory(Bus bus) { this.bus = bus; } @@ -225,12 +229,17 @@ } JAXBContext context; - + Map contextProperties = jaxbContextProperties; + + if (contextProperties == null) { + contextProperties = Collections.emptyMap(); + } + try { if (StringUtils.isEmpty(packageList)) { - context = JAXBContext.newInstance(new Class[0]); + context = JAXBContext.newInstance(new Class[0], contextProperties); } else { - context = JAXBContext.newInstance(packageList, cl); + context = JAXBContext.newInstance(packageList, cl, contextProperties); } } catch (JAXBException jbe) { throw new IllegalStateException("Unable to create JAXBContext for generated packages: " @@ -445,5 +454,21 @@ } return null; } + } + + /** + * Return the map of JAXB context properties used at the time that we create new contexts. + * @return the map + */ + public Map getJaxbContextProperties() { + return jaxbContextProperties; + } + + /** + * Set the map of JAXB context properties used at the time that we create new contexts. + * @param jaxbContextProperties + */ + public void setJaxbContextProperties(Map jaxbContextProperties) { + this.jaxbContextProperties = jaxbContextProperties; } } Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java (original) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBDataBinding.java Tue Nov 27 17:06:45 2007 @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -97,6 +98,9 @@ Class cls; + private Map contextProperties = Collections.emptyMap(); + private Map marshallerProperties = Collections.emptyMap(); + public JAXBDataBinding() { } @@ -123,13 +127,14 @@ @SuppressWarnings("unchecked") public DataWriter createWriter(Class c) { if (c == XMLStreamWriter.class) { - return (DataWriter)new DataWriterImpl(context); + return (DataWriter)new DataWriterImpl(context, marshallerProperties); } else if (c == OutputStream.class) { - return (DataWriter)new DataWriterImpl(context); + return (DataWriter)new DataWriterImpl(context, marshallerProperties); } else if (c == XMLEventWriter.class) { - return (DataWriter)new DataWriterImpl(context); + return (DataWriter)new DataWriterImpl(context, + marshallerProperties); } else if (c == Node.class) { - return (DataWriter)new DataWriterImpl(context); + return (DataWriter)new DataWriterImpl(context, marshallerProperties); } return null; @@ -312,6 +317,11 @@ map.put("com.sun.xml.bind.defaultNamespaceRemap", defaultNs); } + if (contextProperties != null) { + //add any specified context properties into the properties map + map.putAll(contextProperties); + } + //try and read any jaxb.index files that are with the other classes. This should //allow loading of extra classes (such as subclasses for inheritance reasons) //that are in the same package. @@ -425,6 +435,27 @@ */ public Map getDeclaredNamespaceMappings() { return null; + } + + /** + * Return a map of properties. These properties are passed to + * JAXBContext.newInstance when this object creates a context. + * @return the map of JAXB context properties. + */ + public Map getContextProperties() { + return contextProperties; + } + + /** + * Set a map of JAXB context properties. These properties are passed + * to JAXBContext.newInstance when this object creates a context. + * Note that if you create a JAXB context elsewhere, you will + * not respect these properties unless you handle it manually. + * + * @param contextProperties map of properties. + */ + public void setContextProperties(Map contextProperties) { + this.contextProperties = contextProperties; } Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java (original) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java Tue Nov 27 17:06:45 2007 @@ -33,12 +33,16 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.ResourceBundle; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; +import javax.xml.bind.PropertyException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -58,6 +62,7 @@ import org.apache.cxf.common.i18n.BundleUtils; import org.apache.cxf.common.i18n.Message; +import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.service.model.MessagePartInfo; @@ -72,11 +77,14 @@ */ public final class JAXBEncoderDecoder { private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXBEncoderDecoder.class); + private static final Logger LOG = LogUtils.getLogger(JAXBEncoderDecoder.class); private JAXBEncoderDecoder() { } - private static Marshaller createMarshaller(JAXBContext context, Class cls) throws JAXBException { + private static Marshaller createMarshaller(JAXBContext context, Class cls, + Map marshallerProperties) + throws JAXBException { Marshaller jm = null; if (context == null) { context = JAXBContext.newInstance(cls); @@ -85,6 +93,15 @@ jm = context.createMarshaller(); jm.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); jm.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + if (marshallerProperties != null) { + for (Map.Entry propEntry : marshallerProperties.entrySet()) { + try { + jm.setProperty(propEntry.getKey(), propEntry.getValue()); + } catch (PropertyException pe) { + LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe); + } + } + } return jm; } @@ -95,7 +112,8 @@ Object elValue, MessagePartInfo part, Object source, - AttachmentMarshaller am) { + AttachmentMarshaller am, + Map marshallerProperties) { Class cls = null; if (part != null) { cls = part.getTypeClass(); @@ -111,7 +129,7 @@ } try { - Marshaller u = createMarshaller(context, cls); + Marshaller u = createMarshaller(context, cls, marshallerProperties); try { // The Marshaller.JAXB_FRAGMENT will tell the Marshaller not to // generate the xml declaration. @@ -196,7 +214,8 @@ Exception elValue, MessagePartInfo part, Object source, - AttachmentMarshaller am) { + AttachmentMarshaller am, + Map marshallerProperties) { XMLStreamWriter writer = getStreamWriter(source); QName qn = part.getElementQName(); try { @@ -209,8 +228,9 @@ XmlAccessType accessType = accessorType != null ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER; String namespace = part.getElementQName().getNamespaceURI(); - Marshaller u = createMarshaller(context, cls); + Marshaller u = createMarshaller(context, cls, marshallerProperties); try { + // override anything the user asked us to set. // The Marshaller.JAXB_FRAGMENT will tell the Marshaller not to // generate the xml declaration. u.setProperty(Marshaller.JAXB_FRAGMENT, true); @@ -347,16 +367,18 @@ throw new Fault(new Message("UNKNOWN_SOURCE", BUNDLE, source.getClass().getName())); } - public static void marshall(JAXBContext context, Schema schema, Object elValue, Object source) { - marshall(context, schema, elValue, null, source, null); + public static void marshall(JAXBContext context, Schema schema, Object elValue, Object source, + Map marshallerProperties) { + marshall(context, schema, elValue, null, source, null, marshallerProperties); } @SuppressWarnings("unchecked") public static void marshallNullElement(JAXBContext context, Schema schema, - Object source, MessagePartInfo part) { + Object source, MessagePartInfo part, + Map marshallerProperties) { Class clazz = part != null ? (Class) part.getTypeClass() : null; try { - Marshaller u = createMarshaller(context, clazz); + Marshaller u = createMarshaller(context, clazz, marshallerProperties); u.setSchema(schema); try { // The Marshaller.JAXB_FRAGMENT will tell the Marshaller not to @@ -375,8 +397,9 @@ public static void marshall(JAXBContext context, Schema schema, Object elValue, MessagePartInfo part, - Object source) { - marshall(context, schema, elValue, part, source, null); + Object source, + Map marshallerProperties) { + marshall(context, schema, elValue, part, source, null, marshallerProperties); } private static Unmarshaller createUnmarshaller(JAXBContext context, Class cls) throws JAXBException { Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java (original) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/io/DataWriterImpl.java Tue Nov 27 17:06:45 2007 @@ -19,6 +19,9 @@ package org.apache.cxf.jaxb.io; +import java.util.Collections; +import java.util.Map; + import javax.xml.bind.JAXBContext; import org.apache.cxf.databinding.DataWriter; @@ -29,10 +32,18 @@ import org.apache.ws.commons.schema.XmlSchemaElement; public class DataWriterImpl extends JAXBDataBase implements DataWriter { + + private Map marshallerProperties = Collections.emptyMap(); + public DataWriterImpl(JAXBContext ctx) { super(ctx); } + public DataWriterImpl(JAXBContext ctx, Map marshallerProperties) { + super(ctx); + this.marshallerProperties = marshallerProperties; + } + public void write(Object obj, T output) { write(obj, null, output); } @@ -46,13 +57,16 @@ && Boolean.TRUE.equals(part.getProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION"))) { JAXBEncoderDecoder.marshallException(getJAXBContext(), getSchema(), (Exception)obj, - part, output, getAttachmentMarshaller()); + part, output, getAttachmentMarshaller(), + marshallerProperties); } else { JAXBEncoderDecoder.marshall(getJAXBContext(), getSchema(), obj, part, output, - getAttachmentMarshaller()); + getAttachmentMarshaller(), + marshallerProperties); } } else if (obj == null && needToRender(obj, part)) { - JAXBEncoderDecoder.marshallNullElement(getJAXBContext(), getSchema(), output, part); + JAXBEncoderDecoder.marshallNullElement(getJAXBContext(), getSchema(), output, part, + marshallerProperties); } } @@ -62,5 +76,13 @@ return element.isNillable() && element.getMinOccurs() > 0; } return false; + } + + public Map getMarshallerProperties() { + return marshallerProperties; + } + + public void setMarshallerProperties(Map marshallerProperties) { + this.marshallerProperties = marshallerProperties; } } Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBDataBindingTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBDataBindingTest.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBDataBindingTest.java (original) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBDataBindingTest.java Tue Nov 27 17:06:45 2007 @@ -21,9 +21,12 @@ import java.io.OutputStream; +import java.io.StringWriter; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Logger; @@ -34,11 +37,13 @@ import javax.xml.bind.JAXBContext; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import org.w3c.dom.Node; +import com.sun.xml.bind.api.JAXBRIContext; import com.sun.xml.bind.v2.runtime.JAXBContextImpl; import org.apache.cxf.Bus; @@ -47,6 +52,7 @@ import org.apache.cxf.databinding.DataReader; import org.apache.cxf.databinding.DataWriter; import org.apache.cxf.helpers.CastUtils; +import org.apache.cxf.jaxb.fortest.unqualified.UnqualifiedBean; import org.apache.cxf.jaxb.io.DataReaderImpl; import org.apache.cxf.jaxb.io.DataWriterImpl; import org.apache.cxf.jaxb_misc.ObjectFactory; @@ -191,6 +197,27 @@ JAXBContextImpl rictx = (JAXBContextImpl)ctx; assertNotNull(rictx.getBeanInfo(TestJAXBClass.class)); } + } + + @Test + public void testContextProperties() throws Exception { + JAXBDataBinding db = new JAXBDataBinding(); + Map contextProperties = new HashMap(); + contextProperties.put(JAXBRIContext.DEFAULT_NAMESPACE_REMAP, "uri:ultima:thule"); + db.setContextProperties(contextProperties); + Set> classes = new HashSet>(); + classes.add(UnqualifiedBean.class); + db.setContext(db.createJAXBContext(classes)); + DataWriter writer = db.createWriter(XMLStreamWriter.class); + XMLOutputFactory writerFactory = XMLOutputFactory.newInstance(); + StringWriter stringWriter = new StringWriter(); + XMLStreamWriter xmlWriter = writerFactory.createXMLStreamWriter(stringWriter); + UnqualifiedBean bean = new UnqualifiedBean(); + bean.setAriadne("spider"); + writer.write(bean, xmlWriter); + xmlWriter.flush(); + String xml = stringWriter.toString(); + assertTrue(xml.contains("uri:ultima:thule")); } } Modified: incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java (original) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java Tue Nov 27 17:06:45 2007 @@ -25,6 +25,9 @@ import java.io.StringWriter; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; @@ -45,6 +48,8 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; +import com.sun.xml.bind.marshaller.NamespacePrefixMapper; + import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.jaxb_form.ObjectWithQualifiedElementElement; @@ -58,6 +63,7 @@ import org.apache.hello_world_soap_http.types.GreetMeResponse; import org.apache.hello_world_soap_http.types.StringStruct; import org.apache.type_test.doc.TypeTestPortType; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -76,13 +82,8 @@ JAXBContext context; Schema schema; - public JAXBEncoderDecoderTest() { - } - + private Map emptyMarshallerProperties = Collections.emptyMap(); - public JAXBEncoderDecoderTest(String arg0) { - } - @Before public void setUp() throws Exception { @@ -118,7 +119,7 @@ Node node; try { - JAXBEncoderDecoder.marshall(context, null, null, part, elNode); + JAXBEncoderDecoder.marshall(context, null, null, part, elNode, emptyMarshallerProperties); fail("Should have thrown a Fault"); } catch (Fault ex) { //expected - not a valid object @@ -129,7 +130,7 @@ QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName()); part.setElementQName(elName); - JAXBEncoderDecoder.marshall(context, null, obj, part, elNode); + JAXBEncoderDecoder.marshall(context, null, obj, part, elNode, emptyMarshallerProperties); node = elNode.getLastChild(); //The XML Tree Looks like //Hello @@ -146,10 +147,11 @@ //stringStruct.setArg0("hello"); stringStruct.setArg1("world"); // Marshal without the schema should work. - JAXBEncoderDecoder.marshall(context, null, stringStruct, part, elNode); + JAXBEncoderDecoder.marshall(context, null, stringStruct, part, elNode, emptyMarshallerProperties); try { // Marshal with the schema should get an exception. - JAXBEncoderDecoder.marshall(context, schema, stringStruct, part, elNode); + JAXBEncoderDecoder.marshall(context, schema, stringStruct, part, elNode, + emptyMarshallerProperties); fail("Marshal with schema should have thrown a Fault"); } catch (Fault ex) { //expected - not a valid object @@ -172,7 +174,7 @@ XMLOutputFactory opFactory = XMLOutputFactory.newInstance(); opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); XMLEventWriter writer = opFactory.createXMLEventWriter(stringWriter); - JAXBEncoderDecoder.marshall(context, null, testObject, part, writer); + JAXBEncoderDecoder.marshall(context, null, testObject, part, writer, emptyMarshallerProperties); writer.flush(); writer.close(); String xmlResult = stringWriter.toString(); @@ -180,6 +182,45 @@ // so that it could be examined inside the debugger to see how JAXB works. assertTrue(xmlResult.contains("ns3:string2")); } + + @Test + public void testCustomNamespaces() throws Exception { + Map marshallProps = new HashMap(); + NamespacePrefixMapper mapper = new NamespacePrefixMapper() { + + @Override + public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { + if ("http://apache.org/hello_world_soap_http/types".equals(namespaceUri)) { + return "Omnia"; + } else if ("http://cxf.apache.org/jaxb_form".equals(namespaceUri)) { + return "Gallia"; + } + return suggestion; + } + }; + marshallProps.put("com.sun.xml.bind.namespacePrefixMapper", mapper); + ObjectWithQualifiedElementElement testObject = new ObjectWithQualifiedElementElement(); + testObject.setString1("twine"); + testObject.setString2("cord"); + + QName elName = new QName(wrapperAnnotation.targetNamespace(), + wrapperAnnotation.localName()); + MessagePartInfo part = new MessagePartInfo(elName, null); + part.setElement(true); + part.setElementQName(elName); + + StringWriter stringWriter = new StringWriter(); + XMLOutputFactory opFactory = XMLOutputFactory.newInstance(); + opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + XMLEventWriter writer = opFactory.createXMLEventWriter(stringWriter); + JAXBEncoderDecoder.marshall(context, null, testObject, part, writer, marshallProps); + writer.flush(); + writer.close(); + String xmlResult = stringWriter.toString(); + // the following is a bit of a crock, but, to tell the truth, this test case most exists + // so that it could be examined inside the debugger to see how JAXB works. + assertTrue(xmlResult.contains("Gallia:string2")); + } @Test public void testMarshallIntoStax() throws Exception { @@ -198,7 +239,7 @@ //STARTDOCUMENT/ENDDOCUMENT is not required //writer.add(eFactory.createStartDocument("utf-8", "1.0")); - JAXBEncoderDecoder.marshall(context, null, obj, part, writer); + JAXBEncoderDecoder.marshall(context, null, obj, part, writer, emptyMarshallerProperties); //writer.add(eFactory.createEndDocument()); writer.flush(); writer.close(); @@ -256,7 +297,8 @@ Document doc = DOMUtils.createDocument(); Element elNode = doc.createElementNS(elName.getNamespaceURI(), elName.getLocalPart()); - JAXBEncoderDecoder.marshall(context, null, new String("TestSOAPMessage"), part, elNode); + JAXBEncoderDecoder.marshall(context, null, + new String("TestSOAPMessage"), part, elNode, emptyMarshallerProperties); assertNotNull(elNode.getChildNodes()); assertEquals("TestSOAPMessage", elNode.getFirstChild().getFirstChild().getNodeValue()); @@ -366,7 +408,7 @@ //STARTDOCUMENT/ENDDOCUMENT is not required //writer.add(eFactory.createStartDocument("utf-8", "1.0")); - JAXBEncoderDecoder.marshall(context, null, obj, writer); + JAXBEncoderDecoder.marshall(context, null, obj, writer, emptyMarshallerProperties); //writer.add(eFactory.createEndDocument()); writer.flush(); writer.close(); Added: incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/unqualified/UnqualifiedBean.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/unqualified/UnqualifiedBean.java?rev=598839&view=auto ============================================================================== --- incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/unqualified/UnqualifiedBean.java (added) +++ incubator/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/fortest/unqualified/UnqualifiedBean.java Tue Nov 27 17:06:45 2007 @@ -0,0 +1,39 @@ +/** + * 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.cxf.jaxb.fortest.unqualified; + +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Test class that is not in any namespace, for testing JAXB context properties. + */ +@XmlRootElement() +public class UnqualifiedBean { + private String ariadne; + + public String getAriadne() { + return ariadne; + } + + public void setAriadne(String ariadne) { + this.ariadne = ariadne; + } + +} Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java?rev=598839&r1=598838&r2=598839&view=diff ============================================================================== --- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java (original) +++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/ClientServerTest.java Tue Nov 27 17:06:45 2007 @@ -49,7 +49,8 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; -//import org.apache.cxf.Bus; +import com.sun.xml.bind.api.JAXBRIContext; + import org.apache.cxf.Bus; import org.apache.cxf.binding.soap.Soap11; import org.apache.cxf.bus.CXFBusFactory; @@ -838,28 +839,30 @@ } @Test - public void testDynamicClientFactory() { + public void testDynamicClientFactory() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); String wsdlUrl = null; - try { - wsdlUrl = wsdl.toURI().toString(); - } catch (URISyntaxException e) { - e.printStackTrace(); - fail("Can't get the hello_world.wsdl url"); - } - try { - //TODO test fault exceptions - DynamicClientFactory dcf = DynamicClientFactory.newInstance(); - Client client = dcf.createClient(wsdlUrl, serviceName, portName); - client.invoke("greetMe", "test"); - Object[] result = client.invoke("sayHi"); - assertNotNull("no response received from service", result); - assertEquals("Bonjour", result[0]); - } catch (Exception e) { - e.printStackTrace(); - fail("There is some excpetion happened "); - } + wsdlUrl = wsdl.toURI().toString(); + + //TODO test fault exceptions + DynamicClientFactory dcf = DynamicClientFactory.newInstance(); + Client client = dcf.createClient(wsdlUrl, serviceName, portName); + client.invoke("greetMe", "test"); + Object[] result = client.invoke("sayHi"); + assertNotNull("no response received from service", result); + assertEquals("Bonjour", result[0]); + //TODO: the following isn't a real test. We need to test against a service + // that would actually notice the difference. At least it ensures that + // specifying the property does not explode. + Map jaxbContextProperties = new HashMap(); + jaxbContextProperties.put(JAXBRIContext.DEFAULT_NAMESPACE_REMAP, "uri:ultima:thule"); + dcf.setJaxbContextProperties(jaxbContextProperties); + client = dcf.createClient(wsdlUrl, serviceName, portName); + client.invoke("greetMe", "test"); + result = client.invoke("sayHi"); + assertNotNull("no response received from service", result); + assertEquals("Bonjour", result[0]); } @Test