20 package net.sf.gridarta.plugin;
23 import java.io.IOException;
30 import nu.xom.Builder;
31 import nu.xom.Document;
32 import nu.xom.Element;
33 import nu.xom.ParsingException;
34 import org.apache.log4j.Category;
35 import org.apache.log4j.Logger;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.XMLReader;
40 import org.xml.sax.helpers.XMLReaderFactory;
68 public static <G extends GameObject<G, A, R>, A extends
MapArchObject<A>, R extends
Archetype<G, A, R>>
PluginModel<G, A, R> loadPlugins(@NotNull
final PluginParameterFactory<G, A, R> pluginParameterFactory, @NotNull
final PluginModelParser<G, A, R> pluginModelParser, @NotNull
final ErrorView errorView, @NotNull
final File pluginsDir) {
70 final File[] files = pluginsDir.listFiles();
77 for (
final File pluginFile : files) {
78 final String name = pluginFile.getName();
79 if (!name.startsWith(
".") && !name.endsWith(
"~") && pluginFile.isFile()) {
85 if (LOG.isDebugEnabled()) {
86 LOG.debug(
"storing with code " + plugin.
getCode());
90 }
catch (
final IOException ex) {
92 }
catch (
final ParsingException ex) {
94 }
catch (
final SAXException ex) {
99 if (LOG.isInfoEnabled()) {
100 LOG.info(
"Loaded " + (pluginModel.
getPluginCount() - pluginCount) +
" plugins from '" + pluginsDir +
"'.");
105 public static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends
Archetype<G, A, R>>
Plugin<G, A, R> loadXML(@NotNull
final PluginModelParser<G, A, R> pluginModelParser, @NotNull
final File file)
throws IOException, ParsingException, SAXException {
106 final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
107 final Builder builder =
new Builder(xmlReader,
false);
108 final Document d = builder.build(file);
109 return loadXML(pluginModelParser, d, file);
113 private static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends
Archetype<G, A, R>>
Plugin<G, A, R> loadXML(@NotNull
final PluginModelParser<G, A, R> pluginModelParser, @NotNull
final Document doc, @Nullable
final File file)
throws IOException {
114 final Element elt = doc.getRootElement();
116 throw new IOException(
"plugin file is empty");
118 if (!elt.getLocalName().equalsIgnoreCase(
"script")) {
119 throw new IOException(
"missing root element named \"script\"");
124 if (LOG.isDebugEnabled()) {
125 LOG.debug(
"plugin: " + plugin.
getName());
void setFile(@Nullable final File file)
Sets the location to save this plugin to.
Factory for Plugin Parameters.
Plugin< G, A, R > getPlugin(@NotNull final String name)
static final Category LOG
The Logger for printing log messages.
Utility class for loading plugins.
Defines possible error categories for ErrorView instances.
Interface for classes displaying error messages.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
PluginModelLoader()
Private constructor to prevent instantiation.
String getName()
Returns the name of this plugin.
GameObjects are the objects based on Archetypes found on maps.
String getCode()
Returns the executable code of this plugin.
boolean addPlugin(@NotNull final Plugin< G, A, R > plugin)
Add a new plugin.
Interface for MapArchObjects.