1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 #ifndef INCLUDED_OOX_MATHML_IMPORTUTILS_HXX
10 #define INCLUDED_OOX_MATHML_IMPORTUTILS_HXX
12 #include <com/sun/star/xml/sax/XFastAttributeList.hpp>
13 #include <oox/token/tokens.hxx>
17 #include <oox/dllapi.h>
22 namespace formulaimport
25 // used to differentiate between tags that opening or closing
26 const int TAG_OPENING
= 1 << 29;
27 const int TAG_CLOSING
= 1 << 30;
29 // you probably want to #define these to something shorter in the .cxx file,
30 // but they must be done as macros, otherwise they wouldn't be usable for case values,
31 // and macros cannot be namespaced
32 #define XML_STREAM_OPENING( token ) ( TAG_OPENING | token )
33 #define XML_STREAM_CLOSING( token ) ( TAG_CLOSING | token )
36 Class for storing a stream of xml tokens.
38 A part of an XML file can be parsed and stored in this stream, from which it can be read
39 as if parsed linearly. The purpose of this class is to allow simpler handling of XML
40 files, unlike the usual LO way of using callbacks, context handlers and similar needlesly
41 complicated stuff (YMMV).
43 The advantages of this approach is easy to read and debug code (as it is just functions
44 reading tokens one by one and calling other functions, compared to having to use callbacks
45 and temporary storage). The disadvantage is that the XML structure needs to be handled
48 Note that tag identifiers are simply int values and the API does not care besides matching
49 their values to XML stream contents and requiring that the values are not as high as TAG_OPENING.
50 Be prepared for the fact that some of the functions may throw exceptions if the input
51 stream does not match the required token (TBD).
53 The API tries to make the common idioms as simple as possible, see the following examples.
55 Parse <tagone attr="value"><tagtwo>text</tagtwo></tagone> , where tagtwo is optional:
57 XmlStream::Tag tagoneTag = stream.ensureOpeningTag( tagone );
58 if( attributeTag.hasAttribute( attr ))
59 ... = attributeTag.attribute( attr, defaultValueOfTheRightType );
60 if( XmlStream::Tag tagtwoTag = stream.checkOpeningTag( tagtwo ))
63 stream.ensureClosingTag( tagtwo );
65 stream.ensureClosingTag( tagone );
68 Parse an element that may contain several sub-elements of different types in random order:
70 stream.ensureOpeningTag( element );
71 while( !stream.atEnd() && stream.currentToken() != CLOSING( element ))
73 switch( stream.currentToken())
75 case OPENING( subelement1 ):
78 case OPENING( subelement2 ):
79 ... process subelement2;
82 stream.handleUnexpectedTag();
85 stream.ensureClosingTag( element );
88 If there may not be a zero number of sub-elements, use a helper bool variable or use a do-while loop.
90 Parse an element that may contain an unknown number of sub-elements of the same type:
92 stream.ensureOpeningTag( element );
93 while( !stream.atEnd() && stream.findTag( OPENING( subelement )))
97 stream.ensureClosingTag( element );
100 If there may not be a zero number of sub-elements, use a helper bool variable or use a do-while loop.
104 class OOX_DLLPUBLIC XmlStream
109 Structure representing a list of attributes.
111 // One could theoretically use oox::AttributeList, but that complains if the passed reference is empty,
112 // which would be complicated to avoid here. Also, parsers apparently reuse the same instance of XFastAttributeList,
113 // which means using oox::AttributeList would make them all point to the one instance.
114 struct OOX_DLLPUBLIC AttributeList
116 bool hasAttribute( int token
) const;
117 OUString
& operator[] (int token
);
118 OUString
attribute( int token
, const OUString
& def
= OUString()) const;
119 bool attribute( int token
, bool def
) const;
120 sal_Unicode
attribute( int token
, sal_Unicode def
) const;
121 // when adding more attribute() overloads, add also to XmlStream itself
123 std::map
< int, OUString
> attrs
;
126 Structure representing a tag, including its attributes and content text immediatelly following it.
128 struct OOX_DLLPUBLIC Tag
130 Tag( int token
= XML_TOKEN_INVALID
,
131 const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XFastAttributeList
>& attributes
= com::sun::star::uno::Reference
< com::sun::star::xml::sax::XFastAttributeList
>(),
132 const OUString
& text
= OUString());
134 const AttributeList
& attribs
);
135 int token
; ///< tag type, or XML_TOKEN_INVALID
136 AttributeList attributes
;
139 This function returns value of the given attribute, or the passed default value if not found.
140 The type of the default value selects the return type (OUString here).
142 OUString
attribute( int token
, const OUString
& def
= OUString()) const;
146 bool attribute( int token
, bool def
) const;
150 sal_Unicode
attribute( int token
, sal_Unicode def
) const;
151 // when adding more attribute() overloads, add also to XmlStream::AttributeList and inline below
153 Converts to true if the tag has a valid token, false otherwise. Allows simple
154 usage in if(), for example 'if( XmlStream::Tag foo = stream.checkOpeningTag( footoken ))'.
156 operator bool() const;
159 @return true if current position is at the end of the XML stream
163 @return data about the current tag
165 Tag
currentTag() const;
167 @return the token for the current tag
169 int currentToken() const;
171 Moves position to the next tag.
173 void moveToNextTag();
175 Ensures that an opening tag with the given token is read. If the current tag does not match,
176 writes out a warning and tries to recover by skipping tags until found (or until the current element would end).
177 If found, the position in the stream is afterwards moved to the next tag.
178 @return the matching found opening tag, or empty tag if not found
180 Tag
ensureOpeningTag( int token
);
182 Tries to find an opening tag with the given token. Works similarly like ensureOpeningTag(),
183 but if a matching tag is not found, the position in the stream is not altered. The primary
184 use of this function is to check for optional elements.
185 @return the matching found opening tag, or empty tag if not found
187 Tag
checkOpeningTag( int token
);
189 Ensures that a closing tag with the given token is read. Like ensureOpeningTag(),
190 if not, writes out a warning and tries to recover by skiping tags until found (or until the current element would end).
191 If found, the position in the stream is afterwards moved to the next tag.
193 void ensureClosingTag( int token
);
195 Tries to find the given token, until either found (returns true) or end of current element.
196 Position in the stream is set to make the tag current (i.e. it will be the next one read).
198 bool findTag( int token
);
200 Handle the current (unexpected) tag.
202 void handleUnexpectedTag();
204 Tag
checkTag( int token
, bool optional
);
205 bool findTagInternal( int token
, bool silent
);
206 void skipElementInternal( int token
, bool silent
);
207 std::vector
< Tag
> tags
;
212 This class is used for creating XmlStream.
214 Simply use this class and then pass it as XmlStream to the consumer.
218 class OOX_DLLPUBLIC XmlStreamBuilder
222 void appendOpeningTag( int token
,
223 const com::sun::star::uno::Reference
< com::sun::star::xml::sax::XFastAttributeList
>& attributes
= com::sun::star::uno::Reference
< com::sun::star::xml::sax::XFastAttributeList
>());
224 void appendOpeningTag( int token
,
225 const AttributeList
& attribs
);
226 void appendClosingTag( int token
);
227 // appends the characters after the last appended token
228 void appendCharacters( const OUString
& characters
);
232 OUString
XmlStream::Tag::attribute( int t
, const OUString
& def
) const
234 return attributes
.attribute( t
, def
);
238 bool XmlStream::Tag::attribute( int t
, bool def
) const
240 return attributes
.attribute( t
, def
);
244 sal_Unicode
XmlStream::Tag::attribute( int t
, sal_Unicode def
) const
246 return attributes
.attribute( t
, def
);
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */