private void pushStack(final Namespace mns, Namespace[] newscope,
                         final List<Namespace> toadd) {
    // OK, we've checked the namespaces in the Element, and 'toadd' contains
    // all namespaces that are not already in scope.
    depth++;

    if (depth >= scope.length) {
      // we need more space on the stack.
      scope = Arrays.copyOf(scope, scope.length * 2);
      added = Arrays.copyOf(added, scope.length);
    }

    // Sort out the added namespaces.
    if (toadd.isEmpty()) {
      // nothing changed in the scope.
      added[depth] = EMPTY;
    }
    else {
      added[depth] = toadd.toArray(new Namespace[0]);
      if (added[depth][0] == mns) {
        Arrays.sort(added[depth], 1, added[depth].length, NSCOMP);
      }
      else {
        Arrays.sort(added[depth], NSCOMP);
      }
    }

    if (mns != newscope[0]) {
      if (toadd.isEmpty()) {
        // we need to make newscope a copy of the previous level's
        // scope, because it is not yet a copy.
        newscope = newscope.clone();
      }
      // we need to take the Namespace at position 0, and insert it
      // in it's place later in the array.
      // we need to take the mns from later in the array, and move it
      // to the front.
      final Namespace tmp = newscope[0];
      int ip = -binarySearch(newscope, 1, newscope.length, tmp) - 1;
      // we can be sure that (- ip - 1 ) is >= 1
      // we also know that we want to move the data before the ip
      // backwards one spot, so the math is slightly different....
      ip--;
      System.arraycopy(newscope, 1, newscope, 0, ip);
      newscope[ip] = tmp;

      ip = binarySearch(newscope, 0, newscope.length, mns);
      // we can be sure that ip is >= 0
      System.arraycopy(newscope, 0, newscope, 1, ip);
      newscope[0] = mns;
    }

    scope[depth] = newscope;
  }
private void pushStack(final Namespace mns, Namespace[] newscope,
                         final List<Namespace> toadd)