Update ooo320-m1
[ooovba.git] / writerfilter / inc / resourcemodel / OutputWithDepth.hxx
blobc1ed9562ca79fc444a8f92f34d9cd6150c81e823
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OutputWithDepth.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_OUTPUT_WITH_DEPTH
32 #define INCLUDED_OUTPUT_WITH_DEPTH
34 #include <vector>
35 #include <iostream>
37 namespace writerfilter
40 using namespace ::std;
42 template <typename T>
43 class OutputWithDepth
45 typedef ::std::vector<T> Group_t;
46 Group_t mGroup;
48 unsigned int mnCurrentDepth;
49 unsigned int mnGroupDepth;
51 T mOpenTag;
52 T mCloseTag;
54 protected:
55 virtual void output(const T & aItem) const = 0;
56 void outputGroup();
57 void finalize();
59 public:
60 OutputWithDepth(const T & aOpenTag, const T & aCloseTag);
61 virtual ~OutputWithDepth();
63 void openGroup();
64 void closeGroup();
65 void addItem(const T & aItem);
66 void setDepth(unsigned int nDepth);
69 template <typename T>
70 OutputWithDepth<T>::OutputWithDepth(const T & aOpenTag, const T & aEndTag)
71 : mOpenTag(aOpenTag), mCloseTag(aEndTag)
75 template <typename T>
76 OutputWithDepth<T>::~OutputWithDepth()
80 template <typename T>
81 void OutputWithDepth<T>::finalize()
83 outputGroup();
86 template <typename T>
87 void OutputWithDepth<T>::openGroup()
89 outputGroup();
90 mnGroupDepth = 0;
93 template <typename T>
94 void OutputWithDepth<T>::closeGroup()
96 if (mnGroupDepth > mnCurrentDepth)
97 for (unsigned int i = 0; i < mnGroupDepth - mnCurrentDepth; ++i)
98 output(mOpenTag);
99 else if (mnGroupDepth < mnCurrentDepth)
100 for (unsigned int i = 0; i < mnCurrentDepth - mnGroupDepth; ++i)
101 output(mCloseTag);
103 outputGroup();
105 mnCurrentDepth = mnGroupDepth;
108 template <typename T>
109 void OutputWithDepth<T>::addItem(const T & aItem)
111 mGroup.push_back(aItem);
114 template <typename T>
115 void OutputWithDepth<T>::setDepth(unsigned int nDepth)
117 mnGroupDepth = nDepth;
120 template <typename T>
121 void OutputWithDepth<T>::outputGroup()
123 typename Group_t::iterator aItEnd = mGroup.end();
125 for (typename Group_t::iterator aIt = mGroup.begin(); aIt != aItEnd; aIt++)
127 output(*aIt);
130 mGroup.clear();
133 #endif // INCLUDED_OUTPUT_WITH_DEPTH