1 package org.hibernate.hql.ast.util; 3 4 import java.io.ByteArrayOutputStream ; 5 import java.io.PrintStream ; 6 import java.io.PrintWriter ; 7 import java.lang.reflect.Field ; 8 import java.lang.reflect.Modifier ; 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import org.hibernate.hql.ast.tree.DisplayableNode; 14 import org.hibernate.util.StringHelper; 15 16 import antlr.collections.AST; 17 18 23 public class ASTPrinter { 24 private Map tokenTypeNamesByTokenType; 25 private Class tokenTypeConstants; 26 private boolean showClassNames = true; 27 28 35 public ASTPrinter(Class tokenTypeConstants) { 36 this.tokenTypeConstants = tokenTypeConstants; 37 } 38 39 44 public boolean isShowClassNames() { 45 return showClassNames; 46 } 47 48 53 public void setShowClassNames(boolean showClassNames) { 54 this.showClassNames = showClassNames; 55 } 56 57 63 private void showAst(AST ast, PrintStream out) { 64 showAst( ast, new PrintWriter ( out ) ); 65 } 66 67 73 public void showAst(AST ast, PrintWriter pw) { 74 ArrayList parents = new ArrayList (); 75 showAst( parents, pw, ast ); 76 pw.flush(); 77 } 78 79 86 public String showAsString(AST ast, String header) { 87 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 88 PrintStream ps = new PrintStream ( baos ); 89 ps.println( header ); 90 showAst( ast, ps ); 91 ps.flush(); 92 return new String ( baos.toByteArray() ); 93 } 94 95 102 public static String getConstantName(Class tokenTypeConstants, int type) { 103 String tokenTypeName = null; 104 if ( tokenTypeConstants != null ) { 105 Field [] fields = tokenTypeConstants.getFields(); 106 for ( int i = 0; i < fields.length; i++ ) { 107 Field field = fields[i]; 108 tokenTypeName = getTokenTypeName( field, type, true ); 109 if ( tokenTypeName != null ) { 110 break; } 112 } } 115 if ( tokenTypeName == null ) { 117 tokenTypeName = Integer.toString( type ); 118 } 119 120 return tokenTypeName; 121 } 122 123 private static String getTokenTypeName(Field field, int type, boolean checkType) { 124 if ( Modifier.isStatic( field.getModifiers() ) ) { 125 try { 126 Object value = field.get( null ); 127 if ( !checkType ) { 128 return field.getName(); 129 } 130 else if ( value instanceof Integer ) { 131 Integer integer = ( Integer ) value; 132 if ( integer.intValue() == type ) { 133 return field.getName(); 134 } 135 } } catch ( IllegalArgumentException ignore ) { 138 } 139 catch ( IllegalAccessException ignore ) { 140 } 141 } return null; 143 } 144 145 152 private String getTokenTypeName(int type) { 153 if ( tokenTypeConstants == null ) { 156 return Integer.toString( type ); 157 } 158 159 if ( tokenTypeNamesByTokenType == null ) { 162 Field [] fields = tokenTypeConstants.getFields(); 163 tokenTypeNamesByTokenType = new HashMap (); 164 String tokenTypeName = null; 165 for ( int i = 0; i < fields.length; i++ ) { 166 Field field = fields[i]; 167 tokenTypeName = getTokenTypeName( field, type, false ); 168 if ( tokenTypeName != null ) { 169 try { 170 tokenTypeNamesByTokenType.put( field.get( null ), field.getName() ); 171 } 172 catch ( IllegalAccessException ignore ) { 173 } 174 } 175 } } 178 return ( String ) tokenTypeNamesByTokenType.get( new Integer ( type ) ); 179 } 180 181 private void showAst(ArrayList parents, PrintWriter pw, AST ast) { 182 if ( ast == null ) { 183 pw.println( "AST is null!" ); 184 return; 185 } 186 187 for ( int i = 0; i < parents.size(); i++ ) { 188 AST parent = ( AST ) parents.get( i ); 189 if ( parent.getNextSibling() == null ) { 190 191 pw.print( " " ); 192 } 193 else { 194 pw.print( " | " ); 195 } 196 } 197 198 if ( ast.getNextSibling() == null ) { 199 pw.print( " \\-" ); 200 } 201 else { 202 pw.print( " +-" ); 203 } 204 205 showNode( pw, ast ); 206 207 ArrayList newParents = new ArrayList ( parents ); 208 newParents.add( ast ); 209 for ( AST child = ast.getFirstChild(); child != null; child = child.getNextSibling() ) { 210 showAst( newParents, pw, child ); 211 } 212 newParents.clear(); 213 } 214 215 private void showNode(PrintWriter pw, AST ast) { 216 String s = nodeToString( ast, isShowClassNames() ); 217 pw.println( s ); 218 } 219 220 public String nodeToString(AST ast, boolean showClassName) { 221 if ( ast == null ) { 222 return "{null}"; 223 } 224 StringBuffer buf = new StringBuffer (); 225 buf.append( "[" ).append( getTokenTypeName( ast.getType() ) ).append( "] " ); 226 if ( showClassName ) { 227 buf.append( StringHelper.unqualify( ast.getClass().getName() ) ).append( ": " ); 228 } 229 230 buf.append( "'" ); 231 String text = ast.getText(); 232 appendEscapedMultibyteChars(text, buf); 233 buf.append( "'" ); 234 if ( ast instanceof DisplayableNode ) { 235 DisplayableNode displayableNode = ( DisplayableNode ) ast; 236 buf.append( " " ).append( displayableNode.getDisplayText() ); 238 } 239 String s = buf.toString(); 240 return s; 241 } 242 243 public static void appendEscapedMultibyteChars(String text, StringBuffer buf) { 244 char[] chars = text.toCharArray(); 245 for (int i = 0; i < chars.length; i++) { 246 char aChar = chars[i]; 247 if (aChar > 256) { 248 buf.append("\\u"); 249 buf.append(Integer.toHexString(aChar)); 250 } 251 else 252 buf.append(aChar); 253 } 254 } 255 256 public static String escapeMultibyteChars(String text) 257 { 258 StringBuffer buf = new StringBuffer (); 259 appendEscapedMultibyteChars(text,buf); 260 return buf.toString(); 261 } 262 } 263 | Popular Tags |