summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/docgenerators/docbookgenerator.cpp
blob: bc7107f8d5fd2cccea25075fc7e570f162aecd4d (plain)
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
/***************************************************************************
 *                        docbookgenerator.cpp  -  description             *
 *                           -------------------                           *
 *  copyright            : (C) 2006 by Gael de Chalendar (aka Kleag)       *
 *    (C) 2006 Umbrello UML Modeller Authors <uml-devel@uml.sf.net>        *
 ***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "docbookgenerator.h"

#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <tdetempfile.h>
#include <tdemessagebox.h>
#include <tdeio/job.h>
#include <kstandarddirs.h>
#include <tqfile.h>
#include <tqregexp.h>
#include <tqtextstream.h>

#include "uml.h"
#include "umldoc.h"
#include "umlviewimageexportermodel.h"

extern int xmlLoadExtDtdDefaultValue;

DocbookGenerator::DocbookGenerator()
{
}

DocbookGenerator::~DocbookGenerator() {}


bool DocbookGenerator::generateDocbookForProject()
{
  UMLApp *app = UMLApp::app();
  UMLDoc* umlDoc = app->getDocument();
  KURL url = umlDoc->URL();
  TQString fileName = url.fileName();
  fileName.replace(TQRegExp(".xmi$"),"");
  url.setFileName(fileName);
  kDebug() << "Exporting to directory: " << url << endl;
  generateDocbookForProjectInto(url);
  return true;
}

TDEIO::Job* DocbookGenerator::generateDocbookForProjectInto(const KURL& destDir)
{
  UMLApp* app = UMLApp::app();
  UMLDoc* umlDoc = app->getDocument();

  // export all views
  umlDoc->writeToStatusBar(i18n("Exporting all views..."));
  TQStringList errors = UMLViewImageExporterModel().exportAllViews(
      UMLViewImageExporterModel::mimeTypeToImageType("image/png"),
      destDir, false);
  if (!errors.empty())
  {
#if KDE_IS_VERSION(3,4,0)
    KMessageBox::errorList(app, i18n("Some errors happened when exporting the images:"), errors);
#else
    TQString errorsCaption;
    for (TQStringList::Iterator it = errors.begin(); it != errors.end(); ++it) {
        errorsCaption += "\n" + *it;
    }
    KMessageBox::error(app, i18n("Some errors happened when exporting the images:") + errorsCaption);
#endif
    return 0;
  }

  //write the XMI model in an in-memory char* string
  TQString xmi;
  TQTextOStream xmiStream(&xmi);

  KTempFile tmpfile; // we need this tmp file if we are writing to a remote file

  TQFile file;
  file.setName( tmpfile.name() );

  // lets open the file for writing
  if( !file.open( IO_WriteOnly ) ) {
    KMessageBox::error(0, i18n("There was a problem saving file: %1").arg(tmpfile.name()), i18n("Save Error"));
    return false;
  }
  umlDoc->saveToXMI(*TQT_TQIODEVICE(&file)); // save the xmi stuff to it
  file.close();
  tmpfile.close();

  xsltStylesheetPtr cur = NULL;
  xmlDocPtr doc, res;

  const char *params[16 + 1];
  int nbparams = 0;
  params[nbparams] = NULL;

  TQString xsltFile(TDEGlobal::dirs()->findResource("appdata","xmi2docbook.xsl"));

  xmlSubstituteEntitiesDefault(1);
  xmlLoadExtDtdDefaultValue = 1;
  cur = xsltParseStylesheetFile((const xmlChar *)xsltFile.latin1());
  doc = xmlParseFile((const char*)(tmpfile.name().utf8()));
  res = xsltApplyStylesheet(cur, doc, params);

  KTempFile tmpDocBook;
  tmpDocBook.setAutoDelete(false);

  xsltSaveResultToFile(tmpDocBook.fstream(), res, cur);
  xsltFreeStylesheet(cur);
  xmlFreeDoc(res);
  xmlFreeDoc(doc);

  xsltCleanupGlobals();
  xmlCleanupParser();

  KURL url = umlDoc->URL();
  TQString fileName = url.fileName();
  fileName.replace(TQRegExp(".xmi$"),".docbook");
  url.setPath(destDir.path());
  url.addPath(fileName);
  kDebug() << "Copying result to: " << url << endl;
  TDEIO::Job* job = TDEIO::file_copy(tmpDocBook.file()->name(),url,-1,true,false,false);
  job->setAutoErrorHandlingEnabled(true);

  return job;
}


#include "docbookgenerator.moc"