1 package org.sapia.archie.strategy; 2 3 import org.sapia.archie.Name; 4 import org.sapia.archie.NamePart; 5 import org.sapia.archie.Node; 6 import org.sapia.archie.NotFoundException; 7 import org.sapia.archie.ProcessingException; 8 9 10 21 public class DefaultLookupNodeStrategy implements LookupStrategy { 22 private boolean _create; 23 24 28 public DefaultLookupNodeStrategy(boolean createNotExistingNodes) { 29 _create = createNotExistingNodes; 30 } 31 32 public Object lookup(Name n, Node from) 33 throws NotFoundException, ProcessingException { 34 NamePart currentPart; 35 36 if(n.count() == 0){ 37 return from; 38 } 39 else if (n.first().asString().length() == 0) { 40 n.chopFirst(); 41 from = (Node) new FindRootStrategy().lookup(n, from); 42 } 43 44 Node currentNode = from; 45 46 while (n.hasNextPart()) { 47 currentPart = n.nextPart(); 48 49 if (currentNode.getChild(currentPart) == null) { 50 if (!_create) { 51 throw new NotFoundException("Resolved: " + 52 n.getTo(n.getCurrentIndex()) + 53 "; remaining: " + 54 n.getFrom(n.getCurrentIndex())); 55 } else { 56 currentNode = currentNode.createChild(currentPart); 57 } 58 } else { 59 currentNode = currentNode.getChild(currentPart); 60 } 61 } 62 63 return currentNode; 64 } 65 } 66 | Popular Tags |