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
|
/*
* Copyright 2005-2012 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 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 <iostream>
#include <algorithm>
#include "MIMEScanner.h"
#include "Url.h"
#include "FilterUtils.h"
#include "FileCollector.h"
using namespace std;
class LastIPathAction : public ReducedAction
{
public:
LastIPathAction(const string &ipath) :
ReducedAction(),
m_ipath(ipath),
m_pDocument(NULL)
{
}
virtual ~LastIPathAction()
{
}
virtual bool positionFilter(const Document &doc, Dijon::Filter *pFilter)
{
string::size_type nextPos = m_ipath.find("&next&");
string thisIPath(m_ipath);
if (nextPos != string::npos)
{
thisIPath = m_ipath.substr(0, nextPos);
}
if (pFilter != NULL)
{
#ifdef DEBUG
clog << "LastIPathAction::positionFilter: moving filter for "
<< pFilter->get_mime_type() << " to ipath " << thisIPath << endl;
#endif
pFilter->set_property(Dijon::Filter::OPERATING_MODE, "view");
// Skip forward
pFilter->skip_to_document(thisIPath);
}
if (nextPos != string::npos)
{
m_ipath.erase(0, nextPos + 6);
}
else
{
m_ipath.clear();
}
return true;
}
virtual bool isReduced(const Document &doc)
{
if (m_ipath.empty() == false)
{
// Go one level deeper
#ifdef DEBUG
clog << "LastIPathAction::isReduced: not final" << endl;
#endif
return false;
}
return true;
}
virtual bool takeAction(Document &doc, bool isNested)
{
if (m_ipath.empty() == true)
{
m_pDocument = new Document(doc);
#ifdef DEBUG
clog << "LastIPathAction::takeAction: ipath " << doc.getInternalPath() << " is final" << endl;
#endif
}
return true;
}
Document *getDocument(void)
{
return m_pDocument;
}
protected:
string m_ipath;
Document *m_pDocument;
private:
LastIPathAction(const LastIPathAction &other);
LastIPathAction &operator=(const LastIPathAction &other);
};
FileCollector::FileCollector() :
DownloaderInterface()
{
}
FileCollector::~FileCollector()
{
}
//
// Implementation of DownloaderInterface
//
/// Retrieves the specified document; NULL if error.
Document *FileCollector::retrieveUrl(const DocumentInfo &docInfo)
{
Url thisUrl(docInfo.getLocation());
string protocol(thisUrl.getProtocol());
string ipath(docInfo.getInternalPath());
if (protocol != "file")
{
// We can't handle that type of protocol...
return NULL;
}
string fileLocation(thisUrl.getLocation());
fileLocation += "/";
fileLocation += thisUrl.getFile();
Document *pDocument = new Document(docInfo);
if (pDocument->setDataFromFile(fileLocation) == false)
{
delete pDocument;
return NULL;
}
// Determine the file type
string type(MIMEScanner::scanFile(fileLocation));
// Stop here ?
if (ipath.empty() == true)
{
if (pDocument->getType().empty() == true)
{
pDocument->setType(type);
}
return pDocument;
}
// Reset these to avoid confusing the filters
pDocument->setInternalPath("");
pDocument->setType(type);
LastIPathAction action(ipath);
bool reduced = FilterUtils::reduceDocument(*pDocument, action);
if (reduced == true)
{
Document *pBottomMostDocument = action.getDocument();
if (pBottomMostDocument != NULL)
{
delete pDocument;
return pBottomMostDocument;
}
}
return pDocument;
}
|