Author: fanningpj
Date: Wed Jul 16 19:22:24 2025
New Revision: 1927272

URL: http://svn.apache.org/viewvc?rev=1927272&view=rev
Log:
[XMLBeans-438] Remove elements between

Modified:
    xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Xobj.java
    xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/TypeStore.java
    
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlComplexContentImpl.java

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Xobj.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Xobj.java?rev=1927272&r1=1927271&r2=1927272&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Xobj.java 
(original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Xobj.java Wed 
Jul 16 19:22:24 2025
@@ -2304,6 +2304,39 @@ abstract class Xobj implements TypeStore
     }
 
     @Override
+    public void remove_elements_between(final QName name, final int m, final 
int n) {
+        if (m < 0 || n < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+
+        if (!isContainer()) {
+            throw new IllegalStateException();
+        }
+
+        if (m >= n) {
+            return;
+        }
+
+        ArrayList<Xobj> toRemove = new ArrayList<>();
+        Xobj x;
+        int i = m;
+        int count = 0;
+        for (x = _firstChild; x != null; x = x._nextSibling) {
+            if (x.isElem() && x._name.equals(name) && --i < 0) {
+                toRemove.add(x);
+                count++;
+                if (count >= n - m) {
+                    break; // no need to continue if we've removed enough
+                }
+            }
+        }
+        final int size = toRemove.size();
+        for (int j = size - 1; j >= 0; j--) {
+            removeElement(toRemove.get(j));
+        }
+    }
+
+    @Override
     public void remove_element(QNameSet names, int i) {
         if (i < 0) {
             throw new IndexOutOfBoundsException();
@@ -2343,6 +2376,39 @@ abstract class Xobj implements TypeStore
         }
     }
 
+    @Override
+    public void remove_elements_between(final QNameSet names, final int m, 
final int n) {
+        if (m < 0 || n < 0) {
+            throw new IndexOutOfBoundsException();
+        }
+
+        if (!isContainer()) {
+            throw new IllegalStateException();
+        }
+
+        if (m >= n) {
+            return;
+        }
+
+        ArrayList<Xobj> toRemove = new ArrayList<>();
+        Xobj x;
+        int i = m;
+        int count = 0;
+        for (x = _firstChild; x != null; x = x._nextSibling) {
+            if (x.isElem() && names.contains(x._name) && --i < 0) {
+                toRemove.add(x);
+                count++;
+                if (count >= n - m) {
+                    break; // no need to continue if we've removed enough
+                }
+            }
+        }
+        final int size = toRemove.size();
+        for (int j = size - 1; j >= 0; j--) {
+            removeElement(toRemove.get(j));
+        }
+    }
+
     public TypeStoreUser find_attribute_user(QName name) {
         Xobj a = getAttr(name);
 

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/TypeStore.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/TypeStore.java?rev=1927272&r1=1927271&r2=1927272&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/TypeStore.java 
(original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/TypeStore.java 
Wed Jul 16 19:22:24 2025
@@ -276,16 +276,30 @@ public interface TypeStore extends Names
     void remove_element(QNameSet names, int i);
 
     /**
-     * Removes all elements after the i-th element with the given name.
+     * Removes all elements from the i-th element with the given name.
      * @since 5.4.0
      */
     void remove_elements_after(QName name, int i);
 
     /**
-     * Removes all elements after the i-th element with the given name.
+     * Removes all elements from the i-th element with the given names.
      * @since 5.4.0
      */
-    void remove_elements_after(QNameSet name, int i);
+    void remove_elements_after(QNameSet names, int i);
+
+    /**
+     * Removes all elements from the m-th element with the given name up to 
the n-th
+     * element with the given name but not including the n-th element.
+     * @since 5.4.0
+     */
+    void remove_elements_between(QName name, int m, int n);
+
+    /**
+     * Removes all elements from the m-th element with the given name up to 
the n-th
+     * element with the given name but not including the n-th element.
+     * @since 5.4.0
+     */
+    void remove_elements_between(QNameSet names, int m, int n);
 
     /**
      * Returns the TypeStoreUser underneath the attribute with the given

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlComplexContentImpl.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlComplexContentImpl.java?rev=1927272&r1=1927271&r2=1927272&view=diff
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlComplexContentImpl.java
 (original)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlComplexContentImpl.java
 Wed Jul 16 19:22:24 2025
@@ -396,13 +396,15 @@ public class XmlComplexContentImpl exten
         // ... then come back and insert the elements starting with startSource
         // up to i from the sources array into the current array, starting with
         // startDest
-        int n = i;
-        for (; m > n - startSrc + startDest; m--) {
+        final int n = i;
+        final int startPos = n - startSrc + startDest;
+        if (m > startPos) {
             if (set == null) {
-                store.remove_element(elemName, m - 1);
+                store.remove_elements_between(elemName, startPos, m);
             } else {
-                store.remove_element(set, m - 1);
+                store.remove_elements_between(set, startPos, m);
             }
+            m = startPos;
         }
 
         int j;
@@ -531,14 +533,16 @@ public class XmlComplexContentImpl exten
         // up to i from the sources array into the current array, starting with
         // startDest
         final int n = i;
-
-        if (set == null) {
-            store.remove_elements_after(elemName, n - startSrc + startDest);
-        } else {
-            store.remove_elements_after(set, n - startSrc + startDest);
+        final int startPos = n - startSrc + startDest;
+        if (m > startPos) {
+            if (set == null) {
+                store.remove_elements_between(elemName, startPos, m);
+            } else {
+                store.remove_elements_between(set, startPos, m);
+            }
+            m = startPos;
         }
 
-
         final int size = Math.min(m - startDest, sources.length - startSrc);
         ArrayList<XmlObjectBase> users = new ArrayList<>(size);
         if (set == null) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org
For additional commands, e-mail: commits-h...@poi.apache.org

Reply via email to