ProteoWizard
XMLWriter.hpp
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2007 Spielberg Family Center for Applied Proteomics
8 // Cedars-Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #ifndef _XMLWRITER_HPP_
25 #define _XMLWRITER_HPP_
26 
27 
30 #include "boost/shared_ptr.hpp"
31 #include "boost/iostreams/positioning.hpp"
32 #include "boost/iostreams/filter/counter.hpp"
33 #include <iosfwd>
34 #include <string>
35 #include <vector>
36 
37 
38 namespace pwiz {
39 namespace minimxml {
40 
41 
42 ///
43 /// The XMLWriter class provides simple, tag-level XML syntax writing.
44 /// Internally, XMLWriter keeps a style stack (for client customization
45 /// of the XML style) and an element stack (for element nesting/indentation).
46 ///
48 {
49  public:
50 
51  /// flags to control the XML writing style
52  enum PWIZ_API_DECL StyleFlag
53  {
54  StyleFlag_InlineInner = 0x01, // no whitespace within an element
55  StyleFlag_InlineOuter = 0x02, // no whitespace around an element
57  StyleFlag_AttributesOnMultipleLines = 0x04
58  };
59 
60  /// interface to allow outside observation of data sent to output stream
62  {
63  public:
64  virtual void update(const std::string& output) = 0;
65  virtual ~OutputObserver(){}
66  };
67 
68  /// initial configuration of the XMLWriter
70  {
71  unsigned int initialStyle;
72  unsigned int indentationStep;
74 
76  : initialStyle(0), indentationStep(2), outputObserver(0)
77  {}
78  };
79 
80  /// vector of name/value pairs to be written as XML attributes
81  class PWIZ_API_DECL Attributes : public std::vector< std::pair<std::string,std::string> >
82  {
83  public:
84  void add(const std::string& name, const double& value);
85  void add(const std::string& name, const int& value);
86 
87  template <typename T>
88  inline void add(const std::string& name, const T& value)
89  {
90  push_back(make_pair(name, boost::lexical_cast<std::string>(value)));
91  }
92  };
93 
94  /// constructor
95  XMLWriter(std::ostream& os, const Config& config = Config());
96  virtual ~XMLWriter() {}
97 
98  /// pushes style flags onto the internal style stack
99  void pushStyle(unsigned int flags);
100 
101  /// pops the style stack
102  void popStyle();
103 
104  /// writes a processing instruction
105  void processingInstruction(const std::string& name, const std::string& data);
106 
107  /// tag for indicating an empty element
108  enum EmptyElementTag {NotEmptyElement, EmptyElement};
109 
110  /// writes element start tag
111  void startElement(const std::string& name,
112  const Attributes& attributes = Attributes(),
113  EmptyElementTag emptyElementTag = NotEmptyElement);
114 
115  /// writes element end tag
116  void endElement();
117 
118  /// writes character data;
119  /// autoEscape writes reserved XML characters in the input text in their escaped form
120  /// '&', '<', and '>' are '&amp;', '&lt;', '&gt;' respectively
121  void characters(const std::string& text, bool autoEscape = true);
122 
123  typedef boost::iostreams::stream_offset stream_offset;
124 
125  /// returns current stream position
127 
128  /// returns stream position of next element start tag
130 
131 
132  private:
133  class Impl;
134  boost::shared_ptr<Impl> impl_;
137 };
138 
139 
140 /// Encodes any characters not suitable in an xml:ID or xml:IDREF
141 /// with their hexadecimal value, e.g. " " encodes as "_x0020_"
142 /// This override modifies the input string in place and returns its reference.
143 PWIZ_API_DECL std::string& encode_xml_id(std::string& str);
144 
145 
146 /// Encodes any characters not suitable in an xml:ID or xml:IDREF
147 /// with their hexadecimal value, e.g. " " encodes as "_x0020_"
148 /// This override modifies and returns a copy of the input string.
149 PWIZ_API_DECL std::string encode_xml_id_copy(const std::string& str);
150 
151 
152 //
153 // Template name: basic_charcounter.
154 // Template paramters:
155 // Ch - The character type.
156 // Description: Filter which counts characters.
157 // Based on boost's basic_counter, but
158 // without the line counting, and couting using
159 // stream_offset instead of int
160 //
161 template<typename Ch>
163 public:
164  typedef Ch char_type;
165  struct category
166  : boost::iostreams::dual_use,
167  boost::iostreams::filter_tag,
168  boost::iostreams::multichar_tag,
169  boost::iostreams::optimally_buffered_tag
170  { };
171  explicit basic_charcounter(int first_char = 0)
172  : chars_(first_char)
173  { }
174  boost::iostreams::stream_offset characters() const { return chars_; }
175  std::streamsize optimal_buffer_size() const { return 0; }
176 
177  template<typename Source>
178  std::streamsize read(Source& src, char_type* s, std::streamsize n)
179  {
180  std::streamsize result = boost::iostreams::read(src, s, n);
181  if (result == -1)
182  return -1;
183  chars_ += result;
184  return result;
185  }
186 
187  template<typename Sink>
188  std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
189  {
190  std::streamsize result = boost::iostreams::write(snk, s, n);
191  chars_ += result;
192  return result;
193  }
194 private:
195  boost::iostreams::stream_offset chars_;
196 };
197 BOOST_IOSTREAMS_PIPABLE(basic_charcounter, 1)
198 
199 
202 
203 } // namespace minimxml
204 } // namespace pwiz
205 
206 
207 #endif // _XMLWRITER_HPP_
208 
pwiz::minimxml::XMLWriter::processingInstruction
void processingInstruction(const std::string &name, const std::string &data)
writes a processing instruction
pwiz::minimxml::XMLWriter
The XMLWriter class provides simple, tag-level XML syntax writing.
Definition: XMLWriter.hpp:48
pwiz::minimxml::basic_charcounter::read
std::streamsize read(Source &src, char_type *s, std::streamsize n)
Definition: XMLWriter.hpp:178
pwiz::minimxml::basic_charcounter::chars_
boost::iostreams::stream_offset chars_
Definition: XMLWriter.hpp:195
pwiz::minimxml::XMLWriter::Attributes
vector of name/value pairs to be written as XML attributes
Definition: XMLWriter.hpp:82
pwiz
Definition: ChromatogramList_Filter.hpp:36
pwiz::minimxml::XMLWriter::OutputObserver::~OutputObserver
virtual ~OutputObserver()
Definition: XMLWriter.hpp:65
pwiz::minimxml::basic_charcounter::category
Definition: XMLWriter.hpp:170
pwiz::minimxml::basic_charcounter::characters
boost::iostreams::stream_offset characters() const
Definition: XMLWriter.hpp:174
pwiz::minimxml::XMLWriter::position
stream_offset position() const
returns current stream position
StyleFlag_InlineOuter
StyleFlag_InlineOuter
Definition: XMLWriter.hpp:55
pwiz::msdata::id::value
PWIZ_API_DECL std::string value(const std::string &id, const std::string &name)
convenience function to extract a named value from an id string
pwiz::minimxml::XMLWriter::Config
initial configuration of the XMLWriter
Definition: XMLWriter.hpp:70
pwiz::minimxml::XMLWriter::Config::Config
Config()
Definition: XMLWriter.hpp:75
pwiz::minimxml::XMLWriter::Attributes::add
void add(const std::string &name, const double &value)
pwiz::minimxml::XMLWriter::characters
void characters(const std::string &text, bool autoEscape=true)
writes character data; autoEscape writes reserved XML characters in the input text in their escaped f...
PWIZ_API_DECL
#define PWIZ_API_DECL
Definition: Export.hpp:32
optimized_lexical_cast.hpp
pwiz::minimxml::XMLWriter::Config::outputObserver
OutputObserver * outputObserver
Definition: XMLWriter.hpp:73
pwiz::identdata::IO::write
PWIZ_API_DECL void write(minimxml::XMLWriter &writer, const CV &cv)
pwiz::minimxml::XMLWriter::NotEmptyElement
@ NotEmptyElement
Definition: XMLWriter.hpp:108
Export.hpp
pwiz::minimxml::XMLWriter::Attributes::add
void add(const std::string &name, const T &value)
Definition: XMLWriter.hpp:88
pwiz::minimxml::XMLWriter::positionNext
stream_offset positionNext() const
returns stream position of next element start tag
pwiz::minimxml::XMLWriter::OutputObserver::update
virtual void update(const std::string &output)=0
pwiz::minimxml::encode_xml_id_copy
PWIZ_API_DECL std::string encode_xml_id_copy(const std::string &str)
Encodes any characters not suitable in an xml:ID or xml:IDREF with their hexadecimal value,...
pwiz::minimxml::XMLWriter::impl_
boost::shared_ptr< Impl > impl_
Definition: XMLWriter.hpp:133
pwiz::minimxml::XMLWriter::Config::initialStyle
unsigned int initialStyle
Definition: XMLWriter.hpp:71
pwiz::minimxml::basic_charcounter::write
std::streamsize write(Sink &snk, const char_type *s, std::streamsize n)
Definition: XMLWriter.hpp:188
pwiz::minimxml::XMLWriter::XMLWriter
XMLWriter(const XMLWriter &)
pwiz::minimxml::XMLWriter::EmptyElementTag
EmptyElementTag
tag for indicating an empty element
Definition: XMLWriter.hpp:108
pwiz::minimxml::XMLWriter::popStyle
void popStyle()
pops the style stack
pwiz::minimxml::basic_charcounter::char_type
Ch char_type
Definition: XMLWriter.hpp:164
StyleFlag_Inline
StyleFlag_Inline
Definition: XMLWriter.hpp:56
pwiz::minimxml::basic_charcounter::basic_charcounter
basic_charcounter(int first_char=0)
Definition: XMLWriter.hpp:171
pwiz::minimxml::encode_xml_id
PWIZ_API_DECL std::string & encode_xml_id(std::string &str)
Encodes any characters not suitable in an xml:ID or xml:IDREF with their hexadecimal value,...
pwiz::minimxml::XMLWriter::endElement
void endElement()
writes element end tag
pwiz::minimxml::XMLWriter::startElement
void startElement(const std::string &name, const Attributes &attributes=Attributes(), EmptyElementTag emptyElementTag=NotEmptyElement)
writes element start tag
pwiz::minimxml::XMLWriter::~XMLWriter
virtual ~XMLWriter()
Definition: XMLWriter.hpp:96
pwiz::minimxml::XMLWriter::pushStyle
void pushStyle(unsigned int flags)
pushes style flags onto the internal style stack
pwiz::minimxml::XMLWriter::Config::indentationStep
unsigned int indentationStep
Definition: XMLWriter.hpp:72
pwiz::minimxml::XMLWriter::operator=
XMLWriter & operator=(const XMLWriter &)
pwiz::minimxml::XMLWriter::stream_offset
boost::iostreams::stream_offset stream_offset
Definition: XMLWriter.hpp:123
pwiz::minimxml::basic_charcounter
Definition: XMLWriter.hpp:162
pwiz::minimxml::XMLWriter::OutputObserver
interface to allow outside observation of data sent to output stream
Definition: XMLWriter.hpp:62
pwiz::identdata::IO::read
PWIZ_API_DECL void read(std::istream &is, CV &cv)
pwiz::minimxml::basic_charcounter::optimal_buffer_size
std::streamsize optimal_buffer_size() const
Definition: XMLWriter.hpp:175
pwiz::minimxml::XMLWriter::XMLWriter
XMLWriter(std::ostream &os, const Config &config=Config())
constructor
pwiz::minimxml::XMLWriter::Attributes::add
void add(const std::string &name, const int &value)
StyleFlag_InlineInner
StyleFlag_InlineInner
Definition: XMLWriter.hpp:54