summaryrefslogtreecommitdiff
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
parent4d5ca442d89ee36e7b1abb622e9f3d85b36e0d0c (diff)
more api improving in importing mathml docx
-rw-r--r--oox/inc/oox/mathml/importutils.hxx39
-rw-r--r--oox/source/mathml/importutils.cxx107
-rw-r--r--starmath/source/ooxmlimport.cxx129
-rw-r--r--starmath/source/ooxmlimport.hxx28
4 files changed, 166 insertions, 137 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 ));
diff --git a/starmath/source/ooxmlimport.cxx b/starmath/source/ooxmlimport.cxx
index 16f887ca6c15..6b30142a8721 100644
--- a/starmath/source/ooxmlimport.cxx
+++ b/starmath/source/ooxmlimport.cxx
@@ -68,7 +68,7 @@ OUString SmOoxmlImport::ConvertToStarMath()
// NOT complete
OUString SmOoxmlImport::handleStream()
{
- checkOpeningTag( M_TOKEN( oMath ));
+ stream.ensureOpeningTag( M_TOKEN( oMath ));
OUString ret;
while( !stream.atEnd())
{
@@ -81,140 +81,51 @@ OUString SmOoxmlImport::handleStream()
ret += STR( " " ) + handleF();
break;
default:
- handleUnexpectedTag();
+ stream.handleUnexpectedTag();
break;
}
}
- checkClosingTag( M_TOKEN( oMath ));
+ stream.ensureClosingTag( M_TOKEN( oMath ));
return ret;
}
// NOT complete
OUString SmOoxmlImport::handleF()
{
- checkOpeningTag( M_TOKEN( f ));
+ stream.ensureOpeningTag( M_TOKEN( f ));
if( stream.currentToken() == OPENING_TAG( M_TOKEN( fPr )))
{
// TODO
}
- checkOpeningTag( M_TOKEN( num ));
+ stream.ensureOpeningTag( M_TOKEN( num ));
OUString num = readR();
- checkClosingTag( M_TOKEN( num ));
- checkOpeningTag( M_TOKEN( den ));
+ stream.ensureClosingTag( M_TOKEN( num ));
+ stream.ensureOpeningTag( M_TOKEN( den ));
OUString den = readR();
- checkClosingTag( M_TOKEN( den ));
- checkClosingTag( M_TOKEN( f ));
+ stream.ensureClosingTag( M_TOKEN( den ));
+ stream.ensureClosingTag( M_TOKEN( f ));
return STR( "{" ) + num + STR( "} over {" ) + den + STR( "}" );
}
// NOT complete
OUString SmOoxmlImport::readR()
{
- checkOpeningTag( M_TOKEN( r ));
-
-// checkOpeningTag( OOX_TOKEN( doc, rPr ));
-// checkOpeningTag( OOX_TOKEN( doc, rFonts ));
-// checkClosingTag( OOX_TOKEN( doc, rFonts ));
-// checkClosingTag( OOX_TOKEN( doc, rPr ));
-
+ stream.ensureOpeningTag( M_TOKEN( r ));
+ if( XmlStream::Tag rPr = stream.checkOpeningTag( OOX_TOKEN( doc, rPr )))
+ { // TODO
+// stream.checkOpeningTag( OOX_TOKEN( doc, rFonts ));
+// stream.ensureClosingTag( OOX_TOKEN( doc, rFonts ));
+ stream.ensureClosingTag( OOX_TOKEN( doc, rPr ));
+ }
// TODO can there be more t's ?
- XmlStream::Tag rtag = checkOpeningTag( M_TOKEN( t ));
+ XmlStream::Tag rtag = stream.ensureOpeningTag( M_TOKEN( t ));
+ // TODO bail out if failure?
OUString text = rtag.text;
if( !rtag.attributes.getBool( OOX_TOKEN( xml, space ), false ))
text = text.trim();
- checkClosingTag( M_TOKEN( t ));
- checkClosingTag( M_TOKEN( r ));
+ stream.ensureClosingTag( M_TOKEN( t ));
+ stream.ensureClosingTag( M_TOKEN( r ));
return text;
}
-XmlStream::Tag SmOoxmlImport::checkOpeningTag( int token )
-{
- return checkTag( OPENING( token ), "opening" );
-}
-
-void SmOoxmlImport::checkClosingTag( int token )
-{
- checkTag( CLOSING( token ), "closing" );
-}
-
-XmlStream::Tag SmOoxmlImport::checkTag( int token, const char* txt )
-{
- // either it's the following tag, or find it
- if( stream.currentToken() == token || recoverAndFindTag( token ))
- {
- XmlStream::Tag ret = stream.currentTag();
- stream.moveToNextTag();
- return ret; // ok
- }
- fprintf( stderr, "Expected %s tag %d not found.\n", txt, token );
- return XmlStream::Tag();
-}
-
-bool SmOoxmlImport::recoverAndFindTag( int token )
-{
- int depth = 0;
- for(;
- !stream.atEnd();
- stream.moveToNextTag())
- {
- if( depth > 0 ) // we're inside a nested element, skip those
- {
- if( stream.currentToken() == OPENING( stream.currentToken()))
- {
- fprintf( stderr, "Skipping opening tag %d\n", stream.currentToken());
- ++depth;
- }
- else if( stream.currentToken() == CLOSING( stream.currentToken()))
- { // TODO debug output without the OPENING/CLOSING bits set
- fprintf( stderr, "Skipping closing tag %d\n", stream.currentToken());
- --depth;
- }
- else
- {
- fprintf( stderr, "Malformed token %d\n", stream.currentToken());
- abort();
- }
- continue;
- }
- if( stream.currentToken() == CLOSING( stream.currentToken()))
- return false; // that would be leaving current element, so not found
- if( stream.currentToken() == token )
- return true; // ok, found
- if( stream.currentToken() == OPENING( stream.currentToken()))
- {
- fprintf( stderr, "Skipping opening tag %d\n", stream.currentToken());
- ++depth;
- }
- else
- abort();
- }
- fprintf( stderr, "Unexpected end of stream reached.\n" );
- return false;
-}
-
-void SmOoxmlImport::skipElement( int token )
-{
- int closing = ( token & ~TAG_OPENING ) | TAG_CLOSING; // make it a closing tag
- assert( stream.currentToken() == OPENING( token ));
- // just find the matching closing tag
- if( recoverAndFindTag( closing ))
- {
- stream.moveToNextTag(); // and skip it too
- return;
- }
- fprintf( stderr, "Expected end of element %d not found.\n", token );
-}
-
-void SmOoxmlImport::handleUnexpectedTag()
-{
- if( stream.atEnd())
- return;
- if( stream.currentToken() == CLOSING( stream.currentToken()))
- {
- stream.moveToNextTag(); // just skip it
- return;
- }
- skipElement( stream.currentToken()); // otherwise skip the entire element
-}
-
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/starmath/source/ooxmlimport.hxx b/starmath/source/ooxmlimport.hxx
index 043b44a19ec2..594b8d6a86ed 100644
--- a/starmath/source/ooxmlimport.hxx
+++ b/starmath/source/ooxmlimport.hxx
@@ -46,34 +46,6 @@ private:
rtl::OUString handleStream();
rtl::OUString handleF();
rtl::OUString readR();
- /**
- Checks that the current tag is the given opening token, if not, writes out a warning
- and tries to recover (skips tags until found or until the current element would end).
- In both cases the position is moved to the next tag.
- @return the matching found opening tag, or empty tag
- */
- oox::formulaimport::XmlStream::Tag checkOpeningTag( int token );
- /**
- Checks that the current tag is the given opening token, if not, writes out a warning
- and tries to recover (skips tags until found or until the current element would end).
- In both cases the position is moved to the next tag.
- */
- void checkClosingTag( int token );
- // helper for the two above
- oox::formulaimport::XmlStream::Tag checkTag( int token, const char* txt );
- /**
- 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();
oox::formulaimport::XmlStream& stream;
};