KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > strategy > DefaultLookupNodeStrategy


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 /**
11  * Looks up a <code>Node</code> corresponding to a given name.
12  *
13  * @author Yanick Duchesne
14  *
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class DefaultLookupNodeStrategy implements LookupStrategy {
22   private boolean _create;
23
24   /**
25    * @param createNotExistingNodes if <code>true</code>, parts of looked up names
26    * that have no corresponding node will have a node created for them.
27    */

28   public DefaultLookupNodeStrategy(boolean createNotExistingNodes) {
29     _create = createNotExistingNodes;
30   }
31
32   public Object JavaDoc 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