KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.archie;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.sapia.archie.impl.*;
7
8
9 /**
10  * Abstract implementation of the <code>Node</code> interface.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <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>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public abstract class AbstractNode implements Node {
20   protected Map JavaDoc _children;
21   protected NodeFactory _fac;
22   private NamePart _name;
23   private Name _absolutePath = new Name();
24   private Node _parent;
25   private NameParser _parser;
26
27   protected AbstractNode(NameParser parser, Map JavaDoc children, NodeFactory fac)
28                   throws ProcessingException {
29     _children = children;
30     _fac = fac;
31     _parser = parser;
32     
33     // TODO: this is a hack... Maybe name should be passed in ctor
34
_name = parser.parse("/").first();
35     _absolutePath.add(_name);
36   }
37
38   protected AbstractNode(Map JavaDoc children, NodeFactory fac)
39                   throws ProcessingException {
40     this(new DefaultNameParser(), children, fac);
41   }
42
43   /**
44    * @see org.sapia.archie.Node#createChild(NamePart)
45    */

46   public Node createChild(NamePart name)
47                    throws DuplicateException, ProcessingException {
48     if (_children.containsKey(name)) {
49       throw new DuplicateException(name.asString());
50     }
51
52     Node n = _fac.newNode();
53     n.setUp(this, name);
54     _children.put(name, n);
55     return n;
56   }
57   
58   /**
59    * @see org.sapia.archie.Node#getChild(NamePart)
60    */

61   public Node getChild(NamePart name) {
62     return (Node)_children.get(name);
63   }
64   
65   /**
66    * @see org.sapia.archie.Node#removeChild(org.sapia.archie.NamePart)
67    */

68   public Node removeChild(NamePart name) {
69     return (Node)_children.remove(name);
70   }
71
72   /**
73    * @see org.sapia.archie.Node#getChildren()
74    */

75   public Iterator JavaDoc getChildren() {
76     return _children.values().iterator();
77   }
78
79   /**
80    * @see org.sapia.archie.Node#getName()
81    */

82   public NamePart getName() {
83     return _name;
84   }
85
86   /**
87    * @see Node#getAbsolutePath()
88    */

89   public Name getAbsolutePath() {
90     return (Name)_absolutePath.clone();
91   }
92
93   /**
94    * @see Node#getChildrenCount()
95    */

96   public int getChildrenCount() {
97     return _children.size();
98   }
99
100   /**
101    * @see Node#getChildrenNames()
102    */

103   public Iterator JavaDoc getChildrenNames() {
104     return _children.keySet().iterator();
105   }
106
107   /**
108    * @see Node#getParent()
109    */

110   public Node getParent() {
111     return _parent;
112   }
113
114   /**
115    * @see Node#getNameParser()
116    */

117   public NameParser getNameParser() {
118     return _parser;
119   }
120   
121   /**
122    * @see Node#setUp(Node, NamePart)
123    */

124   public void setUp(Node parent, NamePart name) {
125     _parent = parent;
126     _absolutePath = new Name().add(parent.getAbsolutePath());
127     _name = name;
128     _absolutePath.add(name);
129   }
130
131   public String JavaDoc toString() {
132     return _name.toString();
133   }
134 }
135
Popular Tags