bump product version to 4.1.6.2
[LibreOffice.git] / oox / source / mathml / importutils.cxx
blobafa164b886c897a8a943155f06af86f82449eb1d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include "oox/mathml/importutils.hxx"
12 #include <assert.h>
13 #include <stdio.h>
15 #include <oox/token/namespacemap.hxx>
16 #include <oox/token/tokenmap.hxx>
17 #include <oox/token/tokens.hxx>
18 #include <oox/token/namespaces.hxx>
19 #include <rtl/ustring.hxx>
21 #define OPENING( token ) XML_STREAM_OPENING( token )
22 #define CLOSING( token ) XML_STREAM_CLOSING( token )
24 using namespace com::sun::star;
26 namespace oox
29 namespace formulaimport
32 namespace
34 // a class that inherits from AttributeList, builds the internal data and then will be sliced off
35 // during conversion to the base class
36 class AttributeListBuilder
37 : public XmlStream::AttributeList
39 public:
40 AttributeListBuilder( const uno::Reference< xml::sax::XFastAttributeList >& a );
43 AttributeListBuilder::AttributeListBuilder( const uno::Reference< xml::sax::XFastAttributeList >& a )
45 if( a.get() == NULL )
46 return;
47 uno::Sequence< xml::FastAttribute > aFastAttrSeq = a->getFastAttributes();
48 const xml::FastAttribute* pFastAttr = aFastAttrSeq.getConstArray();
49 sal_Int32 nFastAttrLength = aFastAttrSeq.getLength();
50 for( int i = 0;
51 i < nFastAttrLength;
52 ++i )
54 attrs[ pFastAttr[ i ].Token ] = pFastAttr[ i ].Value;
58 static OUString tokenToString( int token )
60 OUString tokenname = StaticTokenMap::get().getUnicodeTokenName( token & TOKEN_MASK );
61 if( tokenname.isEmpty())
62 tokenname = "???";
63 int nmsp = ( token & NMSP_MASK & ~( TAG_OPENING | TAG_CLOSING ));
64 #if 0 // this is awfully long
65 OUString namespacename = StaticNamespaceMap::get().count( nmsp ) != 0
66 ? StaticNamespaceMap::get()[ nmsp ] : OUString( "???" );
67 #else
68 OUString namespacename;
69 // only few are needed actually
70 switch( nmsp )
72 case NMSP_officeMath:
73 namespacename = "m";
74 break;
75 case NMSP_doc:
76 namespacename = "w";
77 break;
78 default:
79 namespacename = "?";
80 break;
82 #endif
83 if( token == OPENING( token ))
84 return "<" + namespacename + ":" + tokenname + ">";
85 if( token == CLOSING( token ))
86 return "</" + namespacename + ":" + tokenname + ">";
87 // just the name itself, not specified whether opening or closing
88 return namespacename + ":" + tokenname;
91 } // namespace
93 OUString& XmlStream::AttributeList::operator[] (int token)
95 return attrs[token];
98 OUString XmlStream::AttributeList::attribute( int token, const OUString& def ) const
100 std::map< int, OUString >::const_iterator find = attrs.find( token );
101 if( find != attrs.end())
102 return find->second;
103 return def;
106 bool XmlStream::AttributeList::attribute( int token, bool def ) const
108 std::map< int, OUString >::const_iterator find = attrs.find( token );
109 if( find != attrs.end())
111 const OUString sValue = find->second;
112 if( sValue.equalsIgnoreAsciiCase("true") ||
113 sValue.equalsIgnoreAsciiCase("on") ||
114 sValue.equalsIgnoreAsciiCase("t") ||
115 sValue.equalsIgnoreAsciiCase("1") )
116 return true;
117 if( sValue.equalsIgnoreAsciiCase("false") ||
118 sValue.equalsIgnoreAsciiCase("off") ||
119 sValue.equalsIgnoreAsciiCase("f") ||
120 sValue.equalsIgnoreAsciiCase("0") )
121 return false;
122 SAL_WARN( "oox.xmlstream", "Cannot convert \'" << sValue << "\' to bool." );
124 return def;
127 sal_Unicode XmlStream::AttributeList::attribute( int token, sal_Unicode def ) const
129 std::map< int, OUString >::const_iterator find = attrs.find( token );
130 if( find != attrs.end())
132 if( !find->second.isEmpty() )
134 if( find->second.getLength() != 1 )
135 SAL_WARN( "oox.xmlstream", "Cannot convert \'" << find->second << "\' to sal_Unicode, stripping." );
136 return find->second[ 0 ];
139 return def;
142 XmlStream::Tag::Tag( int t, const uno::Reference< xml::sax::XFastAttributeList >& a, const OUString& txt )
143 : token( t )
144 , attributes( AttributeListBuilder( a ))
145 , text( txt )
149 XmlStream::Tag::Tag( int t, const AttributeList& a )
150 : token( t )
151 , attributes( a )
156 XmlStream::Tag::operator bool() const
158 return token != XML_TOKEN_INVALID;
161 XmlStream::XmlStream()
162 : pos( 0 )
164 // make sure our extra bit does not conflict with values used by oox
165 assert( TAG_OPENING > ( 1024 << NMSP_SHIFT ));
168 bool XmlStream::atEnd() const
170 return pos >= tags.size();
173 XmlStream::Tag XmlStream::currentTag() const
175 if( pos >= tags.size())
176 return Tag();
177 return tags[ pos ];
180 int XmlStream::currentToken() const
182 if( pos >= tags.size())
183 return XML_TOKEN_INVALID;
184 return tags[ pos ].token;
187 void XmlStream::moveToNextTag()
189 if( pos < tags.size())
190 ++pos;
193 XmlStream::Tag XmlStream::ensureOpeningTag( int token )
195 return checkTag( OPENING( token ), false );
198 XmlStream::Tag XmlStream::checkOpeningTag( int token )
200 return checkTag( OPENING( token ), true );
203 void XmlStream::ensureClosingTag( int token )
205 checkTag( CLOSING( token ), false );
208 XmlStream::Tag XmlStream::checkTag( int token, bool optional )
210 // either it's the following tag, or find it
211 int savedPos = pos;
212 if( optional )
213 { // avoid printing debug messages about skipping tags if the optional one
214 // will not be found and the position will be reset back
215 if( currentToken() != token && !findTagInternal( token, true ))
217 pos = savedPos;
218 return Tag();
221 if( currentToken() == token || findTag( token ))
223 Tag ret = currentTag();
224 moveToNextTag();
225 return ret; // ok
227 if( optional )
228 { // not a problem, just rewind
229 pos = savedPos;
230 return Tag();
232 SAL_WARN( "oox.xmlstream", "Expected tag " << tokenToString( token ) << " not found." );
233 return Tag();
236 bool XmlStream::findTag( int token )
238 return findTagInternal( token, false );
241 bool XmlStream::findTagInternal( int token, bool silent )
243 int depth = 0;
244 for(;
245 !atEnd();
246 moveToNextTag())
248 if( depth > 0 ) // we're inside a nested element, skip those
250 if( currentToken() == OPENING( currentToken()))
252 if( !silent )
253 SAL_INFO( "oox.xmlstream", "Skipping tag " << tokenToString( currentToken()));
254 ++depth;
256 else if( currentToken() == CLOSING( currentToken()))
258 if( !silent )
259 SAL_INFO( "oox.xmlstream", "Skipping tag " << tokenToString( currentToken()));
260 --depth;
262 else
264 if( !silent )
265 SAL_WARN( "oox.xmlstream", "Malformed token " << currentToken() << " ("
266 << tokenToString( currentToken()) << ")" );
267 abort();
269 continue;
271 if( currentToken() == token )
272 return true; // ok, found
273 if( currentToken() == CLOSING( currentToken()))
274 return false; // that would be leaving current element, so not found
275 if( currentToken() == OPENING( currentToken()))
277 if( !silent )
278 SAL_INFO( "oox.xmlstream", "Skipping tag " << tokenToString( currentToken()));
279 ++depth;
281 else
282 abort();
284 if( !silent )
285 SAL_WARN( "oox.xmlstream", "Unexpected end of stream reached." );
286 return false;
289 void XmlStream::skipElementInternal( int token, bool silent )
291 int closing = ( token & ~TAG_OPENING ) | TAG_CLOSING; // make it a closing tag
292 assert( currentToken() == OPENING( token ));
293 if( !silent )
294 SAL_INFO( "oox.xmlstream", "Skipping unexpected element " << tokenToString( currentToken()));
295 moveToNextTag();
296 // and just find the matching closing tag
297 if( findTag( closing ))
299 if( !silent )
300 SAL_INFO( "oox.xmlstream", "Skipped unexpected element " << tokenToString( token ));
301 moveToNextTag(); // and skip it too
302 return;
304 // this one is an unexpected problem, do not silent it
305 SAL_WARN( "oox.xmlstream", "Expected end of element " << tokenToString( token ) << " not found." );
308 void XmlStream::handleUnexpectedTag()
310 if( atEnd())
311 return;
312 if( currentToken() == CLOSING( currentToken()))
314 SAL_INFO( "oox.xmlstream", "Skipping unexpected tag " << tokenToString( currentToken()));
315 moveToNextTag(); // just skip it
316 return;
318 skipElementInternal( currentToken(), false ); // otherwise skip the entire element
322 void XmlStreamBuilder::appendOpeningTag( int token, const uno::Reference< xml::sax::XFastAttributeList >& attrs )
324 tags.push_back( Tag( OPENING( token ), attrs ));
327 void XmlStreamBuilder::appendOpeningTag( int token, const AttributeList& attrs )
329 tags.push_back( Tag( OPENING( token ), attrs ));
332 void XmlStreamBuilder::appendClosingTag( int token )
334 tags.push_back( Tag( CLOSING( token )));
337 void XmlStreamBuilder::appendCharacters( const OUString& chars )
339 assert( !tags.empty());
340 tags.back().text += chars;
343 } // namespace
344 } // namespace
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */