Author: scheu
Date: Thu Nov 15 07:40:44 2007
New Revision: 595335
URL: http://svn.apache.org/viewvc?rev=595335&view=rev
Log:
WSCOMMONS-279
Contributor:Rich Scheuerle
Debugging/Integration: Samuel Isokpunwu
Avoid XMLStreamWriter.getNamespaceContext() usage
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java?rev=595335&r1=595334&r2=595335&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
Thu Nov 15 07:40:44 2007
@@ -169,7 +169,7 @@
if (!setPrefixFirst) {
if (eNamespace != null) {
if (ePrefix == null) {
- if
(writer.getNamespaceContext().getNamespaceURI(eNamespace) == null) {
+ if (!OMSerializerUtil.isAssociated("", eNamespace,
writer)) {
if (writePrefixList == null) {
writePrefixList = new ArrayList();
@@ -182,7 +182,7 @@
writer.writeStartElement("", reader.getLocalName(),
eNamespace);
} else {
- if
(writer.getNamespaceContext().getNamespaceURI(eNamespace) == null) {
+ if (!OMSerializerUtil.isAssociated(ePrefix, eNamespace,
writer)) {
if (writePrefixList == null) {
writePrefixList = new ArrayList();
writeNSList = new ArrayList();
@@ -411,14 +411,16 @@
prefix = reader.getAttributePrefix(i);
namespaceName = reader.getAttributeNamespace(i);
/*
- Due to parser implementations returning null as the namespace
URI
- (for the empty namespace) we need to make sure that we deal with
- a namespace name that is not null. The best way to work around
this
- issue is to set the namespace uri to "" if it is null
+ Some parser implementations return null for the unqualified
namespace.
+ But getPrefix(null) will throw an exception (according to the
XMLStreamWriter
+ javadoc. We guard against this by using "" for the unqualified
namespace.
*/
- if (namespaceName == null) namespaceName = "";
+ namespaceName =(namespaceName == null) ? "" : namespaceName;
- writerPrefix =
writer.getNamespaceContext().getPrefix(namespaceName);
+ // Using getNamespaceContext should be avoided when not necessary.
+ // Some parser implementations construct a new NamespaceContext
each time it is invoked.
+ // writerPrefix =
writer.getNamespaceContext().getPrefix(namespaceName);
+ writerPrefix = writer.getPrefix(namespaceName);
if (!"".equals(namespaceName)) {
//prefix has already being declared but this particular attrib
has a
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java?rev=595335&r1=595334&r2=595335&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java
Thu Nov 15 07:40:44 2007
@@ -25,6 +25,8 @@
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.OMConstants;
import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
@@ -34,7 +36,7 @@
import java.util.Iterator;
public class OMSerializerUtil {
-
+ private static Log log = LogFactory.getLog(OMSerializerUtil.class);
static long nsCounter = 0;
// This property should be used by the parser to indicate whether
@@ -239,9 +241,7 @@
if (!setPrefixFirst) {
if (eNamespace != null) {
if (ePrefix == null) {
- if (writer.getNamespaceContext().getNamespaceURI("") ==
null
- ||
!writer.getNamespaceContext().getNamespaceURI("").equals(eNamespace)) {
-
+ if (!isAssociated("", eNamespace, writer)) {
if (writePrefixList == null) {
writePrefixList = new ArrayList();
writeNSList = new ArrayList();
@@ -257,7 +257,7 @@
* If
XMLStreamWriter.writeStartElement(prefix,localName,namespaceURI) associates
* the prefix with the namespace ..
*/
- if (writer.getNamespaceContext().getNamespaceURI(ePrefix)
== null) {
+ if (!isAssociated(ePrefix, eNamespace, writer)) {
if (writePrefixList == null) {
writePrefixList = new ArrayList();
writeNSList = new ArrayList();
@@ -570,43 +570,105 @@
*/
public static String generateSetPrefix(String prefix, String namespace,
XMLStreamWriter writer,
boolean attr, boolean
isSetPrefixFirst) throws XMLStreamException {
+ prefix = (prefix == null) ? "" : prefix;
+
+
+ // If the prefix and namespace are already associated, no generation
is needed
+ if (isAssociated(prefix, namespace, writer)) {
+ return null;
+ }
+
+ // Attributes without a prefix always are associated with the
unqualified namespace
+ // according to the schema specification. No generation is needed.
+ if (prefix.length() == 0 && namespace == null && attr) {
+ return null;
+ }
+
// Generate setPrefix/setDefaultNamespace if the prefix is not
associated.
String newPrefix = null;
if (namespace != null) {
// Qualified Namespace
-
- // Get the namespace associated with this writer
- String writerNS =
- writer.getNamespaceContext().getNamespaceURI((prefix ==
null) ? "" : prefix);
- writerNS = (writerNS != null && writerNS.length() == 0) ? null :
writerNS;
-
- if (writerNS == null || !writerNS.equals(namespace)) {
- // Writer has not associated this namespace with a prefix
- if (prefix == null) {
- writer.setDefaultNamespace(namespace);
- newPrefix = "";
- } else {
- writer.setPrefix(prefix, namespace);
- newPrefix = prefix;
- }
+ if (prefix.length() == 0) {
+ writer.setDefaultNamespace(namespace);
+ newPrefix = "";
} else {
- // No Action needed..The writer already has associated this
prefix to this namespace
- if(isSetPrefixFirst){
- newPrefix =
writer.getNamespaceContext().getPrefix(namespace);
- newPrefix = (newPrefix == null || newPrefix.length() == 0)
? null : newPrefix ;
- }
+ writer.setPrefix(prefix, namespace);
+ newPrefix = prefix;
}
} else {
// Unqualified Namespace
-
- // Make sure the default namespace is either not used or disabled
(set to "")
+ // Disable the default namespace
+ writer.setDefaultNamespace("");
+ newPrefix = "";
+ }
+ return newPrefix;
+ }
+ /**
+ * @param prefix
+ * @param namespace
+ * @param writer
+ * @return true if the prefix is associated with the namespace in the
current context
+ */
+ public static boolean isAssociated(String prefix, String namespace,
XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ // NOTE: Calling getNamespaceContext() on many XMLStreamWriter
implementations is expensive.
+ // Please use other writer methods first.
+
+ // For consistency, convert null arguments.
+ // This helps get around the parser implementation differences.
+ // In addition, the getPrefix/getNamespace methods cannot be called
with null parameters.
+ prefix = (prefix == null) ? "" : prefix;
+ namespace = (namespace == null) ? "" : namespace;
+
+ if (namespace.length() > 0) {
+ // QUALIFIED NAMESPACE
+ // Get the namespace associated with the prefix
+ String writerPrefix = writer.getPrefix(namespace);
+ if (prefix.equals(writerPrefix)) {
+ return true;
+ }
+
+ // It is possible that the namespace is associated with multiple
prefixes,
+ // So try getting the namespace as a second step.
+ if (writerPrefix != null) {
+ String writerNS =
writer.getNamespaceContext().getNamespaceURI(prefix);
+ return namespace.equals(writerNS);
+ }
+ return false;
+ } else {
+ // UNQUALIFIED NAMESPACE
+
+ // Cannot associate a prefix with an unqualifed name.
+ // However sometimes axiom creates a fake prefix name if xmns=""
is not in effect.
+ // So return true
+ if (prefix.length() > 0) {
+ return true;
+ }
+
+ // Get the namespace associated with the prefix.
+ // It is illegal to call getPrefix with null, but the
specification is not
+ // clear on what happens if called with "". So the following code
is
+ // protected
+ try {
+ String writerPrefix = writer.getPrefix("");
+ if (writerPrefix != null && writerPrefix.length() == 0) {
+ return true;
+ }
+ } catch (Throwable t) {
+ if (log.isDebugEnabled()) {
+ log.debug("Caught exception from getPrefix(\"\").
Processing continues: " + t);
+ }
+ }
+
+
+
+ // Fallback to using the namespace context
String writerNS = writer.getNamespaceContext().getNamespaceURI("");
- if (writerNS != null && writerNS.length() > 0 && !attr) {
- // Disable the default namespace
- writer.setDefaultNamespace("");
- newPrefix = "";
+ if (writerNS != null && writerNS.length() > 0) {
+ return false;
}
+ return true;
}
- return newPrefix;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]