Gridarta Editor
PluginModelParser.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 
30 import nu.xom.Attribute;
31 import nu.xom.Element;
32 import nu.xom.Elements;
33 import nu.xom.ParentNode;
34 import nu.xom.Text;
35 import org.apache.log4j.Category;
36 import org.apache.log4j.Logger;
37 import org.jetbrains.annotations.NotNull;
38 
44 public class PluginModelParser<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
45 
49  @NotNull
50  private static final String AUTO_BOOT = "autoboot";
51 
55  @NotNull
56  private static final String FILTER = "filter";
57 
61  @NotNull
62  private static final String BASH = "bash";
63 
67  @NotNull
68  private static final Category LOG = Logger.getLogger(PluginModelParser.class);
69 
73  @NotNull
75 
81  this.pluginParameterFactory = pluginParameterFactory;
82  }
83 
89  @NotNull
90  public Plugin<G, A, R> fromXML(@NotNull final Element node) {
91  final Plugin<G, A, R> pluginModel = new Plugin<>(XmlUtils.getChild(node, "name").getValue().trim(), pluginParameterFactory);
92  pluginModel.setCode(XmlUtils.getChild(node, "code").getValue().trim());
93  boolean isAutoBoot = false;
94  boolean isFilter = false;
95  boolean isScript = false;
96  final Elements modeElements = node.getChildElements("mode");
97  if (modeElements.size() == 0) {
98  isScript = true;
99  } else {
100  for (final Element modeChild : new ElementsIterable(modeElements.get(0).getChildElements())) {
101  final boolean value = Boolean.valueOf(modeChild.getValue());
102  final String name = modeChild.getLocalName();
103  if (name.equalsIgnoreCase(AUTO_BOOT) && value) {
104  isAutoBoot = true;
105  } else if (name.equalsIgnoreCase(FILTER) && value) {
106  isFilter = true;
107  } else if (name.equalsIgnoreCase(BASH) && value) {
108  isScript = true;
109  }
110  }
111  }
112  pluginModel.setAutoBoot(isAutoBoot);
113  pluginModel.setFilter(isFilter);
114  pluginModel.setScript(isScript);
115  for (final Element parameter : new ElementsIterable(node.getChildElements("parameter"))) {
116  PluginParameter<G, A, R, ?> pluginParameter;
117  try {
118  pluginParameter = pluginParameterFactory.createParameter(parameter);
119  } catch (final NoSuchParameterException ex) {
120  LOG.warn("Parameter type " + ex.getMessage() + " in plugin " + pluginModel + " is unknown");
121  pluginParameter = pluginParameterFactory.createStringParameter(parameter);
122  }
123  pluginModel.addParameter(pluginParameter);
124  }
125  pluginModel.resetModified();
126  return pluginModel;
127  }
128 
134  @NotNull
135  public Element toXML(@NotNull final Plugin<G, A, R> plugin) {
136  final Element root = new Element("script");
137  final Element name = new Element("name");
138  final Element code = new Element("code");
139  name.appendChild(plugin.getName());
140  code.appendChild(new Text(plugin.getCode()));
141  code.addAttribute(new Attribute("xml:space", "http://www.w3.org/XML/1998/namespace", "preserve"));
142  root.appendChild(name);
143  root.appendChild(code);
144  final ParentNode modes = new Element("mode");
145 
146  final Element autoBoot = new Element(AUTO_BOOT);
147  autoBoot.appendChild(Boolean.toString(plugin.isAutoBoot()));
148  final Element bash = new Element(BASH);
149  bash.appendChild(Boolean.toString(plugin.isScript()));
150  final Element filter = new Element(FILTER);
151  filter.appendChild(Boolean.toString(plugin.isFilter()));
152  modes.appendChild(autoBoot);
153  modes.appendChild(bash);
154  modes.appendChild(filter);
155 
156  root.appendChild(modes);
157  for (final PluginParameter<G, A, R, ?> pluginParameter : plugin) {
158  root.appendChild(plugin.toXML(pluginParameter));
159  }
160  return root;
161  }
162 
163 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.plugin
Definition: BshRunnable.java:20
net.sf.gridarta.plugin.PluginModelParser.toXML
Element toXML(@NotNull final Plugin< G, A, R > plugin)
Converts a Plugin instance to XML representation.
Definition: PluginModelParser.java:135
net.sf.gridarta.plugin.parameter.PluginParameter
Parameter for a Plugin.
Definition: PluginParameter.java:33
net.sf.gridarta.plugin.PluginModelParser.FILTER
static final String FILTER
Whether the plugin is a filter.
Definition: PluginModelParser.java:56
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.plugin.PluginModelParser.pluginParameterFactory
final PluginParameterFactory< G, A, R > pluginParameterFactory
The PluginParameterFactory to use.
Definition: PluginModelParser.java:74
net.sf.gridarta.plugin.Plugin.setScript
void setScript(final boolean script)
Sets whether this plugin is a stand-alone plugin.
Definition: Plugin.java:351
net.sf
net.sf.gridarta.plugin.PluginModelParser
Converter for Plugin instances to or from XML representation.
Definition: PluginModelParser.java:44
net.sf.gridarta.plugin.parameter.PluginParameterFactory.createParameter
PluginParameter< G, A, R, ?> createParameter(@NotNull final Element parameterNode)
Definition: PluginParameterFactory.java:73
net.sf.gridarta.plugin.PluginModelParser.fromXML
Plugin< G, A, R > fromXML(@NotNull final Element node)
Creates a Plugin instance from XML representation.
Definition: PluginModelParser.java:90
net.sf.gridarta.plugin.Plugin.setCode
void setCode(@NotNull final String code)
Sets the executable code of this plugin.
Definition: Plugin.java:165
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.plugin.PluginModelParser.PluginModelParser
PluginModelParser(@NotNull final PluginParameterFactory< G, A, R > pluginParameterFactory)
Creates a new instance.
Definition: PluginModelParser.java:80
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.plugin.parameter.NoSuchParameterException
Thrown if a parameter does not exist.
Definition: NoSuchParameterException.java:29
net.sf.gridarta.utils.XmlUtils.getChild
static Element getChild(@NotNull final Element parentElement, @NotNull final String childName)
Returns a child Element of a parent element.
Definition: XmlUtils.java:46
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.plugin.Plugin.addParameter
void addParameter(@NotNull final PluginParameter< G, A, R, ?> pluginParameter)
Adds a plugin parameter to this plugin.
Definition: Plugin.java:289
net.sf.gridarta.plugin.parameter.PluginParameterFactory.createStringParameter
PluginParameter< G, A, R, ?> createStringParameter(@NotNull final Element parameterNode)
Creates a new StringParameter from XML representation.
Definition: PluginParameterFactory.java:83
net.sf.gridarta.utils.xml.ElementsIterable
Definition: ElementsIterable.java:30
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.plugin.Plugin.setAutoBoot
void setAutoBoot(final boolean autoBoot)
Sets whether this plugin is run whenever the editor starts.
Definition: Plugin.java:339
net.sf.gridarta.plugin.Plugin.setFilter
void setFilter(final boolean filter)
Sets whether this plugin is a filter.
Definition: Plugin.java:363
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.plugin.Plugin< G, A, R >
net.sf.gridarta.utils.xml
Definition: ElementsIterable.java:20
net.sf.gridarta.plugin.PluginModelParser.LOG
static final Category LOG
The Logger for printing log messages.
Definition: PluginModelParser.java:68
net.sf.gridarta.plugin.parameter.PluginParameterFactory
Factory for Plugin Parameters.
Definition: PluginParameterFactory.java:37
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.plugin.Plugin.resetModified
void resetModified()
Marks the plugin as unmodified since last save.
Definition: Plugin.java:401
net.sf.gridarta.utils.XmlUtils
XML related utility functions.
Definition: XmlUtils.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.plugin.PluginModelParser.BASH
static final String BASH
Whether the plugin is a stand-alone plugin.
Definition: PluginModelParser.java:62
net.sf.gridarta.plugin.PluginModelParser.AUTO_BOOT
static final String AUTO_BOOT
Whether the plugin is in auto-boot mode.
Definition: PluginModelParser.java:50
net.sf.gridarta.plugin.parameter
Definition: AbstractPathParameter.java:20