summaryrefslogtreecommitdiffstats
path: root/src/fileimporterbibtex.cpp
blob: 3fc98fa8ae87adb1ada53cb2d6a3177e24bd30c6 (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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/***************************************************************************
*   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 <tqiodevice.h>
#include <tqregexp.h>
#include <tqapplication.h>

#include "file.h"
#include "comment.h"
#include "macro.h"
#include "preamble.h"
#include "entry.h"
#include "element.h"
#include "encoderlatex.h"
#include "value.h"

#include "fileimporterbibtex.h"

#define max(a,b)  ((a)<(b)?(b):(a))

namespace BibTeX
{
    const TQString extraAlphaNumChars = TQString( "?'`-_:.+/$\\\"&" );
    const TQRegExp htmlRegExp = TQRegExp( "</?(a|pre)[^>]*>", false );

    FileImporterBibTeX::FileImporterBibTeX( bool personFirstNameFirst, TQString encoding ) : FileImporter(), m_personFirstNameFirst( personFirstNameFirst ), m_currentChar( ' ' ), m_ignoreComments( FALSE ), m_lineBufferSize( 4096 ), m_encoding( encoding )
    {
        cancelFlag = FALSE;
        m_lineBuffer = new char[m_lineBufferSize];
        m_textStream = NULL;
    }


    FileImporterBibTeX::~FileImporterBibTeX()
    {
        delete[] m_lineBuffer;
    }

    File* FileImporterBibTeX::load( TQIODevice *iodevice )
    {
        m_mutex.lock();
        cancelFlag = FALSE;

        TQString rawText;
        const char *encodingFrom = m_encoding == "latex" ? "utf-8\0" : m_encoding.append( "\0" ).ascii();
        iconv_t iconvHandle = iconv_open( "utf-8", encodingFrom );
        char *convertedLine = new char[m_lineBufferSize * 4];
        int len;
        bool encodingOk = true;
        while ( encodingOk && iodevice->isReadable() && ( len = iodevice->readLine( m_lineBuffer, m_lineBufferSize ) ) > 0 )
        {
            evaluateParameterComments( iconvHandle, m_lineBuffer );

            char *raw = m_lineBuffer;
            char *enc = convertedLine;
            size_t encLen = m_lineBufferSize, rawLen = ( size_t )len;
            size_t result = iconv( iconvHandle, &raw, &rawLen, &enc, &encLen );

            tqApp->processEvents();

            if ( result != 0 )
            {
                TQString problematic = TQString( m_lineBuffer ).mid( max( 0, m_lineBufferSize - encLen - 15 ), 30 );
                if ( problematic.isNull() || problematic.isEmpty() ) problematic = TQString( m_lineBuffer );
                tqDebug( "iconv resulted in error code %i for source encoding %s, maybe file is in different encoding? Problem is somewhere here: \"%s\"", result, encodingFrom, problematic.latin1() );
                encodingOk = false;
                break;
            }
            if ( rawLen > 0 )
            {
                tqDebug( "iconv could not convert complete string, only %i out of %i chars", len - rawLen, len );
                encodingOk = false;
                break;
            }
            enc[0] = '\0';

            /** remove leading UTF-8 byte-order mark (BOM)  */
            int offset = 0;
            while (((( unsigned char )convertedLine[offset] ) == 0xef || (( unsigned char )convertedLine[offset] ) == 0xbb || (( unsigned char )convertedLine[offset] ) == 0xbf ) && offset < 4 )
                ++offset;

            TQString line = TQString::fromUtf8( convertedLine + offset );
            rawText.append( line );
        }
        iconv_close( iconvHandle );
        delete[] convertedLine;

        if ( !encodingOk )
        {
            tqDebug( "Decoding failed, cannot load file. Please fix encoding manually." );
            m_mutex.unlock();
            return NULL;
        }

        /** Cleaning up code comming from DBLP */
        rawText = rawText.replace( htmlRegExp, "" );
        rawText = EncoderLaTeX::currentEncoderLaTeX() ->decode( rawText );
        unescapeLaTeXChars( rawText );
        m_textStream = new TQTextStream( rawText, IO_ReadOnly );
        m_textStream->setEncoding( TQTextStream::UnicodeUTF8 );
        m_currentLineNumber = 0;
        m_posIntCurrentLine = 0;
        m_currentLine = "";

        File *result = new File();
        TQIODevice *streamDevice = m_textStream->device();
        while ( !cancelFlag && !m_textStream->atEnd() )
        {
            emit progress( streamDevice->at(), streamDevice->size() );
            tqApp->processEvents();
            Element * element = nextElement();
            if ( element != NULL )
            {
                Comment *comment = dynamic_cast<Comment*>( element );
                if ( !m_ignoreComments || comment == NULL )
                    result->appendElement( element );
                else
                    delete element;
            }
            tqApp->processEvents();
        }
        emit progress( streamDevice->size(), streamDevice->size() );

        if ( cancelFlag )
        {
            tqDebug( "Loading file has been canceled" );
            delete result;
            result = NULL;
        }

        delete m_textStream;

        m_mutex.unlock();
        return result;
    }

    bool FileImporterBibTeX::guessCanDecode( const TQString & rawText )
    {
        TQString text = EncoderLaTeX::currentEncoderLaTeX() ->decode( rawText );
        return text.find( TQRegExp( "@\\w+\\{.+\\}" ) ) >= 0;
    }

    void FileImporterBibTeX::setIgnoreComments( bool ignoreComments )
    {
        m_ignoreComments = ignoreComments;
    }

    void FileImporterBibTeX::cancel()
    {
        cancelFlag = TRUE;
    }

    Element *FileImporterBibTeX::nextElement()
    {
        Token token = nextToken();

        if ( token == tAt )
        {
            TQString elementType = readSimpleString();
            if ( elementType.lower() == "comment" )
                return readCommentElement();
            else if ( elementType.lower() == "string" )
                return readMacroElement();
            else if ( elementType.lower() == "preamble" )
                return readPreambleElement();
            else if ( !elementType.isEmpty() )
                return readEntryElement( elementType );
            else
            {
                tqDebug( "ElementType is empty" );
                return NULL;
            }
        }
        else if ( token == tUnknown )
        {
            tqDebug( "Unknown token near line %i, treating as comment", m_currentLineNumber );
            return readPlainCommentElement();
        }

        if ( token != tEOF )
            tqDebug( "Don't know how to parse next token near line %i: %s", m_currentLineNumber, tokenidToString( token ).latin1() );

        return NULL;
    }

    Comment *FileImporterBibTeX::readCommentElement()
    {
        while ( m_currentChar != '{' && m_currentChar != '(' && !m_textStream->atEnd() )
            m_currentChar = nextChar();

        return new Comment( readBracketString( m_currentChar ), TRUE );
    }

    Comment *FileImporterBibTeX::readPlainCommentElement()
    {
        TQString result = m_currentChar;
        result += readLine();
        m_currentChar = nextChar();
        while ( !m_textStream->atEnd() && m_currentChar != '@' && !m_currentChar.isSpace() )
        {
            result.append( '\n' ).append( m_currentChar );
            m_currentChar = nextChar();
            result.append( readLine() );
            m_currentChar = nextChar();
        }
        return new Comment( result, FALSE );
    }

    Macro *FileImporterBibTeX::readMacroElement()
    {
        Token token = nextToken();
        while ( token != tBracketOpen )
        {
            if ( token == tEOF )
            {
                tqDebug( "Error in parsing unknown macro (near line %i): Opening curly brace ({) expected", m_currentLineNumber );
                return NULL;
            }
            token = nextToken();
        }

        TQString key = readSimpleString();
        if ( nextToken() != tAssign )
        {
            tqDebug( "Error in parsing macro '%s' (near line %i): Assign symbol (=) expected", key.latin1(), m_currentLineNumber );
            return NULL;
        }

        Macro *macro = new Macro( key );
        do
        {
            bool isStringKey = FALSE;
            TQString text = readString( isStringKey ).replace( TQRegExp( "\\s+" ), " " );
            if ( isStringKey )
                macro->value()->items.append( new MacroKey( text ) );
            else
                macro->value()->items.append( new BibTeX::PlainText( text ) );

            token = nextToken();
        }
        while ( token == tDoublecross );

        return macro;
    }

    Preamble *FileImporterBibTeX::readPreambleElement()
    {
        Token token = nextToken();
        while ( token != tBracketOpen )
        {
            if ( token == tEOF )
            {
                tqDebug( "Error in parsing unknown preamble (near line %i): Opening curly brace ({) expected", m_currentLineNumber );
                return NULL;
            }
            token = nextToken();
        }

        Preamble *preamble = new Preamble( );
        do
        {
            bool isStringKey = FALSE;
            TQString text = readString( isStringKey ).replace( TQRegExp( "\\s+" ), " " );
            if ( isStringKey )
                preamble->value()->items.append( new MacroKey( text ) );
            else
                preamble->value()->items.append( new BibTeX::PlainText( text ) );

            token = nextToken();
        }
        while ( token == tDoublecross );

        return preamble;
    }

    Entry *FileImporterBibTeX::readEntryElement( const TQString& typeString )
    {
        Token token = nextToken();
        while ( token != tBracketOpen )
        {
            if ( token == tEOF )
            {
                tqDebug( "Error in parsing unknown entry (near line %i): Opening curly brace ({) expected", m_currentLineNumber );
                return NULL;
            }
            token = nextToken();
        }

        TQString key = readSimpleString();
        Entry *entry = new Entry( typeString, key );

        token = nextToken();
        do
        {
            if ( token == tBracketClose || token == tEOF )
                break;
            else if ( token != tComma )
            {
                tqDebug( "Error in parsing entry '%s' (near line %i): Comma symbol (,) expected but got 0x%x (token %s)", key.latin1(), m_currentLineNumber, m_currentChar.unicode(), tokenidToString( token ).latin1() );
                delete entry;
                return NULL;
            }

            TQString fieldTypeName = readSimpleString();
            token = nextToken();
            if ( fieldTypeName == TQString::null || token == tBracketClose )
            {
                // entry is buggy, but we still accept it
                break;
            }
            else if ( token != tAssign )
            {
                tqDebug( "Error in parsing entry '%s' (near line %i): Assign symbol (=) expected after field name '%s'", key.latin1(), m_currentLineNumber, fieldTypeName.latin1() );
                delete entry;
                return NULL;
            }

            /** check for duplicate fields */
            if ( entry->getField( fieldTypeName ) != NULL )
            {
                int i = 1;
                TQString appendix = TQString::number( i );
                while ( entry->getField( fieldTypeName + appendix ) != NULL )
                {
                    ++i;
                    appendix = TQString::number( i );
                }
                fieldTypeName += appendix;
            }

            EntryField *entryField = new EntryField( fieldTypeName );

            token = readValue( entryField->value(), entryField->fieldType() );

            entry->addField( entryField );
        }
        while ( TRUE );

        return entry;
    }

    FileImporterBibTeX::Token FileImporterBibTeX::nextToken()
    {
        if ( m_textStream->atEnd() )
            return tEOF;

        Token curToken = tUnknown;

        while (( m_currentChar.isSpace() || m_currentChar == '\t' ) && !m_textStream->atEnd() )
            m_currentChar = nextChar();

        switch ( m_currentChar.latin1() )
        {
        case '@':
            curToken = tAt;
            break;
        case '{':
        case '(':
            curToken = tBracketOpen;
            break;
        case '}':
        case ')':
            curToken = tBracketClose;
            break;
        case ',':
            curToken = tComma;
            break;
        case '=':
            curToken = tAssign;
            break;
        case '#':
            curToken = tDoublecross;
            break;
        default:
            if ( m_textStream->atEnd() )
                curToken = tEOF;
        }

        if ( curToken != tUnknown && curToken != tEOF )
            m_currentChar = nextChar();

        return curToken;
    }

    TQString FileImporterBibTeX::readString( bool &isStringKey )
    {
        while ( m_currentChar.isSpace() )
            m_currentChar = nextChar();

        isStringKey = FALSE;
        switch ( m_currentChar.latin1() )
        {
        case '{':
        case '(':
            return readBracketString( m_currentChar );
        case '"':
            return readQuotedString();
        default:
            isStringKey = TRUE;
            return readSimpleString();
        }
    }

    TQString FileImporterBibTeX::readSimpleString( TQChar until )
    {
        TQString result;

        while ( m_currentChar.isSpace() )
            m_currentChar = nextChar();

        if ( m_currentChar.isLetterOrNumber() || extraAlphaNumChars.contains( m_currentChar ) )
        {
            result.append( m_currentChar );
            m_currentChar = nextChar();
        }

        while ( !m_textStream->atEnd() )
        {
            if ( until != '\0' )
            {
                if ( m_currentChar != until )
                    result.append( m_currentChar );
                else
                    break;
            }
            else
                if ( m_currentChar.isLetterOrNumber() || extraAlphaNumChars.contains( m_currentChar ) )
                    result.append( m_currentChar );
                else if ( m_currentChar == "," || m_currentChar == "(" || m_currentChar == ")" || m_currentChar == "{" || m_currentChar == "}" || m_currentChar == "=" || m_currentChar == "#" || m_currentChar == "@" || m_currentChar.isSpace() )
                    break;
                else
                {
                    tqDebug( "Unknown letter or number: 0x%x", m_currentChar.unicode() );
                    //              break;
                }
            m_currentChar = nextChar();
        }
        return result;
    }

    TQString FileImporterBibTeX::readQuotedString()
    {
        TQString result;
        TQChar lastChar = m_currentChar;
        m_currentChar = nextChar();
        while ( !m_textStream->atEnd() )
        {
            if ( m_currentChar != '"' || lastChar == '\\' )
                result.append( m_currentChar );
            else
                break;
            lastChar = m_currentChar;
            m_currentChar = nextChar();
        }

        /** read character after closing " */
        m_currentChar = nextChar();

        return result;
    }

    TQString FileImporterBibTeX::readLine()
    {
        TQString result = m_currentLine.mid( m_posIntCurrentLine );
        m_posIntCurrentLine = m_currentLine.length() + 2;
        return result;
    }

    TQString FileImporterBibTeX::readBracketString( const TQChar openingBracket )
    {
        TQString result;
        TQChar closingBracket = '}';
        if ( openingBracket == '(' )
            closingBracket = ')';
        int counter = 1;
        m_currentChar = nextChar();
        while ( !m_textStream->atEnd() )
        {
            if ( m_currentChar == openingBracket )
                counter++;
            else if ( m_currentChar == closingBracket )
                counter--;

            if ( counter == 0 )
                break;
            else
                result.append( m_currentChar );
            m_currentChar = nextChar();
        }
        m_currentChar = nextChar();
        return result;
    }

    FileImporterBibTeX::Token FileImporterBibTeX::readValue( Value *value, EntryField::FieldType fieldType )
    {
        Token token = tUnknown;

        do
        {
            bool isStringKey = FALSE;
            TQString text = readString( isStringKey ).replace( TQRegExp( "\\s+" ), " " );

            switch ( fieldType )
            {
            case EntryField::ftKeywords:
            {
                if ( isStringKey )
                    tqDebug( "WARNING: Cannot handle keywords that are macros" );
                else
                    value->items.append( new KeywordContainer( text ) );
            }
            break;
            case EntryField::ftAuthor:
            case EntryField::ftEditor:
            {
                if ( isStringKey )
                    tqDebug( "WARNING: Cannot handle authors/editors that are macros" );
                else
                {
                    TQStringList persons;
                    splitPersons( text, persons );
                    PersonContainer *container = new PersonContainer( m_personFirstNameFirst );
                    for ( TQStringList::ConstIterator pit = persons.constBegin(); pit != persons.constEnd(); ++pit )
                        container->persons.append( new Person( *pit, m_personFirstNameFirst ) );
                    value->items.append( container );
                }
            }
            break;
            case EntryField::ftPages:
                text.replace( TQRegExp( "\\s*--?\\s*" ), TQChar( 0x2013 ) );
            default:
            {
                if ( isStringKey )
                    value->items.append( new MacroKey( text ) );
                else
                    value->items.append( new BibTeX::PlainText( text ) );
            }
            }

            token = nextToken();
        }
        while ( token == tDoublecross );

        return token;
    }

    void FileImporterBibTeX::unescapeLaTeXChars( TQString &text )
    {
        text.replace( "\\&", "&" );
    }

    void FileImporterBibTeX::splitPersons( const TQString& text, TQStringList &persons )
    {
        TQStringList wordList;
        TQString word;
        int bracketCounter = 0;

        for ( unsigned int pos = 0;pos < text.length();++pos )
        {
            if ( text[pos] == '{' )
                ++bracketCounter;
            else if ( text[pos] == '}' )
                --bracketCounter;

            if ( text[pos] == ' ' || text[pos] == '\n' || text[pos] == '\r' )
            {
                if ( word == "and" && bracketCounter == 0 )
                {
                    persons.append( wordList.join( " " ) );
                    wordList.clear();
                }
                else if ( !word.isEmpty() )
                    wordList.append( word );

                word = "";
            }
            else
                word.append( text[pos] );
        }

        wordList.append( word );
        persons.append( wordList.join( " " ) );
    }

    void FileImporterBibTeX::evaluateParameterComments( iconv_t &iconvHandle, const char *cline )
    {
        /** simple preliminary checks before expensive conversion to TQString */
        if ( cline[0] == '@' && cline[1] == 'c' )
        {
            TQString line = TQString( cline ).lower();
            /** check if this file requests a special encoding */
            if ( line.startsWith( "@comment{x-kbibtex-encoding=" ) && line.endsWith( "}\n" ) )
            {
                TQString newEncoding = line.mid( 28, line.length() - 30 );
                tqDebug( "x-kbibtex-encoding=<%s>", newEncoding.latin1() );
                if ( newEncoding == "latex" ) newEncoding = "utf-8";
                iconv_close( iconvHandle );
                iconvHandle = iconv_open( "utf-8", newEncoding.append( '\0' ).ascii() );
            }
        }
    }

    TQChar FileImporterBibTeX::nextChar()
    {
        bool atEndOfLine = m_posIntCurrentLine >= m_currentLine.length();

        while (( m_posIntCurrentLine >= m_currentLine.length() || m_currentLine.isEmpty() || m_currentLine.isNull() ) && !m_textStream->atEnd() )
        {
            m_currentLine = m_textStream->readLine();
            m_posIntCurrentLine = 0;
            ++m_currentLineNumber;
        }

        if ( atEndOfLine )
            return TQChar( ' ' );
        else if ( m_posIntCurrentLine < m_currentLine.length() )
        {
            TQChar result = m_currentLine[m_posIntCurrentLine];
            ++m_posIntCurrentLine;
            return result;
        }

        return TQChar();
    }

    TQString FileImporterBibTeX::tokenidToString( Token token )
    {
        switch ( token )
        {
        case tAt: return TQString( "At" );
        case tBracketClose: return TQString( "BracketClose" );
        case tBracketOpen: return TQString( "BracketOpen" );
        case tAlphaNumText: return TQString( "AlphaNumText" );
        case tAssign: return TQString( "Assign" );
        case tComma: return TQString( "Comma" );
        case tDoublecross: return TQString( "Doublecross" );
        case tEOF: return TQString( "EOF" );
        case tUnknown: return TQString( "Unknown" );
        default: return TQString( "<Unknown>" );
        }
    }
}