From: <aki...@us...> - 2008-10-01 16:15:46
|
Revision: 5309 http://gridarta.svn.sourceforge.net/gridarta/?rev=5309&view=rev Author: akirschbaum Date: 2008-10-01 16:15:21 +0000 (Wed, 01 Oct 2008) Log Message: ----------- Extract ScriptModelParser from ScriptModel. Modified Paths: -------------- trunk/crossfire/src/cfeditor/script/ScriptController.java trunk/src/app/net/sf/gridarta/script/ScriptModel.java Added Paths: ----------- trunk/src/app/net/sf/gridarta/script/ScriptModelParser.java Modified: trunk/crossfire/src/cfeditor/script/ScriptController.java =================================================================== --- trunk/crossfire/src/cfeditor/script/ScriptController.java 2008-10-01 16:02:21 UTC (rev 5308) +++ trunk/crossfire/src/cfeditor/script/ScriptController.java 2008-10-01 16:15:21 UTC (rev 5309) @@ -50,6 +50,7 @@ import net.sf.gridarta.filter.Filter; import net.sf.gridarta.filter.FilterControl; import net.sf.gridarta.script.ScriptModel; +import net.sf.gridarta.script.ScriptModelParser; import net.sf.gridarta.script.parameter.PluginParameter; import net.sf.gridarta.script.parameter.NoSuchParameterException; import net.sf.japi.swing.ActionFactory; @@ -195,7 +196,7 @@ } final ScriptModel cScript = new ScriptModel(); - cScript.fromXML(elt); + ScriptModelParser.fromXML(cScript, elt); cScript.setFile(file); log.debug("script: " + cScript.getName()); if (override || !scripts.containsKey(cScript.getName())) { @@ -318,7 +319,7 @@ public static void saveScript(@NotNull final ScriptModel scriptModel, @NotNull final File file) throws IOException { final FileOutputStream fos = new FileOutputStream(file); try { - final Element root = scriptModel.toXML(); + final Element root = ScriptModelParser.toXML(scriptModel); final Document d = new Document(root); final XMLOutputter out = new XMLOutputter(); out.setFormat(Format.getPrettyFormat()); Modified: trunk/src/app/net/sf/gridarta/script/ScriptModel.java =================================================================== --- trunk/src/app/net/sf/gridarta/script/ScriptModel.java 2008-10-01 16:02:21 UTC (rev 5308) +++ trunk/src/app/net/sf/gridarta/script/ScriptModel.java 2008-10-01 16:15:21 UTC (rev 5309) @@ -32,9 +32,6 @@ import net.sf.gridarta.script.parameter.PluginParameter; import net.sf.gridarta.script.parameter.PluginParameterFactory; import org.apache.log4j.Logger; -import org.jdom.CDATA; -import org.jdom.Element; -import org.jdom.IllegalDataException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -282,6 +279,16 @@ notifyListeners(); } + public void setScriptType(final int type) { + if (scriptType == type) { + return; + } + + scriptType = type; + modified = true; + notifyListeners(); + } + public void setAutoboot(final boolean b) { toggleScriptType(PLUGIN_AUTOBOOT, b); } @@ -294,77 +301,6 @@ toggleScriptType(PLUGIN_FILTER, b); } - public void fromXML(final Element node) { - setName(node.getChildTextTrim("name")); - setCode(node.getChildTextTrim("code")); - scriptType = 0; - final Element mode = node.getChild("mode"); - if (mode == null) { - scriptType = PLUGIN_SCRIPT; - } else { - final List<Element> modes = mode.getChildren(); - for (final Element m : modes) { - final boolean b = Boolean.valueOf(m.getTextTrim()); - final String name = m.getName(); - if ("autoboot".equalsIgnoreCase(name) && b) { - scriptType |= PLUGIN_AUTOBOOT; - } else if ("filter".equalsIgnoreCase(name) && b) { - scriptType |= PLUGIN_FILTER; - } else if ("bash".equalsIgnoreCase(name) && b) { - scriptType |= PLUGIN_SCRIPT; - } - } - } - final List<Element> params = node.getChildren("parameter"); - if (params != null && !params.isEmpty()) { - for (final Element parameter : params) { - final PluginParameter<?, ?> p; - try { - p = PluginParameterFactory.createParameter(parameter); - } catch (final NoSuchParameterException ex) { - log.warn("Cannot create parameter type " + ex.getMessage() + ", dropping parameter"); - continue; - } - addParameter(p); - } - } - modified = false; - } - - public Element toXML() { - final Element root = new Element("script"); - final Element n = new Element("name"); - final Element c = new Element("code"); - n.addContent(name); - try { - c.addContent(new CDATA(code));// protect code in xml! - } catch (final IllegalDataException e) { - //can't be converted to CDATA :( - c.addContent(code); - } - root.addContent(n); - root.addContent(c); - final Element modes = new Element("mode"); - - { - final Element autoboot = new Element("autoboot"); - autoboot.addContent(Boolean.toString((scriptType & PLUGIN_AUTOBOOT) != 0)); - final Element bash = new Element("bash"); - bash.addContent(Boolean.toString((scriptType & PLUGIN_SCRIPT) != 0)); - final Element filter = new Element("filter"); - filter.addContent(Boolean.toString((scriptType & PLUGIN_FILTER) != 0)); - modes.addContent(autoboot); - modes.addContent(bash); - modes.addContent(filter); - } - - root.addContent(modes); - for (final PluginParameter<?, ?> parameter : parameters) { - root.addContent(parameter.toXML()); - } - return root; - } - public void convertType(final int index, final String newType) throws NoSuchParameterException { parameters.set(index, PluginParameterFactory.createParameter(newType, parameters.get(index).toXML())); modified = true; Added: trunk/src/app/net/sf/gridarta/script/ScriptModelParser.java =================================================================== --- trunk/src/app/net/sf/gridarta/script/ScriptModelParser.java (rev 0) +++ trunk/src/app/net/sf/gridarta/script/ScriptModelParser.java 2008-10-01 16:15:21 UTC (rev 5309) @@ -0,0 +1,118 @@ +/* + * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. + * Copyright (C) 2000-2007 The Gridarta Developers. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package net.sf.gridarta.script; + +import java.util.List; +import net.sf.gridarta.script.parameter.NoSuchParameterException; +import net.sf.gridarta.script.parameter.PluginParameter; +import net.sf.gridarta.script.parameter.PluginParameterFactory; +import org.apache.log4j.Logger; +import org.jdom.CDATA; +import org.jdom.Element; +import org.jdom.IllegalDataException; +import org.jetbrains.annotations.NotNull; + +/** + * Utility parser class for {@link ScriptModel} instances. + * @author tchize + */ +public class ScriptModelParser { + + /** The Logger for printing log messages. */ + private static final Logger log = Logger.getLogger(ScriptModelParser.class); + + /** + * Private constructor to prevent instantiation. + */ + private ScriptModelParser() { + } + + public static void fromXML(@NotNull final ScriptModel scriptModel, @NotNull final Element node) { + scriptModel.setName(node.getChildTextTrim("name")); + scriptModel.setCode(node.getChildTextTrim("code")); + int scriptType = 0; + final Element mode = node.getChild("mode"); + if (mode == null) { + scriptType = ScriptModel.PLUGIN_SCRIPT; + } else { + final List<Element> modes = mode.getChildren(); + for (final Element m : modes) { + final boolean b = Boolean.valueOf(m.getTextTrim()); + final String name = m.getName(); + if ("autoboot".equalsIgnoreCase(name) && b) { + scriptType |= ScriptModel.PLUGIN_AUTOBOOT; + } else if ("filter".equalsIgnoreCase(name) && b) { + scriptType |= ScriptModel.PLUGIN_FILTER; + } else if ("bash".equalsIgnoreCase(name) && b) { + scriptType |= ScriptModel.PLUGIN_SCRIPT; + } + } + } + scriptModel.setScriptType(scriptType); + final List<Element> params = node.getChildren("parameter"); + if (params != null && !params.isEmpty()) { + for (final Element parameter : params) { + final PluginParameter<?, ?> p; + try { + p = PluginParameterFactory.createParameter(parameter); + } catch (final NoSuchParameterException ex) { + log.warn("Cannot create parameter type " + ex.getMessage() + ", dropping parameter"); + continue; + } + scriptModel.addParameter(p); + } + } + scriptModel.resetModified(); + } + + public static Element toXML(@NotNull final ScriptModel scriptModel) { + final Element root = new Element("script"); + final Element n = new Element("name"); + final Element c = new Element("code"); + n.addContent(scriptModel.getName()); + try { + c.addContent(new CDATA(scriptModel.getCode()));// protect code in xml! + } catch (final IllegalDataException e) { + //can't be converted to CDATA :( + c.addContent(scriptModel.getCode()); + } + root.addContent(n); + root.addContent(c); + final Element modes = new Element("mode"); + + { + final Element autoboot = new Element("autoboot"); + autoboot.addContent(Boolean.toString(scriptModel.isAutoboot())); + final Element bash = new Element("bash"); + bash.addContent(Boolean.toString(scriptModel.isBash())); + final Element filter = new Element("filter"); + filter.addContent(Boolean.toString(scriptModel.isFilter())); + modes.addContent(autoboot); + modes.addContent(bash); + modes.addContent(filter); + } + + root.addContent(modes); + for (final PluginParameter<?, ?> parameter : scriptModel) { + root.addContent(parameter.toXML()); + } + return root; + } +} // class ScriptModelParser Property changes on: trunk/src/app/net/sf/gridarta/script/ScriptModelParser.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |