1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ast/ast_namespace.h"
17 
18 #include "ast/ast_interface_type.h"
19 #include "ast/ast_sequenceable_type.h"
20 
21 namespace OHOS {
22 namespace Idl {
ASTNamespace(const String & nspaceStr)23 ASTNamespace::ASTNamespace(const String& nspaceStr)
24     : name_(nspaceStr),
25       outerNamespace_(nullptr)
26 {}
27 
AddNamespace(ASTNamespace * innerNspace)28 void ASTNamespace::AddNamespace(ASTNamespace* innerNspace)
29 {
30     if (innerNspace == nullptr) {
31         return;
32     }
33 
34     innerNamespaces_.push_back(innerNspace);
35     innerNspace->outerNamespace_ = this;
36 }
37 
FindNamespace(const String & nspaceStr)38 AutoPtr<ASTNamespace> ASTNamespace::FindNamespace(const String& nspaceStr)
39 {
40     if (nspaceStr.IsEmpty()) {
41         return nullptr;
42     }
43 
44     for (auto nspace : innerNamespaces_) {
45         if (nspace->name_.Equals(nspaceStr)) {
46             return nspace;
47         }
48     }
49     return nullptr;
50 }
51 
GetNamespace(size_t index)52 AutoPtr<ASTNamespace> ASTNamespace::GetNamespace(size_t index)
53 {
54     if (index >= innerNamespaces_.size()) {
55         return nullptr;
56     }
57 
58     return innerNamespaces_[index];
59 }
60 
AddInterface(ASTInterfaceType * interface)61 void ASTNamespace::AddInterface(ASTInterfaceType* interface)
62 {
63     if (interface == nullptr) {
64         return;
65     }
66 
67     interfaces_.push_back(interface);
68 }
69 
GetInterface(size_t index)70 AutoPtr<ASTInterfaceType> ASTNamespace::GetInterface(size_t index)
71 {
72     if (index >= interfaces_.size()) {
73         return nullptr;
74     }
75 
76     return interfaces_[index];
77 }
78 
AddSequenceable(ASTSequenceableType * sequenceable)79 void ASTNamespace::AddSequenceable(ASTSequenceableType* sequenceable)
80 {
81     if (sequenceable == nullptr) {
82         return;
83     }
84 
85     sequenceables_.push_back(sequenceable);
86 }
87 
GetSequenceable(size_t index)88 AutoPtr<ASTSequenceableType> ASTNamespace::GetSequenceable(size_t index)
89 {
90     if (index >= sequenceables_.size()) {
91         return nullptr;
92     }
93 
94     return sequenceables_[index];
95 }
96 
ToString()97 String ASTNamespace::ToString()
98 {
99     String nspaceStr;
100     ASTNamespace* nspace = this;
101     while (nspace != nullptr) {
102         nspaceStr = nspace->name_ + "." + nspaceStr;
103         nspace = nspace->outerNamespace_;
104     }
105     return nspaceStr;
106 }
107 } // namespace Idl
108 } // namespace OHOS
109