summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Hiesserich2013-06-11 16:57:20 (EDT)
committer Florian Thienel2013-06-11 16:57:20 (EDT)
commitebfdb448dd105a14be9864a0c78a087da2d2c0f3 (patch)
tree6b66ed2cd625211abc26e562cddd1c8bf4bd56c3
parentc66c46fdc8eaea179ef35c801d1d92cfe994166a (diff)
downloadorg.eclipse.mylyn.docs.vex-ebfdb448dd105a14be9864a0c78a087da2d2c0f3.zip
org.eclipse.mylyn.docs.vex-ebfdb448dd105a14be9864a0c78a087da2d2c0f3.tar.gz
org.eclipse.mylyn.docs.vex-ebfdb448dd105a14be9864a0c78a087da2d2c0f3.tar.bz2
add declarations for undeclared namespaces on insertFragment;
copy only declared namespaces https://bugs.eclipse.org/bugs/show_bug.cgi?id=409647
-rw-r--r--org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DeepCopyTest.java20
-rw-r--r--org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java35
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/CopyOfElement.java2
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java52
4 files changed, 106 insertions, 3 deletions
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DeepCopyTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DeepCopyTest.java
index 26751c6..4f5fe1b 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DeepCopyTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DeepCopyTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012 Florian Thienel and others.
+ * Copyright (c) 2012, 2013 Florian Thienel and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* Florian Thienel - initial API and implementation
+ * Carsten Hiesserich - additional test for namespace inheritance
*******************************************************************************/
package org.eclipse.vex.core.internal.dom;
@@ -14,6 +15,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
@@ -234,6 +236,22 @@ public class DeepCopyTest {
assertFalse(copiedChildren.hasNext());
}
+ @Test
+ public void givenAnElement_shouldCopyOnlyDeclaredNamespaces() throws Exception {
+ final Element parent = new Element("parent");
+ parent.declareDefaultNamespace("http://namespace/default");
+ parent.declareNamespace("ns1", "http://namespace/ns1");
+ final Element child = new Element("child");
+ child.declareNamespace("ns2", "http://namespace/ns2");
+ parent.addChild(child);
+
+ final DeepCopy deepCopy = new DeepCopy(child);
+ final Element copiedChild = (Element) deepCopy.getNodes().get(0);
+ assertNull(copiedChild.getDeclaredDefaultNamespaceURI());
+ assertEquals(1, copiedChild.getDeclaredNamespacePrefixes().size());
+ assertEquals("ns2", copiedChild.getDeclaredNamespacePrefixes().iterator().next());
+ }
+
private static void assertNodeIsAssociatedElementWithText(final String expectedText, final INode actualNode) {
assertTrue(actualNode.isAssociated());
assertTrue(actualNode instanceof Element);
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java
index b85050c..1b85387 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012 Florian Thienel and others.
+ * Copyright (c) 2012, 2013 Florian Thienel and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* Florian Thienel - initial API and implementation
+ * Carsten Hiesserich - automatically declare undeclared namespaces when copiing (bug 409647)
*******************************************************************************/
package org.eclipse.vex.core.internal.dom;
@@ -120,6 +121,38 @@ public class DocumentTest {
assertNodeEquals(expectedFragment, actualFragment, 0);
}
+ @Test
+ public void insertFragment_shouldDeclareUndeclaredNamespaces() throws Exception {
+ final IDocument document = new Document(new QualifiedName(null, "root"));
+ document.getRootElement().declareNamespace("ns1", "http://ns1");
+ final IElement sourceParent = document.insertElement(2, new QualifiedName("http://ns1", "parent"));
+ sourceParent.declareNamespace("ns2", "http://ns2");
+ final IElement child = document.insertElement(sourceParent.getEndOffset(), new QualifiedName("http://ns2", "child"));
+
+ final IElement targetParent = document.insertElement(document.getRootElement().getEndOffset(), new QualifiedName("http://ns1", "parent"));
+ final IDocumentFragment fragment = document.getFragment(child.getRange());
+ document.insertFragment(targetParent.getEndOffset(), fragment);
+
+ assertEquals(1, targetParent.getDeclaredNamespacePrefixes().size());
+ assertEquals("http://ns2", targetParent.getNamespaceURI(targetParent.getDeclaredNamespacePrefixes().iterator().next()));
+ }
+
+ @Test
+ public void insertFragment_shouldNotOverrideDeclaredNamespacePrefixes() throws Exception {
+ final IDocument document = new Document(new QualifiedName(null, "root"));
+ document.getRootElement().declareNamespace("ns1", "http://ns1");
+ final IElement sourceParent = document.insertElement(2, new QualifiedName("http://ns1", "parent"));
+ sourceParent.declareNamespace("ns2", "http://ns2");
+ final IElement child = document.insertElement(sourceParent.getEndOffset(), new QualifiedName("http://ns2", "child"));
+
+ final IElement targetParent = document.insertElement(document.getRootElement().getEndOffset(), new QualifiedName("http://ns1", "parent"));
+ final IDocumentFragment fragment = document.getFragment(child.getRange());
+ document.insertFragment(targetParent.getEndOffset(), fragment);
+
+ assertEquals("http://ns1", targetParent.getNamespaceURI("ns1"));
+ assertEquals("http://ns2", targetParent.getNamespaceURI("ns2"));
+ }
+
private static void assertNodesEqual(final Iterable<? extends INode> expected, final Iterable<? extends INode> actual, final int rangeOffsetExpected) {
final Iterator<? extends INode> expectedIterator = expected.iterator();
final Iterator<? extends INode> actualIterator = actual.iterator();
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/CopyOfElement.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/CopyOfElement.java
index d851bf9..d3a47fd 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/CopyOfElement.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/CopyOfElement.java
@@ -43,7 +43,7 @@ public class CopyOfElement extends BaseNodeVisitor {
element.declareDefaultNamespace(source.getDeclaredDefaultNamespaceURI());
- for (final String prefix : source.getNamespacePrefixes()) {
+ for (final String prefix : source.getDeclaredNamespacePrefixes()) {
element.declareNamespace(prefix, source.getNamespaceURI(prefix));
}
}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java
index 1c3620a..27f655c 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java
@@ -15,7 +15,11 @@ package org.eclipse.vex.core.internal.dom;
import java.text.MessageFormat;
import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.QualifiedName;
@@ -44,6 +48,8 @@ import org.eclipse.vex.core.provisional.dom.IValidator;
*/
public class Document extends Parent implements IDocument {
+ private static final String DEFAULT_NAMESPACE_PREFIX = "ns";
+
private final Element rootElement;
private final ListenerList<IDocumentListener, DocumentEvent> listeners = new ListenerList<IDocumentListener, DocumentEvent>(IDocumentListener.class);
@@ -357,6 +363,8 @@ public class Document extends Parent implements IDocument {
getContent().insertContent(offset, fragment.getContent());
+ final Set<String> undeclaredNamespaces = new HashSet<String>();
+
final DeepCopy deepCopy = new DeepCopy(fragment);
final List<Node> newNodes = deepCopy.getNodes();
int nextOffset = offset;
@@ -364,8 +372,12 @@ public class Document extends Parent implements IDocument {
parent.insertChildAt(nextOffset, newNode);
associateDeeply(newNode, offset);
nextOffset = newNode.getEndOffset() + 1;
+
+ undeclaredNamespaces.addAll(newNode.accept(new FindUndeclaredNamespacesVisitor()));
}
+ declareNamespaces(undeclaredNamespaces, parent);
+
fireContentInserted(new ContentChangeEvent(this, parent, new ContentRange(offset, offset + fragment.getContent().length() - 1)));
}
@@ -379,6 +391,14 @@ public class Document extends Parent implements IDocument {
node.associate(getContent(), node.getRange().moveBy(offset));
}
+ private void declareNamespaces(final Collection<String> namespaceURIs, final IElement element) {
+ final NamespacePrefixGenerator namespacePrefixGenerator = new NamespacePrefixGenerator(element, DEFAULT_NAMESPACE_PREFIX);
+ for (final String namespaceURI : namespaceURIs) {
+ final String namespacePrefix = namespacePrefixGenerator.next();
+ element.declareNamespace(namespacePrefix, namespaceURI);
+ }
+ }
+
public void delete(final ContentRange range) throws DocumentValidationException {
final IParent surroundingParent = getParentAt(range.getStartOffset());
final IParent parentAtEndOffset = getParentAt(range.getEndOffset());
@@ -583,4 +603,36 @@ public class Document extends Parent implements IDocument {
listeners.fireEvent("contentInserted", e);
}
+ /*
+ * Internal Helper Classes
+ */
+
+ private static class NamespacePrefixGenerator implements Iterator<String> {
+ final IElement element;
+ final String prefix;
+ int namespaceIndex = 1;
+
+ public NamespacePrefixGenerator(final IElement element, final String prefix) {
+ this.element = element;
+ this.prefix = prefix;
+ }
+
+ public boolean hasNext() {
+ return true;
+ }
+
+ public String next() {
+ final String result = prefix + namespaceIndex++;
+ if (!element.getNamespacePrefixes().contains(result)) {
+ return result;
+ }
+ return next();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException("remove not supported");
+ }
+
+ }
+
}