JSTryStatementImpl.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.JSTokenTypes; 
21   import com.intellij.lang.javascript.psi.JSCatchBlock; 
22   import com.intellij.lang.javascript.psi.JSElementVisitor; 
23   import com.intellij.lang.javascript.psi.JSStatement; 
24   import com.intellij.lang.javascript.psi.JSTryStatement; 
25   import com.intellij.psi.PsiElementVisitor; 
26   import com.intellij.psi.tree.IElementType; 
27    
28   /** 
29    * Created by IntelliJ IDEA. 
30    * User: max 
31    * Date: Jan 30, 2005 
32    * Time: 9:59:44 PM 
33    * To change this template use File | Settings | File Templates. 
34    */ 
35   public class JSTryStatementImpl extends JSElementImpl implements JSTryStatement { 
36     public JSTryStatementImpl(final ASTNode node) { 
37       super(node); 
38     } 
39    
40     public JSStatement getStatement() { 
41       ASTNode child = getNode().getFirstChildNode(); 
42       while (child != null) { 
43         final IElementType type = child.getElementType(); 
44         if (JSElementTypes.STATEMENTS.isInSet(type)) return (JSStatement)child.getPsi(); 
45         if (type == JSTokenTypes.FINALLY_KEYWORD) break; 
46         child = child.getTreeNext(); 
47       } 
48       return null; 
49     } 
50    
51     public JSCatchBlock getCatchBlock() { 
52       final ASTNode catchChild = getNode().findChildByType(JSElementTypes.CATCH_BLOCK); 
53       if (catchChild == null) { 
54         return null; 
55       } 
56       return (JSCatchBlock)catchChild.getPsi(); 
57     } 
58    
59     public JSStatement getFinallyStatement() { 
60       ASTNode child = getNode().getFirstChildNode(); 
61       boolean foundFinally = false; 
62       while (child != null) { 
63         final IElementType type = child.getElementType(); 
64         if (foundFinally && JSElementTypes.STATEMENTS.isInSet(type)) return (JSStatement)child.getPsi(); 
65         if (type == JSTokenTypes.FINALLY_KEYWORD) { 
66           foundFinally = true; 
67         } 
68         child = child.getTreeNext(); 
69       } 
70       return null; 
71     } 
72    
73     public void accept(PsiElementVisitor visitor) { 
74       if (visitor instanceof JSElementVisitor) { 
75         ((JSElementVisitor)visitor).visitJSTryStatement(this); 
76       } 
77       else { 
78         visitor.visitElement(this); 
79       } 
80     } 
81    
82     public String toString() { 
83       return "JSTryStatement"; 
84     } 
85   } 
86