Author: heiko.braun(a)jboss.com
Date: 2007-04-03 12:36:52 -0400 (Tue, 03 Apr 2007)
New Revision: 2751
Modified:
trunk/jbossws-core/src/java/org/jboss/ws/core/soap/XMLFragment.java
trunk/jbossws-tests/src/java/org/jboss/test/ws/common/soap/XMLFragmentTestCase.java
Log:
Skip processing instructions in xml fragments
Modified: trunk/jbossws-core/src/java/org/jboss/ws/core/soap/XMLFragment.java
===================================================================
--- trunk/jbossws-core/src/java/org/jboss/ws/core/soap/XMLFragment.java 2007-04-03
16:28:03 UTC (rev 2750)
+++ trunk/jbossws-core/src/java/org/jboss/ws/core/soap/XMLFragment.java 2007-04-03
16:36:52 UTC (rev 2751)
@@ -67,6 +67,8 @@
private Source source;
+ private final static String XML_PROC = "<?xml";
+
// An exception that is created when a client
// accesses a StreamSource that can only be read once
private RuntimeException streamSourceAccessMarker;
@@ -148,7 +150,10 @@
public void writeTo(OutputStream out) throws IOException
{
- writeSourceInternal(new PrintWriter(out));
+ PrintWriter printWriter = new PrintWriter(out);
+ writeSourceInternal(printWriter);
+ printWriter.flush();
+ printWriter.close();
}
/**
@@ -170,18 +175,45 @@
StreamSource streamSource = (StreamSource)source;
Reader reader = streamSource.getReader();
+ {
if (reader == null)
reader = new InputStreamReader(streamSource.getInputStream());
+ }
- char[] cbuf = new char[1024];
+ char[] cbuf = new char[5];
int r = reader.read(cbuf);
+ int xmlProc = -1;
if (r == -1)
throw new IOException("StreamSource already exhausted");
while (r > 0)
{
- writer.write(cbuf, 0, r);
+ if(xmlProc<0 && new String(cbuf).equals(XML_PROC)) // new
fragment
+ {
+ xmlProc = 0;
+ }
+ else if(xmlProc<0) // no processing instruction
+ {
+ xmlProc = 1;
+ }
+ else if(xmlProc==0) // within processing instruction
+ {
+ String tmp = new String(cbuf);
+ int i = tmp.indexOf(">");
+ if(i!=-1)
+ {
+ if(i<tmp.length()) writer.write(tmp.substring(i+1));
+ xmlProc=1;
+ r = reader.read(cbuf);
+ continue;
+ }
+
+ }
+
+ // regular contents
+ if(xmlProc>0)
+ writer.write(cbuf, 0, r);
r = reader.read(cbuf);
}
}
@@ -200,8 +232,12 @@
private Source beginSourceAccess(Source source)
{
+
+ if(source instanceof BufferedStreamSource)
+ return source; // no need to buffer those
+
// Buffer the source content
- if (source instanceof StreamSource && !(source instanceof
BufferedStreamSource))
+ if (source instanceof StreamSource)
{
source = new BufferedStreamSource((StreamSource)source);
}
Modified:
trunk/jbossws-tests/src/java/org/jboss/test/ws/common/soap/XMLFragmentTestCase.java
===================================================================
---
trunk/jbossws-tests/src/java/org/jboss/test/ws/common/soap/XMLFragmentTestCase.java 2007-04-03
16:28:03 UTC (rev 2750)
+++
trunk/jbossws-tests/src/java/org/jboss/test/ws/common/soap/XMLFragmentTestCase.java 2007-04-03
16:36:52 UTC (rev 2751)
@@ -40,6 +40,10 @@
public static String XML_STRING =
"<parent><child/></parent>";
+ public static String JAXB_FRAGMENT = "<?xml version=\"1.0\"
encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
+ "<ns2:sayHelloResponse
xmlns:ns2=\"http://hello.org/wsdl\">\n" +
+
"<return>WSEjbWebServiceProvider-SayHello</return></ns2:sayHelloResponse>";
+
public void testDOMSourceFragment() throws Exception
{
Element srcElement = DOMUtils.parse(XML_STRING);
@@ -60,4 +64,36 @@
xmlFragment.writeTo(writer);
assertEquals(XML_STRING, new String(bout.toByteArray()));
}
+
+ public void testJAXBFragments() throws Exception
+ {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ PrintWriter writer = new PrintWriter( bout);
+
+ XMLFragment fragment = new XMLFragment(JAXB_FRAGMENT);
+ fragment.writeTo(writer);
+ writer.flush();
+
+ String s = bout.toString();
+ //System.out.println(s);
+
+ assertTrue("Empty result returned", s.length()>0);
+ assertFalse("Should not contain processing instruction",
s.startsWith("<?xml"));
+ }
+
+ public void testJAXBFragments2() throws Exception
+ {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ PrintWriter writer = new PrintWriter( bout);
+
+ XMLFragment fragment = new XMLFragment(XML_STRING);
+ fragment.writeTo(writer);
+ writer.flush();
+
+ String s = bout.toString();
+
+ assertTrue("Empty result returned", s.length()>0);
+ assertFalse("Should not contain processing instruction",
s.startsWith("<?xml"));
+ assertEquals(XML_STRING, s);
+ }
}