You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2021/11/11 09:13:34 UTC

[jackrabbit-filevault] branch bugfix/JCRVLT-549 created (now 83d3d66)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch bugfix/JCRVLT-549
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git.


      at 83d3d66  JCRVLT-549 consider node name constraints

This branch includes the following new commits:

     new 83d3d66  JCRVLT-549 consider node name constraints

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[jackrabbit-filevault] 01/01: JCRVLT-549 consider node name constraints

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/JCRVLT-549
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git

commit 83d3d6612fa8a5db85811a98ae7de4c603085c1a
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Nov 11 10:13:22 2021 +0100

    JCRVLT-549 consider node name constraints
    
    simplify type checking
---
 .../vault/fs/impl/io/DocViewSAXImporter.java       | 54 +++++++++++++---------
 .../{MandatoryNode.java => MandatoryNodeIT.java}   |  2 +-
 2 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/impl/io/DocViewSAXImporter.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/impl/io/DocViewSAXImporter.java
index d3f1807..0d9bfaa 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/impl/io/DocViewSAXImporter.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/impl/io/DocViewSAXImporter.java
@@ -1266,12 +1266,25 @@ public class DocViewSAXImporter extends RejectingEntityDefaultHandler implements
                         } else {
                             if (wspFilter.getImportMode(path) == ImportMode.REPLACE) {
 
+                                boolean shouldRemoveChild = true;
                                 // check if child is not protected
                                 if (child.getDefinition().isProtected()) {
+                                    shouldRemoveChild = false;
                                     log.debug("Refuse to delete protected child node: {}", path);
-                                } else if (child.getDefinition().isMandatory() && !hasSiblingWithSameType(child)) {
-                                    log.debug("Refuse to delete mandatory child node: {}", path);
-                                } else {
+                                } else if (child.getDefinition().isMandatory()) {
+                                    // get relevant child node definition from parent's effective node type
+                                    EffectiveNodeType ent = EffectiveNodeType.ofNode(child.getParent());
+                                    Optional<NodeDefinition> childNodeDefinition = ent.getApplicableChildNodeDefinition(child.getName(), child.getPrimaryNodeType());
+                                    if (!childNodeDefinition.isPresent()) {
+                                        // this should never happen as then child.getDefinition().isMandatory() would have returned false in the first place...
+                                        throw new IllegalStateException("Could not find applicable child node definition for mandatory child node " + child.getPath());
+                                    } else {
+                                        if (!hasSiblingWithPrimaryTypesAndName(child, childNodeDefinition.get().getRequiredPrimaryTypes(), childNodeDefinition.get().getName())) {
+                                            log.debug("Refuse to delete mandatory child node: {}", path);
+                                        }
+                                    }
+                                } 
+                                if (shouldRemoveChild) {
                                     importInfo.onDeleted(path);
                                     child.remove();
                                 }
@@ -1298,29 +1311,24 @@ public class DocViewSAXImporter extends RejectingEntityDefaultHandler implements
         }
     }
 
-    private boolean hasSiblingWithSameType(Node child) throws RepositoryException {
-
-        Node parent = child.getParent();
-
-        try {
-            EffectiveNodeType ent = EffectiveNodeType.ofNode(parent);
-            String typeName = ent.getApplicableChildNodeDefinition(child.getName(), child.getPrimaryNodeType()).get().getName();
-
-            NodeIterator iter = parent.getNodes();
-            while (iter.hasNext()) {
-                Node sibling = iter.nextNode();
-                if (!sibling.isSame(child)) {
-                    Optional<NodeDefinition> childDef = ent.getApplicableChildNodeDefinition(sibling.getName(),
-                            sibling.getPrimaryNodeType());
-                    try {
-                        if (typeName.equals(childDef.get().getName())) {
-                            return true;
-                        }
-                    } catch (NoSuchElementException ignored) {
+    private boolean hasSiblingWithPrimaryTypesAndName(Node child, NodeType[] requiredPrimaryNodeTypes, String requiredName) throws RepositoryException {
+        NodeIterator iter = child.getParent().getNodes();
+        while (iter.hasNext()) {
+            Node sibling = iter.nextNode();
+            if (!sibling.isSame(child)) {
+                NodeType stype = sibling.getPrimaryNodeType();
+                boolean allmatch = true;
+                // check type: due to inheritance multiple primary node types need to be checked
+                for (NodeType requiredPrimaryNodeType : requiredPrimaryNodeTypes) {
+                    allmatch &= stype.isNodeType(requiredPrimaryNodeType.getName());
+                }
+                // check name
+                if (requiredName.equals("*") || requiredName.equals(child.getName())) {
+                    if (allmatch) {
+                        return true;
                     }
                 }
             }
-        } catch (NoSuchElementException ignored) {
         }
         return false;
     }
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/MandatoryNode.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/MandatoryNodeIT.java
similarity index 98%
rename from vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/MandatoryNode.java
rename to vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/MandatoryNodeIT.java
index 510c5dc..d8b832a 100644
--- a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/MandatoryNode.java
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/MandatoryNodeIT.java
@@ -24,7 +24,7 @@ import org.apache.jackrabbit.vault.packaging.PackageException;
 import org.junit.Test;
 
 
-public class MandatoryNode extends IntegrationTestBase {
+public class MandatoryNodeIT extends IntegrationTestBase {
  
     @Test
     public void testMultipleMandatoryChildNodesReplace()