JSSwitchStatementImpl.java

1    /* 
2     * Copyright 2000-2005 JetBrains s.r.o. 
3     * 
4     * Licensed under the Apache License, Version 2.0 (the "License"); 
5     * you may not use this file except in compliance with the License. 
6     * You may obtain a copy of the License at 
7     * 
8     * http://www.apache.org/licenses/LICENSE-2.0 
9     * 
10    * Unless required by applicable law or agreed to in writing, software 
11    * distributed under the License is distributed on an "AS IS" BASIS, 
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13    * See the License for the specific language governing permissions and 
14    * limitations under the License. 
15    */ 
16   package com.intellij.lang.javascript.psi.impl; 
17    
18   import com.intellij.lang.ASTNode; 
19   import com.intellij.lang.javascript.JSElementTypes; 
20   import com.intellij.lang.javascript.psi.JSCaseClause; 
21   import com.intellij.lang.javascript.psi.JSElementVisitor; 
22   import com.intellij.lang.javascript.psi.JSExpression; 
23   import com.intellij.lang.javascript.psi.JSSwitchStatement; 
24   import com.intellij.psi.PsiElementVisitor; 
25   import com.intellij.psi.tree.TokenSet; 
26    
27   /** 
28    * Created by IntelliJ IDEA. 
29    * User: max 
30    * Date: Jan 30, 2005 
31    * Time: 10:08:20 PM 
32    * To change this template use File | Settings | File Templates. 
33    */ 
34   public class JSSwitchStatementImpl extends JSElementImpl implements JSSwitchStatement { 
35     private static final TokenSet CASE_CLAUSE_FILTER = TokenSet.create(JSElementTypes.CASE_CLAUSE); 
36    
37     public JSSwitchStatementImpl(final ASTNode node) { 
38       super(node); 
39     } 
40    
41     public JSExpression getSwitchExpression() { 
42       final ASTNode[] nodes = getNode().getChildren(JSElementTypes.EXPRESSIONS); 
43       return (JSExpression)(nodes.length == 1 ? nodes[0].getPsi() : null); 
44     } 
45    
46     public JSCaseClause[] getCaseClauses() { 
47       final ASTNode[] nodes = getNode().getChildren(CASE_CLAUSE_FILTER); 
48       final JSCaseClause[] clauses = new JSCaseClause[nodes.length]; 
49       for (int i = 0; i < clauses.length; i++) { 
50         clauses[i] = (JSCaseClause)nodes[i].getPsi(); 
51       } 
52       return clauses; 
53     } 
54    
55     public void accept(PsiElementVisitor visitor) { 
56       if (visitor instanceof JSElementVisitor) { 
57         ((JSElementVisitor)visitor).visitJSSwitchStatement(this); 
58       } 
59       else { 
60         visitor.visitElement(this); 
61       } 
62     } 
63    
64     public String toString() { 
65       return "JSSwitchStatement"; 
66     } 
67   } 
68