1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.tree;
33
34 import java.io.File;
35 import java.io.Serializable;
36
37 import com.unitesk.atp.dynattrs.Accessor;
38 import com.unitesk.atp.dynattrs.BeanMapAttributed;
39
40 /***
41 * Attributed Tree.
42 *
43 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
44 * @version $Id: TreeClass.java,v 1.14 2005/10/08 05:28:39 all-x Exp $
45 */
46 public class TreeClass implements Tree, Serializable
47 {
48 protected File file;
49
50 /***
51 * Returns source file for this tree or
52 * null if tree was build not from file.
53 */
54 public File getFile() { return file; }
55 public void setFile( File file ) { this.file = file; }
56
57 protected Node root;
58
59 /***
60 * Returns root of this tree.
61 * @return root node or null if it was not set
62 */
63 public Node getRootNode() { return root; }
64
65 /***
66 * Sets root node of this tree.
67 *
68 * @param root Root node
69 * @throws NullPointerException if root is null
70 */
71 public void setRootNode( Node root )
72 {
73 if( root == null ) throw new NullPointerException( "rootNode" );
74 this.root = root;
75 }
76
77 /***
78 * The default implementation of {@link Node} interface.
79 * Uses {@link BeanMapAttributed} as implementation of
80 * {@link com.unitesk.atp.dynattrs.Attributed}
81 * interface.
82 */
83 public abstract static class NodeClass
84 extends BeanMapAttributed implements Node
85 {
86 public Node getParent()
87 {
88 return parent;
89 }
90
91 public void setParent( Node parent )
92 {
93 if( parent == null )
94 {
95 throw new NullPointerException();
96 }
97
98 if( this.parent != null )
99 {
100 throw new IllegalArgumentException( this.parent.toString() );
101 }
102
103 this.parent = parent;
104 }
105
106 public void resetParent()
107 {
108 this.parent = null;
109 }
110
111 /***
112 * Provides access to 'this' through
113 * {@link com.unitesk.atp.dynattrs.Attributed} interface.
114 */
115 public Node getThis()
116 {
117 return this;
118 }
119
120 public final void makeChild( String name )
121 {
122 Object value = getAttribute( name );
123
124 if( value == null ) return;
125
126 if( Accessor.isIndexed( value ) )
127 {
128 for( int i = 0; i < Accessor.sizeIndexed( value ); i++ )
129 {
130 Node elem = (Node)Accessor.getIndexed( value, i );
131
132 if( elem != null )
133 {
134 elem.setParent( this );
135 }
136 }
137 } else {
138 ((Node)value).setParent( this );
139 }
140 }
141
142
143
144
145 private Node parent = null;
146 }
147
148 /***
149 * Abstract base class for translation of TreeDL enumeration types.
150 */
151 public abstract static class EnumClass implements Enum, Serializable
152 {
153 public final Class keyClass()
154 {
155 return getClass();
156 }
157
158 public final int key()
159 {
160 return key;
161 }
162
163 public final boolean check( Enum value )
164 {
165 if( keyClass() != value.keyClass() ) return false;
166 int mask = value.key();
167 return (key() & mask) == mask;
168 }
169
170 protected EnumClass( int key )
171 {
172 this.key = key;
173 }
174
175 private int key;
176 }
177 }