summaryrefslogtreecommitdiffstats
path: root/src/translators/bibtexmlexporter.cpp
blob: 85934b293521e75a94a191760a5e4168a47075aa (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
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
/*************************************************************************
    copyright            : (C) 2003-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include <config.h>

#include "bibtexmlexporter.h"
#include "bibtexhandler.h"
#include "../document.h"
#include "../collections/bibtexcollection.h"
#include "../latin1literal.h"
#include "../filehandler.h"
#include "tellico_xml.h"
#include "../stringset.h"

#include <klocale.h>
#include <kdebug.h>

#include <tqvbox.h>
#include <tqdom.h>
#include <tqregexp.h>
#include <tqtextcodec.h>

using Tellico::Export::BibtexmlExporter;

TQString BibtexmlExporter::formatString() const {
  return i18n("Bibtexml");
}

TQString BibtexmlExporter::fileFilter() const {
  return i18n("*.xml|Bibtexml Files (*.xml)") + TQChar('\n') + i18n("*|All Files");
}

bool BibtexmlExporter::exec() {
  Data::CollPtr c = collection();
  if(!c || c->type() != Data::Collection::Bibtex) {
    return false;
  }
  const Data::BibtexCollection* coll = static_cast<const Data::BibtexCollection*>(c.data());

// there are some special fields
// the entry-type specifies the entry type - book, inproceedings, whatever
  TQString typeField;
// the key specifies the cite-key
  TQString keyField;

  const TQString bibtex = TQString::tqfromLatin1("bibtex");
// keep a list of all the 'ordinary' fields to iterate through later
  Data::FieldVec fields;
  Data::FieldVec vec = coll->fields();
  for(Data::FieldVec::Iterator it = vec.begin(); it != vec.end(); ++it) {
    TQString bibtexField = it->property(bibtex);
    if(bibtexField == Latin1Literal("entry-type")) {
      typeField = it->name();
    } else if(bibtexField == Latin1Literal("key")) {
      keyField = it->name();
    } else if(!bibtexField.isEmpty()) {
      fields.append(it);
    }
  }

  TQDomImplementation impl;
  TQDomDocumentType doctype = impl.createDocumentType(TQString::tqfromLatin1("file"),
                                                     TQString(),
                                                     XML::dtdBibtexml);
  //default namespace
  const TQString& ns = XML::nsBibtexml;

  TQDomDocument dom = impl.createDocument(ns, TQString::tqfromLatin1("file"), doctype);

  // root element
  TQDomElement root = dom.documentElement();

  TQString encodeStr = TQString::tqfromLatin1("version=\"1.0\" encoding=\"");
  if(options() & Export::ExportUTF8) {
    encodeStr += TQString::tqfromLatin1("UTF-8");
  } else {
    encodeStr += TQString::tqfromLatin1(TQTextCodec::codecForLocale()->mimeName());
  }
  encodeStr += '"';

  // createDocument creates a root node, insert the processing instruction before it
  dom.insertBefore(dom.createProcessingInstruction(TQString::tqfromLatin1("xml"), encodeStr), root);
  TQString comment = TQString::tqfromLatin1("Generated by Tellico ") + TQString::tqfromLatin1(VERSION);
  dom.insertBefore(dom.createComment(comment), root);

  Data::ConstFieldPtr field;
  Data::FieldVec::ConstIterator fIt, end = fields.constEnd();
  bool format = options() & Export::ExportFormatted;

  StringSet usedKeys;
  TQString type, key, newKey, value, elemName, parElemName;
  TQDomElement btElem, entryElem, parentElem, fieldElem;
  for(Data::EntryVec::ConstIterator entryIt = entries().begin(); entryIt != entries().end(); ++entryIt) {
    key = entryIt->field(keyField);
    if(key.isEmpty()) {
      key = BibtexHandler::bibtexKey(entryIt.data());
    }
    TQString newKey = key;
    char c = 'a';
    while(usedKeys.has(newKey)) {
      // duplicate found!
      newKey = key + c;
      ++c;
    }
    key = newKey;
    usedKeys.add(key);

    btElem = dom.createElement(TQString::tqfromLatin1("entry"));
    btElem.setAttribute(TQString::tqfromLatin1("id"), key);
    root.appendChild(btElem);

    type = entryIt->field(typeField);
    if(type.isEmpty()) {
      kdWarning() << "BibtexmlExporter::exec() - the entry for '" << entryIt->title()
                  << "' has no entry-type, skipping it!" << endl;
      continue;
    }

    entryElem = dom.createElement(type);
    btElem.appendChild(entryElem);

    // now iterate over attributes
    for(fIt = fields.constBegin(); fIt != end; ++fIt) {
      field = fIt.data();
      value = entryIt->field(field->name(), format);
      if(value.isEmpty()) {
        continue;
      }

/* Bibtexml has special container elements for titles, authors, editors, and keywords
   I'm going to ignore the titlelist element for right now. All authors are contained in
   an authorlist element, editors in an editorlist element, and keywords are in a
   keywords element, and themselves as a keyword. Also, Bibtexml can format names
   similar to docbook, with first, middle, last, etc elements. I'm going to ignore that
   for now, too.*/
      elemName = field->property(bibtex);
      // split text for author, editor, and keywords
      if(elemName == Latin1Literal("author") ||
         elemName == Latin1Literal("editor") ||
         elemName == Latin1Literal("keywords")) {
        if(elemName == Latin1Literal("author")) {
          parElemName = TQString::tqfromLatin1("authorlist");
        } else if(elemName == Latin1Literal("editor")) {
          parElemName = TQString::tqfromLatin1("editorlist");
        } else { // keywords
          parElemName = TQString::tqfromLatin1("keywords");
          elemName = TQString::tqfromLatin1("keyword");
        }

        parentElem = dom.createElement(parElemName);
        const TQStringList values = entryIt->fields(field->name(), false);
        for(TQStringList::ConstIterator it = values.begin(); it != values.end(); ++it) {
          fieldElem = dom.createElement(elemName);
          fieldElem.appendChild(dom.createTextNode(*it));
          parentElem.appendChild(fieldElem);
        }
        if(parentElem.hasChildNodes()) {
          entryElem.appendChild(parentElem);
        }
      } else {
        fieldElem = dom.createElement(elemName);
        fieldElem.appendChild(dom.createTextNode(value));
        entryElem.appendChild(fieldElem);
      }
    }
  }

  return FileHandler::writeTextURL(url(), dom.toString(),
                                   options() & ExportUTF8, options() & Export::ExportForce);
}

#include "bibtexmlexporter.moc"