MCPcopy Create free account
hub / github.com/BaseXdb/basex / root

Method root

basex-core/src/main/java/org/basex/data/Namespaces.java:214–250  ·  view source on GitHub ↗

Finds the nearest namespace node on the ancestor axis of the insert location and sets it as new root. Possible candidates for this node are collected and the match with the highest PRE value between ancestors and candidates is determined. @param pre PRE value @param data data reference

(final int pre, final Data data)

Source from the content-addressed store, hash-verified

212 * @param data data reference
213 */
214 void root(final int pre, final Data data) {
215 // collect possible candidates for namespace root
216 final List<NSNode> cand = new LinkedList<>();
217 NSNode nd = root;
218 cand.add(nd);
219 for(int p; (p = nd.find(pre)) > -1;) {
220 // add candidate to stack
221 nd = nd.child(p);
222 cand.add(0, nd);
223 }
224
225 nd = root;
226 if(cand.size() > 1) {
227 // compare candidates to ancestors of PRE value
228 int ancPre = pre;
229 // take first candidate from stack
230 NSNode curr = cand.remove(0);
231 while(ancPre > -1 && nd == root) {
232 // if the current candidate's PRE value is lower than the current ancestor of par or par
233 // itself, we have to look for a potential match for this candidate. therefore we iterate
234 // through ancestors until we find one with a lower than or the same PRE value as the
235 // current candidate.
236 while(ancPre > curr.pre()) ancPre = data.parent(ancPre, data.kind(ancPre));
237 // this is the new root
238 if(ancPre == curr.pre()) nd = curr;
239 // no potential for infinite loop, because dummy root is always a match,
240 // in this case ancPre ends iteration
241 if(!cand.isEmpty()) curr = cand.remove(0);
242 }
243 }
244
245 final int uriId = uriIdForPrefix(Token.EMPTY, pre, data);
246 defaults.set(level, uriId);
247 // remember URI before insert of first node n to connect siblings of n to according namespace
248 defaults.set(level - 1, uriId);
249 current = nd;
250 }
251
252 /**
253 * Caches and returns all namespace nodes in the namespace structure with a minimum PRE value.

Callers 1

loopMethod · 0.45

Calls 11

findMethod · 0.95
childMethod · 0.95
preMethod · 0.95
uriIdForPrefixMethod · 0.95
addMethod · 0.65
sizeMethod · 0.65
removeMethod · 0.65
kindMethod · 0.65
setMethod · 0.65
parentMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected