1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_OUTPUT_WITH_DEPTH
25 #define INCLUDED_OUTPUT_WITH_DEPTH
26 
27 #include <vector>
28 #include <iostream>
29 
30 namespace writerfilter
31 {
32 
33 using namespace ::std;
34 
35 template <typename T>
36 class OutputWithDepth
37 {
38     typedef ::std::vector<T> Group_t;
39     Group_t mGroup;
40 
41     unsigned int mnCurrentDepth;
42     unsigned int mnGroupDepth;
43 
44     T mOpenTag;
45     T mCloseTag;
46 
47 protected:
48     virtual void output(const T & aItem) const = 0;
49     void outputGroup();
50     void finalize();
51 
52 public:
53     OutputWithDepth(const T & aOpenTag, const T & aCloseTag);
54     virtual ~OutputWithDepth();
55 
56     void openGroup();
57     void closeGroup();
58     void addItem(const T & aItem);
59     void setDepth(unsigned int nDepth);
60 };
61 
62 template <typename T>
OutputWithDepth(const T & aOpenTag,const T & aEndTag)63 OutputWithDepth<T>::OutputWithDepth(const T & aOpenTag, const T & aEndTag)
64 : mOpenTag(aOpenTag), mCloseTag(aEndTag)
65 {
66 }
67 
68 template <typename T>
~OutputWithDepth()69 OutputWithDepth<T>::~OutputWithDepth()
70 {
71 }
72 
73 template <typename T>
finalize()74 void OutputWithDepth<T>::finalize()
75 {
76     outputGroup();
77 }
78 
79 template <typename T>
openGroup()80 void OutputWithDepth<T>::openGroup()
81 {
82     outputGroup();
83     mnGroupDepth = 0;
84 }
85 
86 template <typename T>
closeGroup()87 void OutputWithDepth<T>::closeGroup()
88 {
89     if (mnGroupDepth > mnCurrentDepth)
90         for (unsigned int i = 0; i < mnGroupDepth - mnCurrentDepth; ++i)
91             output(mOpenTag);
92     else if (mnGroupDepth < mnCurrentDepth)
93         for (unsigned int i = 0; i < mnCurrentDepth - mnGroupDepth; ++i)
94             output(mCloseTag);
95 
96     outputGroup();
97 
98     mnCurrentDepth = mnGroupDepth;
99 }
100 
101 template <typename T>
addItem(const T & aItem)102 void OutputWithDepth<T>::addItem(const T & aItem)
103 {
104     mGroup.push_back(aItem);
105 }
106 
107 template <typename T>
setDepth(unsigned int nDepth)108 void OutputWithDepth<T>::setDepth(unsigned int nDepth)
109 {
110     mnGroupDepth = nDepth;
111 }
112 
113 template <typename T>
outputGroup()114 void OutputWithDepth<T>::outputGroup()
115 {
116     typename Group_t::iterator aItEnd = mGroup.end();
117 
118     for (typename Group_t::iterator aIt = mGroup.begin(); aIt != aItEnd; aIt++)
119     {
120         output(*aIt);
121     }
122 
123     mGroup.clear();
124 }
125 }
126 #endif // INCLUDED_OUTPUT_WITH_DEPTH
127