1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
/* AbiWord
* Copyright (C) 1998 AbiSource, Inc.
* Copyright (C) 2003 Tomas Frydrych <tomas@frydrych.uklinux.net>
* Copyright (C) 2004 Martin Sevior <msevior@physics.unimelb.edu.au>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include "ut_types.h"
#include "ut_assert.h"
#include "ut_debugmsg.h"
#include "ut_string.h"
#include "ut_iconv.h"
#include "ie_imp_MathML.h"
#include "pd_Document.h"
#include "xap_EncodingManager.h"
#include "ap_Dialog_Id.h"
#include "xap_DialogFactory.h"
#include "xap_Dlg_Encoding.h"
#include "ap_Prefs.h"
#include "ie_imp_Text.h"
/*****************************************************************/
/*****************************************************************/
static const AbiMathViewEntityMapItem mathmlEntitiesMap[] = {
#include "entitiesMap.inc"
};
static int compareEntities(const void * pSz, const void * pEnt)
{
AbiMathViewEntityMapItem ** pE = NULL;
pE = reinterpret_cast<AbiMathViewEntityMapItem **>(reinterpret_cast<void **>(const_cast<void *>(pEnt)));
const char * sz1 = reinterpret_cast<const char *>(pSz);
const char * sz2 = (*pE)->szEntity;
return strcmp(sz1, sz2);
}
static int sortEntities(const void * pEnt1, const void * pEnt2)
{
AbiMathViewEntityMapItem ** pE1 = NULL;
AbiMathViewEntityMapItem ** pE2 = NULL;
pE1 = reinterpret_cast<AbiMathViewEntityMapItem **>(reinterpret_cast<void **>(const_cast<void *>(pEnt1)));
pE2 = reinterpret_cast<AbiMathViewEntityMapItem **>(reinterpret_cast<void **>(const_cast<void *>(pEnt2)));
const char * sz1 = (*pE1)->szEntity;
const char * sz2 = (*pE2)->szEntity;
return strcmp(sz1, sz2);
}
IE_Imp_MathML_EntityTable::IE_Imp_MathML_EntityTable ()
{
UT_sint32 MapSize = sizeof(mathmlEntitiesMap) / sizeof(AbiMathViewEntityMapItem);
for (UT_sint32 i = 0; i < MapSize - 1; i++)
{
AbiMathViewEntityMapItem * pEnt = const_cast<AbiMathViewEntityMapItem * >(&mathmlEntitiesMap[i]);
m_vecEntityMap.addItem(pEnt);
}
m_vecEntityMap.qsort(sortEntities);
}
IE_Imp_MathML_EntityTable::~IE_Imp_MathML_EntityTable ()
{
//
}
/* caller's responsibility to free returned bytebuf
*/
bool IE_Imp_MathML_EntityTable::convert(const char * buffer, unsigned long length, UT_ByteBuf & To) const
{
if (!buffer || !length)
{
UT_ASSERT(buffer && length);
return false;
}
const char * ptr1 = buffer;
const char * end = buffer + length;
bool bOkay = false;
while (*ptr1 && (end - ptr1 > 6))
{
if (*ptr1 == '<')
{
if (strncmp(ptr1, "<math", 5) == 0)
{
bOkay = true;
ptr1 += 5;
break;
}
}
++ptr1;
}
if (!bOkay)
{
return false;
}
const char * start = buffer;
while (end - ptr1 > 7)
{
if (*ptr1 == 0)
{
break;
}
if (*ptr1 != '&')
{
++ptr1;
continue;
}
if (ptr1 != start)
{
To.append(reinterpret_cast<const UT_Byte *>(start), ptr1 - start);
}
bool bEntity = true;
const char * ptr2 = ptr1 + 1;
while (end - ptr2 > 7)
{
if (*ptr2 == 0)
{
bEntity = false;
break;
}
if (*ptr2 == ';')
{
break;
}
switch (*ptr2)
{
case '<': case '>': case '&': case '\'': case '"': case ' ': // non-exhaustive list of bad characters for entity; quick check only
bEntity = false;
default:
break;
}
if (!bEntity)
{
break;
}
++ptr2;
}
if (bEntity)
{
if (*(ptr1 + 1) == '#') // unicode value; just leave alone...
{
++ptr2;
To.append(reinterpret_cast<const UT_Byte *>(ptr1), ptr2 - ptr1);
ptr1 = ptr2;
}
else
{
/* Make a copy of the string, excluding the & and ;
*/
UT_sint32 isize = static_cast<UT_sint32>(ptr2 - (ptr1 + 1));
char * pszNew = new char[isize+1];
for (UT_sint32 i = 0; i < isize; i++)
{
pszNew[i] = ptr1[1+i];
}
pszNew[isize] = 0;
UT_sint32 pos = m_vecEntityMap.binarysearch(reinterpret_cast<void *>(pszNew), compareEntities);
if (pos >= 0)
{
AbiMathViewEntityMapItem * pE = m_vecEntityMap.getNthItem(pos);
const char * szEntVal = pE->szVal;
UT_DEBUGMSG(("Replacing entity \"&%s;\" with \"%s\".\n", pszNew, szEntVal));
To.append(reinterpret_cast<const UT_Byte *>(szEntVal), strlen(szEntVal));
ptr1 = ptr2 + 1;
}
else
{
UT_DEBUGMSG(("Entity \"&%s;\" unmatched; leaving alone...\n", pszNew));
++ptr2;
To.append(reinterpret_cast<const UT_Byte *>(ptr1), ptr2 - ptr1);
ptr1 = ptr2;
}
DELETEPV(pszNew);
}
}
else // huh?
{
UT_DEBUGMSG(("Found unmatched \"&\" - replacing with \"&\"!\n"));
const char * amp = "&";
To.append(reinterpret_cast<const UT_Byte *>(amp), 5);
++ptr1;
}
start = ptr1;
}
To.append(reinterpret_cast<const UT_Byte *>(start), end - start);
return true;
}
/*****************************************************************/
/*****************************************************************/
IE_Imp_MathML_Sniffer::IE_Imp_MathML_Sniffer (const IE_Imp_MathML_EntityTable & EntityTable) :
IE_ImpSniffer(IE_IMPEXPNAME_MATHML, true),
m_EntityTable(&EntityTable)
{
//
}
// supported suffixes
static IE_SuffixConfidence IE_Imp_MathML_Sniffer__SuffixConfidence[] = {
{ "mathml", UT_CONFIDENCE_PERFECT },
{ "xml", UT_CONFIDENCE_GOOD },
{ "", UT_CONFIDENCE_ZILCH }
};
const IE_SuffixConfidence * IE_Imp_MathML_Sniffer::getSuffixConfidence ()
{
return IE_Imp_MathML_Sniffer__SuffixConfidence;
}
// supported mimetypes
static IE_MimeConfidence IE_Imp_MathML_Sniffer__MimeConfidence[] = {
{ IE_MIME_MATCH_FULL, "application/mathml+xml", UT_CONFIDENCE_GOOD },
{ IE_MIME_MATCH_FULL, "application/mathml", UT_CONFIDENCE_GOOD },
{ IE_MIME_MATCH_CLASS, "text", UT_CONFIDENCE_SOSO },
{ IE_MIME_MATCH_BOGUS, "", UT_CONFIDENCE_ZILCH }
};
const IE_MimeConfidence * IE_Imp_MathML_Sniffer::getMimeConfidence ()
{
return IE_Imp_MathML_Sniffer__MimeConfidence;
}
/*!
Check if buffer contains data meant for this importer.
We don't attmpt to recognize since other filetypes (HTML) can
use the same encodings a text file can.
We also don't want to steal recognition when user wants to use
the Encoded Text importer.
*/
UT_Confidence_t IE_Imp_MathML_Sniffer::recognizeContents(const char * szBuf,
UT_uint32 /*iNumbytes*/)
{
const char * magic = "<math";
if(strncmp(szBuf, magic, strlen(magic)) == 0)
return UT_CONFIDENCE_PERFECT;
return UT_CONFIDENCE_ZILCH;
}
UT_Error IE_Imp_MathML_Sniffer::constructImporter(PD_Document * pDocument,
IE_Imp ** ppie)
{
IE_Imp_MathML * p = new IE_Imp_MathML(pDocument, *m_EntityTable);
*ppie = p;
return UT_OK;
}
bool IE_Imp_MathML_Sniffer::getDlgLabels(const char ** pszDesc,
const char ** pszSuffixList,
IEFileType * ft)
{
*pszDesc = "MathML (.xml, .mathml)";
*pszSuffixList = "*.xml; *.mathml";
*ft = getFileType();
return true;
}
/*****************************************************************/
/*****************************************************************/
#define X_CleanupIfError(error,exp) do { if (((error)=(exp)) != UT_OK) goto Cleanup; } while (0)
/*
Import MathML data from a plain text file
\param szFilename Name of file to import
Simply fills a UT_byteBuf with the contents of the MathML
*/
UT_Error IE_Imp_MathML::_loadFile(GsfInput * fp)
{
ImportStreamFile * pStream = new ImportStreamFile(fp);
UT_Error error;
pStream->init(NULL);
X_CleanupIfError(error,_parseStream(pStream));
error = UT_OK;
Cleanup:
delete pStream;
return error;
}
#undef X_CleanupIfError
/*****************************************************************/
/*****************************************************************/
/*
Construct text importer
\param pDocument Document to import MathML into
Uses current document's encoding if it is set
*/
IE_Imp_MathML::IE_Imp_MathML(PD_Document * pDocument, const IE_Imp_MathML_EntityTable & EntityTable) :
IE_Imp(pDocument),
m_pByteBuf(new UT_ByteBuf),
m_EntityTable(&EntityTable)
{
//
}
IE_Imp_MathML::~IE_Imp_MathML(void)
{
DELETEP(m_pByteBuf);
}
/*****************************************************************/
#define X_ReturnIfFail(exp,error) do { bool b = (exp); if (!b) return (error); } while (0)
#define X_ReturnNoMemIfError(exp) X_ReturnIfFail(exp,UT_IE_NOMEMORY)
/*!
Parse stream contents into the document
\param stream Stream to import from
This code is used for both files and the clipboard
*/
UT_Error IE_Imp_MathML::_parseStream(ImportStream * pStream)
{
UT_return_val_if_fail(pStream, UT_ERROR);
UT_ByteBuf BB;
UT_UCSChar c;
unsigned char uc;
while (pStream->getChar(c))
{
// Check to identify & ignore BOM
if(c!=((UT_UCSChar)0x00EF) && c!=((UT_UCSChar)0x00BB) && c!=((UT_UCSChar)0x00BF))
{
uc = static_cast<unsigned char>(c);
BB.append(&uc,1);
}
}
return m_EntityTable->convert(reinterpret_cast<const char *>(BB.getPointer(0)), BB.getLength(), *m_pByteBuf) ? UT_OK : UT_ERROR;
}
bool IE_Imp_MathML::pasteFromBuffer(PD_DocumentRange * pDocRange,
const unsigned char * pData, UT_uint32 lenData,
const char * /* encoding */)
{
UT_return_val_if_fail(getDoc() == pDocRange->m_pDoc,false);
UT_return_val_if_fail(pDocRange->m_pos1 == pDocRange->m_pos2,false);
ImportStreamClipboard stream(pData, lenData);
setClipboard (pDocRange->m_pos1);
stream.init(NULL);
_parseStream(&stream);
return true;
}
|