11package org .jabref .cli ;
22
3- import java .io .File ;
43import java .io .FileReader ;
54import java .io .IOException ;
65import java .io .StringWriter ;
7- import java .nio .charset . StandardCharsets ;
6+ import java .nio .file . Paths ;
87import java .util .Collection ;
98import java .util .List ;
109import java .util .Optional ;
1716import org .jabref .logic .importer .ImportFormatPreferences ;
1817import org .jabref .logic .importer .ParserResult ;
1918import org .jabref .logic .importer .fileformat .BibtexParser ;
20- import org .jabref .logic .xmp .XMPPreferences ;
21- import org .jabref .logic .xmp .XMPUtil ;
19+ import org .jabref .logic .xmp .XmpPreferences ;
20+ import org .jabref .logic .xmp .XmpUtilReader ;
21+ import org .jabref .logic .xmp .XmpUtilWriter ;
2222import org .jabref .model .database .BibDatabaseMode ;
2323import org .jabref .model .entry .BibEntry ;
2424import org .jabref .preferences .JabRefPreferences ;
2525
26- import org .apache .jempbox . impl . XMLUtil ;
27- import org .apache .jempbox . xmp . XMPMetadata ;
26+ import org .apache .xmpbox . XMPMetadata ;
27+ import org .apache .xmpbox . xml . XmpSerializer ;
2828
2929public class XMPUtilMain {
3030
@@ -62,18 +62,16 @@ public static void main(String[] args) throws IOException, TransformerException
6262 Globals .prefs = JabRefPreferences .getInstance ();
6363 }
6464
65- XMPPreferences xmpPreferences = Globals .prefs .getXMPPreferences ();
65+ XmpPreferences xmpPreferences = Globals .prefs .getXMPPreferences ();
6666 ImportFormatPreferences importFormatPreferences = Globals .prefs .getImportFormatPreferences ();
6767
68- switch ( args .length ) {
69- case 0 :
68+ int argsLength = args .length ;
69+ if ( argsLength == 0 ) {
7070 usage ();
71- break ;
72- case 1 :
73-
71+ } else if (argsLength == 1 ) {
7472 if (args [0 ].endsWith (".pdf" )) {
7573 // Read from pdf and write as BibTex
76- List <BibEntry > l = XMPUtil . readXMP ( new File ( args [0 ]) , xmpPreferences );
74+ List <BibEntry > l = XmpUtilReader . readXmp ( args [0 ], xmpPreferences );
7775
7876 BibEntryWriter bibtexEntryWriter = new BibEntryWriter (
7977 new LatexFieldFormatter (Globals .prefs .getLatexFieldFormatterPreferences ()), false );
@@ -92,63 +90,62 @@ public static void main(String[] args) throws IOException, TransformerException
9290
9391 if (entries .isEmpty ()) {
9492 System .err .println ("Could not find BibEntry in " + args [0 ]);
95- } else {
96- System .out .println (XMPUtil .toXMP (entries , result .getDatabase (), xmpPreferences ));
9793 }
9894 }
9995 } else {
10096 usage ();
10197 }
102- break ;
103- case 2 :
98+ } else if (argsLength == 2 ) {
10499 if ("-x" .equals (args [0 ]) && args [1 ].endsWith (".pdf" )) {
105100 // Read from pdf and write as BibTex
106- Optional <XMPMetadata > meta = XMPUtil . readRawXMP ( new File (args [1 ]));
101+ List <XMPMetadata > meta = XmpUtilReader . readRawXmp ( Paths . get (args [1 ]));
107102
108- if (meta .isPresent ()) {
109- XMLUtil .save (meta .get ().getXMPDocument (), System .out , StandardCharsets .UTF_8 .name ());
103+ if (!meta .isEmpty ()) {
104+ XmpSerializer serializer = new XmpSerializer ();
105+ serializer .serialize (meta .get (0 ), System .out , true );
110106 } else {
111107 System .err .println ("The given pdf does not contain any XMP-metadata." );
112108 }
113- break ;
109+ return ;
114110 }
115111
116112 if (args [0 ].endsWith (".bib" ) && args [1 ].endsWith (".pdf" )) {
117- ParserResult result = new BibtexParser (importFormatPreferences , Globals .getFileUpdateMonitor ()).parse (new FileReader (args [0 ]));
113+ try (FileReader reader = new FileReader (args [0 ])) {
114+ ParserResult result = new BibtexParser (importFormatPreferences , Globals .getFileUpdateMonitor ()).parse (reader );
118115
119- Collection <BibEntry > entries = result .getDatabase ().getEntries ();
116+ List <BibEntry > entries = result .getDatabase ().getEntries ();
120117
121- if (entries .isEmpty ()) {
122- System .err .println ("Could not find BibEntry in " + args [0 ]);
123- } else {
124- XMPUtil .writeXMP (new File (args [1 ]), entries , result .getDatabase (), false , xmpPreferences );
125- System .out .println ("XMP written." );
118+ if (entries .isEmpty ()) {
119+ System .err .println ("Could not find BibEntry in " + args [0 ]);
120+ } else {
121+ XmpUtilWriter .writeXmp (Paths .get (args [1 ]), entries , result .getDatabase (), xmpPreferences );
122+ System .out .println ("XMP written." );
123+ }
126124 }
127- break ;
125+ return ;
128126 }
129127
130128 usage ();
131- break ;
132- case 3 :
129+ } else if (argsLength == 3 ) {
133130 if (!args [1 ].endsWith (".bib" ) && !args [2 ].endsWith (".pdf" )) {
134131 usage ();
135- break ;
132+ return ;
136133 }
137134
138- ParserResult result = new BibtexParser (importFormatPreferences , Globals .getFileUpdateMonitor ()).parse (new FileReader (args [1 ]));
135+ try (FileReader reader = new FileReader (args [1 ])) {
136+ ParserResult result = new BibtexParser (importFormatPreferences , Globals .getFileUpdateMonitor ()).parse (reader );
139137
140- Optional <BibEntry > bibEntry = result .getDatabase ().getEntryByKey (args [0 ]);
138+ Optional <BibEntry > bibEntry = result .getDatabase ().getEntryByKey (args [0 ]);
141139
142- if (bibEntry .isPresent ()) {
143- XMPUtil . writeXMP ( new File (args [2 ]), bibEntry .get (), result .getDatabase (), xmpPreferences );
140+ if (bibEntry .isPresent ()) {
141+ XmpUtilWriter . writeXmp ( Paths . get (args [2 ]), bibEntry .get (), result .getDatabase (), xmpPreferences );
144142
145- System .out .println ("XMP written." );
146- } else {
147- System .err .println ("Could not find BibEntry " + args [0 ] + " in " + args [0 ]);
143+ System .out .println ("XMP written." );
144+ } else {
145+ System .err .println ("Could not find BibEntry " + args [0 ] + " in " + args [0 ]);
146+ }
148147 }
149- break ;
150-
151- default :
148+ } else {
152149 usage ();
153150 }
154151 }
@@ -167,13 +164,13 @@ private static void usage() {
167164 System .out .println ("Read from PDF and print raw XMP:" );
168165 System .out .println (" xmpUtil -x <pdf>" );
169166 System .out
170- .println ("Write the entry in <bib> given by <key> to the PDF:" );
167+ .println ("Write the entry in <bib> given by <key> to the PDF:" );
171168 System .out .println (" xmpUtil <key> <bib> <pdf>" );
172169 System .out .println ("Write all entries in <bib> to the PDF:" );
173170 System .out .println (" xmpUtil <bib> <pdf>" );
174171 System .out .println ("" );
175172 System .out
176- .println ("To report bugs visit https://issues.jabref.org" );
173+ .println ("To report bugs visit https://issues.jabref.org" );
177174 }
178175
179176}
0 commit comments