Author: veithen
Date: Wed Feb 6 22:36:45 2013
New Revision: 1443249
URL: http://svn.apache.org/viewvc?rev=1443249&view=rev
Log:
More SAX serialization code.
Modified:
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/ScopedNamespaceContext.java
webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/util/namespace/ScopedNamespaceContextTest.java
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/Serializer.java
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/sax/SAXSerializer.java
Modified:
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/ScopedNamespaceContext.java
URL:
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/ScopedNamespaceContext.java?rev=1443249&r1=1443248&r2=1443249&view=diff
==============================================================================
---
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/ScopedNamespaceContext.java
(original)
+++
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/util/namespace/ScopedNamespaceContext.java
Wed Feb 6 22:36:45 2013
@@ -109,6 +109,50 @@ public class ScopedNamespaceContext exte
bindings = scopeIndexes[--scopes];
}
+ /**
+ * Get the number of namespace bindings defined in this context, in all
scopes. This number
+ * increases every time {@link #setPrefix(String, String)} is called. It
decreases when
+ * {@link #endScope()} is called (unless no bindings have been added in
the current scope).
+ *
+ * @return the namespace binding count
+ */
+ public int getBindingsCount() {
+ return bindings;
+ }
+
+ /**
+ * Get the index of the first namespace binding defined in the current
scope. Together with
+ * {@link #getBindingsCount()} this method can be used to iterate over the
namespace bindings
+ * defined in the current scope.
+ *
+ * @return the index of the first namespace binding defined in the current
scope
+ */
+ public int getFirstBindingInCurrentScope() {
+ return scopes == 0 ? 0 : scopeIndexes[scopes-1];
+ }
+
+ /**
+ * Get the prefix of the binding with the given index.
+ *
+ * @param index
+ * the index of the binding
+ * @return the prefix
+ */
+ public String getPrefix(int index) {
+ return prefixArray[index];
+ }
+
+ /**
+ * Get the namespace URI of the binding with the given index.
+ *
+ * @param index
+ * the index of the binding
+ * @return the namespace URI
+ */
+ public String getNamespaceURI(int index) {
+ return uriArray[index];
+ }
+
protected String doGetNamespaceURI(String prefix) {
for (int i=bindings-1; i>=0; i--) {
if (prefix.equals(prefixArray[i])) {
Modified:
webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/util/namespace/ScopedNamespaceContextTest.java
URL:
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/util/namespace/ScopedNamespaceContextTest.java?rev=1443249&r1=1443248&r2=1443249&view=diff
==============================================================================
---
webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/util/namespace/ScopedNamespaceContextTest.java
(original)
+++
webservices/axiom/trunk/modules/axiom-api/src/test/java/org/apache/axiom/util/namespace/ScopedNamespaceContextTest.java
Wed Feb 6 22:36:45 2013
@@ -71,18 +71,34 @@ public class ScopedNamespaceContextTest
public void testScope() {
ScopedNamespaceContext nc = new ScopedNamespaceContext();
nc.setPrefix("ns1", "urn:ns1");
+ assertEquals(0, nc.getFirstBindingInCurrentScope());
+ assertEquals(1, nc.getBindingsCount());
nc.startScope();
nc.setPrefix("ns2", "urn:ns2");
+ assertEquals(1, nc.getFirstBindingInCurrentScope());
+ assertEquals(2, nc.getBindingsCount());
nc.startScope();
nc.setPrefix("ns3", "urn:ns3");
+ assertEquals(2, nc.getFirstBindingInCurrentScope());
+ assertEquals(3, nc.getBindingsCount());
assertEquals("urn:ns1", nc.getNamespaceURI("ns1"));
assertEquals("urn:ns2", nc.getNamespaceURI("ns2"));
assertEquals("urn:ns3", nc.getNamespaceURI("ns3"));
+ assertEquals("ns1", nc.getPrefix(0));
+ assertEquals("urn:ns1", nc.getNamespaceURI(0));
+ assertEquals("ns2", nc.getPrefix(1));
+ assertEquals("urn:ns2", nc.getNamespaceURI(1));
+ assertEquals("ns3", nc.getPrefix(2));
+ assertEquals("urn:ns3", nc.getNamespaceURI(2));
nc.endScope();
+ assertEquals(1, nc.getFirstBindingInCurrentScope());
+ assertEquals(2, nc.getBindingsCount());
assertEquals("urn:ns1", nc.getNamespaceURI("ns1"));
assertEquals("urn:ns2", nc.getNamespaceURI("ns2"));
assertEquals(XMLConstants.NULL_NS_URI, nc.getNamespaceURI("ns3"));
nc.endScope();
+ assertEquals(0, nc.getFirstBindingInCurrentScope());
+ assertEquals(1, nc.getBindingsCount());
assertEquals("urn:ns1", nc.getNamespaceURI("ns1"));
assertEquals(XMLConstants.NULL_NS_URI, nc.getNamespaceURI("ns2"));
assertEquals(XMLConstants.NULL_NS_URI, nc.getNamespaceURI("ns3"));
Modified:
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/Serializer.java
URL:
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/Serializer.java?rev=1443249&r1=1443248&r2=1443249&view=diff
==============================================================================
---
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/Serializer.java
(original)
+++
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/Serializer.java
Wed Feb 6 22:36:45 2013
@@ -327,6 +327,24 @@ public abstract class Serializer {
*/
protected abstract void beginStartElement(String prefix, String
namespaceURI, String localName) throws OutputException;
+ /**
+ * Add the given namespace to the element. The implementation of this
method must take the
+ * appropriate actions such that the following two conditions are
satisfied:
+ * <ul>
+ * <li>A namespace declaration is written to the output.
+ * <li>The namespace binding defined by the parameters is visible in the
namespace context of
+ * the {@link XMLStreamWriter} that {@link
#serializePushOMDataSource(OMDataSource)} passes to
+ * {@link OMDataSource#serialize(XMLStreamWriter)} if an {@link
OMDataSource} is serialized in
+ * the scope of the current element (and of course unless the namespace
binding is hidden by a
+ * namespace defined on a nested element).
+ * </ul>
+ *
+ * @param prefix
+ * the namespace prefix; never <code>null</code>
+ * @param namespaceURI
+ * the namespace URI; never <code>null</code>
+ * @throws OutputException
+ */
protected abstract void addNamespace(String prefix, String namespaceURI)
throws OutputException;
protected abstract void addAttribute(String prefix, String namespaceURI,
String localName, String value) throws OutputException;
Modified:
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/sax/SAXSerializer.java
URL:
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/sax/SAXSerializer.java?rev=1443249&r1=1443248&r2=1443249&view=diff
==============================================================================
---
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/sax/SAXSerializer.java
(original)
+++
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/serializer/push/sax/SAXSerializer.java
Wed Feb 6 22:36:45 2013
@@ -18,6 +18,8 @@
*/
package org.apache.axiom.om.impl.common.serializer.push.sax;
+import java.util.Stack;
+
import javax.activation.DataHandler;
import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
@@ -36,6 +38,8 @@ public class SAXSerializer extends Seria
private final ContentHandler contentHandler;
private final LexicalHandler lexicalHandler;
private final ScopedNamespaceContext nsContext = new
ScopedNamespaceContext();
+ private int depth;
+ private Stack elementNameStack = new Stack();
private String elementURI;
private String elementLocalName;
private String elementQName;
@@ -75,15 +79,20 @@ public class SAXSerializer extends Seria
} else {
elementQName = prefix + ":" + localName;
}
+ nsContext.startScope();
}
protected void addNamespace(String prefix, String namespaceURI) throws
OutputException {
- // TODO
- throw new UnsupportedOperationException();
+ nsContext.setPrefix(prefix, namespaceURI);
+ try {
+ contentHandler.startPrefixMapping(prefix, namespaceURI);
+ } catch (SAXException ex) {
+ throw new SAXOutputException(ex);
+ }
+ // TODO: depending on the http://xml.org/sax/features/xmlns-uris
feature, we also need to add an attribute
}
- protected void addAttribute(String prefix, String namespaceURI, String
localName, String value)
- throws OutputException {
+ protected void addAttribute(String prefix, String namespaceURI, String
localName, String value) throws OutputException {
// TODO
throw new UnsupportedOperationException();
}
@@ -94,6 +103,9 @@ public class SAXSerializer extends Seria
} catch (SAXException ex) {
throw new SAXOutputException(ex);
}
+ elementNameStack.push(elementURI);
+ elementNameStack.push(elementLocalName);
+ elementNameStack.push(elementQName);
elementURI = null;
elementLocalName = null;
elementQName = null;
@@ -101,8 +113,17 @@ public class SAXSerializer extends Seria
}
public void writeEndElement() throws OutputException {
- // TODO
- throw new UnsupportedOperationException();
+ try {
+ String elementQName = (String)elementNameStack.pop();
+ String elementLocalName = (String)elementNameStack.pop();
+ String elementURI = (String)elementNameStack.pop();
+ contentHandler.endElement(elementURI, elementLocalName,
elementQName);
+ for (int i=nsContext.getBindingsCount()-1;
i>=nsContext.getFirstBindingInCurrentScope(); i--) {
+ contentHandler.endPrefixMapping(nsContext.getPrefix(i));
+ }
+ } catch (SAXException ex) {
+ throw new SAXOutputException(ex);
+ }
}
public void writeText(int type, String data) throws OutputException {