20 package net.sf.gridarta.model.artifact;
22 import java.io.BufferedReader;
24 import java.io.FileInputStream;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
42 import org.apache.log4j.Category;
43 import org.apache.log4j.Logger;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
90 public boolean accept(
final File dir,
final String name) {
91 final File fullPath =
new File(dir, name);
93 return fullPath.isDirectory() && !name.equalsIgnoreCase(
"cvs") && !name.equalsIgnoreCase(
".xvpics") && !name.equalsIgnoreCase(
".svn") || name.toLowerCase().endsWith(
".art");
116 return Collections.unmodifiableCollection(invObjects);
129 public void loadArchesFromArtifacts(@NotNull
final File f, @NotNull
final String panelName, @NotNull
final String folderName) {
132 if (LOG.isInfoEnabled()) {
133 LOG.info(
"Loaded " + (archetypeSet.
getArchetypeCount() - archetypes) +
" artifacts from '" + f +
"'.");
149 if (f.isDirectory()) {
150 final String[] traverse = f.list(artifactFilenameFilter);
151 if (traverse != null) {
152 Arrays.sort(traverse);
153 for (
final String entry : traverse) {
172 private void loadArtifact(@NotNull
final ErrorViewCollector errorViewCollector, @NotNull
final File f, @NotNull
final String archPath, @NotNull
final String panelName, @NotNull
final String folderName) {
174 try (FileInputStream fis =
new FileInputStream(f)) {
175 try (InputStreamReader isr =
new InputStreamReader(fis)) {
176 try (BufferedReader in =
new BufferedReader(isr)) {
177 loadArtifact(in, errorViewCollector, archPath, panelName, folderName);
181 }
catch (
final IOException ex) {
195 public void loadArtifact(@NotNull
final BufferedReader in, @NotNull
final ErrorViewCollector errorViewCollector, @NotNull
final String archPath, @NotNull
final String panelName, @NotNull
final String folderName)
throws IOException {
197 @Nullable String name = null;
198 @Nullable String defArchName = null;
201 @Nullable String editorPath = null;
203 final String thisLine2 = in.readLine();
204 if (thisLine2 == null) {
208 final String thisLine = thisLine2.trim();
210 if (!thisLine.startsWith(
"#") && !thisLine.isEmpty()) {
211 if (thisLine.startsWith(
"artifact ")) {
212 name = thisLine.substring(9);
213 }
else if (thisLine.startsWith(
"def_arch ")) {
214 defArchName = thisLine.substring(9);
215 }
else if (thisLine.startsWith(
"editor ")) {
216 final String editor = thisLine.substring(7);
217 final int tIndex = editor.indexOf(
':');
219 editorCode = Integer.parseInt(editor);
221 editorPath = editor.substring(tIndex + 1);
223 editorCode = Integer.parseInt(editor.substring(0, tIndex));
225 }
else if (thisLine.startsWith(
"Object")) {
226 if (editorCode == -1) {
229 if (editorCode == 0 || editorCode == 2) {
232 final String objTitle = thisLine.length() > 7 ? thisLine.substring(7) :
"";
235 if (defArchName == null) {
237 }
else if (defArchName.isEmpty()) {
239 }
else if (name == null) {
241 }
else if (name.isEmpty()) {
245 final R archetype = archetypeSet.
getArchetype(defArchName);
246 if (editorCode != 0 && editorCode != 2) {
250 archetypeParser.
parseArchetypeFromStream(in, editorCode == 2 ? null : archetype, thisLine, name, panelName, folderName, archPath, invObjects, errorViewCollector);
255 final String thisLine3 = in.readLine();
256 if (thisLine3 == null || thisLine3.equals(
"end")) {
262 errorViewCollector.addWarning(
ErrorViewCategory.
ARTIFACT_ENTRY_INVALID,
"line " + lineCount +
", Object '" + defArchName +
"' / '" + name +
"' / '" + objTitle +
"' references undefined archetype '" + ex.getMessage() +
"'");
final ErrorView errorView
The ErrorView for reporting errors.
Convenience class for adding messages to a ErrorView instance using a fixed category name...
Reading and writing of maps, handling of paths.
void loadArtifact(@NotNull final BufferedReader in, @NotNull final ErrorViewCollector errorViewCollector, @NotNull final String archPath, @NotNull final String panelName, @NotNull final String folderName)
Loads one artifact.
void loadArchesFromArtifactsRecursive(@NotNull final File f, @NotNull final String archPath, @NotNull final String panelName, @NotNull final String folderName)
This method takes a filename or directory name as argument.
final List< G > invObjects
Collects all inventory objects.
Defines possible error categories for ErrorView instances.
final ArchetypeSet< G, A, R > archetypeSet
The ArchetypeSet for looking up archetypes.
Interface for classes displaying error messages.
Base package of all Gridarta classes.
final AbstractArchetypeParser< G, A, R, ?> archetypeParser
The AbstractArchetypeParser to use.
Reflects a game object (object on a map).
int getArchetypeCount()
Returns the number of Archetypes available.
R getArchetype(@NotNull String archetypeName)
Returns an Archetype by its name.
Collection< G > getInvObjects()
Returns all collected inventory objects.
void loadArtifact(@NotNull final ErrorViewCollector errorViewCollector, @NotNull final File f, @NotNull final String archPath, @NotNull final String panelName, @NotNull final String folderName)
Loads one artifact.
static final Category LOG
The logger for printing log messages.
GameObjects are the objects based on Archetypes found on maps.
void parseArchetypeFromStream(@NotNull final BufferedReader in, @Nullable final R prototype, @Nullable final String line, @Nullable final String archName, @NotNull final String panelName, @NotNull final String folderName, @NotNull final String archPath, @NotNull final List< G > invObjects, @NotNull final ErrorViewCollector errorViewCollector)
Exception thrown if an Archetype does not exist.
void loadArchesFromArtifacts(@NotNull final File f, @NotNull final String panelName, @NotNull final String folderName)
This method takes a filename or directory name as argument.
ArtifactParser(@NotNull final ArchetypeSet< G, A, R > archetypeSet, @NotNull final ErrorView errorView, @NotNull final AbstractArchetypeParser< G, A, R, ?> archetypeParser)
Creates a new instance.
Interface that captures similarities between different ArchetypeSet implementations.
Abstract base implementation of ArchetypeParser.
Parser for artifact definitions.
Interface for MapArchObjects.
final FilenameFilter artifactFilenameFilter
A FilenameFilter that matches "*.art" files.