summaryrefslogtreecommitdiffstats
path: root/src/value.cpp
blob: 8499f4aac4532eb4e15a9c2e69e4a4620b37f8f1 (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
/***************************************************************************
*   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 <tqregexp.h>
#include <tqstring.h>
#include <tqstringlist.h>

#include "value.h"

namespace BibTeX
{

    ValueTextInterface::ValueTextInterface( const ValueTextInterface* other ) : m_text( other->text() )
    {
// nothing
    }

    ValueTextInterface::ValueTextInterface( const TQString& text ) : m_text( text )
    {
// nothing
    }

    void ValueTextInterface::setText( const TQString& text )
    {
        m_text = text;
    }

    TQString ValueTextInterface::text() const
    {
        return m_text;
    }

    TQString ValueTextInterface::simplifiedText() const
    {
        return text().replace( TQRegExp( "\\\\[A-Za-z0-9]+" ), "" ).replace( '{', "" ).replace( '}', "" );
    }

    void ValueTextInterface::replace( const TQString &before, const TQString &after )
    {
        if ( before == text() || before == simplifiedText() )
            setText( after );
    }

    bool ValueTextInterface::containsPattern( const TQString &pattern, bool caseSensitive )
    {
        return text().contains( pattern, caseSensitive ) || simplifiedText().contains( pattern, caseSensitive );
    }

    ValueItem::ValueItem( const TQString& text ) : ValueTextInterface( text )
    {
// nothing
    }

    Keyword::Keyword( Keyword *other ) : ValueTextInterface( other )
    {
// nothing
    }

    Keyword::Keyword( const TQString& text ) : ValueTextInterface( text )
    {
// nothing
    }

    Keyword *Keyword::clone()
    {
        return new Keyword( text() );
    }

    KeywordContainer::KeywordContainer(): ValueItem( "" )
    {
// nothing
    }

    KeywordContainer::KeywordContainer( const TQString& text ) : ValueItem( text )
    {
        setText( text );
    }

    KeywordContainer::KeywordContainer( KeywordContainer *other ) : ValueItem( TQString::null )
    {
        for ( TQValueList<Keyword*>::Iterator it = other->keywords.begin(); it != other->keywords.end(); ++it )
            keywords.append(( *it )->clone() );
    }

    KeywordContainer::KeywordContainer( const TQStringList& list ) : ValueItem( TQString::null )
    {
        setList( list );
    }

    ValueItem *KeywordContainer::clone()
    {
        return new KeywordContainer( this );
    }

    void KeywordContainer::setList( const TQStringList& list )
    {
        keywords.clear();
        for ( TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
            keywords.append( new Keyword( *it ) );
    }

    void KeywordContainer::append( const TQString& text )
    {
        bool contains = FALSE;

        for ( TQValueList<Keyword*>::ConstIterator it = keywords.begin(); !contains && it != keywords.end(); ++it )
            contains = TQString::compare(( *it )->text(), text ) == 0;

        if ( contains == 0 )
            keywords.append( new Keyword( text ) );
    }

    void KeywordContainer::remove( const TQString& text )
    {
        bool contains = FALSE;
        for ( TQValueList<Keyword*>::Iterator it = keywords.begin(); !contains && it != keywords.end(); ++it )
            if ( TQString::compare(( *it )->text(), text ) == 0 )
            {
                keywords.remove( it );
                break;
            }
    }

    void KeywordContainer::setText( const TQString& text )
    {
        ValueItem::setText( text );

        TQRegExp splitRegExp;
        if ( text.contains( ";" ) )
            splitRegExp = TQRegExp( "\\s*;\\s*" );
        else
            splitRegExp = TQRegExp( "\\s*,\\s*" );

        keywords.clear();
        TQStringList keywordList = TQStringList::split( splitRegExp, text, FALSE );
        for ( TQStringList::ConstIterator it = keywordList.begin(); it != keywordList.end(); ++it )
            keywords.append( new Keyword( *it ) );
    }

    TQString KeywordContainer::text() const
    {
        TQString result;
        bool first = true;
        for ( TQValueList<Keyword*>::ConstIterator it = keywords.begin(); it != keywords.end(); ++it )
        {
            if ( !first )
                result.append( "; " );
            else first = false;
            result.append(( *it )->text() );
        }
        return result;
    }

    void KeywordContainer::replace( const TQString &before, const TQString &after )
    {
        for ( TQValueList<Keyword*>::ConstIterator it = keywords.begin(); it != keywords.end(); ++it )
            ( *it )->replace( before, after );
    }

    Person::Person( const TQString& text, bool firstNameFirst ) : ValueTextInterface( text ), m_firstNameFirst( firstNameFirst )
    {
        setText( text );
    }

    Person::Person( const TQString& firstName, const TQString& lastName, bool firstNameFirst ) : ValueTextInterface( TQString( firstName ).append( " " ).append( lastName ) ), m_firstName( firstName ), m_lastName( lastName ), m_firstNameFirst( firstNameFirst )
    {
// nothing
    }

    Person *Person::clone()
    {
        return new Person( m_firstName, m_lastName, m_firstNameFirst );
    }

    void Person::setText( const TQString& text )
    {
        ValueTextInterface::setText( text );

        TQStringList segments;
        bool containsComma = splitName( text, segments );
        m_firstName = "";
        m_lastName = "";

        if ( segments.isEmpty() )
            return;

        if ( !containsComma )
        {
            /** PubMed uses a special writing style for names, where the last name is followed by single capital letter, each being the first letter of each first name */
            /** So, check how many single capital letters are at the end of the given segment list */
            int singleCapitalLettersCounter = 0;
            int p = segments.count() - 1;
            while ( segments[p].length() == 1 && segments[p].compare( segments[p].upper() ) == 0 )
            {
                --p;
                ++singleCapitalLettersCounter;
            }

            if ( singleCapitalLettersCounter > 0 )
            {
                /** this is a special case for names from PubMed, which are formatted like "Fischer T" */
                /** all segment values until the first single letter segment are last name parts */
                for ( int i = 0; i < p; ++i )
                    m_lastName.append( segments[i] ).append( " " );
                m_lastName.append( segments[p] );
                /** single letter segments are first name parts */
                for ( unsigned int i = p + 1; i < segments.count() - 1; ++i )
                    m_firstName.append( segments[i] ).append( " " );
                m_firstName.append( segments[segments.count() - 1] );
            }
            else
            {
                int from = segments.count() - 1;
                m_lastName = segments[from];
                /** check for lower case parts of the last name such as "van", "von", "de", ... */
                while ( from > 0 )
                {
                    if ( segments[from - 1].compare( segments[from - 1].lower() ) != 0 )
                        break;
                    --from;
                    m_lastName.prepend( " " );
                    m_lastName.prepend( segments[from] );
                }

                if ( from > 0 )
                {
                    /** there are segments left for the first name */
                    m_firstName = *segments.begin();
                    for ( TQStringList::Iterator it = ++segments.begin(); from > 1; ++it, --from )
                    {
                        m_firstName.append( " " );
                        m_firstName.append( *it );
                    }
                }
            }
        }
        else
        {
            bool inLastName = TRUE;
            for ( unsigned int i = 0; i < segments.count();++i )
            {
                if ( segments[i] == "," )
                    inLastName = FALSE;
                else if ( inLastName )
                {
                    if ( !m_lastName.isEmpty() ) m_lastName.append( " " );
                    m_lastName.append( segments[i] );
                }
                else
                {
                    if ( !m_firstName.isEmpty() ) m_firstName.append( " " );
                    m_firstName.append( segments[i] );
                }
            }
        }
    }

    TQString Person::text() const
    {
        return text( m_firstNameFirst );
    }

    TQString Person::text( bool firstNameFirst ) const
    {

        if ( m_firstName.isEmpty() )
            return m_lastName;
        else
            return firstNameFirst ? m_firstName + " " + m_lastName : m_lastName + ", " + m_firstName;
    }

    TQString Person::firstName()
    {
        return m_firstName;
    }
    TQString Person::lastName()
    {
        return m_lastName;
    }

    /** Splits a name into single words. If the name's text was reversed (Last, First), the result will be true and the comma will be added to segments. Otherwise the functions result will be false. This function respects protecting {...}. */
    bool Person::splitName( const TQString& text, TQStringList& segments )
    {
        int bracketCounter = 0;
        bool result = FALSE;
        TQString buffer = "";

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

            if ( text[pos] == ' ' && bracketCounter == 0 )
            {
                if ( !buffer.isEmpty() )
                {
                    segments.append( buffer );
                    buffer = "";
                }
            }
            else if ( text[pos] == ',' && bracketCounter == 0 )
            {
                if ( !buffer.isEmpty() )
                {
                    segments.append( buffer );
                    buffer = "";
                }
                segments.append( "," );
                result = TRUE;
            }
            else
                buffer.append( text[pos] );
        }

        if ( !buffer.isEmpty() )
            segments.append( buffer );

        return result;
    }

    PersonContainer::PersonContainer( bool firstNameFirst ) : ValueItem( TQString::null ), m_firstNameFirst( firstNameFirst )
    {
        // nothing
    }

    PersonContainer::PersonContainer( const TQString& text, bool firstNameFirst ) : ValueItem( text ), m_firstNameFirst( firstNameFirst )
    {
        persons.append( new Person( text, m_firstNameFirst ) );
    }

    ValueItem *PersonContainer::clone()
    {
        PersonContainer *result = new PersonContainer( m_firstNameFirst );
        for ( TQValueList<Person*>::ConstIterator it = persons.begin(); it != persons.end(); ++it )
            result->persons.append(( *it )->clone() );

        return result;
    }

    void PersonContainer::setText( const TQString& text )
    {
        ValueTextInterface::setText( text );
        tqDebug( "You cannot set a text ('%s') to a Value object", text.latin1() );
    }

    TQString PersonContainer::text() const
    {
        TQString result;
        bool first = TRUE;

        for ( TQValueList<Person*>::ConstIterator it = persons.begin(); it != persons.end(); ++it )
        {
            if ( !first )
                result.append( " and " );
            else
                first = FALSE;
            result.append(( *it )->text() );
        }

        return result;
    }

    void PersonContainer::replace( const TQString &before, const TQString &after )
    {
        for ( TQValueList<Person*>::ConstIterator it = persons.begin(); it != persons.end(); ++it )
            ( *it )->replace( before, after );
    }

    MacroKey::MacroKey( const TQString& text ) : ValueItem( text )
    {
        m_isValid = isValidInternal();
    }

    ValueItem *MacroKey::clone()
    {
        return new MacroKey( text() );
    }

    void MacroKey::setText( const TQString& text )
    {
        ValueItem::setText( text );
        m_isValid = isValidInternal();
    }

    bool MacroKey::isValid()
    {
        return m_isValid;
    }

    bool MacroKey::isValidInternal()
    {
        return !text().contains( TQRegExp( "![-.:/+_a-zA-Z0-9]" ) );
    }

    PlainText::PlainText( const TQString& text ) : ValueItem( text )
    {
        // nothing
    }

    ValueItem *PlainText::clone()
    {
        return new PlainText( text() );
    }

    Value::Value() : ValueTextInterface( TQString::null )
    {
        // nothing
    }

    Value::Value( const Value *other ) : ValueTextInterface( other )
    {
        for ( TQValueList<ValueItem*>::ConstIterator it = other->items.begin(); it != other->items.end(); ++it )
            items.append(( *it )->clone() );
    }

    Value::Value( const TQString& text, bool isMacroKey ): ValueTextInterface( text )
    {
        ValueItem *item = NULL;
        if ( isMacroKey )
            item = new MacroKey( text );
        else
            item = new PlainText( text );
        items.append( item );
    }

    void Value::setText( const TQString& text )
    {
        ValueTextInterface::setText( text );
        tqDebug( "You cannot set a text ('%s') to a Value object", text.latin1() );
    }

    TQString Value::text() const
    {
        TQString result;

        for ( TQValueList<ValueItem*>::ConstIterator it = items.begin(); it != items.end(); ++it )
            result.append(( *it )->text() );

        return result;
    }

    void Value::replace( const TQString &before, const TQString &after )
    {
        for ( TQValueList<ValueItem*>::ConstIterator it = items.begin(); it != items.end(); ++it )
            ( *it )->replace( before, after );
    }
}