summaryrefslogtreecommitdiff
path: root/oox
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@suse.cz>2011-11-24 13:19:57 +0100
committerLuboš Luňák <l.lunak@suse.cz>2011-11-24 18:43:58 +0100
commit5b94957f7f25c8a607735d038c4f22e8009b99ea (patch)
tree97d70b4216c9f50bca2c24e37b82408aa3ed0ff4 /oox
parent4d5ca442d89ee36e7b1abb622e9f3d85b36e0d0c (diff)
more api improving in importing mathml docx
Diffstat (limited to 'oox')
-rw-r--r--oox/inc/oox/mathml/importutils.hxx39
-rw-r--r--oox/source/mathml/importutils.cxx107
2 files changed, 146 insertions, 0 deletions
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 ));