001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.model.artifact;
021
022import java.io.BufferedReader;
023import java.io.File;
024import java.io.IOException;
025import java.io.Reader;
026import java.io.StringReader;
027import java.util.ArrayList;
028import java.util.List;
029import net.sf.gridarta.model.anim.AnimationObjects;
030import net.sf.gridarta.model.anim.TestAnimationObjects;
031import net.sf.gridarta.model.archetype.Archetype;
032import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
033import net.sf.gridarta.model.archetype.TestArchetype;
034import net.sf.gridarta.model.archetype.TestArchetypeBuilder;
035import net.sf.gridarta.model.archetype.TestArchetypeFactory;
036import net.sf.gridarta.model.archetype.TestDefaultArchetype;
037import net.sf.gridarta.model.archetype.UndefinedArchetypeException;
038import net.sf.gridarta.model.archetypeset.ArchetypeSet;
039import net.sf.gridarta.model.archetypeset.DefaultArchetypeSet;
040import net.sf.gridarta.model.errorview.ErrorViewCollector;
041import net.sf.gridarta.model.errorview.TestErrorView;
042import net.sf.gridarta.model.face.FaceObjectProviders;
043import net.sf.gridarta.model.face.FaceObjects;
044import net.sf.gridarta.model.face.TestFaceObjects;
045import net.sf.gridarta.model.gameobject.TestGameObject;
046import net.sf.gridarta.model.gameobject.TestGameObjectFactory;
047import net.sf.gridarta.model.io.AbstractArchetypeParser;
048import net.sf.gridarta.model.io.TestArchetypeParser;
049import net.sf.gridarta.model.maparchobject.TestMapArchObject;
050import net.sf.gridarta.utils.ResourceIcons;
051import org.jetbrains.annotations.NotNull;
052
053/**
054 * Parser for artifacts definitions.
055 * @author Andreas Kirschbaum
056 */
057public class TestParser {
058
059    /**
060     * The {@link FaceObjectProviders} instance.
061     */
062    @NotNull
063    private final FaceObjectProviders faceObjectProviders;
064
065    /**
066     * The {@link ArchetypeSet} instance.
067     */
068    @NotNull
069    private final ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> archetypeSet;
070
071    /**
072     * The {@link AnimationObjects} instance.
073     */
074    @NotNull
075    private final AnimationObjects animationObjects = new TestAnimationObjects();
076
077    /**
078     * The {@link ErrorViewCollector} instance.
079     */
080    @NotNull
081    private final ErrorViewCollector errorViewCollector;
082
083    /**
084     * The {@link ArtifactParser} instance.
085     */
086    @NotNull
087    private final ArtifactParser<TestGameObject, TestMapArchObject, TestArchetype> artifactParser;
088
089    /**
090     * Creates a new instance.
091     */
092    public TestParser() {
093        this(new TestErrorView());
094    }
095
096    /**
097     * Creates a new instance.
098     * @param errorView the error view to use for parsing
099     */
100    public TestParser(@NotNull final TestErrorView errorView) {
101        final ResourceIcons resourceIcons = new ResourceIcons();
102        final File file = new File("*string*");
103        errorViewCollector = new ErrorViewCollector(errorView, file);
104        final FaceObjects faceObjects = new TestFaceObjects();
105        faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons);
106        final TestArchetypeFactory archetypeFactory = new TestArchetypeFactory(faceObjectProviders, animationObjects);
107        final TestGameObjectFactory gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects);
108        archetypeSet = new DefaultArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype>(archetypeFactory, null);
109        final TestArchetypeBuilder archetypeBuilder = new TestArchetypeBuilder(gameObjectFactory);
110        final AbstractArchetypeParser<TestGameObject, TestMapArchObject, TestArchetype, TestArchetypeBuilder> archetypeParser = new TestArchetypeParser(archetypeBuilder, animationObjects, archetypeSet);
111        final List<TestGameObject> invObjects = new ArrayList<TestGameObject>();
112        artifactParser = new ArtifactParser<TestGameObject, TestMapArchObject, TestArchetype>(archetypeSet, errorView, archetypeParser, invObjects);
113    }
114
115    /**
116     * Adds a new archetype.
117     * @param archetypeName the archetype name
118     * @param attributes the archetype's attributes; may be empty
119     * @throws DuplicateArchetypeException if the archetype name is not unique
120     */
121    public void addArchetype(@NotNull final String archetypeName, @NotNull final String... attributes) throws DuplicateArchetypeException {
122        final TestArchetype baseArchetype = new TestDefaultArchetype(archetypeName, faceObjectProviders, animationObjects);
123        final StringBuilder objectText = new StringBuilder();
124        for (final String attribute : attributes) {
125            objectText.append(attribute);
126            objectText.append('\n');
127        }
128        baseArchetype.setObjectText(objectText.toString());
129        archetypeSet.addArchetype(baseArchetype);
130    }
131
132    /**
133     * Parses artifacts definitions.
134     * @param artifacts the artifacts definitions
135     * @throws IOException if parsing fails
136     */
137    public void parseArtifacts(@NotNull final String artifacts) throws IOException {
138        final Reader reader = new StringReader(artifacts);
139        final BufferedReader bufferedReader = new BufferedReader(reader);
140        try {
141            artifactParser.loadArtifact(bufferedReader, errorViewCollector, "", "panel", "folder");
142        } finally {
143            bufferedReader.close();
144        }
145    }
146
147    /**
148     * Returns an {@link Archetype} by name.
149     * @param archetypeName the archetype name
150     * @return the archetype
151     * @throws UndefinedArchetypeException if the archetype name is undefined
152     */
153    @NotNull
154    public Archetype<TestGameObject, TestMapArchObject, TestArchetype> getArchetype(@NotNull final String archetypeName) throws UndefinedArchetypeException {
155        return archetypeSet.getArchetype(archetypeName);
156    }
157
158    /**
159     * Returns the number of defined archetypes.
160     * @return the number of defined archetypes
161     */
162    public int getArchetypeCount() {
163        return archetypeSet.getArchetypeCount();
164    }
165
166}