View Javadoc

1   /*
2    * Copyright (c) 2001-2005,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         // implementation
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 }