From: <aki...@us...> - 2008-07-31 10:00:04
|
Revision: 4603 http://gridarta.svn.sourceforge.net/gridarta/?rev=4603&view=rev Author: akirschbaum Date: 2008-07-31 10:00:10 +0000 (Thu, 31 Jul 2008) Log Message: ----------- Extract ArchetypeTypeSetParser from ArchetypeTypeSet. Modified Paths: -------------- trunk/crossfire/src/cfeditor/CMainControl.java trunk/daimonin/src/daieditor/CMainControl.java trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSet.java Added Paths: ----------- trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSetParser.java Modified: trunk/crossfire/src/cfeditor/CMainControl.java =================================================================== --- trunk/crossfire/src/cfeditor/CMainControl.java 2008-07-30 22:53:45 UTC (rev 4602) +++ trunk/crossfire/src/cfeditor/CMainControl.java 2008-07-31 10:00:10 UTC (rev 4603) @@ -54,6 +54,7 @@ import net.sf.gridarta.MainControl; import net.sf.gridarta.XmlHelper; import net.sf.gridarta.archtype.ArchetypeTypeSet; +import net.sf.gridarta.archtype.ArchetypeTypeSetParser; import net.sf.gridarta.gameobject.anim.AnimationObjects; import net.sf.gridarta.gameobject.match.GameObjectMatcher; import net.sf.gridarta.gameobject.match.GameObjectMatchers; @@ -257,7 +258,7 @@ log.error("Cannot create XML parser: " + ex.getMessage()); throw new MissingResourceException("Cannot create XML parser: " + ex.getMessage(), null, null); } - archetypeTypeSet = new ArchetypeTypeSet(xmlHelper.getDocumentBuilder(), xmlHelper.getXPath()); + archetypeTypeSet = new ArchetypeTypeSet(); final GameObjectMatchers gameObjectMatchers = new GameObjectMatchers(xmlHelper.getDocumentBuilder(), xmlHelper.getXPath()); new ArchetypeSetSpellLoader<GameObject, MapArchObject, Archetype>().load(archetypeSet, Archetype.TYPE_SPELL, gameObjectSpells); gameObjectSpells.sort(); @@ -289,9 +290,10 @@ } final MapActions mapActions = new MapActions(mainView, this, getMapManager(), exitMatcher, mapFileFilter, selectedSquareView); archetypeTypeSet.getListTable().put("event", ScriptArchUtils.getEventTypes()); + final ArchetypeTypeSetParser archetypeTypeSetParser = new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), xmlHelper.getXPath(), archetypeTypeSet); try { final String filename = IOUtils.getResourceURLAsString(getConfigurationDirectory(), CommonConstants.TYPEDEF_FILE); - archetypeTypeSet.loadTypesFromXML(filename); + archetypeTypeSetParser.loadTypesFromXML(filename); } catch (final FileNotFoundException ex) { log.error("Cannot read " + CommonConstants.TYPEDEF_FILE + ": " + ex.getMessage()); } Modified: trunk/daimonin/src/daieditor/CMainControl.java =================================================================== --- trunk/daimonin/src/daieditor/CMainControl.java 2008-07-30 22:53:45 UTC (rev 4602) +++ trunk/daimonin/src/daieditor/CMainControl.java 2008-07-31 10:00:10 UTC (rev 4603) @@ -58,6 +58,7 @@ import net.sf.gridarta.MainControl; import net.sf.gridarta.XmlHelper; import net.sf.gridarta.archtype.ArchetypeTypeSet; +import net.sf.gridarta.archtype.ArchetypeTypeSetParser; import net.sf.gridarta.gameobject.anim.AnimationObjects; import net.sf.gridarta.gameobject.face.FaceObjectProviders; import net.sf.gridarta.gameobject.match.GameObjectMatcher; @@ -317,7 +318,7 @@ log.error("Cannot create XML parser: " + ex.getMessage()); throw new MissingResourceException("Cannot create XML parser: " + ex.getMessage(), null, null); } - archetypeTypeSet = new ArchetypeTypeSet(xmlHelper.getDocumentBuilder(), xmlHelper.getXPath()); + archetypeTypeSet = new ArchetypeTypeSet(); final GameObjectMatchers gameObjectMatchers = new GameObjectMatchers(xmlHelper.getDocumentBuilder(), xmlHelper.getXPath()); gameObjectSpells.sort(); XMLSpellLoader.load(getConfigurationDirectory(), CommonConstants.SPELL_FILE, xmlHelper.getDocumentBuilder(), numberSpells); @@ -348,9 +349,10 @@ throw new MissingResourceException("GameObjectMatcher 'exit' does not exist", null, null); } final MapActions mapActions = new MapActions(mainView, this, getMapManager(), exitMatcher, mapFileFilter, selectedSquareView); + final ArchetypeTypeSetParser archetypeTypeSetParser = new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), xmlHelper.getXPath(), archetypeTypeSet); try { final String filename = IOUtils.getResourceURLAsString(getConfigurationDirectory(), CommonConstants.TYPEDEF_FILE); - archetypeTypeSet.loadTypesFromXML(filename); + archetypeTypeSetParser.loadTypesFromXML(filename); } catch (final FileNotFoundException ex) { log.error("Cannot read " + CommonConstants.TYPEDEF_FILE + ": " + ex.getMessage()); } Modified: trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSet.java =================================================================== --- trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSet.java 2008-07-30 22:53:45 UTC (rev 4602) +++ trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSet.java 2008-07-31 10:00:10 UTC (rev 4603) @@ -19,25 +19,15 @@ package net.sf.gridarta.archtype; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathExpressionException; -import net.sf.gridarta.CommonConstants; import net.sf.gridarta.gameobject.Archetype; import net.sf.gridarta.gameobject.GameObject; -import net.sf.japi.xml.NodeListIterator; import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.SAXException; /** * Common base class for ArchetypeTypeSet. @@ -48,20 +38,14 @@ /** The Logger for printing log messages. */ private static final Logger log = Logger.getLogger(ArchetypeTypeSet.class); - /** DocumentBuilder. */ - private final DocumentBuilder documentBuilder; - - /** The XPath for using XPath. */ - private final XPath xpath; - /** The default type. */ - private ArchetypeType defaultArchetypeType = null; + ArchetypeType defaultArchetypeType = null; /** * The default (fallback) type used for game objects not matching any * defined type. */ - private ArchetypeType fallbackArchetypeType = null; + ArchetypeType fallbackArchetypeType = null; /** * Lists with all ArchetypeTypes. Contains all but the default @@ -69,7 +53,7 @@ * @todo verify whether the default ArchetypeType really is not included in * this list. */ - private final List<ArchetypeType> archetypeTypeList = new ArrayList<ArchetypeType>(); // All but the default ArchetypeType + final List<ArchetypeType> archetypeTypeList = new ArrayList<ArchetypeType>(); // All but the default ArchetypeType /** * Table with List objects for lists (value) accessible by name (key). The @@ -78,151 +62,27 @@ * @todo Refactor the List to contain Pairs or something like that so it * doesn't contain two types. */ - private final Map<String, List<?>> listTable; + final Map<String, List<?>> listTable; /** Table with CAttribBitmask objects (value) accessible by name (key). */ - private final Map<String, CAttribBitmask> bitmaskTable; + final Map<String, CAttribBitmask> bitmaskTable; // table with type arch type name as keys (String), and arch type object as values (ArchetypeType) - private final Map<String, ArchetypeType> archetypeTypeNames = new HashMap<String, ArchetypeType>(); + final Map<String, ArchetypeType> archetypeTypeNames = new HashMap<String, ArchetypeType>(); /** * Constructor - Parsing all the data from the xml definitions file * 'types.xml'. - * @param documentBuilder DocumentBuilder to use for parsing XML. - * @param xpath XPath to use for applying for XPath expressions. * @todo I consume too much time. Why? XPath is too slow. Let's hope Beta4 * arrives soon, then some XPath that contains backup compatible expressions * can be replaced by faster direct access */ - public ArchetypeTypeSet(@NotNull final DocumentBuilder documentBuilder, @NotNull final XPath xpath) { - this.xpath = xpath; - this.documentBuilder = documentBuilder; + public ArchetypeTypeSet() { listTable = new HashMap<String, List<?>>(); bitmaskTable = new HashMap<String, CAttribBitmask>(); } - public void loadTypesFromXML(@NotNull final String filename) { - final Map<String, List<String>> ignoreListTable = new HashMap<String, List<String>>(); - try { - // parse xml document - final Document doc = documentBuilder.parse(filename); - - // start parsing the xml - final Element root = doc.getDocumentElement(); - parseBitmasks(root); - parseLists(root); - parseIgnoreLists(root, ignoreListTable); - defaultArchetypeType = parseDefaultType(root, ignoreListTable); - parseTypes(root, ignoreListTable); - - if (log.isInfoEnabled()) { - log.info("Loaded " + archetypeTypeList.size() + " types from '" + filename + "\'"); - } - } catch (final SAXException e) { - log.error("Parsing error in '" + filename + "':\n" + e.getMessage()); - } catch (final IOException e) { - log.error("Cannot read file '" + filename + "'!"); - } catch (final XPathExpressionException e) { - log.error("XPath error: " + e.getMessage()); - } - - fallbackArchetypeType = archetypeTypeNames.get("Misc"); - if (fallbackArchetypeType == null) { - fallbackArchetypeType = new ArchetypeType(defaultArchetypeType); - } - } - - private void parseBitmasks(final Element root) throws XPathExpressionException { - for (final Element elem : new NodeListIterator<Element>(xpath, root, "bitmasks/bitmask")) { - bitmaskTable.put(elem.getAttribute("name"), new CAttribBitmask(xpath, elem)); - } - } - - private void parseLists(final Element root) throws ArchetypeTypeParseException, XPathExpressionException { - for (final Element elem : new NodeListIterator<Element>(xpath, root, "lists/list")) { - if (elem.getAttribute("name") == null) { - throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': cannot load list element without 'name'."); - } else { - final List<?> list = parseListFromElement(elem); - if (list != null && !list.isEmpty()) { - listTable.put(elem.getAttribute("name"), list); - } - } - } - } - /** - * Parse a list vector from an xml list element. - * @param root element to parse - * @return List with data parsed from <var>root</var> - * @throws XPathExpressionException In case of XPath issues (should never - * happen). - * @todo improve this comment - */ - private List<?> parseListFromElement(final Element root) throws ArchetypeTypeParseException, XPathExpressionException { - // XXX The list created here contains altering types: Integer for even, String for odd indices. - final List<Object> list = new ArrayList<Object>(); - for (final Element elem : new NodeListIterator<Element>(xpath, root, "listentry")) { - // every list entry adds value (Integer) and name (String) to the vector - try { - list.add(Integer.valueOf(elem.getAttribute("value"))); - list.add(" " + elem.getAttribute("name").trim()); - } catch (final NumberFormatException ignore) { - throw new ArchetypeTypeParseException("In '" + CommonConstants.TYPEDEF_FILE + "', list " + root.getAttribute("name") + ": value '" + elem.getAttribute("value") + "' is not an integer."); - } - } - return list; - } - - private void parseIgnoreLists(final Element root, @NotNull final Map<String, List<String>> ignoreListTable) throws ArchetypeTypeParseException, XPathExpressionException { - for (final Element elem : new NodeListIterator<Element>(xpath, root, "ignore_list|ignorelists/ignore_list")) { - if (elem.getAttribute("name") == null) { - throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': cannot load ignore_list element without 'name'."); - } else { - final String lname = elem.getAttribute("name").trim(); - final List<String> content = new ArrayList<String>(); - for (final Element el2 : new NodeListIterator<Element>(elem, ArchetypeTypeParser.XML_ATTRIBUTE)) { - final Attr a = el2.getAttributeNode(ArchetypeAttributeParser.XML_KEY_ARCH); - if (a != null) { - content.add(a.getValue().trim()); - } else { - throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': ignore_list '" + lname + "' has " + ArchetypeTypeParser.XML_ATTRIBUTE + " missing '" + ArchetypeAttributeParser.XML_KEY_ARCH + "'."); - } - } - // now add the list vector to the ignoreListTable: - if (!content.isEmpty()) { - ignoreListTable.put(lname, content); - } - } - } - } - - private ArchetypeType parseDefaultType(final Element root, @NotNull final Map<String, List<String>> ignoreListTable) throws ArchetypeTypeParseException { - // parse default type - final Element el = NodeListIterator.getFirstChild(root, "default_type"); - if (el == null) { - throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': default_type element is missing!"); - } - - return ArchetypeTypeParser.load(null, el, this, ignoreListTable); - } - - private void parseTypes(final Element root, @NotNull final Map<String, List<String>> ignoreListTable) throws ArchetypeTypeParseException { - // parse all type elements - for (final Element elem : new NodeListIterator<Element>(root, "type")) { - if (!"no".equals(elem.getAttribute("available"))) { - final ArchetypeType newType = ArchetypeTypeParser.load(defaultArchetypeType, elem, this, ignoreListTable); - if (newType != null) { - // attach the new ArchetypeType element to the list - archetypeTypeList.add(newType); - archetypeTypeNames.put(newType.getTypeName(), newType); - } - } - } - } - - /** * Returns the list table which contains all definitions of list types for * arch attributes. * @return list table Added: trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSetParser.java =================================================================== --- trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSetParser.java (rev 0) +++ trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSetParser.java 2008-07-31 10:00:10 UTC (rev 4603) @@ -0,0 +1,200 @@ +/* + * 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.archtype; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import net.sf.gridarta.CommonConstants; +import net.sf.japi.xml.NodeListIterator; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +/** + * Parser for {@link ArchetypeTypeSet}s. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @author Andreas Kirschbaum + */ +public class ArchetypeTypeSetParser { + + /** The Logger for printing log messages. */ + private static final Logger log = Logger.getLogger(ArchetypeTypeSetParser.class); + + /** + * The document builder to use. + */ + @NotNull + private final DocumentBuilder documentBuilder; + + /** + * The XPath for using XPath. + */ + @NotNull + private final XPath xpath; + + /** + * The archetype type set to update. + */ + @NotNull + private final ArchetypeTypeSet archetypeTypeSet; + + /** + * Creates a new instance. + * @param documentBuilder the document builder to use + * @param xpath the XPath instance to use for applying for XPath + * expressions + * @param archetypeTypeSet the archetype type set to update + */ + public ArchetypeTypeSetParser(@NotNull final DocumentBuilder documentBuilder, @NotNull final XPath xpath, @NotNull final ArchetypeTypeSet archetypeTypeSet) { + this.documentBuilder = documentBuilder; + this.xpath = xpath; + this.archetypeTypeSet = archetypeTypeSet; + } + + public void loadTypesFromXML(@NotNull final String filename) { + final Map<String, List<String>> ignoreListTable = new HashMap<String, List<String>>(); + try { + // parse xml document + final Document doc = documentBuilder.parse(filename); + + // start parsing the xml + final Element root = doc.getDocumentElement(); + parseBitmasks(root); + parseLists(root); + parseIgnoreLists(root, ignoreListTable); + archetypeTypeSet.defaultArchetypeType = parseDefaultType(root, ignoreListTable); + parseTypes(root, ignoreListTable); + + if (log.isInfoEnabled()) { + log.info("Loaded " + archetypeTypeSet.archetypeTypeList.size() + " types from '" + filename + "\'"); + } + } catch (final SAXException e) { + log.error("Parsing error in '" + filename + "':\n" + e.getMessage()); + } catch (final IOException e) { + log.error("Cannot read file '" + filename + "'!"); + } catch (final XPathExpressionException e) { + log.error("XPath error: " + e.getMessage()); + } + + archetypeTypeSet.fallbackArchetypeType = archetypeTypeSet.archetypeTypeNames.get("Misc"); + if (archetypeTypeSet.fallbackArchetypeType == null) { + archetypeTypeSet.fallbackArchetypeType = new ArchetypeType(archetypeTypeSet.defaultArchetypeType); + } + } + + private void parseBitmasks(final Element root) throws XPathExpressionException { + for (final Element elem : new NodeListIterator<Element>(xpath, root, "bitmasks/bitmask")) { + archetypeTypeSet.bitmaskTable.put(elem.getAttribute("name"), new CAttribBitmask(xpath, elem)); + } + } + + private void parseLists(final Element root) throws ArchetypeTypeParseException, XPathExpressionException { + for (final Element elem : new NodeListIterator<Element>(xpath, root, "lists/list")) { + if (elem.getAttribute("name") == null) { + throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': cannot load list element without 'name'."); + } else { + final List<?> list = parseListFromElement(elem); + if (list != null && !list.isEmpty()) { + archetypeTypeSet.listTable.put(elem.getAttribute("name"), list); + } + } + } + } + + /** + * Parse a list vector from an xml list element. + * @param root element to parse + * @return List with data parsed from <var>root</var> + * @throws XPathExpressionException In case of XPath issues (should never + * happen). + * @todo improve this comment + */ + private List<?> parseListFromElement(final Element root) throws ArchetypeTypeParseException, XPathExpressionException { + // XXX The list created here contains altering types: Integer for even, String for odd indices. + final List<Object> list = new ArrayList<Object>(); + for (final Element elem : new NodeListIterator<Element>(xpath, root, "listentry")) { + // every list entry adds value (Integer) and name (String) to the vector + try { + list.add(Integer.valueOf(elem.getAttribute("value"))); + list.add(" " + elem.getAttribute("name").trim()); + } catch (final NumberFormatException ignore) { + throw new ArchetypeTypeParseException("In '" + CommonConstants.TYPEDEF_FILE + "', list " + root.getAttribute("name") + ": value '" + elem.getAttribute("value") + "' is not an integer."); + } + } + return list; + } + + private void parseIgnoreLists(final Element root, @NotNull final Map<String, List<String>> ignoreListTable) throws ArchetypeTypeParseException, XPathExpressionException { + for (final Element elem : new NodeListIterator<Element>(xpath, root, "ignore_list|ignorelists/ignore_list")) { + if (elem.getAttribute("name") == null) { + throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': cannot load ignore_list element without 'name'."); + } else { + final String lname = elem.getAttribute("name").trim(); + final List<String> content = new ArrayList<String>(); + for (final Element el2 : new NodeListIterator<Element>(elem, ArchetypeTypeParser.XML_ATTRIBUTE)) { + final Attr a = el2.getAttributeNode(ArchetypeAttributeParser.XML_KEY_ARCH); + if (a != null) { + content.add(a.getValue().trim()); + } else { + throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': ignore_list '" + lname + "' has " + ArchetypeTypeParser.XML_ATTRIBUTE + " missing '" + ArchetypeAttributeParser.XML_KEY_ARCH + "'."); + } + } + // now add the list vector to the ignoreListTable: + if (!content.isEmpty()) { + ignoreListTable.put(lname, content); + } + } + } + } + + private ArchetypeType parseDefaultType(final Element root, @NotNull final Map<String, List<String>> ignoreListTable) throws ArchetypeTypeParseException { + // parse default type + final Element el = NodeListIterator.getFirstChild(root, "default_type"); + if (el == null) { + throw new ArchetypeTypeParseException("In file '" + CommonConstants.TYPEDEF_FILE + "': default_type element is missing!"); + } + + return ArchetypeTypeParser.load(null, el, archetypeTypeSet, ignoreListTable); + } + + private void parseTypes(final Element root, @NotNull final Map<String, List<String>> ignoreListTable) throws ArchetypeTypeParseException { + // parse all type elements + for (final Element elem : new NodeListIterator<Element>(root, "type")) { + if (!"no".equals(elem.getAttribute("available"))) { + final ArchetypeType newType = ArchetypeTypeParser.load(archetypeTypeSet.defaultArchetypeType, elem, archetypeTypeSet, ignoreListTable); + if (newType != null) { + // attach the new ArchetypeType element to the list + archetypeTypeSet.archetypeTypeList.add(newType); + archetypeTypeSet.archetypeTypeNames.put(newType.getTypeName(), newType); + } + } + } + } + +} // class ArchetypeTypeSetParser Property changes on: trunk/src/app/net/sf/gridarta/archtype/ArchetypeTypeSetParser.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. |