From: <aki...@us...> - 2009-12-09 20:59:43
|
Revision: 7371 http://gridarta.svn.sourceforge.net/gridarta/?rev=7371&view=rev Author: akirschbaum Date: 2009-12-09 20:59:35 +0000 (Wed, 09 Dec 2009) Log Message: ----------- Fix initialization errors introduced in r7369. Modified Paths: -------------- trunk/src/app/net/sf/gridarta/maincontrol/DefaultMainControl.java trunk/src/app/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java trunk/src/app/net/sf/gridarta/model/match/GameObjectMatchersParser.java trunk/src/app/net/sf/gridarta/model/spells/XMLSpellLoader.java trunk/src/test/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java Modified: trunk/src/app/net/sf/gridarta/maincontrol/DefaultMainControl.java =================================================================== --- trunk/src/app/net/sf/gridarta/maincontrol/DefaultMainControl.java 2009-12-09 19:54:29 UTC (rev 7370) +++ trunk/src/app/net/sf/gridarta/maincontrol/DefaultMainControl.java 2009-12-09 20:59:35 UTC (rev 7371) @@ -387,16 +387,7 @@ try { final URL url = IOUtils.getResource(globalSettings.getConfigurationDirectory(), CommonConstants.TYPEDEF_FILE); final ErrorViewCollector typesErrorViewCollector = new ErrorViewCollector(errorView, url); - try { - final InputStream inputStream = url.openStream(); - try { - archetypeTypeSetParser.loadTypesFromXML(typesErrorViewCollector, inputStream); - } finally { - inputStream.close(); - } - } catch (final IOException ex) { - typesErrorViewCollector.addWarning(ErrorViewCategory.TYPES_FILE_INVALID, ex.getMessage()); - } + archetypeTypeSetParser.loadTypesFromXML(typesErrorViewCollector, new InputSource(url.toString())); final ArchetypeTypeList eventTypeSetTmp = archetypeTypeSet.getList("event"); if (eventTypeSetTmp == null) { typesErrorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "list 'list_event' does not exist"); Modified: trunk/src/app/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java =================================================================== --- trunk/src/app/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java 2009-12-09 19:54:29 UTC (rev 7370) +++ trunk/src/app/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java 2009-12-09 20:59:35 UTC (rev 7371) @@ -20,7 +20,6 @@ package net.sf.gridarta.model.archetypetype; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -43,6 +42,7 @@ import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -94,12 +94,13 @@ this.archetypeTypeParser = archetypeTypeParser; } - public void loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final InputStream inputStream) { + public void loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final InputSource inputSource) { // parse xml document final Document doc; try { - doc = documentBuilder.parse(inputStream); + doc = documentBuilder.parse(inputSource); } catch (final SAXException e) { + e.printStackTrace(); errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "parsing error: " + e.getMessage()); return; } catch (final IOException e) { Modified: trunk/src/app/net/sf/gridarta/model/match/GameObjectMatchersParser.java =================================================================== --- trunk/src/app/net/sf/gridarta/model/match/GameObjectMatchersParser.java 2009-12-09 19:54:29 UTC (rev 7370) +++ trunk/src/app/net/sf/gridarta/model/match/GameObjectMatchersParser.java 2009-12-09 20:59:35 UTC (rev 7371) @@ -20,7 +20,6 @@ package net.sf.gridarta.model.match; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import java.util.Collection; import java.util.Iterator; @@ -35,6 +34,7 @@ import org.jetbrains.annotations.NotNull; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -77,40 +77,35 @@ */ public void readGameObjectMatchers(@NotNull final URL url, @NotNull final Collection<NamedGameObjectMatcher> gameObjectMatchers, @NotNull final Map<String, NamedGameObjectMatcher> gameObjectMatchersByIds, @NotNull final ErrorViewCollector errorViewCollector) throws IOException { final GameObjectMatcherParser aom = new GameObjectMatcherParser(xpath); - final InputStream inputStream = url.openStream(); + final Document doc; try { - final Document doc; + doc = documentBuilder.parse(new InputSource(url.toString())); + } catch (final SAXException ex) { + throw new IOException("sax exception: " + ex.getMessage(), ex); + } + int editType = 1; + final Iterator<Element> it = new NodeListIterator<Element>(doc.getElementsByTagName("GameObjectMatcher")); + while (it.hasNext()) { + final Element node = it.next(); + final NamedGameObjectMatcher archObjectMatcher; try { - doc = documentBuilder.parse(inputStream); - } catch (final SAXException ex) { - throw new IOException("sax exception: " + ex.getMessage(), ex); + archObjectMatcher = aom.parseMatcher(node, editType); + } catch (final ParsingException ex) { + errorViewCollector.addWarning(ErrorViewCategory.GAMEOBJECTMATCHERS_ENTRY_INVALID, node.getAttribute("id") + ": " + ex.getMessage()); + continue; } - int editType = 1; - final Iterator<Element> it = new NodeListIterator<Element>(doc.getElementsByTagName("GameObjectMatcher")); - while (it.hasNext()) { - final Element node = it.next(); - final NamedGameObjectMatcher archObjectMatcher; - try { - archObjectMatcher = aom.parseMatcher(node, editType); - } catch (final ParsingException ex) { - errorViewCollector.addWarning(ErrorViewCategory.GAMEOBJECTMATCHERS_ENTRY_INVALID, node.getAttribute("id") + ": " + ex.getMessage()); - continue; + gameObjectMatchers.add(archObjectMatcher); + gameObjectMatchersByIds.put(archObjectMatcher.getID(), archObjectMatcher); + if (editType != 0 && !archObjectMatcher.isSystemMatcher()) { + editType <<= 1; + if (editType == 0) { + errorViewCollector.addWarning(ErrorViewCategory.GAMEOBJECTMATCHERS_ENTRY_INVALID, "too many GameObjectMatchers, ignoring rest"); } - gameObjectMatchers.add(archObjectMatcher); - gameObjectMatchersByIds.put(archObjectMatcher.getID(), archObjectMatcher); - if (editType != 0 && !archObjectMatcher.isSystemMatcher()) { - editType <<= 1; - if (editType == 0) { - errorViewCollector.addWarning(ErrorViewCategory.GAMEOBJECTMATCHERS_ENTRY_INVALID, "too many GameObjectMatchers, ignoring rest"); - } - } } - if (log.isInfoEnabled()) { - log.info("Loaded " + gameObjectMatchers.size() + " GameObjectMatchers from '" + url + "'."); - } - } finally { - inputStream.close(); } + if (log.isInfoEnabled()) { + log.info("Loaded " + gameObjectMatchers.size() + " GameObjectMatchers from '" + url + "'."); + } } } // class GameObjectMatchersParser Modified: trunk/src/app/net/sf/gridarta/model/spells/XMLSpellLoader.java =================================================================== --- trunk/src/app/net/sf/gridarta/model/spells/XMLSpellLoader.java 2009-12-09 19:54:29 UTC (rev 7370) +++ trunk/src/app/net/sf/gridarta/model/spells/XMLSpellLoader.java 2009-12-09 20:59:35 UTC (rev 7371) @@ -22,7 +22,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import java.util.Iterator; import javax.xml.parsers.DocumentBuilder; @@ -36,6 +35,7 @@ import org.jetbrains.annotations.NotNull; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -66,44 +66,38 @@ final URL url = IOUtils.getResource(baseDir, filename); final ErrorViewCollector errorViewCollector = new ErrorViewCollector(errorView, url); try { - // parse xml document - final InputStream inputStream = url.openStream(); - try { - final Document doc = documentBuilder.parse(inputStream); + final Document doc = documentBuilder.parse(new InputSource(url.toString())); - // retrieve the spell data from the xml - final Element root = doc.getDocumentElement(); - if (root == null || !"spells".equalsIgnoreCase(root.getNodeName())) { - errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "root element 'spells' is missing"); - } else { - // initialize array with appropriate size - int numSpells = 0; - final Iterator<Element> it = new NodeListIterator<Element>(root, "spell"); - while (it.hasNext()) { - final Element spellElem = it.next(); - if (spellElem.getAttribute("id") == null) { - errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "found 'spell' element without 'id'"); - } else if (spellElem.getAttribute("name") == null) { - errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "found 'spell' element without 'name'"); - } else { - try { - // parse spell number and -name - spells.add(new NumberSpell(spellElem.getAttribute("name").trim(), Integer.parseInt(spellElem.getAttribute("id")))); - numSpells++; - } catch (final NumberFormatException de) { - errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "parsing error: spell id '" + spellElem.getAttribute("id") + "' is not an integer."); - } + // retrieve the spell data from the xml + final Element root = doc.getDocumentElement(); + if (root == null || !"spells".equalsIgnoreCase(root.getNodeName())) { + errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "root element 'spells' is missing"); + } else { + // initialize array with appropriate size + int numSpells = 0; + final Iterator<Element> it = new NodeListIterator<Element>(root, "spell"); + while (it.hasNext()) { + final Element spellElem = it.next(); + if (spellElem.getAttribute("id") == null) { + errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "found 'spell' element without 'id'"); + } else if (spellElem.getAttribute("name") == null) { + errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "found 'spell' element without 'name'"); + } else { + try { + // parse spell number and -name + spells.add(new NumberSpell(spellElem.getAttribute("name").trim(), Integer.parseInt(spellElem.getAttribute("id")))); + numSpells++; + } catch (final NumberFormatException de) { + errorViewCollector.addWarning(ErrorViewCategory.SPELLS_ENTRY_INVALID, "parsing error: spell id '" + spellElem.getAttribute("id") + "' is not an integer."); } } + } - // loading successful - log.info("Loaded " + numSpells + " defined spells from '" + url + "'"); - if (numSpells == 0) { - errorViewCollector.addWarning(ErrorViewCategory.SPELLS_FILE_INVALID, "no content"); - } + // loading successful + log.info("Loaded " + numSpells + " defined spells from '" + url + "'"); + if (numSpells == 0) { + errorViewCollector.addWarning(ErrorViewCategory.SPELLS_FILE_INVALID, "no content"); } - } finally { - inputStream.close(); } } catch (final SAXException ex) { errorViewCollector.addWarning(ErrorViewCategory.SPELLS_FILE_INVALID, "parsing error:" + ex.getMessage()); Modified: trunk/src/test/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java =================================================================== --- trunk/src/test/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java 2009-12-09 19:54:29 UTC (rev 7370) +++ trunk/src/test/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java 2009-12-09 20:59:35 UTC (rev 7371) @@ -35,6 +35,7 @@ import org.jetbrains.annotations.Nullable; import org.junit.Assert; import org.junit.Test; +import org.xml.sax.InputSource; /** * Regression tests for {@link ArchetypeTypeSetParser}. @@ -376,7 +377,7 @@ private void check(final String typesXml, final boolean expectedHasErrors, final String expectedResult) throws ParserConfigurationException, UnsupportedEncodingException { final ArchetypeTypeSet<TestGameObject, TestMapArchObject, TestArchetype> archetypeTypeSet = new ArchetypeTypeSet<TestGameObject, TestMapArchObject, TestArchetype>(); final ArchetypeTypeSetParser<TestGameObject, TestMapArchObject, TestArchetype> parser = createArchetypeTypeSetParser(archetypeTypeSet); - parser.loadTypesFromXML(errorViewCollector, createInputStream(typesXml)); + parser.loadTypesFromXML(errorViewCollector, createInputSource(typesXml)); Assert.assertEquals(expectedHasErrors, errorView.hasErrors()); Assert.assertEquals(expectedResult, archetypeTypeSet.toString()); } @@ -403,14 +404,14 @@ * created */ @NotNull - private static InputStream createInputStream(@NotNull final String string) throws UnsupportedEncodingException { + private static InputSource createInputSource(@NotNull final String string) throws UnsupportedEncodingException { final String typesString = "<?xml version=\"1.0\" standalone=\"no\" ?>\n" + "<!DOCTYPE types SYSTEM \"types.dtd\">\n" + "<types>\n" + string + "</types>\n"; - return new ByteArrayInputStream(typesString.getBytes("UTF-8")); + return new InputSource(new ByteArrayInputStream(typesString.getBytes("UTF-8"))); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |