01: /*
02: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
03: *
04: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
05: *
06: * The contents of this file are subject to the terms of either the GNU
07: * General Public License Version 2 only ("GPL") or the Common
08: * Development and Distribution License("CDDL") (collectively, the
09: * "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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
27: */
28: package org.netbeans.modules.cnd.gotodeclaration.util;
29:
30: import org.netbeans.modules.cnd.api.model.*;
31: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
32:
33: /**
34: * Utility class with methods that returns context information
35: * @author Vladimir Kvashin
36: */
37: public class ContextUtil {
38:
39: private ContextUtil() {
40: }
41:
42: /**
43: * Return the name of the context of the scope element -
44: * either namespace or (for file-level, such as C-style statics) file
45: * (If the element is a nested class, return containing class' namespace)
46: */
47: public static String getContextName(CsmScopeElement element) {
48: CsmScope scope = element.getScope();
49: if (CsmKindUtilities.isClass(scope)) {
50: CsmClass cls = ((CsmClass) scope);
51: CsmNamespace ns = getClassNamespace(cls);
52: return ns.getQualifiedName().toString();
53: } else if (CsmKindUtilities.isNamespace(scope)) {
54: return ((CsmNamespace) scope).getQualifiedName().toString();
55: } else if (CsmKindUtilities.isFile(scope)) {
56: return ((CsmFile) scope).getName().toString();
57: }
58: return "";
59: }
60:
61: /**
62: * Returns the namespace the given class belongs
63: * (even if it's a nested class)
64: */
65: public static CsmNamespace getClassNamespace(CsmClass cls) {
66: CsmScope scope = cls.getScope();
67: while (scope != null && CsmKindUtilities.isClass(scope)) {
68: CsmClass outer = (CsmClass) scope;
69: scope = outer.getScope();
70: }
71: return CsmKindUtilities.isNamespace(scope) ? (CsmNamespace) scope
72: : null;
73: }
74:
75: /**
76: * Returns the full name of the class:
77: * for a top-level class it's just a class name,
78: * for a nested class, it contain outer class name
79: * (but in any case without a namespace)
80: */
81: public static String getClassFullName(CsmClass cls) {
82: StringBuilder sb = new StringBuilder(cls.getName());
83: CsmScope scope = cls.getScope();
84: while (scope != null && CsmKindUtilities.isClass(scope)) {
85: CsmClass outer = (CsmClass) scope;
86: sb.insert(0, "::"); // NOI18N
87: sb.insert(0, (outer).getName());
88: scope = outer.getScope();
89: }
90:
91: return sb.toString();
92: }
93:
94: }
|