1
1
package org .jabref .gui ;
2
2
3
+ import java .io .ByteArrayInputStream ;
3
4
import java .io .IOException ;
5
+ import java .nio .charset .StandardCharsets ;
4
6
import java .util .Collections ;
5
7
import java .util .List ;
6
8
import java .util .Optional ;
28
30
import org .slf4j .LoggerFactory ;
29
31
30
32
public class ClipBoardManager {
33
+ private static final Logger LOGGER = LoggerFactory .getLogger (ClipBoardManager .class );
31
34
32
35
public static final DataFormat XML = new DataFormat ("application/xml" );
33
-
34
- private static final Logger LOGGER = LoggerFactory .getLogger (ClipBoardManager .class );
35
36
36
37
private final Clipboard clipboard ;
37
-
38
38
private final ImportFormatReader importFormatReader ;
39
39
40
40
public ClipBoardManager () {
@@ -63,9 +63,8 @@ public String getContents() {
63
63
String result = clipboard .getString ();
64
64
if (result == null ) {
65
65
return "" ;
66
- } else {
67
- return result ;
68
66
}
67
+ return result ;
69
68
}
70
69
71
70
public void setHtmlContent (String html ) {
@@ -89,40 +88,55 @@ public void setContent(List<BibEntry> entries) throws IOException {
89
88
clipboard .setContent (content );
90
89
}
91
90
92
- public List <BibEntry > extractEntries () {
91
+ public List <BibEntry > extractData () {
93
92
Object entries = clipboard .getContent (DragAndDropDataFormats .ENTRIES );
94
93
94
+ if (entries == null ) {
95
+ return handleStringData (clipboard .getString ());
96
+ }
97
+ return handleBibTeXData ((String ) entries );
98
+ }
99
+
100
+ private List <BibEntry > handleBibTeXData (String entries ) {
95
101
BibtexParser parser = new BibtexParser (Globals .prefs .getImportFormatPreferences (), Globals .getFileUpdateMonitor ());
96
- if (entries != null ) {
97
- // We have determined that the clipboard data is a set of entries (serialized as a string).
98
- try {
99
- return parser .parseEntries ((String ) entries );
100
- } catch (ParseException ex ) {
101
- LOGGER .error ("Could not paste" , ex );
102
- }
103
- } else {
104
- String data = clipboard .getString ();
105
- if (data != null ) {
106
- try {
107
- // fetch from doi
108
- Optional <DOI > doi = DOI .parse (data );
109
- if (doi .isPresent ()) {
110
- LOGGER .info ("Found DOI in clipboard" );
111
- Optional <BibEntry > entry = new DoiFetcher (Globals .prefs .getImportFormatPreferences ()).performSearchById (doi .get ().getDOI ());
112
- return OptionalUtil .toList (entry );
113
- } else {
114
- try {
115
- UnknownFormatImport unknownFormatImport = importFormatReader .importUnknownFormat (data );
116
- return unknownFormatImport .parserResult .getDatabase ().getEntries ();
117
- } catch (ImportException e ) {
118
- // import failed and result will be empty
119
- }
120
- }
121
- } catch (FetcherException ex ) {
122
- LOGGER .error ("Error while fetching" , ex );
123
- }
124
- }
102
+ try {
103
+ return parser .parseEntries (new ByteArrayInputStream (entries .getBytes (StandardCharsets .UTF_8 )));
104
+ } catch (ParseException ex ) {
105
+ LOGGER .error ("Could not paste" , ex );
106
+ return Collections .emptyList ();
107
+ }
108
+ }
109
+
110
+ private List <BibEntry > handleStringData (String data ) {
111
+ if (data == null || data .isEmpty ()) {
112
+ return Collections .emptyList ();
113
+ }
114
+
115
+ Optional <DOI > doi = DOI .parse (data );
116
+ if (doi .isPresent ()) {
117
+ return fetchByDOI (doi .get ());
118
+ }
119
+
120
+ return tryImportFormats (data );
121
+ }
122
+
123
+ private List <BibEntry > tryImportFormats (String data ) {
124
+ try {
125
+ UnknownFormatImport unknownFormatImport = importFormatReader .importUnknownFormat (data );
126
+ return unknownFormatImport .parserResult .getDatabase ().getEntries ();
127
+ } catch (ImportException ignored ) {
128
+ return Collections .emptyList ();
129
+ }
130
+ }
131
+
132
+ private List <BibEntry > fetchByDOI (DOI doi ) {
133
+ LOGGER .info ("Found DOI in clipboard" );
134
+ try {
135
+ Optional <BibEntry > entry = new DoiFetcher (Globals .prefs .getImportFormatPreferences ()).performSearchById (doi .getDOI ());
136
+ return OptionalUtil .toList (entry );
137
+ } catch (FetcherException ex ) {
138
+ LOGGER .error ("Error while fetching" , ex );
139
+ return Collections .emptyList ();
125
140
}
126
- return Collections .emptyList ();
127
141
}
128
142
}
0 commit comments