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