2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.php.model.impl;
43
import java.util.LinkedList;
44
import java.util.List;
47
import org.netbeans.api.languages.ASTItem;
48
import org.netbeans.api.languages.ASTNode;
49
import org.netbeans.api.languages.ASTToken;
50
import org.netbeans.modules.php.model.Modifier;
51
import org.netbeans.modules.php.model.impl.factory.ClassBodyFactory;
58
public final class Utils {
60
public static final String VISIBILITY = "Visibility"; // NOI18N
62
public static final String VARIABLE = "php_variable"; // NOI18N
64
public static final String REFERENCE = "&"; // NOI18N
66
public static final String WHITESPACE = "php_whitespace"; // NOI18N
68
public static final String COMMENT = "php_comment"; // NOI18N
70
public static final String LINE_COMMENT = "php_line_comment"; // NOI18N
72
public static final String IDENTIFIER = "php_identifier"; // NOI18N
74
public static final String OPERATOR = "php_operator"; // NOI18N
80
public static boolean skipToken( ASTToken token ) {
81
String type = token.getTypeName();
82
return type.equals( WHITESPACE ) || type.equals( COMMENT ) ||
83
type.equals( LINE_COMMENT );
86
public static ASTNode getNarrowNode(ASTNode node ){
87
List<ASTItem> children = node.getChildren();
88
if ( children.size() != 1 ){
92
ASTItem item = children.get( 0 );
93
if ( item instanceof ASTNode ){
94
return getNarrowNode( (ASTNode) item);
102
public static String getNarrowType( ASTNode node ) {
103
return getNarrowNode(node ).getNT();
106
public static Modifier getModifier( ASTItem item ){
107
if ( item instanceof ASTToken ){
108
ASTToken token = (ASTToken) item;
109
String text = token.getIdentifier();
110
return Modifier.forString( text );
112
if ( item instanceof ASTNode ){
113
ASTNode node = (ASTNode) item;
114
String nt = node.getNT();
115
if ( !nt.equals( VISIBILITY)){
118
return Modifier.forString( node.getAsText().trim() );
123
public static List<Modifier> getModifiers( ASTNode node){
124
List<Modifier> ret = new LinkedList<Modifier>();
125
List<ASTItem> children = node.getChildren();
126
for (ASTItem item : children) {
127
if ( item instanceof ASTNode ){
128
ASTNode child = (ASTNode)item;
129
if ( child.getNT().equals( VISIBILITY) ){
130
Modifier mod = Modifier.forString( child.getAsText().trim() );
140
public static ASTNode getClassMemberNode( ASTNode node ) {
141
return node.getNode( ClassBodyFactory.CLASS_MEMBER );
144
public static class NodeFinder {
145
public NodeFinder( ASTNode node , Set<String> collection) {
147
myTypes = collection;
150
public void check() {
151
checkNode( myStartNode );
154
public boolean isFound() {
158
public ASTNode getNode() {
162
public String getType() {
166
private void checkNode( ASTNode node ) {
167
List<ASTItem> items = node.getChildren();
169
if ( items.size() != 1) {
172
ASTItem item = items.get( 0 );
173
if ( !( item instanceof ASTNode )) {
176
ASTNode child = (ASTNode) item;
177
if ( myTypes.contains(child.getNT() )) {
179
myType = child.getNT();
185
private boolean isFound;
187
private ASTNode myNode;
189
private String myType;
191
private ASTNode myStartNode;
193
private Set<String> myTypes;