[jboss-svn-commits] JBossWS SVN: r700 - in branches/jbossws-1.0_Heiko/src: main/java/org/jboss/ws/soap test/java/org/jboss/test/ws/soap

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 7 09:50:20 EDT 2006


Author: heiko.braun at jboss.com
Date: 2006-08-07 09:50:14 -0400 (Mon, 07 Aug 2006)
New Revision: 700

Added:
   branches/jbossws-1.0_Heiko/src/main/java/org/jboss/ws/soap/SAAJElementWriter.java
   branches/jbossws-1.0_Heiko/src/test/java/org/jboss/test/ws/soap/SAAJElementWriterTestCase.java
Log:
move from DOMWriter to SAAJElementWriter

Added: branches/jbossws-1.0_Heiko/src/main/java/org/jboss/ws/soap/SAAJElementWriter.java
===================================================================
--- branches/jbossws-1.0_Heiko/src/main/java/org/jboss/ws/soap/SAAJElementWriter.java	2006-08-07 13:49:55 UTC (rev 699)
+++ branches/jbossws-1.0_Heiko/src/main/java/org/jboss/ws/soap/SAAJElementWriter.java	2006-08-07 13:50:14 UTC (rev 700)
@@ -0,0 +1,189 @@
+/*
+* 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.soap;
+
+import javax.xml.soap.SOAPElement;
+import java.io.*;
+import java.util.Iterator;
+
+/**
+ * Writes a SAAJ elements to an output stream.
+ *
+ * @see SOAPElementImpl
+ *
+ * @author Heiko Braun <heiko.braun at jboss.com>
+ * @version $Id$
+ * @since Aug 4, 2006
+ */
+public class SAAJElementWriter {
+
+   // Print writer
+   private PrintWriter out;
+   // True, if canonical output
+   private boolean canonical;
+   // True, if pretty printing should be used
+   private boolean prettyprint;
+   // True, if the XML declaration should be written
+   private boolean writeXMLDeclaration;
+   // Explicit character set encoding
+   private String charsetName;  
+   // True, if the XML declaration has been written
+   private boolean wroteXMLDeclaration;
+
+   public SAAJElementWriter(Writer w)
+   {
+      this.out = new PrintWriter(w);
+   }
+
+   public SAAJElementWriter(OutputStream stream)
+   {
+      try
+      {
+         this.out = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
+      }
+      catch (UnsupportedEncodingException e)
+      {
+         // ignore, UTF-8 should be available
+      }
+   }
+
+   public SAAJElementWriter(OutputStream stream, String charsetName)
+   {
+      try
+      {
+         this.out = new PrintWriter(new OutputStreamWriter(stream, charsetName));
+         this.charsetName = charsetName;
+         this.writeXMLDeclaration = true;
+      }
+      catch (UnsupportedEncodingException e)
+      {
+         throw new IllegalArgumentException("Unsupported encoding: " + charsetName);
+      }
+   }
+
+   /**
+    * Print a node with explicit prettyprinting.
+    * The defaults for all other DOMWriter properties apply.
+    *
+    */
+   public static String printSOAPElement(SOAPElementImpl element, boolean prettyprint)
+   {
+      StringWriter strw = new StringWriter();
+      new SAAJElementWriter(strw).setPrettyprint(prettyprint).print(element);
+      return strw.toString();
+   }
+
+   public boolean isCanonical()
+   {
+      return canonical;
+   }
+
+   /**
+    * Set wheter entities should appear in their canonical form.
+    * The default is false.
+    */
+   public SAAJElementWriter setCanonical(boolean canonical)
+   {
+      this.canonical = canonical;
+      return this;
+   }
+
+   public boolean isPrettyprint()
+   {
+      return prettyprint;
+   }
+
+   /**
+    * Set wheter element should be indented.
+    * The default is false.
+    */
+   public SAAJElementWriter setPrettyprint(boolean prettyprint)
+   {
+      this.prettyprint = prettyprint;
+      return this;
+   }
+
+   public boolean isWriteXMLDeclaration()
+   {
+      return writeXMLDeclaration;
+   }
+
+   /**
+    * Set wheter the XML declaration should be written.
+    * The default is false.
+    */
+   public SAAJElementWriter setWriteXMLDeclaration(boolean writeXMLDeclaration)
+   {
+      this.writeXMLDeclaration = writeXMLDeclaration;
+      return this;
+   }
+
+   public void print(SOAPElementImpl element)
+   {
+      printInternal(element);
+   }
+
+   private void printInternal(SOAPElementImpl element)
+   {
+      // is there anything to do?
+      if (element == null)
+      {
+         return;
+      }
+
+      if (wroteXMLDeclaration == false && writeXMLDeclaration == true && canonical == false)
+      {
+         out.print("<?xml version='1.0'");
+         if (charsetName != null)
+            out.print(" encoding='" + charsetName + "'");
+
+         out.println("?>");
+         wroteXMLDeclaration = true;
+      }
+
+      writeElement(element, out, prettyprint);
+
+      out.flush();
+   }
+
+   private static void writeElement(SOAPElementImpl element, PrintWriter out, boolean pretty) {
+
+      // the element itself
+      String endTag = element.write(out, pretty);
+
+      // and it's children
+      Iterator it = element.getChildElements();
+      while(it.hasNext())
+      {
+         Object child = it.next();
+         if(child instanceof SOAPElement)
+         {
+            SOAPElementImpl childElement = (SOAPElementImpl)child;
+            writeElement(childElement, out, pretty);
+         }
+      }
+
+      if(endTag!=null)
+         out.write(endTag);
+   }
+
+}


Property changes on: branches/jbossws-1.0_Heiko/src/main/java/org/jboss/ws/soap/SAAJElementWriter.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Added: branches/jbossws-1.0_Heiko/src/test/java/org/jboss/test/ws/soap/SAAJElementWriterTestCase.java
===================================================================
--- branches/jbossws-1.0_Heiko/src/test/java/org/jboss/test/ws/soap/SAAJElementWriterTestCase.java	2006-08-07 13:49:55 UTC (rev 699)
+++ branches/jbossws-1.0_Heiko/src/test/java/org/jboss/test/ws/soap/SAAJElementWriterTestCase.java	2006-08-07 13:50:14 UTC (rev 700)
@@ -0,0 +1,151 @@
+/*
+* 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.test.ws.soap;
+
+import org.jboss.test.ws.JBossWSTest;
+import org.jboss.ws.jaxrpc.Style;
+import org.jboss.ws.soap.MessageFactoryImpl;
+import org.jboss.ws.soap.SOAPElementImpl;
+import org.jboss.ws.soap.SAAJElementWriter;
+import org.jboss.util.xml.DOMWriter;
+
+import javax.xml.soap.SOAPEnvelope;
+import javax.xml.soap.SOAPMessage;
+import java.io.*;
+
+/**
+ * @author Heiko Braun <heiko.braun at jboss.com>
+ * @version $Id$
+ * @since Aug 4, 2006
+ */
+public class SAAJElementWriterTestCase extends JBossWSTest {
+
+   String envStr =
+       "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+           " <env:Body>" +
+           "  <businessList generic='2.0' operator='JBOSS' xmlns='urn:uddi-org:api_v2'>" +
+           "   <businessInfos>" +
+           "    <businessInfo businessKey='892ac280-c16b-11d5-85ad-801eef211111'>" +
+           "     <name xml:lang='en'>Demi Credit</name>" +
+           "     <description xml:lang='en'>A smaller demo app used for illustrating UDDI inquiry.</description>" +
+           "     <serviceInfos>" +
+           "      <serviceInfo businessKey='9a26b6e0-c15f-11d5-85a3-801eef208714' serviceKey='860eca90-c16d-11d5-85ad-801eef208714'>" +
+           "       <name xml:lang='en'>DCAmail</name>" +
+           "      </serviceInfo>" +
+           "     </serviceInfos>" +
+           "    </businessInfo>" +
+           "   </businessInfos>" +
+           "  </businessList>" +
+           " </env:Body>" +
+           "</env:Envelope>";
+
+   public void testWriterDocLit() throws Exception {
+
+      for(int i=1; i<10; i++)
+      {
+         String fileName = "resources/soap/req" + i + ".xml";
+         //System.out.println("Testing " + fileName);
+
+         File source = new File(fileName);
+         InputStream inputStream = new BufferedInputStream(new FileInputStream(source));
+
+         MessageFactoryImpl factory = new MessageFactoryImpl();
+         factory.setStyle(Style.DOCUMENT);
+         SOAPMessage soapMsg = factory.createMessage(null, inputStream);
+         SOAPEnvelope env = soapMsg.getSOAPPart().getEnvelope();
+
+         try
+         {
+            String xml = dump(env);
+            verify(xml);
+         }
+         catch (Exception e)
+         {
+            System.err.println(fileName + " FAILED:");
+            System.err.println(e.getMessage());
+            fail(e.getMessage());
+         }
+
+      }
+   }
+
+   public void testWriterRPC() throws Exception {
+
+      for(int i=1; i<10; i++)
+      {
+         String fileName = "resources/soap/req" + i + ".xml";
+         //System.out.println("Testing " + fileName);
+
+         File source = new File(fileName);
+         InputStream inputStream = new BufferedInputStream(new FileInputStream(source));
+
+         MessageFactoryImpl factory = new MessageFactoryImpl();
+         factory.setStyle(Style.RPC);
+         SOAPMessage soapMsg = factory.createMessage(null, inputStream);
+         SOAPEnvelope env = soapMsg.getSOAPPart().getEnvelope();
+
+         String xml = dump(env);
+         verify(xml);
+      }
+  }
+
+    public void testWriterMessage() throws Exception {
+
+       for(int i=1; i<10; i++)
+       {
+          String fileName = "resources/soap/req" + i + ".xml";
+          //System.out.println("Testing " + fileName);
+
+          File source = new File(fileName);
+          InputStream inputStream = new BufferedInputStream(new FileInputStream(source));
+
+          MessageFactoryImpl factory = new MessageFactoryImpl();
+          factory.setStyle(null);
+          SOAPMessage soapMsg = factory.createMessage(null, inputStream);
+          SOAPEnvelope env = soapMsg.getSOAPPart().getEnvelope();
+
+          String xml = dump(env);
+          verify(xml);
+       }
+   }
+
+   private String dump(SOAPEnvelope env) throws Exception
+   {
+      String xml = SAAJElementWriter.printSOAPElement((SOAPElementImpl)env, true);
+      //System.out.println(xml);
+      //System.out.println("");
+      return xml;
+   }
+
+   private void verify(String xml) throws Exception {
+      ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
+
+      MessageFactoryImpl factory = new MessageFactoryImpl();
+      factory.setStyle(Style.RPC);
+      SOAPMessage soapMsg = factory.createMessage(null, inputStream);
+      SOAPEnvelope env = soapMsg.getSOAPPart().getEnvelope();
+
+      String marshalled = DOMWriter.printNode(env, true);
+      //System.out.println( marshalled );
+      //System.out.println("\n\n");
+   }
+}


Property changes on: branches/jbossws-1.0_Heiko/src/test/java/org/jboss/test/ws/soap/SAAJElementWriterTestCase.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF




More information about the jboss-svn-commits mailing list