xhtmlgenerator.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "xhtmlgenerator.h"
00016
00017 #include <libxml/xmlmemory.h>
00018 #include <libxml/debugXML.h>
00019 #include <libxml/HTMLtree.h>
00020 #include <libxml/xmlIO.h>
00021 #include <libxml/xinclude.h>
00022 #include <libxml/catalog.h>
00023 #include <libxslt/xslt.h>
00024 #include <libxslt/xsltInternals.h>
00025 #include <libxslt/transform.h>
00026 #include <libxslt/xsltutils.h>
00027
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <ktempfile.h>
00031 #include <kmessagebox.h>
00032 #include <kio/job.h>
00033 #include <kstandarddirs.h>
00034 #include <qfile.h>
00035 #include <qregexp.h>
00036 #include <qtextstream.h>
00037
00038 #include "uml.h"
00039 #include "umldoc.h"
00040 #include "umlviewimageexportermodel.h"
00041 #include "docbookgenerator.h"
00042
00043 extern int xmlLoadExtDtdDefaultValue;
00044
00045 XhtmlGenerator::XhtmlGenerator()
00046 {
00047 }
00048
00049 XhtmlGenerator::~XhtmlGenerator() {}
00050
00051
00052 bool XhtmlGenerator::generateXhtmlForProject()
00053 {
00054 UMLApp *app = UMLApp::app();
00055 UMLDoc* umlDoc = app->getDocument();
00056 KURL url = umlDoc->URL();
00057 QString fileName = url.fileName();
00058 fileName.replace(QRegExp(".xmi$"),"");
00059 url.setFileName(fileName);
00060 kDebug() << "Exporting to directory: " << url << endl;
00061 return generateXhtmlForProjectInto(url);
00062 }
00063
00064 bool XhtmlGenerator::generateXhtmlForProjectInto(const KURL& destDir)
00065 {
00066 kDebug() << "First convert to docbook" << endl;
00067 m_destDir = destDir;
00068
00069 KIO::Job* docbookJob = DocbookGenerator().generateDocbookForProjectInto(destDir);
00070 if (docbookJob == 0)
00071 {
00072 return false;
00073 }
00074 kDebug() << "Connecting..." << endl;
00075 connect(docbookJob, SIGNAL(result( KIO::Job * )), this, SLOT(slotDocbookToXhtml( KIO::Job *)));
00076 return true;
00077 }
00078
00079 void XhtmlGenerator::slotDocbookToXhtml(KIO::Job * docbookJob)
00080 {
00081 kDebug() << "Now convert docbook to html..." << endl;
00082 if ( docbookJob->error() )
00083 {
00084 docbookJob->showErrorDialog( 0L );
00085 return;
00086 }
00087
00088 UMLApp* app = UMLApp::app();
00089 UMLDoc* umlDoc = app->getDocument();
00090
00091 const KURL& url = umlDoc->URL();
00092 QString docbookName = url.fileName();
00093 docbookName.replace(QRegExp(".xmi$"),".docbook");
00094
00095 KURL docbookUrl = m_destDir;
00096 docbookUrl.addPath(docbookName);
00097
00098 xsltStylesheetPtr cur = NULL;
00099 xmlDocPtr doc, res;
00100
00101 const char *params[16 + 1];
00102 int nbparams = 0;
00103 params[nbparams] = NULL;
00104
00105 QString xsltFileName(KGlobal::dirs()->findResource("appdata","docbook2xhtml.xsl"));
00106 kDebug() << "XSLT file is'"<<xsltFileName<<"'" << endl;
00107 QFile xsltFile(xsltFileName);
00108 xsltFile.open(IO_ReadOnly);
00109 QString xslt = xsltFile.readAll();
00110 kDebug() << "XSLT is'"<<xslt<<"'" << endl;
00111 xsltFile.close();
00112
00113 QString localXsl = KGlobal::dirs()->findResource("data","ksgmltools2/docbook/xsl/html/docbook.xsl");
00114 kDebug() << "Local xsl is'"<<localXsl<<"'" << endl;
00115 if (!localXsl.isEmpty())
00116 {
00117 localXsl = QString("href=\"file://") + localXsl + "\"";
00118 xslt.replace(QRegExp("href=\"http://[^\"]*\""),localXsl);
00119 }
00120 KTempFile tmpXsl;
00121 *tmpXsl.textStream() << xslt;
00122 tmpXsl.file()->close();
00123
00124
00125 xmlSubstituteEntitiesDefault(1);
00126 xmlLoadExtDtdDefaultValue = 1;
00127 kDebug() << "Parsing stylesheet " << tmpXsl.name() << endl;
00128 cur = xsltParseStylesheetFile((const xmlChar *)tmpXsl.name().latin1());
00129 kDebug() << "Parsing file " << docbookUrl.path() << endl;
00130 doc = xmlParseFile((const char*)(docbookUrl.path().utf8()));
00131 kDebug() << "Applying stylesheet " << endl;
00132 res = xsltApplyStylesheet(cur, doc, params);
00133
00134 KTempFile tmpXhtml;
00135 tmpXhtml.setAutoDelete(false);
00136
00137 kDebug() << "Writing HTML result to temp file: " << tmpXhtml.file()->name() << endl;
00138 xsltSaveResultToFile(tmpXhtml.fstream(), res, cur);
00139
00140 xsltFreeStylesheet(cur);
00141 xmlFreeDoc(res);
00142 xmlFreeDoc(doc);
00143
00144 xsltCleanupGlobals();
00145 xmlCleanupParser();
00146
00147 QString xhtmlName = url.fileName();
00148 xhtmlName.replace(QRegExp(".xmi$"),".html");
00149 KURL xhtmlUrl = m_destDir;
00150 xhtmlUrl.addPath(xhtmlName);
00151
00152 kDebug() << "Copying HTML result to: " << xhtmlUrl << endl;
00153 KIO::Job* job = KIO::file_copy(tmpXhtml.file()->name(),xhtmlUrl,-1,true,false,false);
00154 job->setAutoErrorHandlingEnabled(true);
00155 connect (job, SIGNAL(result( KIO::Job* )), this, SLOT(slotHtmlCopyFinished( KIO::Job* )));
00156
00157 QString cssFileName(KGlobal::dirs()->findResource("appdata","xmi.css"));
00158 kDebug() << "CSS file is'"<<cssFileName<<"'" << endl;
00159 KURL cssUrl = m_destDir;
00160 cssUrl.addPath("xmi.css");
00161 KIO::Job* cssJob = KIO::file_copy(cssFileName,cssUrl,-1,true,false,false);
00162 cssJob->setAutoErrorHandlingEnabled(true);
00163 }
00164
00165 void XhtmlGenerator::slotHtmlCopyFinished( KIO::Job* )
00166 {
00167 kDebug() << "HTML copy finished: emiting finished" << endl;
00168 emit(finished());
00169 }
00170
00171 #include "xhtmlgenerator.moc"
This file is part of the documentation for umbrello Version 3.1.0.