umbrello API Documentation

docbookgenerator.cpp

00001 /***************************************************************************
00002  *                        docbookgenerator.cpp  -  description             *
00003  *                           -------------------                           *
00004  *  copyright            : (C) 2006 by Gael de Chalendar (aka Kleag)       *
00005  *    (C) 2006 Umbrello UML Modeller Authors <uml-devel@uml.sf.net>        *
00006  ***************************************************************************
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  ***************************************************************************/
00014 
00015 #include "docbookgenerator.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 
00042 extern int xmlLoadExtDtdDefaultValue;
00043 
00044 DocbookGenerator::DocbookGenerator()
00045 {
00046 }
00047 
00048 DocbookGenerator::~DocbookGenerator() {}
00049 
00050 
00051 bool DocbookGenerator::generateDocbookForProject()
00052 {
00053   UMLApp *app = UMLApp::app();
00054   UMLDoc* umlDoc = app->getDocument();
00055   KURL url = umlDoc->URL();
00056   QString fileName = url.fileName();
00057   fileName.replace(QRegExp(".xmi$"),"");
00058   url.setFileName(fileName);
00059   kDebug() << "Exporting to directory: " << url << endl;
00060   generateDocbookForProjectInto(url);
00061   return true;
00062 }
00063 
00064 KIO::Job* DocbookGenerator::generateDocbookForProjectInto(const KURL& destDir)
00065 {
00066   UMLApp* app = UMLApp::app();
00067   UMLDoc* umlDoc = app->getDocument();
00068 
00069   // export all views
00070   umlDoc->writeToStatusBar(i18n("Exporting all views..."));
00071   QStringList errors = UMLViewImageExporterModel().exportAllViews(
00072       UMLViewImageExporterModel::mimeTypeToImageType("image/png"),
00073       destDir, false);
00074   if (!errors.empty())
00075   {
00076     KMessageBox::errorList(app, i18n("Some errors happened when exporting the images:"), errors);
00077     return 0;
00078   }
00079 
00080   //write the XMI model in an in-memory char* string
00081   QString xmi;
00082   QTextOStream xmiStream(&xmi);
00083 
00084   KTempFile tmpfile; // we need this tmp file if we are writing to a remote file
00085 
00086   QFile file;
00087   file.setName( tmpfile.name() );
00088 
00089   // lets open the file for writing
00090   if( !file.open( IO_WriteOnly ) ) {
00091     KMessageBox::error(0, i18n("There was a problem saving file: %1").arg(tmpfile.name()), i18n("Save Error"));
00092     return false;
00093   }
00094   umlDoc->saveToXMI(file); // save the xmi stuff to it
00095   file.close();
00096   tmpfile.close();
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 xsltFile(KGlobal::dirs()->findResource("appdata","xmi2docbook.xsl"));
00106 
00107   xmlSubstituteEntitiesDefault(1);
00108   xmlLoadExtDtdDefaultValue = 1;
00109   cur = xsltParseStylesheetFile((const xmlChar *)xsltFile.latin1());
00110   doc = xmlParseFile((const char*)(tmpfile.name().utf8()));
00111   res = xsltApplyStylesheet(cur, doc, params);
00112 
00113   KTempFile tmpDocBook;
00114   tmpDocBook.setAutoDelete(false);
00115 
00116   xsltSaveResultToFile(tmpDocBook.fstream(), res, cur);
00117   xsltFreeStylesheet(cur);
00118   xmlFreeDoc(res);
00119   xmlFreeDoc(doc);
00120 
00121   xsltCleanupGlobals();
00122   xmlCleanupParser();
00123 
00124   KURL url = umlDoc->URL();
00125   QString fileName = url.fileName();
00126   fileName.replace(QRegExp(".xmi$"),".docbook");
00127   url.setPath(destDir.path());
00128   url.addPath(fileName);
00129   kDebug() << "Copying result to: " << url << endl;
00130   KIO::Job* job = KIO::file_copy(tmpDocBook.file()->name(),url,-1,true,false,false);
00131   job->setAutoErrorHandlingEnabled(true);
00132 
00133   return job;
00134 }
00135 
00136 
00137 #include "docbookgenerator.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:56 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003