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
|
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2001 Eva Brucherseifer <eva@kde.org>
SPDX-FileCopyrightText: 2005 Bram Schoenmakers <bramschoenmakers@kde.nl>
based on kspread csv export filter by David Faure
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "htmlimport.h"
#include "HtmlImportDebug.h"
//#include <exportdialog.h>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QByteArray>
#include <QEventLoop>
#include <KPluginFactory>
#include <KoFilterChain.h>
#include <KoXmlWriter.h>
#include <KoOdfWriteStore.h>
#include <KoGenStyles.h>
#include <KoGenStyle.h>
#include <QDomText>
#include <QDomDocument>
#include <QDomElement>
#include <QString>
//using namespace Calligra::Sheets;
K_PLUGIN_FACTORY_WITH_JSON(HTMLImportFactory, "calligra_filter_html2ods.json",
registerPlugin<HTMLImport>();)
HTMLImport::HTMLImport(QObject* parent, const QVariantList&)
: KoFilter(parent)
{
}
HTMLImport::~HTMLImport()
= default;
KoFilter::ConversionStatus HTMLImport::convert(const QByteArray& from, const QByteArray& to)
{
if (to != "application/vnd.oasis.opendocument.spreadsheet" || from != "text/html") {
warnHtml << "Invalid mimetypes " << to << " " << from;
return KoFilter::NotImplemented;
}
QString inputFile = m_chain->inputFile();
QString outputFile = m_chain->outputFile();
debugHtml<<"inputFile="<<inputFile<<"outputFile="<<outputFile;
// check if the inout file exists
m_inputDir = QFileInfo(m_chain->inputFile()).dir();
if(!m_inputDir.exists())
return KoFilter::StupidError;
// create output store
KoStore* storeout = KoStore::createStore(outputFile, KoStore::Write, "application/vnd.oasis.opendocument.spreadsheet", KoStore::Zip);
if (!storeout)
return KoFilter::FileNotFound;
KoOdfWriteStore oasisStore(storeout);
m_manifestWriter = oasisStore.manifestWriter("application/vnd.oasis.opendocument.spreadsheet");
m_store = &oasisStore;
m_mainStyles = new KoGenStyles();
KoXmlWriter* bodyWriter = m_store->bodyWriter();
m_store->contentWriter(); // we need to create the instance even if the contentWriter is not used
bodyWriter->startElement("office:body");
KoFilter::ConversionStatus result = loadUrl(QUrl::fromLocalFile(m_chain->inputFile()));
if(result != KoFilter::OK)
warnHtml << "Failed to load url=" << m_chain->inputFile();
bodyWriter->endElement(); // office:body
if(m_store->closeContentWriter())
m_manifestWriter->addManifestEntry("content.xml", "text/xml");
if(createStyle())
m_manifestWriter->addManifestEntry("styles.xml", "text/xml");
if(createMeta())
m_manifestWriter->addManifestEntry("meta.xml", "text/xml");
m_store->closeManifestWriter();
delete storeout;
m_manifestWriter = nullptr;
m_store = nullptr;
return result;
}
bool HTMLImport::createStyle()
{
if (!m_store->store()->open("styles.xml"))
return false;
KoStoreDevice dev(m_store->store());
KoXmlWriter* stylesWriter = new KoXmlWriter(&dev);
stylesWriter->startDocument("office:document-styles");
stylesWriter->startElement("office:document-styles");
stylesWriter->addAttribute("xmlns:office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0");
stylesWriter->addAttribute("xmlns:style", "urn:oasis:names:tc:opendocument:xmlns:style:1.0");
stylesWriter->addAttribute("xmlns:text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0");
stylesWriter->addAttribute("xmlns:table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0");
stylesWriter->addAttribute("xmlns:draw", "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0");
stylesWriter->addAttribute("xmlns:fo", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0");
stylesWriter->addAttribute("xmlns:svg", "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0");
stylesWriter->addAttribute("office:version", "1.0");
m_mainStyles->saveOdfStyles(KoGenStyles::MasterStyles, stylesWriter);
m_mainStyles->saveOdfStyles(KoGenStyles::DocumentStyles, stylesWriter); // office:style
m_mainStyles->saveOdfStyles(KoGenStyles::DocumentAutomaticStyles, stylesWriter); // office:automatic-styles
stylesWriter->endElement(); // office:document-styles
stylesWriter->endDocument();
delete stylesWriter;
return m_store->store()->close();
}
bool HTMLImport::createMeta()
{
if (!m_store->store()->open("meta.xml"))
return false;
KoStoreDevice dev(m_store->store());
KoXmlWriter* metaWriter = new KoXmlWriter(&dev);
metaWriter->startDocument("office:document-meta");
metaWriter->startElement("office:document-meta");
metaWriter->addAttribute("xmlns:office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0");
metaWriter->addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
metaWriter->addAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");
metaWriter->addAttribute("xmlns:meta", "urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
metaWriter->startElement("office:meta");
//metaWriter->startElement("dc:title");
//metaWriter->addTextNode(workbook->property(Workbook::PIDSI_TITLE).toString());
//metaWriter->endElement();
//metaWriter->startElement("dc:subject", false);
//metaWriter->addTextNode(workbook->property(Workbook::PIDSI_SUBJECT).toString());
//metaWriter->endElement();
metaWriter->endElement(); // office:meta
metaWriter->endElement(); // office:document-meta
metaWriter->endDocument();
delete metaWriter;
return m_store->store()->close();
}
KoFilter::ConversionStatus HTMLImport::loadUrl(const QUrl &url)
{
debugHtml << url;
KoXmlWriter* bodyWriter = m_store->bodyWriter();
//KoXmlWriter* contentWriter = m_store->contentWriter();
QStringList sheets;
{
QDomDocument doc("mydocument");
QFile file(url.toLocalFile());
if (!file.open(QIODevice::ReadOnly))
return KoFilter::ConversionStatus::StorageCreationError;
if (!doc.setContent(&file)) {
file.close();
return KoFilter::ConversionStatus::FileNotFound;
}
file.close();
QDomNodeList body = doc.elementsByTagName("body");
QDomNode docbody = body.item(0);
if (!docbody.isNull()) {
m_states.push(InBody);
bodyWriter->startElement("office:spreadsheet");
parseNode(docbody);
bodyWriter->endElement(); // office:spreadsheet
m_states.pop();
}
// frames
QDomNodeList frameset = doc.elementsByTagName("frameset");
QDomNode frame = frameset.item(0);
if (!frame.isNull()) {
for(int i = 0; i < frameset.length(); ++i) {
for (QDomNode n = frameset.item(i).firstChild(); !n.isNull(); n = n.nextSibling()) {
QDomElement f = n.toElement();
if(!f.isNull() && f.nodeName().toLower() == "frame" && f.attribute("name") == "frSheet")
sheets.append(f.attribute("src"));
}
}
}
}
// the QDOMDocument is no more and we can call us recursively now.
if(!sheets.isEmpty()) {
m_states.push(InFrameset);
foreach(const QString &src, sheets) {
const QUrl u = QUrl::fromLocalFile(QFileInfo(m_inputDir, src).absoluteFilePath());
loadUrl(u);
}
m_states.pop();
}
return KoFilter::OK;
}
void HTMLImport::parseNode(QDomNode node)
{
KoXmlWriter* bodyWriter = m_store->bodyWriter();
//KoXmlWriter* contentWriter = m_store->contentWriter();
// check if this is a text node.
if (node.isText()) {
QDomText t = node.toText();
if(!m_states.isEmpty() && m_states.top() == InCell) {
const QString s = t.data().trimmed();
if(!s.isEmpty()) {
//debugHtml<<"TEXT tagname=" << node.nodeName() << "TEXT="<<t.data().string();
bodyWriter->addAttribute("office:value-type", "string");
bodyWriter->addAttribute("office:string-value", s);
}
}
return; // no children anymore...
}
QString tag = node.nodeName().toLower();
if(tag == "table") {
m_states.push(InTable);
bodyWriter->startElement("table:table");
// hack to get some name defined
static int sheetCount = 0;
bodyWriter->addAttribute("table:name", QString("Sheet %1").arg(++sheetCount));
}
else if(tag == "tr") {
m_states.push(InRow);
bodyWriter->startElement("table:table-row");
//xmlWriter->addAttribute("table:number-columns-spanned", );
//xmlWriter->addAttribute("table:number-rows-spanned", );
}
else if(tag == "td") {
m_states.push(InCell);
bodyWriter->startElement("table:table-cell");
} else {
m_states.push(InNone);
}
//debugHtml<<"...START nodeName="<<node.nodeName();
QDomElement e = node.toElement();
bool go_recursive = true;
if (!e.isNull()) {
//parseStyle(e); // get the CSS information
go_recursive = parseTag(e); // get the tag information
}
if (go_recursive) {
for (QDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling()) {
parseNode(n);
}
}
State state = m_states.pop();
if(state == InTable || state == InRow || state == InCell) {
bodyWriter->endElement();
}
//debugHtml<<"...END nodeName="<<node.nodeName();
}
bool HTMLImport::parseTag(QDomElement element)
{
QString tag = element.tagName().toLower();
// Don't handle the content of comment- or script-nodes.
return !(element.nodeType() == QDomNode::NodeType::CommentNode || tag == "script");
}
#include <htmlimport.moc>
|