10c49cbb5d454cb8382217132ed09cb3adc38435
[debian/jabref.git] / src / java / net / sf / jabref / export / PluginBasedExportFormat.java
1 package net.sf.jabref.export;
2
3 import java.io.*;
4 import java.net.URL;
5
6 import net.sf.jabref.Globals;
7 import net.sf.jabref.plugin.core.generated._JabRefPlugin.ExportFormatTemplateExtension;
8
9 /**
10  * Class for export formats defined in plugins.
11  * 
12  * Needed since resources might be loaded from a plugin-jar.
13  * 
14  */
15 public class PluginBasedExportFormat extends ExportFormat {
16
17         public ExportFormatTemplateExtension extension;
18
19         /**
20          * Load the plugin from the given extension. Might be null if extension
21          * could not be loaded.
22          * 
23          * @param extension
24          * @return
25          */
26         public static PluginBasedExportFormat getFormat(
27                 ExportFormatTemplateExtension extension) {
28
29                 String consoleName = extension.getConsoleName();
30                 String displayName = extension.getDisplayName();
31                 String layoutFilename = extension.getLayoutFilename();
32                 String fileExtension = extension.getExtension();
33
34                 if ("".equals(fileExtension) || "".equals(displayName)
35                         || "".equals(consoleName) || "".equals(layoutFilename)) {
36                         Globals.logger("Could not load extension " + extension.getId());
37                         return null;
38                 }
39
40                 return new PluginBasedExportFormat(displayName, consoleName,
41                         layoutFilename, fileExtension, extension);
42         }
43
44         public PluginBasedExportFormat(String displayName, String consoleName,
45                 String layoutFileName, String fileExtension,
46                 ExportFormatTemplateExtension extension) {
47                 super(displayName, consoleName, layoutFileName, null, fileExtension);
48                 this.extension = extension;
49         }
50
51         @Override
52         public Reader getReader(String filename) throws IOException {
53                 URL reso = extension.getDirAsUrl(filename);
54
55                 if (reso != null) {
56                         try {
57                                 return new InputStreamReader(reso.openStream());
58                         } catch (FileNotFoundException ex) {
59                                 // If that didn't work, try below
60                         }
61                 }
62
63                 try {
64                         return new FileReader(new File(filename));
65                 } catch (FileNotFoundException ex) {
66                         // If that did not work, throw the IOException below
67                 }
68                 throw new IOException(Globals.lang("Could not find layout file")
69                         + ": '" + filename + "'.");
70         }
71 }