MongoType
MongoDB Collection Data Dump with BSON Types
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BSONDotNotationDump.hpp
Go to the documentation of this file.
1 
28 //----------------------------------------------------------------------------
29 
30 #ifndef BSONDOTNOTATIONDUMP_HPP_
31 #define BSONDOTNOTATIONDUMP_HPP_
32 
33 #include <mongotype.hpp>
34 #include <Parameters.hpp>
35 #include <IBSONRenderer.hpp>
36 #include <BSONTypeFormatter.hpp>
37 #include <BSONObjectParser.hpp>
38 
39 namespace mongotype {
40 
51 class BSONDotNotationDump : virtual public IBSONRenderer, virtual protected IBSONObjectVisitor {
53  deque<string> dotStack;
54  function<ostream&()> getOStream; // std::function required to store a closure.
55 
56 protected: // IBSONObjectVisitor overrides.
57 
58  virtual void onParseStart() { }
59 
60  virtual void onParseEnd() { }
61 
62  virtual void onObjectStart(const BSONParserStack& stack) {
63  if (stack.top().getArrayIndex() >= 0) { // If the object is an element of an array...
64  string s("[");
65  s += to_string(stack.top().getArrayIndex());
66  s += "]"; // Output an array index first.
67  dotStack.push_back(s);
68  }
69  }
70 
71  virtual void onObjectEnd(const BSONParserStack& stack) {
72  if (stack.top().getArrayIndex() >= 0) { // If the object is an element of an array...
73  dotStack.pop_back();
74  }
75  }
76 
77  virtual void onArrayStart(const BSONParserStack& stack) {
78  string s(".");
79  s += stack.top().getArray().fieldName();
80  dotStack.push_back(s);
81  }
82 
83  virtual void onArrayEnd(const BSONParserStack& stack) {
84  dotStack.pop_back();
85  }
86 
87  virtual void onElement(const BSONParserStack& stack) {
88  const BSONElement& element = stack.top().getElement();
89  BSONTypeFormatter type(params, element);
90  string acc;
91  for (string s: dotStack) {
92  acc += s;
93  }
94  acc += '.';
95  acc += string(element);
96  acc += " ";
97  acc += type.to_string();
98  getOStream() << acc << "\n";
99  }
100 
101 public: // User Interface
102 
108  BSONDotNotationDump(Parameters& pparams, string& initialToken) :
109  params(pparams) {
110  dotStack.clear();
111  dotStack.push_back(initialToken);
112  };
113 
114  virtual ~BSONDotNotationDump() {};
115 
116  /*
117  * \param[in] os The output stream to which the object(s) are rendered.
118  */
119  virtual void setOutputStream(std::ostream& os) {
120  getOStream = [&] () -> ostream& { return os; }; // Wrap closure around ostream.
121  }
122 
123  /*
124  * \param[in] prefix The string to be output before the object is rendered, or NULL.
125  */
126  virtual void begin(const char* prefix) {
127  if (prefix != NULL) {
128  getOStream() << prefix;
129  }
130  }
131 
132  /*
133  * \param[in] suffix The string to be output after the object is rendered, or NULL.
134  */
135  virtual void end(const char* suffix) {
136  if (suffix != NULL) {
137  getOStream() << suffix;
138  }
139  }
140 
141  virtual void render(const BSONObj& object, int docIndex, int docCount) {
142  getOStream() << "\n";
143  BSONObjectParser objectParser(*this); // Construct a parser around this event handler.
144  objectParser.parse(object); // Parse the object and write the text output the the output stream.
145  }
146 };
147 
148 } /* namespace mongotype */
149 #endif /* BSONDOTNOTATIONDUMP_HPP_ */