From 5b94957f7f25c8a607735d038c4f22e8009b99ea Mon Sep 17 00:00:00 2001 From: Luboš Luňák Date: Thu, 24 Nov 2011 13:19:57 +0100 Subject: more api improving in importing mathml docx --- oox/inc/oox/mathml/importutils.hxx | 39 ++++++++++++++ oox/source/mathml/importutils.cxx | 107 +++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) (limited to 'oox') diff --git a/oox/inc/oox/mathml/importutils.hxx b/oox/inc/oox/mathml/importutils.hxx index f7c353da9887..eb7af2be09b0 100644 --- a/oox/inc/oox/mathml/importutils.hxx +++ b/oox/inc/oox/mathml/importutils.hxx @@ -75,6 +75,11 @@ public: int token; ///< tag type, or XML_TOKEN_INVALID AttributeList attributes; rtl::OUString text; + /** + Converts to true if the tag has a valid token, false otherwise. Allows simple + usage in if(), for example 'if( XmlStream::Tag foo = stream.checkOpeningTag( footoken ))'. + */ + operator bool() const; }; /** @return true if current position is at the end of the XML stream @@ -92,7 +97,41 @@ public: Moves position to the next tag. */ void moveToNextTag(); + /** + Ensures that an opening tag with the given token is read. If the current tag does not match, + writes out a warning and tries to recover by skipping tags until found (or until the current element would end). + If found, the position in the stream is afterwards moved to the next tag. + @return the matching found opening tag, or empty tag if not found + */ + Tag ensureOpeningTag( int token ); + /** + Tries to find an opening tag with the given token. Works similarly like ensureOpeningTag(), + but if a matching tag is not found, the position in the stream is not altered. The primary + use of this function is to check for optional elements. + @return the matching found opening tag, or empty tag if not found + */ + Tag checkOpeningTag( int token ); + /** + Ensures that a closing tag with the given token is read. Like ensureOpeningTag(), + if not, writes out a warning and tries to recover by skiping tags until found (or until the current element would end). + If found, the position in the stream is afterwards moved to the next tag. + */ + void ensureClosingTag( int token ); + /** + Tries to find the given token, until either found (returns true) or end of current element. + Position in the stream is set to make the tag current. + */ + bool recoverAndFindTag( int token ); + /** + Skips the given element (i.e. reads up to and including the matching closing tag). + */ + void skipElement( int token ); + /** + Handle the current (unexpected) tag. + */ + void handleUnexpectedTag(); protected: + Tag checkTag( int token, bool optional, const char* txt ); std::vector< Tag > tags; unsigned int pos; }; diff --git a/oox/source/mathml/importutils.cxx b/oox/source/mathml/importutils.cxx index 95f81c802e83..ac25b89f6bd3 100644 --- a/oox/source/mathml/importutils.cxx +++ b/oox/source/mathml/importutils.cxx @@ -40,6 +40,11 @@ namespace oox namespace formulaimport { +XmlStream::XmlStream::Tag::operator bool() const +{ + return token != XML_TOKEN_INVALID; +} + XmlStream::XmlStream() : pos( 0 ) { @@ -72,6 +77,108 @@ void XmlStream::moveToNextTag() ++pos; } +XmlStream::Tag XmlStream::ensureOpeningTag( int token ) +{ + return checkTag( OPENING( token ), true, "opening" ); +} + +XmlStream::Tag XmlStream::checkOpeningTag( int token ) +{ + return checkTag( OPENING( token ), false, "opening" ); +} + +void XmlStream::ensureClosingTag( int token ) +{ + checkTag( CLOSING( token ), true, "closing" ); +} + +XmlStream::Tag XmlStream::checkTag( int token, bool optional, const char* txt ) +{ + // either it's the following tag, or find it + int savedPos = pos; + if( currentToken() == token || recoverAndFindTag( token )) + { + Tag ret = currentTag(); + moveToNextTag(); + return ret; // ok + } + if( optional ) + { // not a problem, just rewind + pos = savedPos; + return Tag(); + } + fprintf( stderr, "Expected %s tag %d not found.\n", txt, token ); + return Tag(); +} + +bool XmlStream::recoverAndFindTag( int token ) +{ + int depth = 0; + for(; + !atEnd(); + moveToNextTag()) + { + if( depth > 0 ) // we're inside a nested element, skip those + { + if( currentToken() == OPENING( currentToken())) + { + fprintf( stderr, "Skipping opening tag %d\n", currentToken()); + ++depth; + } + else if( currentToken() == CLOSING( currentToken())) + { // TODO debug output without the OPENING/CLOSING bits set + fprintf( stderr, "Skipping closing tag %d\n", currentToken()); + --depth; + } + else + { + fprintf( stderr, "Malformed token %d\n", currentToken()); + abort(); + } + continue; + } + if( currentToken() == token ) + return true; // ok, found + if( currentToken() == CLOSING( currentToken())) + return false; // that would be leaving current element, so not found + if( currentToken() == OPENING( currentToken())) + { + fprintf( stderr, "Skipping opening tag %d\n", currentToken()); + ++depth; + } + else + abort(); + } + fprintf( stderr, "Unexpected end of stream reached.\n" ); + return false; +} + +void XmlStream::skipElement( int token ) +{ + int closing = ( token & ~TAG_OPENING ) | TAG_CLOSING; // make it a closing tag + assert( currentToken() == OPENING( token )); + // just find the matching closing tag + if( recoverAndFindTag( closing )) + { + moveToNextTag(); // and skip it too + return; + } + fprintf( stderr, "Expected end of element %d not found.\n", token ); +} + +void XmlStream::handleUnexpectedTag() +{ + if( atEnd()) + return; + if( currentToken() == CLOSING( currentToken())) + { + moveToNextTag(); // just skip it + return; + } + skipElement( currentToken()); // otherwise skip the entire element +} + + void XmlStreamBuilder::appendOpeningTag( int token, const uno::Reference< xml::sax::XFastAttributeList >& attrs ) { tags.push_back( Tag( OPENING( token ), attrs )); -- cgit