From: <aki...@us...> - 2013-10-09 21:49:09
|
Revision: 9427 http://sourceforge.net/p/gridarta/code/9427 Author: akirschbaum Date: 2013-10-09 21:49:04 +0000 (Wed, 09 Oct 2013) Log Message: ----------- Simplify ArchetypeTypeSet loading code. Modified Paths: -------------- trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/DefaultMainControl.java trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/GridartaEditor.java trunk/src/model/src/main/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java trunk/src/model/src/test/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java Modified: trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/DefaultMainControl.java =================================================================== --- trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/DefaultMainControl.java 2013-10-09 21:30:39 UTC (rev 9426) +++ trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/DefaultMainControl.java 2013-10-09 21:49:04 UTC (rev 9427) @@ -157,7 +157,8 @@ return gameObjectMatchers; } - public static void loadArchetypeTypeSet(@NotNull final ErrorView errorView, @NotNull final ProjectSettings projectSettings, @NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final XmlHelper xmlHelper) { + @NotNull + public static ArchetypeTypeSet loadArchetypeTypeSet(@NotNull final ErrorView errorView, @NotNull final ProjectSettings projectSettings, @NotNull final XmlHelper xmlHelper) { final ArchetypeAttributeFactory archetypeAttributeFactory = new DefaultArchetypeAttributeFactory(); final ArchetypeAttributeParser archetypeAttributeParser = new ArchetypeAttributeParser(archetypeAttributeFactory); final ArchetypeTypeParser archetypeTypeParser = new ArchetypeTypeParser(archetypeAttributeParser); @@ -166,14 +167,15 @@ final ErrorViewCollector typesErrorViewCollector = new ErrorViewCollector(errorView, url); xmlHelper.getDocumentBuilder().setErrorHandler(new ErrorViewCollectorErrorHandler(typesErrorViewCollector, ErrorViewCategory.GAMEOBJECTMATCHERS_FILE_INVALID)); try { - final ArchetypeTypeSetParser archetypeTypeSetParser = new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), archetypeTypeSet, archetypeTypeParser); - archetypeTypeSetParser.loadTypesFromXML(typesErrorViewCollector, new InputSource(url.toString())); + final ArchetypeTypeSetParser archetypeTypeSetParser = new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), archetypeTypeParser); + return archetypeTypeSetParser.loadTypesFromXML(typesErrorViewCollector, new InputSource(url.toString())); } finally { xmlHelper.getDocumentBuilder().setErrorHandler(null); } } catch (final FileNotFoundException ex) { errorView.addWarning(ErrorViewCategory.TYPES_FILE_INVALID, CommonConstants.TYPEDEF_FILE + ": " + ex.getMessage()); } + return new ArchetypeTypeSet(); } @NotNull Modified: trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/GridartaEditor.java =================================================================== --- trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/GridartaEditor.java 2013-10-09 21:30:39 UTC (rev 9426) +++ trunk/src/gridarta/src/main/java/net/sf/gridarta/maincontrol/GridartaEditor.java 2013-10-09 21:49:04 UTC (rev 9427) @@ -259,9 +259,7 @@ editorFactory.initSmoothFaces(faceObjects); final int doubleFaceOffset = editorFactory.getDoubleFaceOffset(); final FaceObjectProviders faceObjectProviders = new FaceObjectProviders(doubleFaceOffset, faceObjects, systemIcons); - final ArchetypeTypeSet archetypeTypeSet = new ArchetypeTypeSet(); final AnimationObjects animationObjects = new DefaultAnimationObjects(); - final GameObjectFactory<G, A, R> gameObjectFactory = editorFactory.newGameObjectFactory(faceObjectProviders, animationObjects, archetypeTypeSet); final ArchetypeFactory<G, A, R> archetypeFactory = editorFactory.newArchetypeFactory(faceObjectProviders, animationObjects); final ArchFaceProvider archFaceProvider = new ArchFaceProvider(); final ArchetypeChooserModel<G, A, R> archetypeChooserModel = new ArchetypeChooserModel<G, A, R>(); @@ -279,6 +277,8 @@ } final ProjectSettings projectSettings = editorFactory.newProjectSettings(editorSettings); + final ArchetypeTypeSet archetypeTypeSet = DefaultMainControl.loadArchetypeTypeSet(errorView, projectSettings, xmlHelper); + final GameObjectFactory<G, A, R> gameObjectFactory = editorFactory.newGameObjectFactory(faceObjectProviders, animationObjects, archetypeTypeSet); final PathManager pathManager = new PathManager(projectSettings); final ConfigSource configSource = mode == GridartaRunMode.COLLECT_ARCHES ? configSourceFactory.getFilesConfigSource() : configSourceFactory.getConfigSource(projectSettings.getConfigSourceName()); @@ -303,7 +303,6 @@ final ScriptedEventEditor<G, A, R> scriptedEventEditor = new ScriptedEventEditor<G, A, R>(projectSettings); final GameObjectMatchers gameObjectMatchers = DefaultMainControl.loadGameObjectMatchers(xmlHelper, errorView, projectSettings); - DefaultMainControl.loadArchetypeTypeSet(errorView, projectSettings, archetypeTypeSet, xmlHelper); final DelegatingMapValidator<G, A, R> validators = DefaultMainControl.loadValidators(errorView, editorFactory, projectSettings, gameObjectMatchers, archetypeTypeSet, validatorPreferences, mapWriter); final Map<String, TreasureTreeNode> specialTreasureLists = DefaultMainControl.loadSpecialTreasureLists(errorView, xmlHelper.getDocumentBuilder(), projectSettings); final TreasureTree treasureTree = TreasureLoader.parseTreasures(errorView, specialTreasureLists, configSource, projectSettings); Modified: trunk/src/model/src/main/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java =================================================================== --- trunk/src/model/src/main/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java 2013-10-09 21:30:39 UTC (rev 9426) +++ trunk/src/model/src/main/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParser.java 2013-10-09 21:49:04 UTC (rev 9427) @@ -59,12 +59,6 @@ private final DocumentBuilder documentBuilder; /** - * The {@link ArchetypeTypeSet} to update. - */ - @NotNull - private final ArchetypeTypeSet archetypeTypeSet; - - /** * The {@link ArchetypeTypeParser} }to use. */ @NotNull @@ -73,12 +67,10 @@ /** * Creates a new instance. * @param documentBuilder the document builder to use - * @param archetypeTypeSet the archetype type set to update * @param archetypeTypeParser the archetype type parser to use */ - public ArchetypeTypeSetParser(@NotNull final DocumentBuilder documentBuilder, @NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final ArchetypeTypeParser archetypeTypeParser) { + public ArchetypeTypeSetParser(@NotNull final DocumentBuilder documentBuilder, @NotNull final ArchetypeTypeParser archetypeTypeParser) { this.documentBuilder = documentBuilder; - this.archetypeTypeSet = archetypeTypeSet; this.archetypeTypeParser = archetypeTypeParser; } @@ -87,19 +79,21 @@ * @param errorViewCollector the error view collector for reporting errors * @param inputSource the input source providing the contents of the * types.xml file + * @return the loaded archetype types */ - public void loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final InputSource inputSource) { + @NotNull + public ArchetypeTypeSet loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final InputSource inputSource) { final Document doc; try { doc = documentBuilder.parse(inputSource); } catch (final SAXException e) { errorViewCollector.addWarning(ErrorViewCategory.TYPES_ENTRY_INVALID, "parsing error: " + e.getMessage()); - return; + return new ArchetypeTypeSet(); } catch (final IOException e) { errorViewCollector.addWarning(ErrorViewCategory.TYPES_FILE_INVALID, e.getMessage()); - return; + return new ArchetypeTypeSet(); } - loadTypesFromXML(errorViewCollector, inputSource.getSystemId(), doc); + return loadTypesFromXML(errorViewCollector, inputSource.getSystemId(), doc); } /** @@ -108,32 +102,37 @@ * @param filename the filename of the types.xml file for error messages * @param document the document containing the contents of the types.xml * file + * @return the loaded archetype types */ - public void loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final String filename, @NotNull final Document document) { + @NotNull + public ArchetypeTypeSet loadTypesFromXML(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final String filename, @NotNull final Document document) { + final ArchetypeTypeSet archetypeTypeSet = new ArchetypeTypeSet(); final Element rootElement = document.getDocumentElement(); - parseBitmasks(XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_BITMASKS), errorViewCollector); - parseLists(XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_LISTS), errorViewCollector); + parseBitmasks(archetypeTypeSet, XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_BITMASKS), errorViewCollector); + parseLists(archetypeTypeSet, XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_LISTS), errorViewCollector); final IgnorelistsDefinition ignorelistsDefinition = parseIgnoreLists(XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_IGNORELISTS), errorViewCollector); - parseAttributeOrder(NodeListIterator.getFirstChild(rootElement, Constants.XML_ELEMENT_ATTRIBUTE_ORDER), errorViewCollector); - final ArchetypeType defaultArchetypeType = parseDefaultType(XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_DEFAULT_TYPE), errorViewCollector, ignorelistsDefinition); + parseAttributeOrder(archetypeTypeSet, NodeListIterator.getFirstChild(rootElement, Constants.XML_ELEMENT_ATTRIBUTE_ORDER), errorViewCollector); + final ArchetypeType defaultArchetypeType = parseDefaultType(archetypeTypeSet, XmlUtils.getChild(rootElement, Constants.XML_ELEMENT_DEFAULT_TYPE), errorViewCollector, ignorelistsDefinition); final Iterator<Element> typeElements = new NodeListIterator<Element>(rootElement, Constants.XML_ELEMENT_TYPE); while (typeElements.hasNext()) { - parseTypes(typeElements.next(), errorViewCollector, defaultArchetypeType, ignorelistsDefinition); + parseTypes(archetypeTypeSet, typeElements.next(), errorViewCollector, defaultArchetypeType, ignorelistsDefinition); } if (log.isInfoEnabled()) { log.info("Loaded " + archetypeTypeSet.getArchetypeTypeCount() + " types from '" + filename + "\'"); } + return archetypeTypeSet; } /** * Parses the {@link Constants#XML_ELEMENT_BITMASKS} section of a types.xml * file. + * @param archetypeTypeSet the archetype types being loaded * @param bitmasksElement the element to parse * @param errorViewCollector the error view collector for reporting errors */ - private void parseBitmasks(@NotNull final Element bitmasksElement, @NotNull final ErrorViewCollector errorViewCollector) { + private static void parseBitmasks(@NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final Element bitmasksElement, @NotNull final ErrorViewCollector errorViewCollector) { final Iterator<Element> bitmaskIterator = new NodeListIterator<Element>(bitmasksElement, Constants.XML_ELEMENT_BITMASK); while (bitmaskIterator.hasNext()) { final Element bitmaskElement = bitmaskIterator.next(); @@ -254,23 +253,25 @@ /** * Parses the {@link Constants#XML_ELEMENT_LISTS} section of a types.xml * file. + * @param archetypeTypeSet the archetype types being loaded * @param listsElement the element to parse * @param errorViewCollector the error view collector for reporting errors */ - private void parseLists(@NotNull final Element listsElement, @NotNull final ErrorViewCollector errorViewCollector) { + private static void parseLists(@NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final Element listsElement, @NotNull final ErrorViewCollector errorViewCollector) { final Iterator<Element> listIterator = new NodeListIterator<Element>(listsElement, Constants.XML_ELEMENT_LIST); while (listIterator.hasNext()) { - parseList(listIterator.next(), errorViewCollector); + parseList(archetypeTypeSet, listIterator.next(), errorViewCollector); } } /** * Parses an {@link Constants#XML_ELEMENT_LIST} section of a types.xml file - * and adds it to {@link #archetypeTypeSet}. + * and adds it to {@code archetypeTypeSet}. + * @param archetypeTypeSet the archetype types being loaded * @param listElement the element to parse * @param errorViewCollector the error view collector for reporting errors */ - private void parseList(@NotNull final Element listElement, @NotNull final ErrorViewCollector errorViewCollector) { + private static void parseList(@NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final Element listElement, @NotNull final ErrorViewCollector errorViewCollector) { final ArchetypeTypeList list = new ArchetypeTypeList(); final Iterator<Element> listentryIterator = new NodeListIterator<Element>(listElement, Constants.XML_ELEMENT_LISTENTRY); while (listentryIterator.hasNext()) { @@ -324,10 +325,11 @@ /** * Parses the {@link Constants#XML_ELEMENT_ATTRIBUTE_ORDER} section of a * types.xml file. + * @param archetypeTypeSet the archetype types being loaded * @param attributeOrderElement the element to parse or {@code null} * @param errorViewCollector the error view collector for reporting errors */ - private void parseAttributeOrder(@Nullable final Node attributeOrderElement, @NotNull final ErrorViewCollector errorViewCollector) { + private static void parseAttributeOrder(@NotNull final ArchetypeTypeSet archetypeTypeSet, @Nullable final Node attributeOrderElement, @NotNull final ErrorViewCollector errorViewCollector) { if (attributeOrderElement != null) { for (final String value : StringUtils.PATTERN_WHITESPACE_NEWLINE.split(attributeOrderElement.getTextContent())) { if (!value.isEmpty()) { @@ -377,26 +379,28 @@ /** * Parses the {@link Constants#XML_ELEMENT_DEFAULT_TYPE} element of a * types.xml file. + * @param archetypeTypeSet the archetype types being loaded * @param defaultTypeElement the element to parse * @param errorViewCollector the error view collector for reporting errors * @param ignorelistsDefinition the contents of the "ignorelists" section * @return the default type */ @NotNull - private ArchetypeType parseDefaultType(@NotNull final Element defaultTypeElement, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final IgnorelistsDefinition ignorelistsDefinition) { + private ArchetypeType parseDefaultType(@NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final Element defaultTypeElement, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final IgnorelistsDefinition ignorelistsDefinition) { return archetypeTypeParser.loadAttributeList(defaultTypeElement, errorViewCollector, null, archetypeTypeSet, ignorelistsDefinition, true); } /** * Parses an {@link Constants#XML_ELEMENT_TYPE} element of a types.xml * file. + * @param archetypeTypeSet the archetype types being loaded * @param typeElement the type element to parse * @param errorViewCollector the error view collector for reporting errors * @param defaultArchetypeType the default archetype type to copy attributes * from * @param ignorelistsDefinition the contents of the "ignorelists" section */ - private void parseTypes(@NotNull final Element typeElement, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final ArchetypeType defaultArchetypeType, @NotNull final IgnorelistsDefinition ignorelistsDefinition) { + private void parseTypes(@NotNull final ArchetypeTypeSet archetypeTypeSet, @NotNull final Element typeElement, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final ArchetypeType defaultArchetypeType, @NotNull final IgnorelistsDefinition ignorelistsDefinition) { if (!typeElement.getAttribute(Constants.XML_TYPE_AVAILABLE).equals("no")) { archetypeTypeSet.addArchetypeType(archetypeTypeParser.loadAttributeList(typeElement, errorViewCollector, defaultArchetypeType, archetypeTypeSet, ignorelistsDefinition, false)); } Modified: trunk/src/model/src/test/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java =================================================================== --- trunk/src/model/src/test/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java 2013-10-09 21:30:39 UTC (rev 9426) +++ trunk/src/model/src/test/java/net/sf/gridarta/model/archetypetype/ArchetypeTypeSetParserTest.java 2013-10-09 21:49:04 UTC (rev 9427) @@ -231,8 +231,7 @@ */ @NotNull private ArchetypeTypeSet loadArchetypeTypeSet(@NotNull final String name) throws ParserConfigurationException { - final ArchetypeTypeSet archetypeTypeSet = new ArchetypeTypeSet(); - final ArchetypeTypeSetParser parser = createArchetypeTypeSetParser(archetypeTypeSet); + final ArchetypeTypeSetParser parser = createArchetypeTypeSetParser(); final InputStream typesXml = getClass().getResourceAsStream("ArchetypeTypeSetParserTest-" + name + "-types.xml"); System.out.println("ArchetypeTypeSetParserTest-" + name + "-types.xml"); @@ -240,23 +239,21 @@ final InputSource inputSource = new InputSource(typesXml); inputSource.setSystemId("types.dtd"); - parser.loadTypesFromXML(errorViewCollector, inputSource); - return archetypeTypeSet; + return parser.loadTypesFromXML(errorViewCollector, inputSource); } /** * Creates a new {@link ArchetypeTypeSetParser} instance. - * @param archetypeTypeSet the archetype type set to modify * @return the archetype type set parser instance * @throws ParserConfigurationException if the parser cannot be created */ @NotNull - private static ArchetypeTypeSetParser createArchetypeTypeSetParser(@NotNull final ArchetypeTypeSet archetypeTypeSet) throws ParserConfigurationException { + private static ArchetypeTypeSetParser createArchetypeTypeSetParser() throws ParserConfigurationException { final XmlHelper xmlHelper = new XmlHelper(); final ArchetypeAttributeFactory archetypeAttributeFactory = new TestArchetypeAttributeFactory(); final ArchetypeAttributeParser archetypeAttributeParser = new ArchetypeAttributeParser(archetypeAttributeFactory); final ArchetypeTypeParser archetypeTypeParser = new ArchetypeTypeParser(archetypeAttributeParser); - return new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), archetypeTypeSet, archetypeTypeParser); + return new ArchetypeTypeSetParser(xmlHelper.getDocumentBuilder(), archetypeTypeParser); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |