-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
BibEntry.java
867 lines (741 loc) · 29.7 KB
/
BibEntry.java
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
package org.jabref.model.entry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import org.jabref.model.EntryTypes;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.event.EntryEventSource;
import org.jabref.model.entry.event.FieldAddedOrRemovedEvent;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.strings.LatexToUnicodeAdapter;
import org.jabref.model.strings.StringUtil;
import com.google.common.base.Strings;
import com.google.common.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BibEntry implements Cloneable {
public static final String TYPE_HEADER = "entrytype";
public static final String OBSOLETE_TYPE_HEADER = "bibtextype";
public static final String KEY_FIELD = "bibtexkey";
public static final String DEFAULT_TYPE = "misc";
protected static final String ID_FIELD = "id";
private static final Logger LOGGER = LoggerFactory.getLogger(BibEntry.class);
private static final Pattern REMOVE_TRAILING_WHITESPACE = Pattern.compile("\\s+$");
private final SharedBibEntryData sharedBibEntryData;
/**
* Map to store the words in every field
*/
private final Map<String, Set<String>> fieldsAsWords = new HashMap<>();
/**
* Cache that stores latex free versions of fields.
*/
private final Map<String, String> latexFreeFields = new ConcurrentHashMap<>();
private final EventBus eventBus = new EventBus();
private String id;
private final StringProperty type = new SimpleStringProperty();
private ObservableMap<String, String> fields = FXCollections.observableMap(new ConcurrentHashMap<>());
private String parsedSerialization = "";
private String commentsBeforeEntry = "";
/**
* Marks whether the complete serialization, which was read from file, should be used.
*
* Is set to false, if parts of the entry change. This causes the entry to be serialized based on the internal state (and not based on the old serialization)
*/
private boolean changed;
/**
* Constructs a new BibEntry. The internal ID is set to IdGenerator.next()
*/
public BibEntry() {
this(IdGenerator.next(), DEFAULT_TYPE);
}
/**
* Constructs a new BibEntry with the given ID and given type
*
* @param id The ID to be used
* @param type The type to set. May be null or empty. In that case, DEFAULT_TYPE is used.
*/
private BibEntry(String id, String type) {
Objects.requireNonNull(id, "Every BibEntry must have an ID");
this.id = id;
setType(type);
this.sharedBibEntryData = new SharedBibEntryData();
}
/**
* Constructs a new BibEntry. The internal ID is set to IdGenerator.next()
*/
public BibEntry(EntryType type) {
this(IdGenerator.next(), type.getName());
}
public Optional<FieldChange> setMonth(Month parsedMonth) {
return setField(FieldName.MONTH, parsedMonth.getJabRefFormat());
}
/**
* Returns the text stored in the given field of the given bibtex entry
* which belongs to the given database.
* <p>
* If a database is given, this function will try to resolve any string
* references in the field-value.
* Also, if a database is given, this function will try to find values for
* unset fields in the entry linked by the "crossref" field, if any.
*
* @param field The field to return the value of.
* @param database maybenull
* The database of the bibtex entry.
* @return The resolved field value or null if not found.
*/
public Optional<String> getResolvedFieldOrAlias(String field, BibDatabase database) {
if (TYPE_HEADER.equals(field) || OBSOLETE_TYPE_HEADER.equals(field)) {
Optional<EntryType> entryType = EntryTypes.getType(getType(), BibDatabaseMode.BIBLATEX);
if (entryType.isPresent()) {
return Optional.of(entryType.get().getName());
} else {
return Optional.of(StringUtil.capitalizeFirst(getType()));
}
}
if (KEY_FIELD.equals(field)) {
return getCiteKeyOptional();
}
Optional<String> result = getFieldOrAlias(field);
// If this field is not set, and the entry has a crossref, try to look up the
// field in the referred entry: Do not do this for the bibtex key.
if (!result.isPresent() && (database != null)) {
Optional<BibEntry> referred = database.getReferencedEntry(this);
result = referred.flatMap(entry -> entry.getFieldOrAlias(field));
}
return result.map(resultText -> BibDatabase.getText(resultText, database));
}
/**
* Returns this entry's ID.
*/
public String getId() {
return id;
}
/**
* Sets this entry's ID, provided the database containing it
* doesn't veto the change.
*
* @param id The ID to be used
*/
public void setId(String id) {
Objects.requireNonNull(id, "Every BibEntry must have an ID");
String oldId = this.id;
eventBus.post(new FieldChangedEvent(this, BibEntry.ID_FIELD, id, oldId));
this.id = id;
changed = true;
}
/**
* Returns the cite key AKA citation key AKA BibTeX key, or null if it is not set.
* Note: this is <emph>not</emph> the internal Id of this entry. The internal Id is always present, whereas the BibTeX key might not be present.
*/
@Deprecated
public String getCiteKey() {
return fields.get(KEY_FIELD);
}
/**
* Sets the cite key AKA citation key AKA BibTeX key. Note: This is <emph>not</emph> the internal Id of this entry.
* The internal Id is always present, whereas the BibTeX key might not be present.
*
* @param newCiteKey The cite key to set. Must not be null; use {@link #clearCiteKey()} to remove the cite key.
*/
public Optional<FieldChange> setCiteKey(String newCiteKey) {
return setField(KEY_FIELD, newCiteKey);
}
public Optional<String> getCiteKeyOptional() {
return Optional.ofNullable(fields.get(KEY_FIELD));
}
public boolean hasCiteKey() {
return !Strings.isNullOrEmpty(getCiteKey());
}
/**
* Returns this entry's type.
*/
public String getType() {
return type.getValue();
}
public StringProperty typeProperty() {
return type;
}
/**
* Sets this entry's type.
*/
public Optional<FieldChange> setType(EntryType type) {
return this.setType(type.getName());
}
/**
* Sets this entry's type.
*/
public Optional<FieldChange> setType(String type) {
return setType(type, EntryEventSource.LOCAL);
}
/**
* Sets this entry's type.
*/
public Optional<FieldChange> setType(String type, EntryEventSource eventSource) {
String newType;
if (Strings.isNullOrEmpty(type)) {
newType = DEFAULT_TYPE;
} else {
newType = type;
}
String oldType = getField(TYPE_HEADER).orElse(null);
if (newType.equals(oldType)) {
return Optional.empty();
}
this.type.setValue(newType.toLowerCase(Locale.ENGLISH));
changed = true;
FieldChange change = new FieldChange(this, TYPE_HEADER, oldType, newType);
eventBus.post(new FieldChangedEvent(change, eventSource));
return Optional.of(change);
}
/**
* Returns an set containing the names of all fields that are
* set for this particular entry.
*
* @return a set of existing field names
*/
public Set<String> getFieldNames() {
return new TreeSet<>(fields.keySet());
}
/**
* Returns the contents of the given field as an Optional.
*/
public Optional<String> getField(String name) {
return Optional.ofNullable(fields.get(toLowerCase(name)));
}
/**
* Returns true if the entry has the given field, or false if it is not set.
*/
public boolean hasField(String name) {
return fields.containsKey(toLowerCase(name));
}
private String toLowerCase(String fieldName) {
Objects.requireNonNull(fieldName, "field name must not be null");
return fieldName.toLowerCase(Locale.ENGLISH);
}
/**
* Internal method used to get the content of a field (or its alias)
*
* Used by {@link #getFieldOrAlias(String)} and {@link #getFieldOrAliasLatexFree(String)}
*
* @param name name of the field
* @param getFieldInterface
*
* @return determined field value
*/
private Optional<String> genericGetFieldOrAlias(String name, GetFieldInterface getFieldInterface) {
Optional<String> fieldValue = getFieldInterface.getValueForField(toLowerCase(name));
if (fieldValue.isPresent() && !fieldValue.get().isEmpty()) {
return fieldValue;
}
// No value of this field found, so look at the alias
String aliasForField = EntryConverter.FIELD_ALIASES.get(name);
if (aliasForField != null) {
return getFieldInterface.getValueForField(aliasForField);
}
// Finally, handle dates
if (FieldName.DATE.equals(name)) {
Optional<Date> date = Date.parse(
getFieldInterface.getValueForField(FieldName.YEAR),
getFieldInterface.getValueForField(FieldName.MONTH),
getFieldInterface.getValueForField(FieldName.DAY));
return date.map(Date::getNormalized);
}
if (FieldName.YEAR.equals(name) || FieldName.MONTH.equals(name) || FieldName.DAY.equals(name)) {
Optional<String> date = getFieldInterface.getValueForField(FieldName.DATE);
if (!date.isPresent()) {
return Optional.empty();
}
Optional<Date> parsedDate = Date.parse(date.get());
if (parsedDate.isPresent()) {
if (FieldName.YEAR.equals(name)) {
return parsedDate.get().getYear().map(Object::toString);
}
if (FieldName.MONTH.equals(name)) {
return parsedDate.get().getMonth().map(Month::getJabRefFormat);
}
if (FieldName.DAY.equals(name)) {
return parsedDate.get().getDay().map(Object::toString);
}
} else {
// Date field not in valid format
LOGGER.debug("Could not parse date " + date.get());
return Optional.empty();
}
}
return Optional.empty();
}
public Optional<DOI> getDOI() {
return getField(FieldName.DOI).flatMap(DOI::parse);
}
/**
* Return the LaTeX-free contents of the given field or its alias an an Optional
*
* For details see also {@link #getFieldOrAlias(String)}
*
* @param name the name of the field
* @return the stored latex-free content of the field (or its alias)
*/
public Optional<String> getFieldOrAliasLatexFree(String name) {
return genericGetFieldOrAlias(name, this::getLatexFreeField);
}
/**
* Returns the contents of the given field or its alias as an Optional
* <p>
* The following aliases are considered (old bibtex <-> new biblatex) based
* on the biblatex documentation, chapter 2.2.5:<br>
* address <-> location <br>
* annote <-> annotation <br>
* archiveprefix <-> eprinttype <br>
* journal <-> journaltitle <br>
* key <-> sortkey <br>
* pdf <-> file <br
* primaryclass <-> eprintclass <br>
* school <-> institution <br>
* These work bidirectional. <br>
* </p>
*
* <p>
* Special attention is paid to dates: (see the biblatex documentation,
* chapter 2.3.8)
* The fields 'year' and 'month' are used if the 'date'
* field is empty. Conversely, getFieldOrAlias("year") also tries to
* extract the year from the 'date' field (analogously for 'month').
* </p>
*/
public Optional<String> getFieldOrAlias(String name) {
return genericGetFieldOrAlias(name, this::getField);
}
/**
* Sets a number of fields simultaneously. The given HashMap contains field
* names as keys, each mapped to the value to set.
*/
public void setField(Map<String, String> fields) {
Objects.requireNonNull(fields, "fields must not be null");
fields.forEach(this::setField);
}
/**
* Set a field, and notify listeners about the change.
*
* @param name The field to set
* @param value The value to set
* @param eventSource Source the event is sent from
*/
public Optional<FieldChange> setField(String name, String value, EntryEventSource eventSource) {
Objects.requireNonNull(name, "field name must not be null");
Objects.requireNonNull(value, "field value must not be null");
String fieldName = toLowerCase(name);
if (value.isEmpty()) {
return clearField(fieldName);
}
String oldValue = getField(fieldName).orElse(null);
boolean isNewField = oldValue == null;
if (value.equals(oldValue)) {
return Optional.empty();
}
if (BibEntry.ID_FIELD.equals(fieldName)) {
throw new IllegalArgumentException("The field name '" + name + "' is reserved");
}
changed = true;
fields.put(fieldName, value.intern());
invalidateFieldCache(fieldName);
FieldChange change = new FieldChange(this, fieldName, oldValue, value);
if (isNewField) {
eventBus.post(new FieldAddedOrRemovedEvent(change, eventSource));
} else {
eventBus.post(new FieldChangedEvent(change, eventSource));
}
return Optional.of(change);
}
public Optional<FieldChange> setField(String name, Optional<String> value, EntryEventSource eventSource) {
if (value.isPresent()) {
return setField(name, value.get(), eventSource);
}
return Optional.empty();
}
/**
* Set a field, and notify listeners about the change.
*
* @param name The field to set.
* @param value The value to set.
*/
public Optional<FieldChange> setField(String name, String value) {
return setField(name, value, EntryEventSource.LOCAL);
}
/**
* Remove the mapping for the field name, and notify listeners about
* the change.
*
* @param name The field to clear.
*/
public Optional<FieldChange> clearField(String name) {
return clearField(name, EntryEventSource.LOCAL);
}
/**
* Remove the mapping for the field name, and notify listeners about
* the change including the {@link EntryEventSource}.
*
* @param name The field to clear.
* @param eventSource the source a new {@link FieldChangedEvent} should be posten from.
*/
public Optional<FieldChange> clearField(String name, EntryEventSource eventSource) {
String fieldName = toLowerCase(name);
if (BibEntry.ID_FIELD.equals(fieldName)) {
throw new IllegalArgumentException("The field name '" + name + "' is reserved");
}
Optional<String> oldValue = getField(fieldName);
if (!oldValue.isPresent()) {
return Optional.empty();
}
changed = true;
fields.remove(fieldName);
invalidateFieldCache(fieldName);
FieldChange change = new FieldChange(this, fieldName, oldValue.get(), null);
eventBus.post(new FieldAddedOrRemovedEvent(change, eventSource));
return Optional.of(change);
}
/**
* Determines whether this entry has all the given fields present. If a non-null
* database argument is given, this method will try to look up missing fields in
* entries linked by the "crossref" field, if any.
*
* @param allFields An array of field names to be checked.
* @param database The database in which to look up crossref'd entries, if any. This
* argument can be null, meaning that no attempt will be made to follow crossrefs.
* @return true if all fields are set or could be resolved, false otherwise.
*/
public boolean allFieldsPresent(Collection<String> allFields, BibDatabase database) {
for (String field : allFields) {
String fieldName = toLowerCase(field);
// OR fields
if (fieldName.contains(FieldName.FIELD_SEPARATOR)) {
String[] altFields = field.split(FieldName.FIELD_SEPARATOR);
if (!atLeastOnePresent(altFields, database)) {
return false;
}
} else {
if (!this.getResolvedFieldOrAlias(fieldName, database).isPresent()) {
return false;
}
}
}
return true;
}
private boolean atLeastOnePresent(String[] fieldsToCheck, BibDatabase database) {
for (String field : fieldsToCheck) {
String fieldName = toLowerCase(field);
Optional<String> value = this.getResolvedFieldOrAlias(fieldName, database);
if ((value.isPresent()) && !value.get().isEmpty()) {
return true;
}
}
return false;
}
/**
* Returns a clone of this entry. Useful for copying.
* This will set a new ID for the cloned entry to be able to distinguish both copies.
*/
@Override
public Object clone() {
BibEntry clone = new BibEntry(IdGenerator.next(), type.getValue());
clone.fields = FXCollections.observableMap(new ConcurrentHashMap<>(fields));
return clone;
}
/**
* This returns a canonical BibTeX serialization. Special characters such as "{" or "&" are NOT escaped, but written
* as is
* <p>
* Serializes all fields, even the JabRef internal ones. Does NOT serialize "KEY_FIELD" as field, but as key
*/
@Override
public String toString() {
return CanonicalBibtexEntry.getCanonicalRepresentation(this);
}
/**
* @param maxCharacters The maximum number of characters (additional
* characters are replaced with "..."). Set to 0 to disable truncation.
* @return A short textual description of the entry in the format:
* Author1, Author2: Title (Year)
*/
public String getAuthorTitleYear(int maxCharacters) {
String[] s = new String[] {getField(FieldName.AUTHOR).orElse("N/A"), getField(FieldName.TITLE).orElse("N/A"),
getField(FieldName.YEAR).orElse("N/A")};
String text = s[0] + ": \"" + s[1] + "\" (" + s[2] + ')';
if ((maxCharacters <= 0) || (text.length() <= maxCharacters)) {
return text;
}
return text.substring(0, maxCharacters + 1) + "...";
}
/**
* Returns the title of the given BibTeX entry as an Optional.
*
* @return an Optional containing the title of a BibTeX entry in case it exists, otherwise return an empty Optional.
*/
public Optional<String> getTitle() {
return getField(FieldName.TITLE);
}
/**
* Will return the publication date of the given bibtex entry conforming to ISO 8601, i.e. either YYYY or YYYY-MM.
*
* @return will return the publication date of the entry or null if no year was found.
*/
public Optional<Date> getPublicationDate() {
return getFieldOrAlias(FieldName.DATE).flatMap(Date::parse);
}
public String getParsedSerialization() {
return parsedSerialization;
}
public void setParsedSerialization(String parsedSerialization) {
changed = false;
this.parsedSerialization = parsedSerialization;
}
public void setCommentsBeforeEntry(String parsedComments) {
// delete trailing whitespaces (between entry and text)
this.commentsBeforeEntry = REMOVE_TRAILING_WHITESPACE.matcher(parsedComments).replaceFirst("");
}
public boolean hasChanged() {
return changed;
}
public void setChanged(boolean changed) {
this.changed = changed;
}
public Optional<FieldChange> putKeywords(List<String> keywords, Character delimiter) {
Objects.requireNonNull(delimiter);
return putKeywords(new KeywordList(keywords), delimiter);
}
public Optional<FieldChange> putKeywords(KeywordList keywords, Character delimiter) {
Objects.requireNonNull(keywords);
Optional<String> oldValue = this.getField(FieldName.KEYWORDS);
if (keywords.isEmpty()) {
// Clear keyword field
if (oldValue.isPresent()) {
return this.clearField(FieldName.KEYWORDS);
} else {
return Optional.empty();
}
}
// Set new keyword field
String newValue = keywords.getAsString(delimiter);
return this.setField(FieldName.KEYWORDS, newValue);
}
/**
* Check if a keyword already exists (case insensitive), if not: add it
*
* @param keyword Keyword to add
*/
public void addKeyword(String keyword, Character delimiter) {
Objects.requireNonNull(keyword, "keyword must not be null");
if (keyword.isEmpty()) {
return;
}
addKeyword(new Keyword(keyword), delimiter);
}
public void addKeyword(Keyword keyword, Character delimiter) {
KeywordList keywords = this.getKeywords(delimiter);
keywords.add(keyword);
this.putKeywords(keywords, delimiter);
}
/**
* Add multiple keywords to entry
*
* @param keywords Keywords to add
*/
public void addKeywords(Collection<String> keywords, Character delimiter) {
Objects.requireNonNull(keywords);
keywords.forEach(keyword -> addKeyword(keyword, delimiter));
}
public KeywordList getKeywords(Character delimiter) {
Optional<String> keywordsContent = getField(FieldName.KEYWORDS);
return keywordsContent.map(content -> KeywordList.parse(content, delimiter)).orElse(new KeywordList());
}
public KeywordList getResolvedKeywords(Character delimiter, BibDatabase database) {
Optional<String> keywordsContent = getResolvedFieldOrAlias(FieldName.KEYWORDS, database);
return keywordsContent.map(content -> KeywordList.parse(content, delimiter)).orElse(new KeywordList());
}
public Optional<FieldChange> removeKeywords(KeywordList keywordsToRemove, Character keywordDelimiter) {
KeywordList keywordList = getKeywords(keywordDelimiter);
keywordList.removeAll(keywordsToRemove);
return putKeywords(keywordList, keywordDelimiter);
}
public Optional<FieldChange> replaceKeywords(KeywordList keywordsToReplace, Keyword newValue,
Character keywordDelimiter) {
KeywordList keywordList = getKeywords(keywordDelimiter);
keywordList.replaceAll(keywordsToReplace, newValue);
return putKeywords(keywordList, keywordDelimiter);
}
public Collection<String> getFieldValues() {
return fields.values();
}
public Map<String, String> getFieldMap() {
return fields;
}
public SharedBibEntryData getSharedBibEntryData() {
return sharedBibEntryData;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if ((o == null) || (getClass() != o.getClass())) {
return false;
}
BibEntry entry = (BibEntry) o;
return Objects.equals(type.getValue(), entry.type.getValue())
&& Objects.equals(fields, entry.fields)
&& Objects.equals(commentsBeforeEntry, entry.commentsBeforeEntry);
}
@Override
public int hashCode() {
return Objects.hash(type.getValue(), fields);
}
public void registerListener(Object object) {
this.eventBus.register(object);
}
public void unregisterListener(Object object) {
try {
this.eventBus.unregister(object);
} catch (IllegalArgumentException e) {
// occurs if the event source has not been registered, should not prevent shutdown
LOGGER.debug("Problem unregistering", e);
}
}
public BibEntry withField(String field, String value) {
setField(field, value);
return this;
}
/*
* Returns user comments (arbitrary text before the entry), if they exist. If not, returns the empty String
*/
public String getUserComments() {
return commentsBeforeEntry;
}
public List<ParsedEntryLink> getEntryLinkList(String fieldName, BibDatabase database) {
return getField(fieldName).map(fieldValue -> EntryLinkList.parse(fieldValue, database))
.orElse(Collections.emptyList());
}
public Optional<FieldChange> setEntryLinkList(String fieldName, List<ParsedEntryLink> list) {
return setField(fieldName, EntryLinkList.serialize(list));
}
public Set<String> getFieldAsWords(String field) {
String fieldName = toLowerCase(field);
Set<String> storedList = fieldsAsWords.get(fieldName);
if (storedList != null) {
return storedList;
} else {
String fieldValue = fields.get(fieldName);
if (fieldValue == null) {
return Collections.emptySet();
} else {
HashSet<String> words = new HashSet<>(StringUtil.getStringAsWords(fieldValue));
fieldsAsWords.put(fieldName, words);
return words;
}
}
}
public Optional<FieldChange> clearCiteKey() {
return clearField(KEY_FIELD);
}
private void invalidateFieldCache(String fieldName) {
latexFreeFields.remove(fieldName);
fieldsAsWords.remove(fieldName);
}
public Optional<String> getLatexFreeField(String name) {
if (!hasField(name) && !TYPE_HEADER.equals(name)) {
return Optional.empty();
} else if (latexFreeFields.containsKey(name)) {
return Optional.ofNullable(latexFreeFields.get(toLowerCase(name)));
} else if (KEY_FIELD.equals(name)) {
// the key field should not be converted
Optional<String> citeKey = getCiteKeyOptional();
latexFreeFields.put(name, citeKey.get());
return citeKey;
} else if (TYPE_HEADER.equals(name)) {
Optional<EntryType> entryType = EntryTypes.getType(getType(), BibDatabaseMode.BIBLATEX);
if (entryType.isPresent()) {
String entryName = entryType.get().getName();
latexFreeFields.put(name, entryName);
return Optional.of(entryName);
} else {
return Optional.of(StringUtil.capitalizeFirst(getType()));
}
} else {
String latexFreeField = LatexToUnicodeAdapter.format(getField(name).get()).intern();
latexFreeFields.put(name, latexFreeField);
return Optional.of(latexFreeField);
}
}
public Optional<FieldChange> setFiles(List<LinkedFile> files) {
Optional<String> oldValue = this.getField(FieldName.FILE);
String newValue = FileFieldWriter.getStringRepresentation(files);
if (oldValue.isPresent() && oldValue.get().equals(newValue)) {
return Optional.empty();
}
return this.setField(FieldName.FILE, newValue);
}
/**
* Gets a list of linked files.
*
* @return the list of linked files, is never null but can be empty.
* Changes to the underlying list will have no effect on the entry itself. Use {@link #addFile(LinkedFile)}
*/
public List<LinkedFile> getFiles() {
//Extract the path
Optional<String> oldValue = getField(FieldName.FILE);
if (!oldValue.isPresent()) {
return new ArrayList<>(); //Return new ArrayList because emptyList is immutable
}
return FileFieldParser.parse(oldValue.get());
}
public void setDate(Date date) {
date.getYear().ifPresent(year -> setField(FieldName.YEAR, year.toString()));
date.getMonth().ifPresent(this::setMonth);
date.getDay().ifPresent(day -> setField(FieldName.DAY, day.toString()));
}
public Optional<Month> getMonth() {
return getFieldOrAlias(FieldName.MONTH).flatMap(Month::parse);
}
public ObjectBinding<String> getFieldBinding(String fieldName) {
return Bindings.valueAt(fields, fieldName);
}
public ObjectBinding<String> getCiteKeyBinding() {
return getFieldBinding(KEY_FIELD);
}
public Optional<FieldChange> addFile(LinkedFile file) {
List<LinkedFile> linkedFiles = getFiles();
linkedFiles.add(file);
return setFiles(linkedFiles);
}
public ObservableMap<String, String> getFieldsObservable() {
return fields;
}
/**
* Returns a list of observables that represent the data of the entry.
*/
public Observable[] getObservables() {
return new Observable[] {fields};
}
private interface GetFieldInterface {
Optional<String> getValueForField(String fieldName);
}
}