-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reintroduce include statements (#1028)
Signed-off-by: applenick <[email protected]> Signed-off-by: Samuel Roach <[email protected]>
- Loading branch information
1 parent
842aaa8
commit 75bf28c
Showing
13 changed files
with
340 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/src/main/java/tc/oc/pgm/api/map/includes/MapInclude.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tc.oc.pgm.api.map.includes; | ||
|
||
import java.util.Collection; | ||
import org.jdom2.Content; | ||
|
||
/** Represents a snippet of XML that can be referenced for reuse * */ | ||
public interface MapInclude { | ||
|
||
/** | ||
* Get a unique id which identifies this MapInclude. | ||
* | ||
* @return A unique id | ||
*/ | ||
String getId(); | ||
|
||
/** | ||
* Get the system file time from when this MapInclude file was last modified. | ||
* | ||
* @return Time of last file modification | ||
*/ | ||
long getLastModified(); | ||
|
||
/** | ||
* Gets whether the associated {@link MapInclude} files have changed since last loading. | ||
* | ||
* @param time The current system time | ||
* @return True if given time is newer than last modified time | ||
*/ | ||
boolean hasBeenModified(long time); | ||
|
||
/** | ||
* Get a collection of {@link Content} which can be merged into an existing {@link Document} | ||
* | ||
* @return a collection of {@link Content} | ||
*/ | ||
Collection<Content> getContent(); | ||
} |
34 changes: 34 additions & 0 deletions
34
core/src/main/java/tc/oc/pgm/api/map/includes/MapIncludeProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tc.oc.pgm.api.map.includes; | ||
|
||
import java.util.Collection; | ||
import org.jdom2.Document; | ||
import tc.oc.pgm.api.Config; | ||
import tc.oc.pgm.util.xml.InvalidXMLException; | ||
|
||
/** A processor to determine which {@link MapInclude}s should be included when loading a map * */ | ||
public interface MapIncludeProcessor { | ||
|
||
/** | ||
* Process the given {@link Document} and return a collection of {@link MapInclude}s. | ||
* | ||
* @param document A map document | ||
* @return A collection of map includes, collection will be empty if none are found. | ||
* @throws InvalidXMLException If the given document is not found or able to be parsed. | ||
*/ | ||
Collection<MapInclude> getMapIncludes(Document document) throws InvalidXMLException; | ||
|
||
/** | ||
* Get a {@link MapInclude} by its id | ||
* | ||
* @param includeId ID of the map include | ||
* @return A {@link MapInclude} | ||
*/ | ||
MapInclude getMapIncludeById(String includeId); | ||
|
||
/** | ||
* Reload the processor to fetch new map includes or reload existing ones. | ||
* | ||
* @param config A configuration file. | ||
*/ | ||
void reload(Config config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
core/src/main/java/tc/oc/pgm/map/includes/MapIncludeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package tc.oc.pgm.map.includes; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collection; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.jdom2.Content; | ||
import org.jdom2.Document; | ||
import org.jdom2.JDOMException; | ||
import tc.oc.pgm.api.map.exception.MapMissingException; | ||
import tc.oc.pgm.api.map.includes.MapInclude; | ||
|
||
public class MapIncludeImpl implements MapInclude { | ||
|
||
private final String id; | ||
private final Document source; | ||
private final AtomicLong lastModified; | ||
|
||
public MapIncludeImpl(File file) throws MapMissingException, JDOMException, IOException { | ||
try { | ||
InputStream fileStream = new FileInputStream(file); | ||
this.id = file.getName().replace(".xml", ""); | ||
this.source = MapIncludeProcessorImpl.DOCUMENT_FACTORY.get().build(fileStream); | ||
} catch (FileNotFoundException e) { | ||
throw new MapMissingException(file.getPath(), "Unable to read map include document", e); | ||
} finally { | ||
lastModified = new AtomicLong(file.lastModified()); | ||
} | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public Collection<Content> getContent() { | ||
return source.getRootElement().cloneContent(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == null || !(other instanceof MapInclude)) return false; | ||
return ((MapInclude) other).getId().equalsIgnoreCase(getId()); | ||
} | ||
|
||
@Override | ||
public long getLastModified() { | ||
return lastModified.get(); | ||
} | ||
|
||
@Override | ||
public boolean hasBeenModified(long time) { | ||
return time > lastModified.get(); | ||
} | ||
} |
Oops, something went wrong.