001 package org.maltparser.core.plugin;
002
003 import java.io.File;
004 import java.io.FileNotFoundException;
005 import java.io.IOException;
006 import java.net.MalformedURLException;
007 import java.net.URL;
008 import java.util.jar.Attributes;
009 import java.util.jar.JarFile;
010
011 import org.maltparser.core.exception.MaltChainedException;
012
013 /**
014 The class Plugin contains information about a plug-in that comply to the the MaltParser Plugin Standard.
015
016
017 @author Johan Hall
018
019 @since 1.0
020 */
021 public class Plugin {
022 private JarFile archive;
023 private URL url;
024 private String pluginName;
025
026
027 /**
028 * Creates a plug-in container.
029 *
030 * @param filename The file name that contains the plugin
031 * @throws MaltChainedException
032 */
033 public Plugin(String filename) throws MaltChainedException {
034 this(new File(filename));
035 }
036
037 /**
038 * Creates a plug-in container.
039 *
040 * @param file The jar file that contains the plugin
041 * @throws MaltChainedException
042 */
043 public Plugin(File file) throws MaltChainedException {
044 try {
045 setArchive(new JarFile(file));
046 setUrl(new URL("file", null, file.getAbsolutePath()));
047 register();
048 } catch (FileNotFoundException e) {
049 throw new PluginException("The file '"+file.getPath()+File.separator+file.getName()+"' cannot be found. ", e);
050 } catch (MalformedURLException e) {
051 throw new PluginException("Malformed URL to the jar file '"+archive.getName()+"'. ", e);
052 } catch (IOException e) {
053 throw new PluginException("The jar file '"+file.getPath()+File.separator+file.getName()+"' cannot be initialized. ", e);
054 }
055 }
056
057 /**
058 * @throws MaltChainedException
059 */
060 private void register() throws MaltChainedException {
061 try {
062 Attributes atts = archive.getManifest().getMainAttributes();
063 pluginName = atts.getValue("Plugin-Name");
064 if (pluginName == null) {
065 pluginName = archive.getName();
066 }
067 } catch (IOException e) {
068 throw new PluginException("Could not get the 'Plugin-Name' in the manifest for the plugin (jar-file). ", e);
069 }
070 }
071
072 /**
073 * Returns the archive.
074 *
075 * @return the jar archive.
076 */
077 public JarFile getArchive() {
078 return archive;
079 }
080 /**
081 * Sets the archive to set.
082 *
083 * @param archive The archive to set.
084 */
085 public void setArchive(JarFile archive) {
086 this.archive = archive;
087 }
088 /**
089 * Returns the plug-in name.
090 *
091 * @return the plug-in name.
092 */
093 public String getPluginName() {
094 return pluginName;
095 }
096 /**
097 * Sets the plug-in name
098 *
099 * @param pluginName the plug-in name
100 */
101 public void setPluginName(String pluginName) {
102 this.pluginName = pluginName;
103 }
104
105 /**
106 * Returns the URL
107 *
108 * @return the URL
109 */
110 public URL getUrl() {
111 return url;
112 }
113 /**
114 * Sets the URL.
115 *
116 * @param url the URL
117 */
118 public void setUrl(URL url) {
119 this.url = url;
120 }
121
122 /* (non-Javadoc)
123 * @see java.lang.Object#toString()
124 */
125 public String toString() {
126 StringBuilder sb = new StringBuilder();
127
128 sb.append(pluginName+" : "+url.toString());
129 return sb.toString();
130 }
131 }