1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef ARY_NAMETREENODE_HXX
29 #define ARY_NAMETREENODE_HXX
30 //  KORR_DEPRECATED_3.0
31 //      Replace by ::ary::symtree::Node.
32 
33 // USED SERVICES
34 #include <cosv/tpl/tpltools.hxx>
35 #include <sci_impl.hxx>
36 // HACK because of SunPro 5.2 compiler bug with templates:
37 #include <ary/idl/i_module.hxx>
38 
39 
40 
41 
42 namespace ary
43 {
44 
45 
46 /** Implementation of a node in a namespace-tree.
47 */
48 template<class ITEM_ID>
49 class NameTreeNode
50 {
51   public:
52     typedef NameTreeNode                    self;
53     typedef ITEM_ID                         item_id;
54     typedef StringVector::const_iterator    name_iterator;
55     typedef std::map<String, item_id>       Map_LocalNames;
56 
57     // LIFECYCLE
58                         NameTreeNode();
59                         NameTreeNode(
60                             const String &      i_sName,
61                             const self &        i_rParent,
62                             ITEM_ID             i_nParentId );
63     virtual             ~NameTreeNode();
64 
65     // OPERATIONS
66     void                Add_Name(
67                             const String &      i_sName,
68                             item_id             i_nId );
69     // INQUIRY
70     const String &      Name() const            { return Depth() > 0 ? aCompleteNameChain.back() : String::Null_(); }
71     item_id             Parent() const          { return nParent; }
72     intt                Depth() const           { return aCompleteNameChain.size(); }
73 
74     bool                IsEquivalent(
75                             const NameTreeNode &
76                                                 i_rNode ) const;
77     name_iterator       NameChain_Begin() const { return aCompleteNameChain.begin(); }
78     name_iterator       NameChain_End() const   { return aCompleteNameChain.end(); }
79 
80     item_id             Search_Name(
81                             const String &      i_sName ) const;
82     void                Get_Names(
83                             Dyn_StdConstIterator<ITEM_ID> &
84                                                 o_rResult ) const;
85     const Map_LocalNames &
86                         LocalNames() const      { return aLocalNames; }
87   private:
88     // Locals
89     Map_LocalNames &    LocalNames()            { return aLocalNames; }
90 
91     // DATA
92     Map_LocalNames      aLocalNames;
93     StringVector        aCompleteNameChain;
94     item_id             nParent;
95 };
96 
97 
98 
99 
100 // IMPLEMENTATION
101 template<class ITEM_ID>
102 NameTreeNode<ITEM_ID>::NameTreeNode()
103     :   aLocalNames(),
104         aCompleteNameChain(),
105         nParent(0)
106 {
107 }
108 
109 template<class ITEM_ID>
110 NameTreeNode<ITEM_ID>::NameTreeNode( const String &      i_sName,
111                                      const self &        i_rParent,
112                                      ITEM_ID             i_nParentId )
113     :   aLocalNames(),
114         aCompleteNameChain(),
115         nParent(i_nParentId)
116 {
117     aCompleteNameChain.reserve(i_rParent.Depth()+1);
118     for ( name_iterator it = i_rParent.NameChain_Begin();
119           it != i_rParent.NameChain_End();
120           ++it )
121     {
122         aCompleteNameChain.push_back(*it);
123     }
124     aCompleteNameChain.push_back(i_sName);
125 }
126 
127 template<class ITEM_ID>
128 NameTreeNode<ITEM_ID>::~NameTreeNode()
129 {
130 }
131 
132 
133 template<class ITEM_ID>
134 inline void
135 NameTreeNode<ITEM_ID>::Add_Name( const String &      i_sName,
136                                  item_id             i_nId )
137 {
138     LocalNames().insert( typename Map_LocalNames::value_type(i_sName, i_nId) );
139 }
140 
141 
142 template<class ITEM_ID>
143 inline bool
144 NameTreeNode<ITEM_ID>::IsEquivalent( const NameTreeNode & i_rNode ) const
145 {
146     return aCompleteNameChain == i_rNode.aCompleteNameChain;
147 }
148 
149 template<class ITEM_ID>
150 inline ITEM_ID
151 NameTreeNode<ITEM_ID>::Search_Name( const String & i_sName ) const
152 {
153     return csv::value_from_map(LocalNames(),i_sName, ITEM_ID(0));
154 }
155 
156 template<class ITEM_ID>
157 inline void
158 NameTreeNode<ITEM_ID>::Get_Names( Dyn_StdConstIterator<ITEM_ID> & o_rResult ) const
159 {
160     o_rResult = new SCI_DataInMap<String,item_id>(LocalNames());
161 }
162 
163 
164 // HACK because of SunPro 5.2 compiler bug with templates:
165 //   ary::idl::Module has to be "FIND_NODE::node_type"
166 //   must be solved later somehow.
167 template <class FIND_NODE>
168 typename FIND_NODE::id_type
169 Search_SubTree( const ary::idl::Module &    i_rStart,
170                 const FIND_NODE &           i_rNodeFinder )
171 {
172     const ary::idl::Module *
173         ret = &i_rStart;
174 
175     for ( StringVector::const_iterator  it = i_rNodeFinder.Begin();
176           it != i_rNodeFinder.End() AND ret != 0;
177           ++it )
178     {
179         ret = i_rNodeFinder(ret->Search_Name(*it));
180     }
181 
182     typename FIND_NODE::id_type nret(0);
183     return ret != 0
184             ?   ret->Search_Name(i_rNodeFinder.Name2Search())
185             :   nret;
186 }
187 
188 template <class  FIND_NODE>
189 typename FIND_NODE::id_type
190 Search_SubTree_UpTillRoot( const ary::idl::Module &    i_rStart,
191                            const FIND_NODE &           i_rNodeFinder )
192 {
193     typename FIND_NODE::id_type
194         ret(0);
195     for ( const ary::idl::Module * start = &i_rStart;
196           start != 0 AND NOT ret.IsValid();
197           start = i_rNodeFinder(start->Owner()) )
198     {
199         ret = Search_SubTree( *start,
200                               i_rNodeFinder );
201     }
202     return ret;
203 }
204 // END Hack for SunPro 5.2 compiler bug.
205 
206 
207 
208 
209 }   // namespace ary
210 #endif
211