Namespace[] (List<? super Namespace> store, Namespace namespace, Namespace[] scope) { // Scope is always sorted as the primary namespace first, then the // rest are in prefix order. // We can guarantee that the prefixes are all unique too. // There is always going to be at least two namespaces in scope with // the prefixes : "" and "xml" // As a result we can use the 0th index with impunity. if (namespace == scope[0]) { // we are already in scope. return scope; } if (namespace.getPrefix().equals(scope[0].getPrefix())) { // the prefix is the previous scope's primary prefix. This means // that we know for sure that the input namespace is new-to-scope. store.add(namespace); final Namespace[] nscope = scope.clone(); nscope[0] = namespace; return nscope; } // will return +ve number if the prefix matches too. int ip = binarySearch(scope, 1, scope.length, namespace); if (ip >= 0 && namespace == scope[ip]) { // the namespace is already in scope. return scope; } store.add(namespace); if (ip >= 0) { // a different namespace with the same prefix as us is in-scope. // replace it.... final Namespace[] nscope = scope.clone(); nscope[ip] = namespace; return nscope; } // We are a new prefix in-scope. final Namespace[] nscope = Arrays.copyOf(scope, scope.length + 1); ip = -ip - 1; System.arraycopy(nscope, ip, nscope, ip + 1, nscope.length - ip - 1); nscope[ip] = namespace; return nscope; }