KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > Archie


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 /**
9  * An instance of this class wraps a <code>Node</code> and offers a
10  * user-friendly API on top of the latter.
11  *
12  * @author Yanick Duchesne
13  *
14  * <dl>
15  * <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>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

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   /**
32    * The root node that this instance holds can be acquired to be manipulated
33    * in an application-defined way - by applying a custom lookup algorithm, for
34    * example.
35    *
36    * @see org.sapia.archie.strategy.LookupStrategy
37    *
38    * @return the root <code>Node</code> of this instance.
39    */

40   public Node getRoot() {
41     return _root;
42   }
43
44   /**
45    * Looks up the object with the given name.
46    *
47    * @param n a <code>Name</code>
48    * @return the <code>Object</code> that corresponds to the given name.
49    * @throws NotFoundException if no object could be found for the given name.
50    * @throws ProcessingException if a problem occurs while performing the lookup.
51    */

52   public Object JavaDoc 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   /**
59    * This method can be used by client applications to acquire the
60    * <code>NameParser</code> that this instance holds, in order to create
61    * object representation of string-based names.
62    * <p>
63    * Example:
64    * <pre>
65    * Name aName = archie.getNameParser().parse("some/object/name");
66    * Object anObject = archie.lookup(aName);
67    * </pre>
68    *
69    * @return the <code>NameParser</code> that this instance uses.
70    */

71   public NameParser getNameParser(){
72     return _root.getNameParser();
73   }
74
75   /**
76    * Looks up the <code>Node</code> with the given name.
77    *
78    * @param n a <code>Name</code>
79    * @return the <code>Node</code> that corresponds to the given name.
80    * @throws NotFoundException if no node could be found for the given name.
81    * @throws ProcessingException if a problem occurs while performing the lookup.
82    */

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   /**
93    * Unbinds the value under the given name.
94    *
95    * @param n a <code>Name</code>.
96    */

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       // noop
107
}
108   }
109
110   /**
111    * Binds the given object under the given name.
112    *
113    * @param n a <code>Name</code>
114    * @param o the <code>Object</code> to bind.
115    * @throws DuplicateException if a object already exists for the given name.
116    * @throws ProcessingException if a problem occurs while performing the binding.
117    */

118   public void bind(Name n, Object JavaDoc o)
119             throws DuplicateException, ProcessingException {
120     doBind(n, o, false);
121   }
122   
123   /**
124    * Binds the given object under the given name; if an object already
125    * exists under the given name, it is overwritten.
126    *
127    * @param n a <code>Name</code>
128    * @param o the <code>Object</code> to bind.
129    * @throws DuplicateException if a object already exists for the given name.
130    * @throws ProcessingException if a problem occurs while performing the binding.
131    */

132   public void rebind(Name n, Object JavaDoc o)
133             throws ProcessingException {
134     doBind(n, o, true);
135   }
136   
137   
138   
139   private void doBind(Name n, Object JavaDoc 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