Gridarta Editor
PluginModelLoader.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.plugin;
21 
22 import java.io.File;
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;
41 
47 public class PluginModelLoader<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
48 
52  private static final Category LOG = Logger.getLogger(PluginModelLoader.class);
53 
57  private PluginModelLoader() {
58  }
59 
67  @NotNull
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) {
69  final PluginModel<G, A, R> pluginModel = new PluginModel<>(pluginParameterFactory, pluginModelParser);
70  final File[] files = pluginsDir.listFiles();
71  if (files == null) {
72  errorView.addWarning(ErrorViewCategory.SCRIPTS_DIR_INVALID, pluginsDir + ": directory not readable");
73  return pluginModel;
74  }
75 
76  final int pluginCount = pluginModel.getPluginCount();
77  for (final File pluginFile : files) {
78  final String name = pluginFile.getName();
79  if (!name.startsWith(".") && !name.endsWith("~") && pluginFile.isFile()) {
80  try {
81  final Plugin<G, A, R> plugin = loadXML(pluginModelParser, pluginFile);
82  if (pluginModel.getPlugin(plugin.getName()) == null) {
83  if (LOG.isDebugEnabled()) {
84  LOG.debug("storing with code " + plugin.getCode());
85  }
86  pluginModel.addPlugin(plugin);
87  } else {
88  errorView.addWarning(ErrorViewCategory.SCRIPTS_FILE_INVALID, pluginFile + ": duplicate plugin '" + plugin.getName() + "'");
89  }
90  } catch (final IOException | ParsingException | SAXException ex) {
91  errorView.addWarning(ErrorViewCategory.SCRIPTS_FILE_INVALID, pluginFile + ": " + ex.getMessage());
92  }
93  }
94  }
95  if (LOG.isInfoEnabled()) {
96  LOG.info("Loaded " + (pluginModel.getPluginCount() - pluginCount) + " plugins from '" + pluginsDir + "'.");
97  }
98  return pluginModel;
99  }
100 
101  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 {
102  final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
103  final Builder builder = new Builder(xmlReader, false);
104  final Document d = builder.build(file);
105  return loadXML(pluginModelParser, d, file);
106  }
107 
108  @NotNull
109  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 {
110  final Element elt = doc.getRootElement();
111  if (elt == null) {
112  throw new IOException("plugin file is empty");
113  }
114  if (!elt.getLocalName().equalsIgnoreCase("script")) {
115  throw new IOException("missing root element named \"script\"");
116  }
117 
118  final Plugin<G, A, R> plugin = pluginModelParser.fromXML(elt);
119  plugin.setFile(file);
120  if (LOG.isDebugEnabled()) {
121  LOG.debug("plugin: " + plugin.getName());
122  }
123 
124  return plugin;
125  }
126 
127 }
net.sf.gridarta.model.errorview.ErrorView
Definition: ErrorView.java:28
net.sf.gridarta.plugin
Definition: BshRunnable.java:20
net.sf.gridarta.plugin.Plugin.setFile
void setFile(@Nullable final File file)
Definition: Plugin.java:426
net.sf.gridarta
net.sf.gridarta.model.maparchobject.MapArchObject
Definition: MapArchObject.java:40
files
Standard Edition Runtime Environment README Import and export control rules on cryptographic software vary from country to country The Java Cryptography Java provides two different sets of cryptographic policy files
Definition: README.txt:26
net.sf.gridarta.plugin.PluginModelLoader.PluginModelLoader
PluginModelLoader()
Definition: PluginModelLoader.java:57
net.sf.gridarta.plugin.PluginModel< G, A, R >
net.sf
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.plugin.parameter.PluginParameterFactory
Definition: PluginParameterFactory.java:37
net.sf.gridarta.plugin.PluginModelLoader.loadPlugins
static< G extends GameObject< G, A, R > A extends 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)
Definition: PluginModelLoader.java:68
net.sf.gridarta.plugin.PluginModel.getPlugin
Plugin< G, A, R > getPlugin(@NotNull final String name)
Definition: PluginModel.java:79
net.sf.gridarta.model.gameobject.GameObject
Definition: GameObject.java:36
net.sf.gridarta.model.gameobject
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.plugin.PluginModel.getPluginCount
int getPluginCount()
Definition: PluginModel.java:83
net.sf.gridarta.plugin.PluginModel.addPlugin
boolean addPlugin(@NotNull final Plugin< G, A, R > plugin)
Definition: PluginModel.java:104
net.sf.gridarta.plugin.PluginModelParser< G, A, R >
net.sf.gridarta.plugin.PluginModelLoader
Definition: PluginModelLoader.java:47
net.sf.gridarta.plugin.Plugin< G, A, R >
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.plugin.Plugin.getCode
String getCode()
Definition: Plugin.java:157
net.sf.gridarta.model.errorview.ErrorViewCategory.SCRIPTS_DIR_INVALID
SCRIPTS_DIR_INVALID
Definition: ErrorViewCategory.java:78
net.sf.gridarta.plugin.PluginModelLoader.loadXML
static< G extends GameObject< G, A, R > A extends 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)
Definition: PluginModelLoader.java:109
net.sf.gridarta.model.errorview.ErrorViewCategory.SCRIPTS_FILE_INVALID
SCRIPTS_FILE_INVALID
Definition: ErrorViewCategory.java:80
net.sf.gridarta.model
net.sf.gridarta.model.errorview.ErrorViewCategory
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.plugin.PluginModelLoader.LOG
static final Category LOG
Definition: PluginModelLoader.java:52
net.sf.gridarta.plugin.Plugin.getName
String getName()
Definition: Plugin.java:148
net.sf.gridarta.plugin.PluginModelLoader.loadXML
static< G extends GameObject< G, A, R > A extends R extends Archetype< G, A, R > Plugin< G, A, R > loadXML(@NotNull final PluginModelParser< G, A, R > pluginModelParser, @NotNull final File file)
Definition: PluginModelLoader.java:101
net.sf.gridarta.model.archetype.Archetype
Definition: Archetype.java:41
net.sf.gridarta.plugin.parameter
Definition: AbstractPathParameter.java:20