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
|
/*
* Copyright 2007,2008 Fabrice Colin
*
* 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 Library 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 <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <set>
#include "MIMEScanner.h"
#include "StringManip.h"
#include "TimeConverter.h"
#include "Url.h"
#include "TextConverter.h"
#include "filters/FilterFactory.h"
#include "FilterUtils.h"
#define UNSUPPORTED_TYPE "X-Unsupported"
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::set;
using std::map;
map<string, string> FilterUtils::m_typeAliases;
FilterUtils::FilterUtils()
{
}
FilterUtils::~FilterUtils()
{
}
Dijon::Filter *FilterUtils::getFilter(const string &mimeType)
{
Dijon::Filter *pFilter = NULL;
// Is this type aliased ?
map<string, string>::const_iterator aliasIter = m_typeAliases.find(mimeType);
if (aliasIter != m_typeAliases.end())
{
if (aliasIter->second == UNSUPPORTED_TYPE)
{
// We already know that none of this type's parents are supported
return NULL;
}
pFilter = Dijon::FilterFactory::getFilter(aliasIter->second);
}
else
{
// Is there a filter for this type ?
pFilter = Dijon::FilterFactory::getFilter(mimeType);
}
if (pFilter != NULL)
{
return pFilter;
}
if (mimeType.empty() == false)
{
set<string> parentTypes;
// Try that type's parents
MIMEScanner::getParentTypes(mimeType, parentTypes);
for (set<string>::const_iterator parentIter = parentTypes.begin();
parentIter != parentTypes.end(); ++parentIter)
{
pFilter = Dijon::FilterFactory::getFilter(*parentIter);
if (pFilter != NULL)
{
// Add an alias
m_typeAliases[mimeType] = *parentIter;
return pFilter;
}
}
// This type has no valid parent
m_typeAliases[mimeType] = UNSUPPORTED_TYPE;
}
return NULL;
}
bool FilterUtils::isSupportedType(const string &mimeType)
{
// Is this type aliased ?
map<string, string>::const_iterator aliasIter = m_typeAliases.find(mimeType);
if (aliasIter != m_typeAliases.end())
{
if (aliasIter->second == UNSUPPORTED_TYPE)
{
return false;
}
// We were able to get a filter for this parent type
// or a previous call to isSupportedType() succeeded
return true;
}
if (Dijon::FilterFactory::isSupportedType(mimeType) == false)
{
set<string> parentTypes;
// Try that type's parents
MIMEScanner::getParentTypes(mimeType, parentTypes);
for (set<string>::const_iterator parentIter = parentTypes.begin();
parentIter != parentTypes.end(); ++parentIter)
{
if (Dijon::FilterFactory::isSupportedType(*parentIter) == true)
{
// Add an alias
m_typeAliases[mimeType] = *parentIter;
return true;
}
}
// This type has no valid parent
m_typeAliases[mimeType] = UNSUPPORTED_TYPE;
}
return false;
}
bool FilterUtils::feedFilter(const Document &doc, Dijon::Filter *pFilter)
{
string location(doc.getLocation());
Url urlObj(location);
string fileName;
unsigned int dataLength = 0;
const char *pData = doc.getData(dataLength);
bool fedInput = false;
if (pFilter == NULL)
{
return false;
}
if ((urlObj.getProtocol() == "file") &&
(location .length() > 7))
{
fileName = location.substr(7);
}
// Prefer feeding the data
if (((dataLength > 0) && (pData != NULL)) &&
(pFilter->is_data_input_ok(Dijon::Filter::DOCUMENT_DATA) == true))
{
fedInput = pFilter->set_document_data(pData, dataLength);
}
// ... to feeding the file
if ((fedInput == false) &&
(fileName.empty() == false))
{
if (pFilter->is_data_input_ok(Dijon::Filter::DOCUMENT_FILE_NAME) == true)
{
#ifdef DEBUG
cout << "FilterUtils::feedFilter: feeding file " << fileName << endl;
#endif
fedInput = pFilter->set_document_file(fileName);
}
// ...and to feeding the file's contents
if ((fedInput == false) &&
(pFilter->is_data_input_ok(Dijon::Filter::DOCUMENT_DATA) == true))
{
Document docCopy(doc);
if (docCopy.setDataFromFile(fileName) == false)
{
cerr << "Couldn't load " << fileName << endl;
return false;
}
#ifdef DEBUG
cout << "FilterUtils::feedFilter: feeding contents of file " << fileName << endl;
#endif
pData = docCopy.getData(dataLength);
if ((dataLength > 0) &&
(pData != NULL))
{
fedInput = pFilter->set_document_data(pData, dataLength);
}
else
{
// The file may be empty
fedInput = pFilter->set_document_data(" ", 1);
}
}
}
// ... to feeding data through a temporary file
if ((fedInput == false) &&
((dataLength > 0) && (pData != NULL)) &&
(pFilter->is_data_input_ok(Dijon::Filter::DOCUMENT_FILE_NAME) == true))
{
char inTemplate[18] = "/tmp/filterXXXXXX";
int inFd = mkstemp(inTemplate);
if (inFd != -1)
{
#ifdef DEBUG
cout << "FilterUtils::feedFilter: feeding temporary file " << inTemplate << endl;
#endif
// Save the data
if (write(inFd, (const void*)pData, dataLength) != -1)
{
fedInput = pFilter->set_document_file(inTemplate, true);
if (fedInput == false)
{
// We might as well delete the file now
unlink(inTemplate);
}
}
close(inFd);
}
}
if (fedInput == false)
{
cerr << "Couldn't feed filter for " << doc.getLocation() << endl;
return false;
}
return true;
}
bool FilterUtils::populateDocument(Document &doc, Dijon::Filter *pFilter)
{
string charset, uri, ipath;
if (pFilter == NULL)
{
return false;
}
// Go through the whole thing
const map<string, string> &metaData = pFilter->get_meta_data();
for (map<string, string>::const_iterator metaIter = metaData.begin();
metaIter != metaData.end(); ++metaIter)
{
if (metaIter->first == "charset")
{
charset = metaIter->second;
}
else if (metaIter->first == "date")
{
doc.setTimestamp(metaIter->second);
}
else if (metaIter->first == "ipath")
{
ipath = metaIter->second;
}
else if (metaIter->first == "language")
{
doc.setLanguage(metaIter->second);
}
else if (metaIter->first == "mimetype")
{
doc.setType(StringManip::toLowerCase(metaIter->second));
}
else if (metaIter->first == "size")
{
off_t size = (off_t)atoi(metaIter->second.c_str());
if (size > 0)
{
doc.setSize(size);
}
#ifdef DEBUG
else cout << "FilterUtils::populateDocument: ignoring size zero" << endl;
#endif
}
else if (metaIter->first == "uri")
{
uri = metaIter->second;
if ((uri.length() >= 18) &&
(uri.substr(0, 18) == "file:///tmp/filter"))
{
// We fed the filter a temporary file
uri.clear();
}
}
}
if (uri.empty() == false)
{
doc.setLocation(uri);
}
if (ipath.empty() == false)
{
string location(doc.getLocation());
location += "?";
location += ipath;
doc.setLocation(location);
}
TextConverter converter(20);
// Content and title may have to be converted
map<string, string>::const_iterator contentIter = metaData.find("content");
if (contentIter != metaData.end())
{
string utf8Data(converter.toUTF8(contentIter->second, charset));
if (converter.getErrorsCount() > 0)
{
cerr << doc.getLocation() << " may not have been fully converted to UTF-8" << endl;
}
doc.setData(utf8Data.c_str(), utf8Data.length());
#ifdef DEBUG
cout << "FilterUtils::populateDocument: set " << utf8Data.length() << "/" << contentIter->second.length()
<< " bytes, converted from charset " << charset << endl;
#endif
}
contentIter = metaData.find("title");
if (contentIter != metaData.end())
{
doc.setTitle(converter.toUTF8(contentIter->second, charset));
}
return true;
}
string FilterUtils::stripMarkup(const string &text)
{
if (text.empty() == true)
{
return "";
}
Dijon::Filter *pFilter = Dijon::FilterFactory::getFilter("text/xml");
if (pFilter == NULL)
{
return "";
}
Document doc;
string strippedText;
doc.setData(text.c_str(), text.length());
if ((feedFilter(doc, pFilter) == true) &&
(pFilter->next_document() == true))
{
const map<string, string> &metaData = pFilter->get_meta_data();
map<string, string>::const_iterator contentIter = metaData.find("content");
if (contentIter != metaData.end())
{
strippedText = contentIter->second;
}
}
delete pFilter;
return strippedText;
}
|