/* AbiWord * Copyright (C) 1998 AbiSource, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #include #include #include #include "ut_types.h" #include "ut_assert.h" #include "ut_debugmsg.h" #include "ut_string.h" #include "ie_imp_DocBook.h" #include "ie_types.h" #include "pd_Document.h" #include "ut_growbuf.h" /* * DocBook is a SGML derivate with lots of friggin' tags * We hardly support any of them now, only the ones we export */ /*****************************************************************/ /*****************************************************************/ IE_Imp_DocBook::~IE_Imp_DocBook() { } IE_Imp_DocBook::IE_Imp_DocBook(PD_Document * pDocument) : IE_Imp_XML(pDocument, UT_FALSE) { } /*****************************************************************/ /*****************************************************************/ UT_Bool IE_Imp_DocBook::RecognizeContents(const char * szBuf, UT_uint32 iNumbytes) { // no doubt, this could be better // but this should suffice for all I care if(strstr(szBuf, " #define TT_SECTION 2 // card or section #define TT_BLOCK 3 // a paragraph #define TT_PHRASE 4 // formatted text #define TT_EMPHASIS 5 // emphasized (italic) text #define TT_SUPERSCRIPT 6 // superscript #define TT_SUBSCRIPT 7 // subscript #define TT_BLOCKQUOTE 8 // block quote #define TT_BRIDGEHEAD 9 // heading #define TT_CHAPTER 10 // legacy abiword documents #define TT_TITLE 11 // title struct _TokenTable { const char * m_name; int m_type; }; static struct _TokenTable s_Tokens[] = { { "book", TT_DOCUMENT }, { "section", TT_SECTION }, { "chapter", TT_CHAPTER }, { "para", TT_BLOCK }, { "phrase", TT_PHRASE }, { "emphasis", TT_EMPHASIS }, { "superscript", TT_SUPERSCRIPT }, { "subscript", TT_SUBSCRIPT }, { "bridgehead", TT_BRIDGEHEAD }, { "title", TT_TITLE }, { "blockquote", TT_BLOCKQUOTE }, { "*", TT_OTHER } }; #define TokenTableSize ((sizeof(s_Tokens)/sizeof(s_Tokens[0]))) static UT_uint32 s_mapNameToToken(const XML_Char * name) { for (unsigned int k=0; kappendStrux(PTX_Section,(const XML_Char **)NULL)); return; } case TT_BLOCK: X_VerifyParseState(_PS_Sec); m_parseState = _PS_Block; X_CheckError(m_pDocument->appendStrux(PTX_Block, NULL)); return; case TT_BRIDGEHEAD: X_VerifyParseState(_PS_Sec); m_parseState = _PS_Block; { const XML_Char **p_atts; XML_Char *buf[3]; buf[2] = NULL; const XML_Char *p_val; p_val = _getXMLPropValue((const XML_Char *)"renderas", atts); XML_Char style_att[15] = "Heading a"; style_att[8] = p_val[4]; X_CheckError(m_pDocument->appendStrux(PTX_Block, NULL)); UT_XML_cloneString(buf[0], PT_STYLE_ATTRIBUTE_NAME); UT_XML_cloneString(buf[1], (XML_Char *) style_att); p_atts = (const XML_Char **)buf; X_CheckError(m_pDocument->appendFmt(p_atts)); return; } case TT_BLOCKQUOTE: X_VerifyParseState(_PS_Sec); m_parseState = _PS_Block; { const XML_Char **p_atts; XML_Char *buf[3]; buf[2] = NULL; XML_Char style_att[15] = "Block Text"; X_CheckError(m_pDocument->appendStrux(PTX_Block, NULL)); UT_XML_cloneString(buf[0], PT_STYLE_ATTRIBUTE_NAME); UT_XML_cloneString(buf[1], (XML_Char *) style_att); p_atts = (const XML_Char **)buf; X_CheckError(m_pDocument->appendFmt(p_atts)); return; } case TT_PHRASE: case TT_EMPHASIS: case TT_SUPERSCRIPT: case TT_SUBSCRIPT: X_VerifyParseState(_PS_Block); { const XML_Char **p_atts; XML_Char *buf[3]; UT_XML_cloneString(buf[0], (XML_Char *)"props"); buf[2] = NULL; switch(s_Tokens[tokenIndex].m_type) { case TT_EMPHASIS: UT_XML_cloneString(buf[1], (XML_Char *)"font-style:italic"); break; case TT_SUPERSCRIPT: UT_XML_cloneString(buf[1], (XML_Char *)"text-position:superscript"); break; case TT_SUBSCRIPT: UT_XML_cloneString(buf[1], (XML_Char *)"text-position:subscript"); break; case TT_PHRASE: { const XML_Char *p_val = _getXMLPropValue((const XML_Char *)"role", atts); if(p_val != NULL && !strcmp(p_val, "strong")) UT_XML_cloneString(buf[1], (XML_Char *)"font-weight:bold"); else buf[0] = NULL; break; } default: UT_ASSERT(UT_SHOULD_NOT_HAPPEN); break; } p_atts = (const XML_Char **)buf; X_CheckError(_pushInlineFmt(p_atts)); X_CheckError(m_pDocument->appendFmt(&m_vecInlineFmt)); } return; case TT_TITLE: X_VerifyParseState(_PS_Sec); return; case TT_OTHER: default: UT_DEBUGMSG(("Unknown or knowingly unhandled tag [%s]\n",name)); return; } } void IE_Imp_DocBook::_endElement(const XML_Char *name) { UT_DEBUGMSG(("DocBook import: endElement: %s\n", name)); // xml parser keeps running until buffer consumed X_EatIfAlreadyError(); UT_uint32 tokenIndex = s_mapNameToToken(name); switch (s_Tokens[tokenIndex].m_type) { case TT_DOCUMENT: X_VerifyParseState(_PS_Doc); m_parseState = _PS_Init; return; case TT_SECTION: X_VerifyParseState(_PS_Sec); m_parseState = _PS_Doc; return; case TT_BRIDGEHEAD: case TT_BLOCKQUOTE: UT_ASSERT(m_lenCharDataSeen==0); X_VerifyParseState(_PS_Block); m_parseState = _PS_Sec; X_CheckDocument(_getInlineDepth()==0); _popInlineFmt(); X_CheckError(m_pDocument->appendFmt(&m_vecInlineFmt)); return; case TT_BLOCK: UT_ASSERT(m_lenCharDataSeen==0); X_VerifyParseState(_PS_Block); m_parseState = _PS_Sec; X_CheckDocument(_getInlineDepth()==0); return; case TT_PHRASE: case TT_EMPHASIS: case TT_SUPERSCRIPT: case TT_SUBSCRIPT: UT_ASSERT(m_lenCharDataSeen==0); X_VerifyParseState(_PS_Block); X_CheckDocument(_getInlineDepth()>0); _popInlineFmt(); X_CheckError(m_pDocument->appendFmt(&m_vecInlineFmt)); return; case TT_TITLE: return; case TT_OTHER: default: UT_DEBUGMSG(("Unknown or intentionally unhandled end tag [%s]\n",name)); return; } }