001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.jet.JetNodeTypes;
024    import org.jetbrains.jet.lang.resolve.DescriptorUtils;
025    import org.jetbrains.jet.lang.resolve.name.FqName;
026    import org.jetbrains.jet.lang.resolve.name.Name;
027    
028    import java.util.List;
029    
030    public class JetNamespaceHeader extends JetReferenceExpression {
031        private String qualifiedNameCache = null;
032    
033        public JetNamespaceHeader(@NotNull ASTNode node) {
034            super(node);
035        }
036    
037        @NotNull
038        public List<JetSimpleNameExpression> getParentNamespaceNames() {
039            List<JetSimpleNameExpression> parentParts = findChildrenByType(JetNodeTypes.REFERENCE_EXPRESSION);
040            JetSimpleNameExpression lastPart = (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
041            parentParts.remove(lastPart);
042            return parentParts;
043        }
044    
045        @Nullable
046        public JetSimpleNameExpression getLastPartExpression() {
047            return (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
048        }
049    
050        @Nullable
051        public PsiElement getNameIdentifier() {
052            JetSimpleNameExpression lastPart = (JetSimpleNameExpression)findLastChildByType(JetNodeTypes.REFERENCE_EXPRESSION);
053            if (lastPart == null) {
054                return null;
055            }
056    
057            return lastPart.getIdentifier();
058        }
059    
060        @Override
061        @NotNull
062        public String getName() {
063            PsiElement nameIdentifier = getNameIdentifier();
064            return nameIdentifier == null ? "" : nameIdentifier.getText();
065        }
066    
067        @NotNull
068        public Name getNameAsName() {
069            PsiElement nameIdentifier = getNameIdentifier();
070            return nameIdentifier == null ? DescriptorUtils.ROOT_NAMESPACE_NAME : Name.identifier(nameIdentifier.getText());
071        }
072    
073        public boolean isRoot() {
074            return getName().length() == 0;
075        }
076    
077        @NotNull
078        public FqName getFqName() {
079            String qualifiedName = getQualifiedName();
080            return qualifiedName.isEmpty() ? FqName.ROOT : new FqName(qualifiedName);
081        }
082    
083        @NotNull
084        public FqName getParentFqName(JetReferenceExpression nameExpression) {
085            String parentQualifiedName = getQualifiedNameParentOf(nameExpression);
086            return parentQualifiedName.isEmpty() ? FqName.ROOT : new FqName(parentQualifiedName);
087        }
088    
089        @NotNull
090        public String getQualifiedName() {
091            if (qualifiedNameCache == null) {
092                qualifiedNameCache = getQualifiedNameParentOf(null);
093            }
094    
095            return qualifiedNameCache;
096        }
097    
098        @NotNull
099        private String getQualifiedNameParentOf(@Nullable JetReferenceExpression nameExpression) {
100            StringBuilder builder = new StringBuilder();
101            for (JetSimpleNameExpression e : findChildrenByClass(JetSimpleNameExpression.class)) {
102                if (e == nameExpression) {
103                    break;
104                }
105    
106                if (builder.length() > 0) {
107                    builder.append(".");
108                }
109                builder.append(e.getReferencedName());
110            }
111            return builder.toString();
112        }
113    
114        @Override
115        public void subtreeChanged() {
116            qualifiedNameCache = null;
117        }
118    }
119