1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
;
12 import cc
.squirreljme
.plugin
.util
.CommentReader
;
13 import cc
.squirreljme
.plugin
.util
.ErrorListTokenizer
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
17 import java
.io
.PrintStream
;
18 import java
.nio
.file
.Files
;
19 import java
.nio
.file
.StandardOpenOption
;
21 import java
.util
.TreeMap
;
22 import org
.gradle
.api
.Project
;
23 import org
.gradle
.api
.file
.FileTree
;
24 import org
.gradle
.api
.plugins
.JavaPluginConvention
;
25 import org
.gradle
.api
.tasks
.SourceSet
;
28 * This is a mapping of individual error codes for a project.
32 public final class ErrorListManager
34 /** The project to manage the list for. */
35 protected final Project project
;
37 /** The listed errors. */
38 private final Map
<Integer
, SourceError
> _errors
=
41 /** The error code for this project. */
42 private String _projectErrorCode
;
45 * Initializes the error code manager.
47 * @param __project The project to manage.
48 * @throws NullPointerException On null arguments.
51 public ErrorListManager(Project __project
)
52 throws NullPointerException
54 if (__project
== null)
55 throw new NullPointerException("No project.");
57 this.project
= __project
;
61 * Returns the next available error.
63 * @return The next available error.
66 public final String
next()
70 return this._projectErrorCode
+
71 SourceError
.indexToString(this.nextIndex());
75 * Returns the next available error.
77 * @return The next available error.
80 public final int nextIndex()
84 Map
<Integer
, SourceError
> errors
= this._errors
;
87 for (int i
= 1; i
<= SourceError
._MAX_ERROR_CODE
; i
++)
88 if (!errors
.containsKey(i
))
91 throw new IllegalArgumentException("Ran out of error indexes.");
96 * Prints the errors to the given stream.
98 * @param __out The stream to write to.
99 * @throws NullPointerException On null arguments.
102 public final void print(PrintStream __out
)
103 throws NullPointerException
106 throw new NullPointerException("No stream specified.");
112 for (SourceError e
: this._errors
.values())
118 * Initializes the code map.
122 private void __init()
124 Project project
= this.project
;
125 Map
<Integer
, SourceError
> errors
= this._errors
;
130 if (this._projectErrorCode
!= null)
134 SquirrelJMEPluginConfiguration config
= project
.getExtensions()
135 .<SquirrelJMEPluginConfiguration
>getByType(
136 SquirrelJMEPluginConfiguration
.class);
138 // Fail if this is missing
139 String projectErrorCode
= config
.javaDocErrorCode
;
140 if (projectErrorCode
== null)
141 throw new IllegalStateException(String
.format(
142 "Project %s has no error code.", project
.getName()));
144 // Used as a flag and to indicate the group for this
145 this._projectErrorCode
= projectErrorCode
;
147 // Temporary buffer for data reading
148 StringBuilder buffer
= new StringBuilder();
151 FileTree sources
= project
.getConvention()
152 .getPlugin(JavaPluginConvention
.class).getSourceSets()
153 .getByName(SourceSet
.MAIN_SOURCE_SET_NAME
).getAllJava()
157 for (File source
: sources
)
158 try (InputStream in
= Files
.newInputStream(source
.toPath(),
159 StandardOpenOption
.READ
);
160 ErrorListTokenizer tokenizer
= new ErrorListTokenizer(
161 source
.toPath().getFileName(),
162 new CommentReader(in
)))
164 // Keep reading in errors
167 // Read the next error
168 SourceError error
= tokenizer
.next();
172 // Store error in the map
173 SourceError old
= errors
.put(error
.index
, error
);
177 System
.err
.printf("Duplicate error %s and %s.%n",
181 if (!error
.projectCode
.equals(projectErrorCode
))
182 System
.err
.printf("Error has wrong project: %s," +
183 "should be: %s%n", error
, projectErrorCode
);
186 catch (IOException e
)
188 throw new RuntimeException("Could not decode file.", e
);