xref: /AOO41X/main/autodoc/source/display/toolkit/out_node.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 #include <precomp.h>
29 #include <toolkit/out_node.hxx>
30 
31 
32 // NOT FULLY DEFINED SERVICES
33 #include <algorithm>
34 
35 
36 namespace output
37 {
38 
39 
40 namespace
41 {
42 
43 struct Less_NodePtr
44 {
45     bool                operator()(
46                             Node *              p1,
47                             Node *              p2 ) const
48                         { return p1->Name() < p2->Name(); }
49 };
50 
51 struct Less_NodePtr     C_Less_NodePtr;
52 
53 
54 Node  C_aNullNode(Node::null_object);
55 
56 
57 }   // namepace anonymous
58 
59 
60 //**********************        Node        ***************************//
61 
62 
63 Node::Node()
64     :   sName(),
65   	    pParent(0),
66   	    aChildren(),
67   	    nDepth(0),
68   	    nNameRoomId(0)
69 {
70 }
71 
72 Node::Node( E_NullObject )
73     :   sName(),
74   	    pParent(0),
75   	    aChildren(),
76   	    nDepth(-1),
77   	    nNameRoomId(0)
78 {
79 }
80 
81 Node::Node( const String &  i_name,
82             Node &          i_parent )
83     :   sName(i_name),
84         pParent(&i_parent),
85         aChildren(),
86         nDepth(i_parent.Depth()+1),
87   	    nNameRoomId(0)
88 {
89 }
90 
91 Node::~Node()
92 {
93     for ( List::iterator it = aChildren.begin();
94           it != aChildren.end();
95           ++it )
96     {
97         delete *it;
98     }
99 }
100 
101 Node &
102 Node::Provide_Child( const String & i_name )
103 {
104     Node *
105         ret = find_Child(i_name);
106     if (ret != 0)
107         return *ret;
108     return add_Child(i_name);
109 }
110 
111 void
112 Node::Get_Path( StreamStr &         o_result,
113                 intt                i_maxDepth ) const
114 {
115     // Intentionally 'i_maxDepth != 0', so max_Depth == -1 sets no limit:
116     if (i_maxDepth != 0)
117     {
118         if (pParent != 0)
119             pParent->Get_Path(o_result, i_maxDepth-1);
120         o_result << sName << '/';
121     }
122 }
123 
124 void
125 Node::Get_Chain( StringVector & o_result,
126 				 intt           i_maxDepth ) const
127 {
128     if (i_maxDepth != 0)
129     {
130         // This is called also for the toplevel Node,
131         //   but there happens nothing:
132         if (pParent != 0)
133         {
134             pParent->Get_Chain(o_result, i_maxDepth-1);
135             o_result.push_back(sName);
136         }
137     }
138 }
139 
140 Node *
141 Node::find_Child( const String & i_name )
142 {
143     Node aSearch;
144     aSearch.sName = i_name;
145 
146     List::const_iterator
147         ret = std::lower_bound( aChildren.begin(),
148 							    aChildren.end(),
149                                 &aSearch,
150 						        C_Less_NodePtr );
151     if ( ret != aChildren.end() ? (*ret)->Name() == i_name : false )
152         return *ret;
153 
154     return 0;
155 }
156 
157 Node &
158 Node::add_Child( const String & i_name )
159 {
160     DYN Node *
161         pNew = new Node(i_name,*this);
162     aChildren.insert( std::lower_bound( aChildren.begin(),
163 										aChildren.end(),
164                                         pNew,
165 										C_Less_NodePtr ),
166 			          pNew );
167     return *pNew;
168 }
169 
170 Node &
171 Node::provide_Child( StringVector::const_iterator i_next,
172   	                 StringVector::const_iterator i_end )
173 {
174     if (i_next == i_end)
175         return *this;
176     return Provide_Child(*i_next).provide_Child(i_next+1,i_end);
177 }
178 
179 
180 
181 
182 Node &
183 Node::Null_()
184 {
185     return C_aNullNode;
186 }
187 
188 
189 }   // namespace output
190