[jboss-svn-commits] JBossWS SVN: r662 - trunk/src/main/java/org/jboss/ws/jbossxb

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Aug 3 04:52:29 EDT 2006


Author: heiko.braun at jboss.com
Date: 2006-08-03 04:52:25 -0400 (Thu, 03 Aug 2006)
New Revision: 662

Added:
   trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBMarshaller.java
   trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBSupport.java
   trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBUnmarshaller.java
   trunk/src/main/java/org/jboss/ws/jbossxb/XercesXSMarshallerImpl.java
Log:
merge from 1.0.3.DEV

Added: trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBMarshaller.java
===================================================================
--- trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBMarshaller.java	2006-08-01 16:32:34 UTC (rev 661)
+++ trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBMarshaller.java	2006-08-03 08:52:25 UTC (rev 662)
@@ -0,0 +1,48 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.ws.jbossxb;
+
+import org.xml.sax.ContentHandler;
+import org.w3c.dom.Node;
+
+import java.io.Writer;
+import java.io.OutputStream;
+
+/**
+ * @author Heiko Braun <heiko.braun at jboss.com>
+ * @version $Id$
+ * @since Jul 5, 2006
+ */
+public interface JBossXBMarshaller {
+
+   void marshal(Object obj, Writer writer) throws MarshalException;
+
+   void marshal(Object obj, ContentHandler handler);
+
+   void marshal(Object obj, Node node);
+
+   void marshal(Object obj, OutputStream os) throws MarshalException;
+
+   Object getProperty(String name);
+
+   void setProperty(String name, Object value);
+}


Property changes on: trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBMarshaller.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Added: trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBSupport.java
===================================================================
--- trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBSupport.java	2006-08-01 16:32:34 UTC (rev 661)
+++ trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBSupport.java	2006-08-03 08:52:25 UTC (rev 662)
@@ -0,0 +1,115 @@
+package org.jboss.ws.jbossxb;
+
+import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.ws.common.SOAPMessageContextBase;
+import org.jboss.ws.metadata.EndpointMetaData;
+import org.jboss.ws.metadata.OperationMetaData;
+import org.jboss.ws.metadata.ServiceMetaData;
+import org.jboss.ws.metadata.jaxrpcmapping.JavaWsdlMapping;
+import org.jboss.ws.metadata.wsdl.xmlschema.JBossXSModel;
+import org.jboss.ws.soap.MessageContextAssociation;
+import org.jboss.xb.binding.MarshallingContext;
+import org.jboss.xb.binding.ObjectLocalMarshaller;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.util.Dom2Sax;
+import org.w3c.dom.Element;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+import javax.xml.namespace.QName;
+import java.util.Map;
+
+/**
+ * @author Heiko Braun <heiko.braun at jboss.com>
+ * @since May 31, 2006
+ */
+public class JBossXBSupport {
+
+   /**
+    * Setup SchemaBinding associated with the ServiceMetaData.
+    * In case of an unconfigured call it will be generated from JAXB properties.
+    * <p>
+    * The SchemaBinding expects to have an element binding for the
+    * incomming xml element. Because the same element name can be reused
+    * by various operations with different xml types, we have to add the
+    * element binding on every invocation.
+    *
+    * @see JBossXBConstants#JBXB_ROOT_QNAME
+    * @see JBossXBConstants#JBXB_TYPE_QNAME
+    */
+   public static SchemaBinding getOrCreateSchemaBinding(Map properties)
+   {
+      SchemaBinding schemaBinding = null;
+      SchemaBindingBuilder bindingBuilder = new SchemaBindingBuilder();
+
+      QName xmlName = (QName)properties.get(JBossXBConstants.JBXB_ROOT_QNAME);
+      QName xmlType = (QName)properties.get(JBossXBConstants.JBXB_TYPE_QNAME);
+
+      // Get the eagerly initialized SchameBinding from the ServiceMetaData
+      SOAPMessageContextBase msgContext = MessageContextAssociation.peekMessageContext();
+      if (msgContext != null)
+      {
+         OperationMetaData opMetaData = msgContext.getOperationMetaData();
+         EndpointMetaData epMetaData = opMetaData.getEndpointMetaData();
+         ServiceMetaData serviceMetaData = epMetaData.getServiceMetaData();
+         schemaBinding = serviceMetaData.getSchemaBinding();
+      }
+
+      // In case of an unconfigured call generate the SchemaBinding from JAXB properties
+      if (schemaBinding == null)
+      {
+         JBossXSModel xsModel = (JBossXSModel)properties.get(JBossXBConstants.JBXB_XS_MODEL);
+         JavaWsdlMapping wsdlMapping = (JavaWsdlMapping)properties.get(JBossXBConstants.JBXB_JAVA_MAPPING);
+         schemaBinding = bindingBuilder.buildSchemaBinding(xsModel, wsdlMapping);
+      }
+
+      // The SchemaBinding expects to have an element binding for the
+      // incomming xml element. Because the same element name can be reused
+      // by various operations with different xml types, we have to add the
+      // element binding on every invocation.
+      bindingBuilder.bindParameterToElement(schemaBinding, xmlName, xmlType);
+
+      return schemaBinding;
+   }
+
+   /**
+    * Create a Marshaller that serializes
+    * <code>org.w3c.dom.Element</code>'s to a <code>org.xml.sax.ContentHandler</code>
+    *
+    * @return ObjectLocalMarshaller
+    *
+    * @see org.jboss.xb.binding.MarshallingContext#getContentHandler() 
+    */
+   public static ObjectLocalMarshaller getWildcardMarshaller()
+   {
+      return new ObjectLocalMarshaller() {
+         public void marshal(MarshallingContext ctx, Object o)
+         {
+            if (o == null)
+            {
+               return;
+            }
+
+            Element e = (Element)o;
+            ContentHandler ch = ctx.getContentHandler();
+            try
+            {
+               Dom2Sax.dom2sax(e, ch);
+            }
+            catch (SAXException e1)
+            {
+               throw new IllegalStateException("Failed to marshal DOM element " + new QName(e.getNamespaceURI(), e.getLocalName()) + ": " + e1.getMessage());
+            }
+         }
+      };
+   }
+
+   /**
+    * Create an entity resolver that has local entites registered.
+    */
+   public static JBossEntityResolver createEntityResolver() {
+      JBossEntityResolver resolver = new JBossEntityResolver();
+      resolver.registerLocalEntity("http://www.w3.org/2005/08/addressing", "schema/ws-addr.xsd");      
+      return resolver;
+   }
+}


Property changes on: trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBSupport.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Added: trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBUnmarshaller.java
===================================================================
--- trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBUnmarshaller.java	2006-08-01 16:32:34 UTC (rev 661)
+++ trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBUnmarshaller.java	2006-08-03 08:52:25 UTC (rev 662)
@@ -0,0 +1,37 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.ws.jbossxb;
+
+import java.io.InputStream;
+
+/**
+ * @author Heiko Braun <heiko.braun at jboss.com>
+ * @version $Id$
+ * @since Jul 5, 2006
+ */
+public interface JBossXBUnmarshaller {
+   Object unmarshal(InputStream is) throws UnmarshalException;
+
+   Object getProperty(String name);
+
+   void setProperty(String name, Object value);
+}


Property changes on: trunk/src/main/java/org/jboss/ws/jbossxb/JBossXBUnmarshaller.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Added: trunk/src/main/java/org/jboss/ws/jbossxb/XercesXSMarshallerImpl.java
===================================================================
--- trunk/src/main/java/org/jboss/ws/jbossxb/XercesXSMarshallerImpl.java	2006-08-01 16:32:34 UTC (rev 661)
+++ trunk/src/main/java/org/jboss/ws/jbossxb/XercesXSMarshallerImpl.java	2006-08-03 08:52:25 UTC (rev 662)
@@ -0,0 +1,250 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ws.jbossxb;
+
+// $Id$
+
+import org.apache.xerces.xs.XSModel;
+import org.jboss.logging.Logger;
+import org.jboss.util.NotImplementedException;
+import org.jboss.ws.WSException;
+import org.jboss.ws.metadata.jaxrpcmapping.JavaWsdlMapping;
+import org.jboss.ws.metadata.jaxrpcmapping.JavaXmlTypeMapping;
+import org.jboss.ws.metadata.jaxrpcmapping.VariableMapping;
+import org.jboss.ws.utils.JavaUtils;
+import org.jboss.xb.binding.Constants;
+import org.jboss.xb.binding.MappingObjectModelProvider;
+import org.jboss.xb.binding.XercesXsMarshaller;
+import org.w3c.dom.Node;
+import org.xml.sax.ContentHandler;
+
+import javax.xml.namespace.QName;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.HashMap;
+
+/**
+ * An implementation of a JAXB Marshaller that user XercesXSMarshaller impl.
+ *
+ * @author Thomas.Diesler at jboss.org
+ * @since 18-Oct-2004
+ */
+public class XercesXSMarshallerImpl implements JBossXBMarshaller
+{
+
+   // provide logging
+   private static final Logger log = Logger.getLogger(XercesXSMarshallerImpl.class);
+
+   // The marshaller properties
+   private HashMap properties = new HashMap();
+
+   private XercesXsMarshaller delegate;
+
+   public XercesXSMarshallerImpl()
+   {
+      delegate = new XercesXsMarshaller();
+      delegate.setProperty(XercesXsMarshaller.PROP_OUTPUT_XML_VERSION, "false");
+      delegate.setProperty(XercesXsMarshaller.PROP_OUTPUT_INDENTATION, "false");
+      delegate.declareNamespace("xsi", Constants.NS_XML_SCHEMA_INSTANCE);
+      delegate.setSupportNil(true);
+      delegate.setSimpleContentProperty("_value");
+   }
+
+   /**
+    * Marshal the content tree rooted at obj into a Writer.
+    */
+   public void marshal(Object obj, Writer writer) throws MarshalException
+   {
+      assertRequiredProperties();
+
+      try
+      {
+         QName xmlName = (QName)getProperty(JBossXBConstants.JBXB_ROOT_QNAME);
+         delegate.addRootElement(xmlName);
+
+         QName xmlType = (QName)getProperty(JBossXBConstants.JBXB_TYPE_QNAME);
+         if (xmlType != null)
+         {
+            delegate.setRootTypeQName(xmlType);
+         }
+
+         if (xmlName.getNamespaceURI().length() > 0)
+         {
+            String prefix = xmlName.getPrefix();
+            String nsURI = xmlName.getNamespaceURI();
+            delegate.declareNamespace(prefix, nsURI);
+         }
+
+         MappingObjectModelProvider provider = new MappingObjectModelProvider();
+         provider.setIgnoreLowLine(false);
+         provider.setIgnoreNotFoundField(false);
+
+         // todo complete wsdl mapping merge
+         JavaWsdlMapping wsdlMapping = (JavaWsdlMapping)getProperty(JBossXBConstants.JBXB_JAVA_MAPPING);
+         if (wsdlMapping != null)
+         {
+            JavaXmlTypeMapping[] javaXmlMappings = wsdlMapping.getJavaXmlTypeMappings();
+            if (javaXmlMappings != null)
+            {
+               for (int i = 0; i < javaXmlMappings.length; ++i)
+               {
+                  JavaXmlTypeMapping javaXmlMapping = javaXmlMappings[i];
+                  VariableMapping[] variableMappings = javaXmlMapping.getVariableMappings();
+                  if (variableMappings != null)
+                  {
+                     String clsName = javaXmlMapping.getJavaType();
+                     Class cls = JavaUtils.loadJavaType(clsName, Thread.currentThread().getContextClassLoader());
+                     QName clsQName = javaXmlMapping.getRootTypeQName();
+
+                     if (clsQName != null)
+                     {
+                        if ("element".equals(javaXmlMapping.getQnameScope()))
+                        {
+                           delegate.mapClassToGlobalElement(cls, clsQName.getLocalPart(), clsQName.getNamespaceURI(), null, provider);
+                        }
+                        else
+                        {
+                           delegate.mapClassToGlobalType(cls, clsQName.getLocalPart(), clsQName.getNamespaceURI(), null, provider);
+                           delegate.mapClassToXsiType(cls, clsQName.getNamespaceURI(), clsQName.getLocalPart());
+                        }
+                     }
+
+                     for (int j = 0; j < variableMappings.length; ++j)
+                     {
+                        VariableMapping variableMapping = variableMappings[j];
+                        String javaName = variableMapping.getJavaVariableName();
+                        if (variableMapping.getXmlElementName() != null)
+                        {
+                           String xmlElementName = variableMapping.getXmlElementName();
+                           provider.mapFieldToElement(cls, javaName, "", xmlElementName, null);
+                        }
+                        else if (variableMapping.getXmlAttributeName() != null)
+                        {
+                           log.trace("Unmapped attribute: " + javaName);
+                        }
+                        else if (variableMapping.getXmlWildcard())
+                        {
+                           delegate.mapFieldToWildcard(cls, "_any", JBossXBSupport.getWildcardMarshaller());
+                        }
+                        else
+                        {
+                           log.warn("Unmapped variable: " + javaName);
+                        }
+                     }
+                  }
+               }
+            }
+         }
+
+         if (getProperty(JBossXBConstants.JBXB_XS_MODEL) != null)
+         {
+            XSModel model = (XSModel)getProperty(JBossXBConstants.JBXB_XS_MODEL);
+            delegate.marshal(model, provider, obj, writer);
+         }
+      }
+      catch (RuntimeException e)
+      {
+         throw e;
+      }
+      catch (Exception e)
+      {
+         throw new MarshalException(e);
+      }
+   }
+
+   /**
+    * Marshal the content tree rooted at obj into SAX2 events.
+    */
+   public void marshal(Object obj, ContentHandler handler)
+   {
+      throw new NotImplementedException();
+   }
+
+   /**
+    * Marshal the content tree rooted at obj into a DOM tree.
+    */
+   public void marshal(Object obj, Node node)
+   {
+      throw new NotImplementedException();
+   }
+
+   /**
+    * Marshal the content tree rooted at obj into an output stream.
+    */
+   public void marshal(Object obj, OutputStream os) throws MarshalException
+   {
+      marshal(obj, new OutputStreamWriter(os));
+   }
+
+   /**
+    * Get the particular property in the underlying implementation of
+    * Marshaller.
+    */
+   public Object getProperty(String name)
+   {
+      if (name == null)
+         throw new IllegalArgumentException("name parameter is null");
+
+      return properties.get(name);
+   }
+
+   /**
+    * Set the particular property in the underlying implementation of
+    * Marshaller.
+    *
+    */
+   public void setProperty(String name, Object value)
+   {
+      if (name == null)
+         throw new IllegalArgumentException("name parameter is null");
+
+      properties.put(name, value);
+   }
+
+   /**
+    * Get a DOM tree view of the content tree(Optional).
+    */
+   public Node getNode(Object contentTree)
+   {
+      throw new NotImplementedException();
+   }
+
+   /**
+    * Assert the required properties
+    */
+   private void assertRequiredProperties()
+   {
+      if (getProperty(JBossXBConstants.JBXB_SCHEMA_READER) == null && getProperty(JBossXBConstants.JBXB_XS_MODEL) == null)
+         throw new WSException("Cannot find required property: " + JBossXBConstants.JBXB_XS_MODEL);
+
+      if (getProperty(JBossXBConstants.JBXB_JAVA_MAPPING) == null)
+         throw new WSException("Cannot find required property: " + JBossXBConstants.JBXB_JAVA_MAPPING);
+
+      QName xmlName = (QName)getProperty(JBossXBConstants.JBXB_ROOT_QNAME);
+      if (xmlName == null)
+         throw new WSException("Cannot find required property: " + JBossXBConstants.JBXB_ROOT_QNAME);
+
+      if (xmlName.getNamespaceURI().length() > 0 && xmlName.getPrefix().length() == 0)
+         throw new IllegalArgumentException("The given root element name must be prefix qualified: " + xmlName);
+   }
+}


Property changes on: trunk/src/main/java/org/jboss/ws/jbossxb/XercesXSMarshallerImpl.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF




More information about the jboss-svn-commits mailing list