1 package org.sapia.archie; 2 3 import org.sapia.archie.impl.*; 4 import org.sapia.archie.strategy.DefaultLookupNodeStrategy; 5 import org.sapia.archie.strategy.DefaultLookupStrategy; 6 7 8 20 public class Archie { 21 private Node _root; 22 23 public Archie() throws ProcessingException{ 24 _root = new DefaultNode(); 25 } 26 27 public Archie(Node root) { 28 _root = root; 29 } 30 31 40 public Node getRoot() { 41 return _root; 42 } 43 44 52 public Object lookup(Name n) throws NotFoundException, ProcessingException { 53 n = (Name)n.clone(); 54 DefaultLookupStrategy strat = new DefaultLookupStrategy(); 55 return strat.lookup(n, _root); 56 } 57 58 71 public NameParser getNameParser(){ 72 return _root.getNameParser(); 73 } 74 75 83 84 public Node lookupNode(Name n, boolean create) 85 throws NotFoundException, ProcessingException { 86 n = (Name)n.clone(); 87 DefaultLookupNodeStrategy strat = new DefaultLookupNodeStrategy(create); 88 89 return (Node) strat.lookup(n, _root); 90 } 91 92 97 public void unbind(Name n) throws ProcessingException{ 98 n = (Name)n.clone(); 99 if(n.count() == 0){ 100 return; 101 } 102 NamePart np = n.chopLast(); 103 try{ 104 lookupNode(n, false).removeValue(np); 105 }catch(NotFoundException e){ 106 } 108 } 109 110 118 public void bind(Name n, Object o) 119 throws DuplicateException, ProcessingException { 120 doBind(n, o, false); 121 } 122 123 132 public void rebind(Name n, Object o) 133 throws ProcessingException { 134 doBind(n, o, true); 135 } 136 137 138 139 private void doBind(Name n, Object o, boolean overwrite) throws ProcessingException{ 140 n = (Name)n.clone(); 141 NamePart last = n.last(); 142 143 if ((n.count() == 1) && (last.asString().length() == 0)) { 144 throw new ProcessingException("Cannot bind object with empty name"); 145 } 146 147 last = n.chopLast(); 148 149 DefaultLookupNodeStrategy strat = new DefaultLookupNodeStrategy(true); 150 Node node = null; 151 152 try { 153 node = (Node) strat.lookup(n, _root); 154 } catch (NotFoundException e) { 155 throw new ProcessingException(e.getMessage()); 156 } 157 158 if (!node.putValue(last, o, overwrite)) { 159 throw new DuplicateException(last.asString()); 160 } 161 } 162 } 163 | Popular Tags |