Skip to content

Commit

Permalink
Merge remote-tracking branch 'fork/62'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 11, 2015
2 parents d62219e + 7f8b1c9 commit 30d88b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
26 changes: 25 additions & 1 deletion src/main/java/com/jcabi/xml/XMLDocument.java
Expand Up @@ -293,7 +293,14 @@ public String toString() {
@Override
@NotNull(message = "node is never NULL")
public Node node() {
return Node.class.cast(this.cache).cloneNode(true);
final Node castCache = Node.class.cast(this.cache);
final Node answer;
if (castCache instanceof Document) {
answer = castCache.cloneNode(true);
} else {
answer = this.createImportedNode(castCache);
}
return answer;
}

@Override
Expand Down Expand Up @@ -381,6 +388,23 @@ public XML merge(@NotNull(message = "context can't be NULL")
return new XMLDocument(this.node(), this.context.merge(ctx), this.leaf);
}

/**
* Clones a node and imports it in a new document.
* @param node A node to clone.
* @return A cloned node imported in a dedicated document.
*/
private Node createImportedNode(final Node node) {
final Document document;
try {
document = DFACTORY.newDocumentBuilder().newDocument();
} catch (final ParserConfigurationException ex) {
throw new IllegalStateException(ex);
}
final Node imported = document.importNode(node, true);
document.appendChild(imported);
return imported;
}

/**
* Retrieve XPath query result. Supports returning {@link NodeList} and
* {@link String} types.
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/com/jcabi/xml/XMLDocumentTest.java
Expand Up @@ -40,7 +40,6 @@
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -392,14 +391,9 @@ public void preservesImmutability() throws Exception {

/**
* XMLDocument can apply XPath to cloned node.
*
* This test doesn't work. If I replace "//z9/@a" with "z9/@a" it works
* perfectly. I'm not sure what is that, but would be great to fix it.
*
* @throws Exception If something goes wrong inside
*/
@Test
@Ignore
public void appliesXpathToClonedNode() throws Exception {
final XML xml = new XMLDocument("<t6><z9 a='433'/></t6>");
final XML root = xml.nodes("/t6").get(0);
Expand Down

0 comments on commit 30d88b7

Please sign in to comment.