View Javadoc
1 /* ==================================================================== 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2000 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" must 27 * not be used to endorse or promote products derived from this 28 * software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * nor may "Apache" appear in their name, without prior written 33 * permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 * 54 * Portions of this software are based upon public domain software 55 * originally written at the National Center for Supercomputing Applications, 56 * University of Illinois, Urbana-Champaign. 57 */ 58 59 package net.sourceforge.jane.jjtree; 60 61 import java.io.InputStream; 62 import java.util.Enumeration; 63 import java.util.LinkedList; 64 import java.util.List; 65 66 import org.apache.commons.collections.Predicate; 67 import org.apache.commons.io.IOUtil; 68 69 /*** 70 * Utilities for parsing/manipulating JJTree constructs. 71 */ 72 public class JJTreeUtils 73 implements JavaParserConstants 74 { 75 76 /*** 77 * Returns the child nodes of the given type. 78 */ 79 static public ChildNodeEnumeration childNodes(Node parent, Class type) 80 { 81 return new ChildNodeEnumeration(parent, type); 82 } 83 84 /*** 85 * Find the first child node of the given type, starting at the given index. 86 */ 87 static public Node findChild(Node parent, int startingIndex, Class type) 88 { 89 int count = parent.jjtGetNumChildren(); 90 for (int i=startingIndex; i<count; i++) { 91 if (type.equals(parent.jjtGetChild(i).getClass())) { 92 return parent.jjtGetChild(i); 93 } 94 } 95 return null; 96 } 97 98 /*** 99 * Find the first child node of the given type. 100 */ 101 static public Node findFirstChild(Node parent, Class type) 102 { 103 return findChild(parent, 0, type); 104 } 105 106 /*** 107 * Find the first descendant node of the given type. 108 */ 109 static public Node findFirstDescendant(Node parent, Class type) 110 { 111 NodeFinder finder = new NodeFinder(new TypeMatchPredicate(type)); 112 return finder.findNode(parent); 113 } 114 115 /*** 116 * Returns an array of names from a {@link JavaNameList}. 117 */ 118 static public String[] getNameListArray(Node parent) 119 { 120 JavaNameList nameList = (JavaNameList) 121 JJTreeUtils.findFirstChild(parent, JavaNameList.class); 122 if (nameList == null) 123 return new String[0]; 124 return nameList.getNames(); 125 } 126 127 /*** 128 * Returns the image of the given kind of token. 129 */ 130 static public String getNormalizedTokenImage(int kind) 131 { 132 String image = tokenImage[kind]; 133 if (image.charAt(0) == '\"') 134 return image.substring(1, image.length() - 1); 135 return image; 136 } 137 138 /*** 139 * Returns the compilation unit. 140 */ 141 static public JavaCompilationUnit getCompilationUnit(Node node) 142 { 143 while (node != null && !(node instanceof JavaCompilationUnit)) { 144 node = node.jjtGetParent(); 145 } 146 return (JavaCompilationUnit) node; 147 } 148 149 /*** 150 * Parse the given input stream. 151 */ 152 static public JavaCompilationUnit parse(InputStream in) 153 throws ParseException 154 { 155 if (in == null) { 156 // TODO: Log 157 return null; 158 } 159 try { 160 JavaParser parser = new JavaParser(in); 161 return parser.parse(); 162 } finally { 163 IOUtil.shutdownStream(in); 164 } 165 } 166 167 /*** 168 * Returns the index of the first found token of the given kind. 169 */ 170 static int indexOfToken(JavaParser parser, int kind) 171 { 172 int i = -1; 173 Token token = parser.getToken(++i); 174 while (token.kind != kind) 175 token = parser.getToken(++i); 176 return i; 177 } 178 179 /*** 180 * Find the first token of the given kind. 181 */ 182 static Token findTokenOfKind(JavaParser parser, int kind) 183 { 184 int i = 1; 185 Token token = parser.getToken(i++); 186 while (token.kind != kind) 187 token = parser.getToken(i++); 188 return token; 189 } 190 191 static class TypeMatchPredicate implements Predicate 192 { 193 private Class type; 194 195 /*** 196 * Create a new <code>TypeMatchPredicate</code>. 197 */ 198 public TypeMatchPredicate(Class targetType) 199 { 200 type = targetType; 201 } 202 203 /*** 204 * Evaluates to <code>true</code> if the type matches 205 * the target type. 206 */ 207 public boolean evaluate(Object obj) 208 { 209 return type.equals(obj.getClass()); 210 } 211 } 212 213 } 214

This page was automatically generated by Maven