Author: thomas.diesler(a)jboss.com
Date: 2008-03-14 10:40:51 -0400 (Fri, 14 Mar 2008)
New Revision: 5981
Added:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/BadgerFishXMLEventWriter.java
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/JsonAPITestCase.java
Modified:
stack/native/trunk/.classpath
stack/native/trunk/ant-import-tests/build-testsuite.xml
stack/native/trunk/ant-import/build-thirdparty.xml
stack/native/trunk/version.properties
Log:
[JBWS-1165] JSON support, first cut
Modified: stack/native/trunk/.classpath
===================================================================
--- stack/native/trunk/.classpath 2008-03-14 13:55:13 UTC (rev 5980)
+++ stack/native/trunk/.classpath 2008-03-14 14:40:51 UTC (rev 5981)
@@ -40,5 +40,6 @@
<classpathentry kind="lib" path="thirdparty/jbossws-common.jar"
sourcepath="thirdparty/jbossws-common-src.zip"/>
<classpathentry kind="lib" path="thirdparty/jbossws-spi.jar"
sourcepath="thirdparty/jbossws-spi-src.zip"/>
<classpathentry kind="lib"
path="thirdparty/FastInfoset.jar"/>
+ <classpathentry kind="lib" path="thirdparty/jettison.jar"
sourcepath="thirdparty/jettison-src.jar"/>
<classpathentry kind="output" path="output/eclipse"/>
</classpath>
Modified: stack/native/trunk/ant-import/build-thirdparty.xml
===================================================================
--- stack/native/trunk/ant-import/build-thirdparty.xml 2008-03-14 13:55:13 UTC (rev 5980)
+++ stack/native/trunk/ant-import/build-thirdparty.xml 2008-03-14 14:40:51 UTC (rev 5981)
@@ -75,6 +75,8 @@
<get
src="${jboss.repository}/apache-xerces/${apache-xerces}/lib/xercesImpl.jar"
dest="${thirdparty.dir}/xercesImpl.jar" usetimestamp="true"
verbose="true"/>
<get
src="${jboss.repository}/apache-xerces/${apache-xerces}/lib/xml-apis.jar"
dest="${thirdparty.dir}/xml-apis.jar" usetimestamp="true"
verbose="true"/>
<get
src="${jboss.repository}/apache-xmlsec/${apache-xmlsec}/lib/xmlsec.jar"
dest="${thirdparty.dir}/xmlsec.jar" usetimestamp="true"
verbose="true"/>
+ <get
src="${jboss.repository}/codehaus-jettison/${codehaus-jettison}/lib/jettison.jar"
dest="${thirdparty.dir}/jettison.jar" usetimestamp="true"
verbose="true"/>
+ <get
src="${jboss.repository}/codehaus-jettison/${codehaus-jettison}/lib/jettison-src.jar"
dest="${thirdparty.dir}/jettison-src.jar" usetimestamp="true"
verbose="true"/>
<get src="${jboss.repository}/dom4j/${dom4j}/lib/dom4j.jar"
dest="${thirdparty.dir}/dom4j.jar" usetimestamp="true"
verbose="true"/>
<get src="${jboss.repository}/gnu-getopt/${gnu-getopt}/lib/getopt.jar"
dest="${thirdparty.dir}/getopt.jar" usetimestamp="true"
verbose="true"/>
<get src="${jboss.repository}/hibernate/${hibernate}/lib/hibernate3.jar"
dest="${thirdparty.dir}/hibernate3.jar" usetimestamp="true"
verbose="true"/>
@@ -161,6 +163,7 @@
<pathelement location="${thirdparty.dir}/jboss-remoting.jar"/>
<pathelement
location="${thirdparty.dir}/jboss-xml-binding.jar"/>
<pathelement location="${thirdparty.dir}/jbosssx.jar"/>
+ <pathelement location="${thirdparty.dir}/jettison.jar"/>
<pathelement location="${thirdparty.dir}/mail.jar"/>
<pathelement location="${thirdparty.dir}/policy.jar"/>
<pathelement location="${thirdparty.dir}/servlet-api.jar"/>
Modified: stack/native/trunk/ant-import-tests/build-testsuite.xml
===================================================================
--- stack/native/trunk/ant-import-tests/build-testsuite.xml 2008-03-14 13:55:13 UTC (rev
5980)
+++ stack/native/trunk/ant-import-tests/build-testsuite.xml 2008-03-14 14:40:51 UTC (rev
5981)
@@ -40,8 +40,9 @@
<path id="tests.extra.classpath">
<pathelement location="${core.dir}/thirdparty/FastInfoset.jar"/>
+ <pathelement location="${core.dir}/thirdparty/jaxws-tools.jar"/>
<pathelement
location="${core.dir}/thirdparty/jbossws-framework.jar"/>
- <pathelement location="${core.dir}/thirdparty/jaxws-tools.jar"/>
+ <pathelement location="${core.dir}/thirdparty/jettison.jar"/>
<pathelement location="${core.dir}/thirdparty/policy.jar"/>
<pathelement location="${core.dir}/thirdparty/qdox.jar"/>
<pathelement location="${core.dir}/thirdparty/stax-api.jar"/>
Added:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/BadgerFishXMLEventWriter.java
===================================================================
---
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/BadgerFishXMLEventWriter.java
(rev 0)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/BadgerFishXMLEventWriter.java 2008-03-14
14:40:51 UTC (rev 5981)
@@ -0,0 +1,153 @@
+/*
+ * 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.jaxws.json;
+
+import java.util.Iterator;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.Characters;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+/**
+ * An BadgerFishXMLEventWriter that delegates to an XMLStreamWriter.
+ *
+ * @author Thomas.Diesler(a)jboss.com
+ * @since 12-Mar-2008
+ */
+public class BadgerFishXMLEventWriter implements XMLEventWriter
+{
+ private XMLStreamWriter streamWriter;
+
+ public BadgerFishXMLEventWriter(XMLStreamWriter streamWriter)
+ {
+ this.streamWriter = streamWriter;
+ }
+
+ public void add(XMLEvent event) throws XMLStreamException
+ {
+ if (event.isStartDocument())
+ {
+ streamWriter.writeStartDocument();
+ }
+ else if (event.isStartElement())
+ {
+ StartElement element = event.asStartElement();
+ QName elQName = element.getName();
+ if (elQName.getPrefix().length() > 0 &&
elQName.getNamespaceURI().length() > 0)
+ streamWriter.writeStartElement(elQName.getPrefix(), elQName.getLocalPart(),
elQName.getNamespaceURI());
+ else if (elQName.getNamespaceURI().length() > 0)
+ streamWriter.writeStartElement(elQName.getNamespaceURI(),
elQName.getLocalPart());
+ else
+ streamWriter.writeStartElement(elQName.getLocalPart());
+
+ Iterator attris = element.getAttributes();
+ while (attris.hasNext())
+ {
+ Attribute attr = (Attribute)attris.next();
+ QName atQName = attr.getName();
+ String value = attr.getValue();
+ if (atQName.getPrefix().length() > 0 &&
atQName.getNamespaceURI().length() > 0)
+ streamWriter.writeAttribute(atQName.getPrefix(),
atQName.getNamespaceURI(), atQName.getLocalPart(), value);
+ else if (atQName.getNamespaceURI().length() > 0)
+ streamWriter.writeAttribute(atQName.getNamespaceURI(),
atQName.getLocalPart(), value);
+ else
+ streamWriter.writeAttribute(atQName.getLocalPart(), value);
+ }
+ }
+ else if (event.isNamespace())
+ {
+ Attribute attr = (Attribute)event;
+ QName qname = attr.getName();
+ String nsURI = attr.getValue();
+ streamWriter.writeNamespace(qname.getPrefix(), nsURI);
+ }
+ else if (event.isCharacters())
+ {
+ Characters chars = event.asCharacters();
+ streamWriter.writeCharacters(chars.getData());
+ }
+ else if (event.isEndElement())
+ {
+ streamWriter.writeEndElement();
+ }
+ else if (event.isEndDocument())
+ {
+ streamWriter.writeEndDocument();
+ }
+ else
+ {
+ throw new XMLStreamException("Unsupported event type: " + event);
+ }
+ }
+
+ public void add(XMLEventReader eventReader) throws XMLStreamException
+ {
+ while (eventReader.hasNext())
+ {
+ XMLEvent event = eventReader.nextEvent();
+ add(event);
+ }
+ close();
+ }
+
+ public void close() throws XMLStreamException
+ {
+ streamWriter.close();
+ }
+
+ public void flush() throws XMLStreamException
+ {
+ streamWriter.flush();
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return streamWriter.getNamespaceContext();
+ }
+
+ public String getPrefix(String prefix) throws XMLStreamException
+ {
+ return streamWriter.getPrefix(prefix);
+ }
+
+ public void setDefaultNamespace(String namespace) throws XMLStreamException
+ {
+ streamWriter.setDefaultNamespace(namespace);
+ }
+
+ public void setNamespaceContext(NamespaceContext nsContext) throws XMLStreamException
+ {
+ streamWriter.setNamespaceContext(nsContext);
+ }
+
+ public void setPrefix(String prefix, String uri) throws XMLStreamException
+ {
+ streamWriter.setPrefix(prefix, uri);
+ }
+}
\ No newline at end of file
Property changes on:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/BadgerFishXMLEventWriter.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/JsonAPITestCase.java
===================================================================
--- stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/JsonAPITestCase.java
(rev 0)
+++
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/JsonAPITestCase.java 2008-03-14
14:40:51 UTC (rev 5981)
@@ -0,0 +1,125 @@
+/*
+ * 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.jaxws.json;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.StringWriter;
+
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory;
+import org.codehaus.jettison.badgerfish.BadgerFishXMLOutputFactory;
+import org.codehaus.jettison.json.JSONTokener;
+import org.jboss.wsf.test.JBossWSTest;
+
+/**
+ * Test Json functionality
+ *
+ * @author Thomas.Diesler(a)jboss.com
+ * @since 12-Mar-2008
+ */
+public class JsonAPITestCase extends JBossWSTest
+{
+ public void testSimple() throws Exception
+ {
+ String xmlStr = "<root>hello world</root>";
+ String expStr = "{\"root\":{\"$\":\"hello
world\"}}";
+ String resStr = toJSON(xmlStr);
+ assertEquals("Unexpected result: " + resStr, expStr, resStr);
+
+ String resXML = toXML(resStr);
+ assertEquals("Unexpected result: " + resXML, xmlStr, resXML);
+ }
+
+ public void testSimpleAttribute() throws Exception
+ {
+ String xmlStr = "<root myat=\"value\">hello
world</root>";
+ String expStr =
"{\"root\":{\"@myat\":\"value\",\"$\":\"hello
world\"}}";
+ String resStr = toJSON(xmlStr);
+ assertEquals("Unexpected result: " + resStr, expStr, resStr);
+
+ String resXML = toXML(resStr);
+ assertEquals("Unexpected result: " + resXML, xmlStr, resXML);
+ }
+
+ public void _testDefaultNamespace() throws Exception
+ {
+ String xmlStr = "<root xmlns=\"http://somns\">hello
world</root>";
+ String expStr = "{\"root\":{\"$\":\"hello
world\"}}";
+ String resStr = toJSON(xmlStr);
+ assertEquals("Unexpected result: " + resStr, expStr, resStr);
+
+ String resXML = toXML(resStr);
+ assertEquals("Unexpected result: " + resXML, xmlStr, resXML);
+ }
+
+ private String toJSON(String srcXML) throws FactoryConfigurationError,
XMLStreamException
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(srcXML.getBytes());
+
+ XMLInputFactory readerFactory = XMLInputFactory.newInstance();
+ XMLStreamReader streamReader = readerFactory.createXMLStreamReader(bais);
+ XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader);
+
+ StringWriter strWriter = new StringWriter();
+ BadgerFishXMLOutputFactory writerFactory = new BadgerFishXMLOutputFactory();
+ XMLStreamWriter streamWriter = writerFactory.createXMLStreamWriter(strWriter);
+
+ // UnsupportedOperationException in jettison-1.0-RC2
+ //XMLEventWriter eventWriter = writerFactory.createXMLEventWriter(strWriter);
+
+ XMLEventWriter eventWriter = new BadgerFishXMLEventWriter(streamWriter);
+ eventWriter.add(eventReader);
+ eventWriter.close();
+
+ String jsonStr = strWriter.toString();
+ return jsonStr;
+ }
+
+ private String toXML(String jsonStr) throws XMLStreamException,
FactoryConfigurationError
+ {
+ BadgerFishXMLInputFactory inputFactory = new BadgerFishXMLInputFactory();
+ XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new
JSONTokener(jsonStr));
+ XMLInputFactory readerFactory = XMLInputFactory.newInstance();
+ XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
+ XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(baos);
+ eventWriter.add(eventReader);
+ eventWriter.close();
+
+ String resXML = new String(baos.toByteArray());
+ if (resXML.startsWith("<?xml "))
+ resXML = resXML.substring(resXML.indexOf("?>") + 2);
+
+ return resXML;
+ }
+}
Property changes on:
stack/native/trunk/src/test/java/org/jboss/test/ws/jaxws/json/JsonAPITestCase.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: stack/native/trunk/version.properties
===================================================================
--- stack/native/trunk/version.properties 2008-03-14 13:55:13 UTC (rev 5980)
+++ stack/native/trunk/version.properties 2008-03-14 14:40:51 UTC (rev 5981)
@@ -44,6 +44,7 @@
apache-logging=1.1.0.jboss
apache-xalan=j_2.7.0
apache-xerces=2.9.0
+codehaus-jettison=1.0-RC2
dom4j=1.6.1
gnu-getopt=1.0.10
hibernate=3.2.1.GA