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
38namespace pwiz {
39namespace 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
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.
143PWIZ_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.
149PWIZ_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//
161template<typename Ch>
163public:
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 }
194private:
195 boost::iostreams::stream_offset chars_;
196};
197BOOST_IOSTREAMS_PIPABLE(basic_charcounter, 1)
198
199
202
203} // namespace minimxml
204} // namespace pwiz
205
206
207#endif // _XMLWRITER_HPP_
208
#define PWIZ_API_DECL
Definition Export.hpp:32
StyleFlag_InlineInner
Definition XMLWriter.hpp:54
StyleFlag_InlineOuter
Definition XMLWriter.hpp:55
StyleFlag_Inline
Definition XMLWriter.hpp:56
vector of name/value pairs to be written as XML attributes
Definition XMLWriter.hpp:82
void add(const std::string &name, const int &value)
void add(const std::string &name, const T &value)
Definition XMLWriter.hpp:88
void add(const std::string &name, const double &value)
interface to allow outside observation of data sent to output stream
Definition XMLWriter.hpp:62
virtual void update(const std::string &output)=0
The XMLWriter class provides simple, tag-level XML syntax writing.
Definition XMLWriter.hpp:48
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...
void startElement(const std::string &name, const Attributes &attributes=Attributes(), EmptyElementTag emptyElementTag=NotEmptyElement)
writes element start tag
void endElement()
writes element end tag
stream_offset positionNext() const
returns stream position of next element start tag
void popStyle()
pops the style stack
boost::iostreams::stream_offset stream_offset
XMLWriter & operator=(const XMLWriter &)
XMLWriter(std::ostream &os, const Config &config=Config())
constructor
void processingInstruction(const std::string &name, const std::string &data)
writes a processing instruction
boost::shared_ptr< Impl > impl_
EmptyElementTag
tag for indicating an empty element
XMLWriter(const XMLWriter &)
stream_offset position() const
returns current stream position
void pushStyle(unsigned int flags)
pushes style flags onto the internal style stack
basic_charcounter(int first_char=0)
boost::iostreams::stream_offset chars_
boost::iostreams::stream_offset characters() const
std::streamsize optimal_buffer_size() const
std::streamsize write(Sink &snk, const char_type *s, std::streamsize n)
std::streamsize read(Source &src, char_type *s, std::streamsize n)
basic_charcounter< char > charcounter
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_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,...
basic_charcounter< wchar_t > wcharcounter
initial configuration of the XMLWriter
Definition XMLWriter.hpp:70