~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to scripting/php/srcmodel/src/org/netbeans/modules/php/model/impl/Utils.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.php.model.impl;
 
42
 
 
43
import java.util.LinkedList;
 
44
import java.util.List;
 
45
import java.util.Set;
 
46
 
 
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;
 
52
 
 
53
 
 
54
/**
 
55
 * @author ads
 
56
 *
 
57
 */
 
58
public final class Utils {
 
59
 
 
60
    public  static final String VISIBILITY       = "Visibility";         // NOI18N
 
61
 
 
62
    public  static final String VARIABLE         = "php_variable";       // NOI18N
 
63
    
 
64
    public  static final String REFERENCE        = "&";                  // NOI18N
 
65
    
 
66
    public  static final String WHITESPACE       = "php_whitespace";     // NOI18N
 
67
    
 
68
    public  static final String COMMENT          = "php_comment";        // NOI18N
 
69
    
 
70
    public  static final String LINE_COMMENT     = "php_line_comment";   // NOI18N
 
71
    
 
72
    public  static final String IDENTIFIER       = "php_identifier";     // NOI18N
 
73
    
 
74
    public  static final String OPERATOR         = "php_operator";       // NOI18N
 
75
    
 
76
    // avoid inst-ion
 
77
    private Utils(){
 
78
    }
 
79
    
 
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 );
 
84
    }
 
85
 
 
86
    public static ASTNode getNarrowNode(ASTNode node  ){
 
87
        List<ASTItem> children = node.getChildren();
 
88
        if ( children.size() != 1 ){
 
89
            return node;
 
90
        }
 
91
        else {
 
92
            ASTItem item = children.get( 0 );
 
93
            if ( item instanceof ASTNode ){
 
94
                return getNarrowNode( (ASTNode) item);
 
95
            }
 
96
            else {
 
97
                return node;
 
98
            }
 
99
        }
 
100
    }
 
101
    
 
102
    public static String getNarrowType( ASTNode node ) {
 
103
        return getNarrowNode(node ).getNT();
 
104
    }
 
105
    
 
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 );
 
111
        }
 
112
        if ( item instanceof ASTNode ){
 
113
            ASTNode node = (ASTNode) item;
 
114
            String nt = node.getNT();
 
115
            if ( !nt.equals( VISIBILITY)){
 
116
                return null;
 
117
            }
 
118
            return Modifier.forString( node.getAsText().trim() );
 
119
        }
 
120
        return null;
 
121
    }
 
122
    
 
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() );
 
131
                    if  ( mod != null ){
 
132
                        ret.add( mod );
 
133
                    }
 
134
                }
 
135
            }
 
136
        }
 
137
        return ret;
 
138
    }
 
139
    
 
140
    public static ASTNode getClassMemberNode( ASTNode node ) {
 
141
        return node.getNode( ClassBodyFactory.CLASS_MEMBER );
 
142
    }
 
143
    
 
144
    public static class NodeFinder {
 
145
        public NodeFinder( ASTNode node , Set<String> collection) {
 
146
            myStartNode = node;
 
147
            myTypes = collection;
 
148
        }
 
149
        
 
150
        public void check() {
 
151
            checkNode( myStartNode );
 
152
        }
 
153
        
 
154
        public boolean isFound() {
 
155
            return isFound;
 
156
        }
 
157
        
 
158
        public ASTNode getNode() {
 
159
            return myNode;
 
160
        }
 
161
        
 
162
        public String getType() {
 
163
            return myType;
 
164
        }
 
165
        
 
166
        private void checkNode( ASTNode node ) {
 
167
            List<ASTItem> items =  node.getChildren();
 
168
            
 
169
            if ( items.size() != 1) {
 
170
                return;
 
171
            }
 
172
            ASTItem item = items.get( 0 );
 
173
            if ( !( item instanceof ASTNode )) {
 
174
                return;
 
175
            }
 
176
            ASTNode child = (ASTNode) item;
 
177
            if ( myTypes.contains(child.getNT() )) {
 
178
                myNode = child;
 
179
                myType = child.getNT();
 
180
                isFound = true;
 
181
            }
 
182
            checkNode( child );
 
183
        }
 
184
        
 
185
        private boolean isFound;
 
186
        
 
187
        private ASTNode myNode;
 
188
        
 
189
        private String myType;
 
190
        
 
191
        private ASTNode myStartNode;
 
192
        
 
193
        private Set<String> myTypes;
 
194
    }
 
195
}