1 package org.hibernate.hql.ast.util; 3 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import antlr.ASTFactory; 8 import antlr.collections.AST; 9 import antlr.collections.impl.ASTArray; 10 11 16 public final class ASTUtil { 17 23 private ASTUtil() { 24 } 25 26 34 public static AST create(ASTFactory astFactory, int type, String text) { 35 AST node = astFactory.create( type, text ); 36 return node; 37 } 38 39 48 public static AST createSibling(ASTFactory astFactory, int type, String text, AST prevSibling) { 49 AST node = astFactory.create( type, text ); 50 node.setNextSibling( prevSibling.getNextSibling() ); 51 prevSibling.setNextSibling( node ); 52 return node; 53 } 54 55 public static AST insertSibling(AST node, AST prevSibling) { 56 node.setNextSibling( prevSibling.getNextSibling() ); 57 prevSibling.setNextSibling( node ); 58 return node; 59 } 60 61 72 public static AST createBinarySubtree(ASTFactory factory, int parentType, String parentText, AST child1, AST child2) { 73 ASTArray array = createAstArray( factory, 3, parentType, parentText, child1 ); 74 array.add( child2 ); 75 return factory.make( array ); 76 } 77 78 88 public static AST createParent(ASTFactory factory, int parentType, String parentText, AST child) { 89 ASTArray array = createAstArray( factory, 2, parentType, parentText, child ); 90 return factory.make( array ); 91 } 92 93 public static AST createTree(ASTFactory factory, AST[] nestedChildren) { 94 AST[] array = new AST[2]; 95 int limit = nestedChildren.length - 1; 96 for ( int i = limit; i >= 0; i-- ) { 97 if ( i != limit ) { 98 array[1] = nestedChildren[i + 1]; 99 array[0] = nestedChildren[i]; 100 factory.make( array ); 101 } 102 } 103 return array[0]; 104 } 105 106 113 public static AST findTypeInChildren(AST parent, int type) { 114 AST n = parent.getFirstChild(); 115 while ( n != null && n.getType() != type ) 116 n = n.getNextSibling(); 117 return n; 118 } 119 120 126 public static AST getLastChild(AST n) { 127 return getLastSibling( n.getFirstChild() ); 128 } 129 130 136 private static AST getLastSibling(AST a) { 137 AST last = null; 138 while ( a != null ) { 139 last = a; 140 a = a.getNextSibling(); 141 } 142 return last; 143 } 144 145 151 public static String getDebugString(AST n) { 152 StringBuffer buf = new StringBuffer (); 153 buf.append( "[ " ); 154 buf.append( ( n == null ) ? "{null}" : n.toStringTree() ); 155 buf.append( " ]" ); 156 return buf.toString(); 157 } 158 159 166 public static AST findPreviousSibling(AST parent, AST child) { 167 AST prev = null; 168 AST n = parent.getFirstChild(); 169 while ( n != null ) { 170 if ( n == child ) { 171 return prev; 172 } 173 prev = n; 174 n = n.getNextSibling(); 175 } 176 throw new IllegalArgumentException ( "Child not found in parent!" ); 177 } 178 179 185 public static void makeSiblingOfParent(AST parent, AST child) { 186 AST prev = findPreviousSibling( parent, child ); 187 if ( prev != null ) { 188 prev.setNextSibling( child.getNextSibling() ); 189 } 190 else { parent.setFirstChild( child.getNextSibling() ); 192 } 193 child.setNextSibling( parent.getNextSibling() ); 194 parent.setNextSibling( child ); 195 } 196 197 public static String getPathText(AST n) { 198 StringBuffer buf = new StringBuffer (); 199 getPathText( buf, n ); 200 return buf.toString(); 201 } 202 203 private static void getPathText(StringBuffer buf, AST n) { 204 AST firstChild = n.getFirstChild(); 205 if ( firstChild != null ) { 207 getPathText( buf, firstChild ); 208 } 209 buf.append( n.getText() ); 211 if ( firstChild != null && firstChild.getNextSibling() != null ) { 213 getPathText( buf, firstChild.getNextSibling() ); 214 } 215 } 216 217 public static boolean hasExactlyOneChild(AST n) { 218 return n != null && n.getFirstChild() != null && n.getFirstChild().getNextSibling() == null; 219 } 220 221 public static void appendSibling(AST n, AST s) { 222 while ( n.getNextSibling() != null ) 223 n = n.getNextSibling(); 224 n.setNextSibling( s ); 225 } 226 227 233 public static void insertChild(AST parent, AST child) { 234 if ( parent.getFirstChild() == null ) { 235 parent.setFirstChild( child ); 236 } 237 else { 238 AST n = parent.getFirstChild(); 239 parent.setFirstChild( child ); 240 child.setNextSibling( n ); 241 } 242 } 243 244 247 public static interface FilterPredicate { 248 254 boolean exclude(AST n); 255 } 256 257 260 public abstract static class IncludePredicate implements FilterPredicate { 261 public final boolean exclude(AST node) { 262 return !include( node ); 263 } 264 265 public abstract boolean include(AST node); 266 } 267 268 private static ASTArray createAstArray(ASTFactory factory, int size, int parentType, String parentText, AST child1) { 269 ASTArray array = new ASTArray( size ); 270 array.add( factory.create( parentType, parentText ) ); 271 array.add( child1 ); 272 return array; 273 } 274 275 public static List collectChildren(AST root, FilterPredicate predicate) { 276 List children = new ArrayList (); 277 collectChildren( children, root, predicate ); 278 return children; 279 } 280 281 private static void collectChildren(List children, AST root, FilterPredicate predicate) { 282 for ( AST n = root.getFirstChild(); n != null; n = n.getNextSibling() ) { 283 if ( predicate == null || !predicate.exclude( n ) ) { 284 children.add( n ); 285 } 286 collectChildren( children, n, predicate ); 287 } 288 } 289 290 } 291 | Popular Tags |