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
|
/***************************************************************************
* Copyright (C) 2004-2009 by Thomas Fischer *
* fischer@unix-ag.uni-kl.de *
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <ntqfile.h>
#include <ntqdir.h>
#include <ntqstringlist.h>
#include <ntqapplication.h>
#include <ntqbuffer.h>
#include <ntqprocess.h>
#include <ntqwaitcondition.h>
#include <fileexporterbibtex.h>
#include "fileexporterbibutils.h"
namespace BibTeX
{
FileExporterBibUtils::FileExporterBibUtils( BibTeX::File::FileFormat outputFormat )
: FileExporter(), m_outputFormat( outputFormat ), m_bibTeXExporter( new FileExporterBibTeX() )
{
m_bibTeXExporter->setEncoding( "utf-8" );
m_processBuffer = new TQBuffer();
}
FileExporterBibUtils::~FileExporterBibUtils()
{
delete m_processBuffer;
delete m_bibTeXExporter;
}
bool FileExporterBibUtils::save( TQIODevice* iodevice, const Element* element, TQStringList* errorLog )
{
m_cancelFlag = false;
TQBuffer bibBuffer;
if ( !toBuffer( element, &bibBuffer, errorLog ) )
return false;
if ( !bufferToXMLbuffer( &bibBuffer ) || m_cancelFlag )
return false;
return !m_cancelFlag && xmlBufferToIOdevice( iodevice );
}
bool FileExporterBibUtils::save( TQIODevice* iodevice, const File* bibtexfile, TQStringList* errorLog )
{
emit progress( 0, 3 );
m_cancelFlag = false;
TQBuffer bibBuffer;
if ( !toBuffer( bibtexfile, &bibBuffer, errorLog ) )
return false;
emit progress( 1, 3 );
if ( m_cancelFlag || !bufferToXMLbuffer( &bibBuffer ) )
return false;
emit progress( 2, 3 );
if ( m_cancelFlag || !xmlBufferToIOdevice( iodevice ) )
return false;
emit progress( 3, 3 );
return !m_cancelFlag;
}
void FileExporterBibUtils::cancel()
{
m_bibTeXExporter->cancel();
m_cancelFlag = true;
}
bool FileExporterBibUtils::toBuffer( const File *bibFile, TQBuffer *buffer, TQStringList* errorLog )
{
buffer->open( IO_WriteOnly );
bool result = m_bibTeXExporter->save( buffer, bibFile, errorLog );
buffer->close();
return result;
}
bool FileExporterBibUtils::toBuffer( const Element *bibElement, TQBuffer *buffer, TQStringList* errorLog )
{
buffer->open( IO_WriteOnly );
bool result = m_bibTeXExporter->save( buffer, bibElement, errorLog );
buffer->close();
return result;
}
bool FileExporterBibUtils::bufferToXMLbuffer( TQBuffer *bibBuffer )
{
TQWaitCondition wc;
m_processBuffer->open( IO_WriteOnly );
m_waiting = true;
m_process = new TQProcess( TQStringList::split( ' ', "bib2xml -i utf8" ) );
connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );
m_process->start();
if ( m_process->isRunning() )
{
bibBuffer->open( IO_ReadOnly );
m_process->writeToStdin( bibBuffer->readAll() );
tqApp->processEvents();
m_process->closeStdin();
bibBuffer->close();
int nothingHappens = 20;
while ( m_waiting )
{
wc.wait( 250 );
tqApp->processEvents();
--nothingHappens;
}
if ( nothingHappens <= 0 )
m_process->kill();
if ( !m_process->normalExit() )
{
tqDebug( "%s did not exit in a clean fashion", m_process->arguments()[0].latin1() );
delete m_process;
return false;
}
}
else
{
tqDebug( "%s did not start", m_process->arguments()[0].latin1() );
delete m_process;
return false;
}
m_processBuffer->close();
delete m_process;
return true;
}
bool FileExporterBibUtils::xmlBufferToIOdevice( TQIODevice *iodevice )
{
TQWaitCondition wc;
m_waiting = true;
m_process = NULL;
switch ( m_outputFormat )
{
case BibTeX::File::formatISI:
m_process = new TQProcess( TQStringList::split( ' ', "xml2isi" ) );
break;
case BibTeX::File::formatWordBib:
m_process = new TQProcess( TQStringList::split( ' ', "xml2wordbib" ) );
break;
case BibTeX::File::formatAds:
m_process = new TQProcess( TQStringList::split( ' ', "xml2ads" ) );
break;
case BibTeX::File::formatEndNote:
m_process = new TQProcess( TQStringList::split( ' ', "xml2end" ) );
break;
case BibTeX::File::formatRIS:
m_process = new TQProcess( TQStringList::split( ' ', "xml2ris" ) );
break;
case BibTeX::File::formatMODS:
/* m_process = NULL; */
break;
default:
tqDebug( "Cannot handle output format %i", m_outputFormat );
return false;
}
if ( m_process != NULL )
{
connect( m_process, SIGNAL( processExited() ), this, SLOT( wakeUp() ) );
connect( m_process, SIGNAL( readyReadStdout() ), this, SLOT( slotReadyStdout() ) );
connect( m_process, SIGNAL( readyReadStderr() ), this, SLOT( slotReadyStderr() ) );
if ( m_process->start() )
{
TQBuffer *tempBuffer = m_processBuffer;
m_processBuffer = new TQBuffer();
tempBuffer->open( IO_ReadOnly );
m_process->writeToStdin( tempBuffer->readAll() );
tqApp->processEvents();
m_process->closeStdin();
tempBuffer->close();
m_processBuffer->open( IO_WriteOnly );
int nothingHappens = 20;
while ( m_waiting )
{
wc.wait( 250 );
tqApp->processEvents();
--nothingHappens;
}
m_processBuffer->close();
delete tempBuffer;
if ( nothingHappens <= 0 )
m_process->kill();
if ( ! m_process->normalExit() )
{
delete m_process;
return false;
}
m_processBuffer->open( IO_ReadOnly );
iodevice->writeBlock( m_processBuffer->buffer() );
m_processBuffer->close();
delete m_process;
}
else
{
tqDebug( "%s did not start", m_process->arguments()[0].latin1() );
delete m_process;
return false;
}
}
else
{
m_processBuffer->open( IO_ReadOnly );
iodevice->writeBlock( m_processBuffer->buffer() );
m_processBuffer->close();
}
return true;
}
void FileExporterBibUtils::wakeUp()
{
m_waiting = false;
}
void FileExporterBibUtils::slotReadyStdout()
{
m_processBuffer->writeBlock( m_process->readStdout() );
}
void FileExporterBibUtils::slotReadyStderr()
{
TQByteArray ba = m_process->readStderr();
TQTextStream bats( ba, IO_ReadOnly );
bats.setEncoding( TQTextStream::UnicodeUTF8 );
tqDebug( "%s", bats.read().latin1() );
}
}
#include "fileexporterbibutils.moc"
|