KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > regexp > TreeNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n.regexp;
22
23 /**
24  * Node of a regular expression syntax tree.
25  * This node and its subnodes represent a subexpression of a regular expression.
26  *
27  * @author Marian Petras
28  */

29 class TreeNode {
30
31     static final int REGEXP = 0;
32     static final int MULTI_REGEXP = 1;
33     static final int SIMPLE_REGEXP = 2;
34     static final int Q_REGEXP = 3;
35     static final int QUANTIFIER = 4;
36     static final int NUMBER = 5;
37     static final int METACHAR = 6;
38     static final int UNICODE_CHAR = 7;
39     static final int CHAR = 8;
40     static final int SUBEXPR = 9;
41     static final int POSIX_SET = 10;
42     static final int SET = 11;
43     static final int RANGE = 12;
44     static final int TOKEN = 13;
45
46     /**
47      * index of the first character of a subexpression represented by this node
48      */

49     int start;
50     /**
51      * index of the last character of a subexpression represented by this node
52      */

53     int end;
54     /** type of a subexpression this node represents */
55     private int tokenType;
56     /** attributes of this node */
57     private Object JavaDoc attribs;
58     /** this node's parent node */
59     private TreeNode parent;
60     /** direct subnodes of this node */
61     private java.util.List JavaDoc children;
62
63     /**
64      * Creates a new node representing a given part of a regular expression.
65      *
66      * @param tokenType type of a subexpression this node represents
67      * @param start index of the first character of a subexpression
68      * represented by this node
69      * @param end index of the last character of a subexpression
70      * represented by this node
71      */

72     TreeNode(int tokenType, int start, int end) {
73         this.tokenType = tokenType;
74         this.start = start;
75         this.end = end;
76     }
77
78     /**
79      * Creates a new node representing a given part of a regular expression.
80      *
81      * @param tokenType type of a subexpression this node represents
82      * @param start index of the first character of a subexpression
83      * represented by this node
84      * @param end index of the last character of a subexpression
85      * represented by this node
86      * @param attribs attributes of this node
87      */

88     TreeNode(int tokenType, int start, int end, Object JavaDoc attribs) {
89         this(tokenType, start, end);
90         this.attribs = attribs;
91     }
92
93     /**
94      * Adds a subnode to this node.
95      *
96      * @param child subnode to be added
97      */

98     void add(TreeNode child) {
99         if (child == null) {
100             throw new IllegalArgumentException JavaDoc("null"); //NOI18N
101
}
102
103         child.parent = this;
104
105         if (children == null) {
106             children = new java.util.ArrayList JavaDoc(4);
107         }
108         children.add(child);
109     }
110
111     /**
112      * Returns a regular expression represented by the root node of the whole
113      * tree.
114      *
115      * @return regular expression represented by the whole tree this node
116      * is part of
117      */

118     String JavaDoc getRegexp() {
119
120         TreeNode candidate = this;
121         TreeNode candidParent;
122
123         /* Find the root: */
124         while ((candidParent = candidate.parent) != null) {
125             candidate = candidParent;
126         }
127         assert candidate instanceof TreeNodeRoot;
128
129         return candidate.getRegexp();
130     }
131
132     /**
133      * Returns the type of regular expression represented by this node.
134      *
135      * @return type of regular expression represented by this node's subtree
136      */

137     int getTokenType() {
138         return tokenType;
139     }
140
141     /**
142      * Returns this node's attributes.
143      *
144      * @return attributes of this node
145      * @see #TreeNode(int, int, int, Object)
146      */

147     Object JavaDoc getAttribs() {
148         return attribs;
149     }
150
151     /**
152      * Returns this node's children.
153      *
154      * @return list of this node's direct subnodes;
155      * or <code>null</code> if this node has no subnodes
156      */

157     java.util.List JavaDoc getChildren() {
158         return children != null ? new java.util.ArrayList JavaDoc(children)
159                                 : null;
160     }
161
162 }
163
Popular Tags