More unfinished code.
diff --git a/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java b/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
index cdc08ec..6492e89 100644
--- a/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
+++ b/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
@@ -73,6 +73,15 @@
/** Namespace for element, needed in order to bypass base class handling. */
private OMNamespace definedNamespace = null;
+
+ /**
+ * Flag indicating whether the {@link #definedNamespace} attribute has been set. If this flag is
+ * <code>true</code> and {@link #definedNamespace} is <code>null</code> then the element has no
+ * namespace. If this flag is set to <code>false</code> (in which case {@link #definedNamespace}
+ * is always <code>null</code>) then the namespace is not known and needs to be determined
+ * lazily. The flag is used only if {@link #isExpanded} is <code>false</code>.
+ */
+ private boolean definedNamespaceSet;
/** Flag for parser provided to base element class. */
private boolean isExpanded;
@@ -122,6 +131,7 @@
// Create a deferred namespace that forces an expand to get the prefix
definedNamespace = new DeferredNamespace(ns.getNamespaceURI());
}
+ definedNamespaceSet = true;
}
/**
@@ -146,6 +156,7 @@
// Create a deferred namespace that forces an expand to get the prefix
definedNamespace = new DeferredNamespace(qName.getNamespaceURI());
}
+ definedNamespaceSet = true;
}
public OMSourcedElementImpl(String localName, OMNamespace ns, OMContainer parent, OMFactory factory) {
@@ -293,7 +304,7 @@
String readerLocalName = readerFromDS.getLocalName();
if (localName == null) {
- // The local name was not known in advance; initiliaze it from the reader
+ // The local name was not known in advance; initialize it from the reader
localName = readerLocalName;
} else {
// Make sure element local name and namespace matches what was expected
@@ -553,8 +564,39 @@
public OMNamespace getNamespace() throws OMException {
if (isExpanded()) {
return super.getNamespace();
+ } else if (definedNamespaceSet) {
+ return definedNamespace;
+ } else {
+ if (dataSource instanceof QNameAwareOMDataSource) {
+ String namespaceURI = ((QNameAwareOMDataSource)dataSource).getNamespaceURI();
+ if (namespaceURI != null) {
+ if (namespaceURI.length() == 0) {
+ // No namespace case. definedNamespace is already null, so we only need
+ // to set definedNamespaceSet to true. Note that we don't need to retrieve
+ // the namespace prefix because a prefix can't be bound to the empty
+ // namespace URI.
+ definedNamespaceSet = true;
+ } else {
+ String prefix = ((QNameAwareOMDataSource)dataSource).getPrefix();
+ if (prefix == null) {
+ // Prefix is unknown
+ definedNamespace = new DeferredNamespace(namespaceURI);
+ } else {
+ definedNamespace = new OMNamespaceImpl(namespaceURI, prefix);
+ }
+ definedNamespaceSet = true;
+ }
+ }
+ }
+ if (definedNamespaceSet) {
+ return definedNamespace;
+ } else {
+ // We have no information about the namespace of the element. Need to expand
+ // the element to get it.
+ forceExpand();
+ return super.getNamespace();
+ }
}
- return definedNamespace;
}
public String getPrefix() {
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
index 192732f..4d4aa9c 100644
--- a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
@@ -20,6 +20,8 @@
import java.lang.reflect.Method;
+import javax.xml.namespace.QName;
+
import org.apache.axiom.om.AbstractTestCase;
import org.apache.axiom.om.OMMetaFactory;
import org.apache.axiom.testutils.suite.TestSuiteBuilder;
@@ -326,6 +328,12 @@
addTest(new org.apache.axiom.ts.om.sourcedelement.TestExpand(metaFactory));
addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetLocalNameFromExpansion(metaFactory));
addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetLocalNameFromQNameAwareOMDataSource(metaFactory));
+ addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceFromExpansion(metaFactory, new QName("root")));
+ addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceFromExpansion(metaFactory, new QName("urn:test", "root", "p")));
+ addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceFromExpansion(metaFactory, new QName("urn:test", "root")));
+ addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceFromQNameAwareOMDataSource(metaFactory, new QName("root")));
+ addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceFromQNameAwareOMDataSource(metaFactory, new QName("urn:test", "root", "p")));
+ addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceFromQNameAwareOMDataSource(metaFactory, new QName("urn:test", "root")));
addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceNormalized(metaFactory));
addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceNormalized2(metaFactory));
addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetTextAsStreamWithNonDestructiveOMDataSource(metaFactory));
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetLocalNameFromExpansion.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetLocalNameFromExpansion.java
index 4b8bb01..b5bc5fe 100644
--- a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetLocalNameFromExpansion.java
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetLocalNameFromExpansion.java
@@ -37,5 +37,6 @@
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = factory.createOMElement(new TestDataSource("<root ns='urn:test'/>"));
assertEquals("root", element.getLocalName());
+ assertTrue(element.isExpanded());
}
}
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetNamespaceFromExpansion.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetNamespaceFromExpansion.java
new file mode 100644
index 0000000..ff8be64
--- /dev/null
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetNamespaceFromExpansion.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.sourcedelement;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNamedInformationItem;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMSourcedElement;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that {@link OMNamedInformationItem#getNamespace()} expands the element if the namespace is
+ * not known in advance.
+ */
+public class TestGetNamespaceFromExpansion extends AxiomTestCase {
+ private final QName qname;
+
+ public TestGetNamespaceFromExpansion(OMMetaFactory metaFactory, QName qname) {
+ super(metaFactory);
+ this.qname = qname;
+ addTestProperty("qname", qname.toString());
+ }
+
+ protected void runTest() throws Throwable {
+ OMFactory factory = metaFactory.getOMFactory();
+ OMElement orgElement = factory.createOMElement(qname);
+ OMSourcedElement element = factory.createOMElement(new TestDataSource(orgElement.toString()));
+ OMNamespace ns = element.getNamespace();
+ if (qname.getNamespaceURI().length() == 0) {
+ assertNull(ns);
+ } else {
+ assertEquals(qname.getNamespaceURI(), ns.getNamespaceURI());
+ assertEquals(qname.getPrefix(), ns.getPrefix());
+ }
+ assertTrue(element.isExpanded());
+ }
+}
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetNamespaceFromQNameAwareOMDataSource.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetNamespaceFromQNameAwareOMDataSource.java
new file mode 100644
index 0000000..270fcb3
--- /dev/null
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetNamespaceFromQNameAwareOMDataSource.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.sourcedelement;
+
+import java.io.StringReader;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNamedInformationItem;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMSourcedElement;
+import org.apache.axiom.om.QNameAwareOMDataSource;
+import org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromReader;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that {@link OMNamedInformationItem#getNamespace()} behaves correctly on a
+ * {@link OMSourcedElement} backed by a {@link QNameAwareOMDataSource}.
+ */
+public class TestGetNamespaceFromQNameAwareOMDataSource extends AxiomTestCase {
+ private final QName qname;
+
+ public TestGetNamespaceFromQNameAwareOMDataSource(OMMetaFactory metaFactory, QName qname) {
+ super(metaFactory);
+ this.qname = qname;
+ addTestProperty("qname", qname.toString());
+ }
+
+ protected void runTest() throws Throwable {
+ OMFactory factory = metaFactory.getOMFactory();
+ OMSourcedElement element = factory.createOMElement(
+ new WrappedTextNodeOMDataSourceFromReader(qname, new StringReader("test")));
+ OMNamespace ns = element.getNamespace();
+ if (qname.getNamespaceURI().length() == 0) {
+ assertNull(ns);
+ } else {
+ assertEquals(qname.getNamespaceURI(), ns.getNamespaceURI());
+ assertEquals(qname.getPrefix(), ns.getPrefix());
+ }
+ assertFalse(element.isExpanded());
+ }
+}