mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
added sources for Slick
Former-commit-id: 1647fa32ef6894bd7db44f741f07c2f4dcdf9054 Former-commit-id: 0e5810dcfbe1fd59b13e7cabe9f1e93c5542da2d
This commit is contained in:
46
lib/slick-source/org/newdawn/slick/tests/xml/Entity.java
Normal file
46
lib/slick-source/org/newdawn/slick/tests/xml/Entity.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
/**
|
||||
* A test example of some object data that can be configured via XML
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class Entity {
|
||||
/** X position for the entity */
|
||||
private float x;
|
||||
/** Y position for the entity */
|
||||
private float y;
|
||||
/** items held */
|
||||
private Inventory invent;
|
||||
/** Entity statistics */
|
||||
private Stats stats;
|
||||
|
||||
/**
|
||||
* Called by XML parser to add a configured inventory to the entity
|
||||
*
|
||||
* @param inventory The inventory to be added
|
||||
*/
|
||||
private void add(Inventory inventory) {
|
||||
this.invent = inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by XML parser to add a configured statistics object to the entity
|
||||
*
|
||||
* @param stats The statistics to be added
|
||||
*/
|
||||
private void add(Stats stats) {
|
||||
this.stats = stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump this object to sysout
|
||||
*
|
||||
* @param prefix The prefix to apply to all lines
|
||||
*/
|
||||
public void dump(String prefix) {
|
||||
System.out.println(prefix+"Entity "+x+","+y);
|
||||
invent.dump(prefix+"\t");
|
||||
stats.dump(prefix+"\t");
|
||||
}
|
||||
}
|
||||
34
lib/slick-source/org/newdawn/slick/tests/xml/GameData.java
Normal file
34
lib/slick-source/org/newdawn/slick/tests/xml/GameData.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The top level node of our test structure for XML -> object parsing
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class GameData {
|
||||
/** The list of entities added */
|
||||
private ArrayList entities = new ArrayList();
|
||||
|
||||
/**
|
||||
* Called by XML parser to add a configured entity to the GameData
|
||||
*
|
||||
* @param entity The entity to be added
|
||||
*/
|
||||
private void add(Entity entity) {
|
||||
entities.add(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump this object to sysout
|
||||
*
|
||||
* @param prefix The prefix to apply to all lines
|
||||
*/
|
||||
public void dump(String prefix) {
|
||||
System.out.println(prefix+"GameData");
|
||||
for (int i=0;i<entities.size();i++) {
|
||||
((Entity) entities.get(i)).dump(prefix+"\t");
|
||||
}
|
||||
}
|
||||
}
|
||||
34
lib/slick-source/org/newdawn/slick/tests/xml/Inventory.java
Normal file
34
lib/slick-source/org/newdawn/slick/tests/xml/Inventory.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A test example of some object data that can be configured via XML
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class Inventory {
|
||||
/** The items held in the inventory */
|
||||
private ArrayList items = new ArrayList();
|
||||
|
||||
/**
|
||||
* Called by XML parser to add a configured item to the entity
|
||||
*
|
||||
* @param item The item to be added
|
||||
*/
|
||||
private void add(Item item) {
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump this object to sysout
|
||||
*
|
||||
* @param prefix The prefix to apply to all lines
|
||||
*/
|
||||
public void dump(String prefix) {
|
||||
System.out.println(prefix+"Inventory");
|
||||
for (int i=0;i<items.size();i++) {
|
||||
((Item) items.get(i)).dump(prefix+"\t");
|
||||
}
|
||||
}
|
||||
}
|
||||
23
lib/slick-source/org/newdawn/slick/tests/xml/Item.java
Normal file
23
lib/slick-source/org/newdawn/slick/tests/xml/Item.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
|
||||
/**
|
||||
* A test example of some object data that can be configured via XML
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class Item {
|
||||
/** The name injected by the XML parser */
|
||||
protected String name;
|
||||
/** The condition value injected by the XML parser */
|
||||
protected int condition;
|
||||
|
||||
/**
|
||||
* Dump this object to sysout
|
||||
*
|
||||
* @param prefix The prefix to apply to all lines
|
||||
*/
|
||||
public void dump(String prefix) {
|
||||
System.out.println(prefix+"Item "+name+","+condition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A test example of some object data that can be configured via XML
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class ItemContainer extends Item {
|
||||
/** The items held in this container */
|
||||
private ArrayList items = new ArrayList();
|
||||
|
||||
/**
|
||||
* Called by XML parser to add a configured item to the entity
|
||||
*
|
||||
* @param item The item to be added
|
||||
*/
|
||||
private void add(Item item) {
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the XML to set the name attribute. Note that set methods can
|
||||
* be used as well as direct field injection. In this case the setter *has*
|
||||
* to be used to access the protected field from the super class
|
||||
*
|
||||
* @param name The value to set
|
||||
*/
|
||||
private void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the XML to set the condition attribute. Note that set methods can
|
||||
* be used as well as direct field injection. In this case the setter *has*
|
||||
* to be used to access the protected field from the super class
|
||||
*
|
||||
* @param condition The value to set
|
||||
*/
|
||||
private void setCondition(int condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump this object to sysout
|
||||
*
|
||||
* @param prefix The prefix to apply to all lines
|
||||
*/
|
||||
public void dump(String prefix) {
|
||||
System.out.println(prefix+"Item Container "+name+","+condition);
|
||||
for (int i=0;i<items.size();i++) {
|
||||
((Item) items.get(i)).dump(prefix+"\t");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
import org.newdawn.slick.util.xml.ObjectTreeParser;
|
||||
import org.newdawn.slick.util.xml.SlickXMLException;
|
||||
|
||||
/**
|
||||
* A simple test to check that the object parser from XML works. Read the Javadoc of
|
||||
* ObjectParser to work out whats going on here
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class ObjectParserTest {
|
||||
|
||||
/**
|
||||
* Entity point to our test. Simple read some XML which should
|
||||
* generate an object tree.
|
||||
*
|
||||
* @param argv The arguments passed into the test
|
||||
* @throws SlickXMLException Indicates a failure to parse XML or generate objects
|
||||
*/
|
||||
public static void main(String[] argv) throws SlickXMLException {
|
||||
ObjectTreeParser parser = new ObjectTreeParser("org.newdawn.slick.tests.xml");
|
||||
parser.addElementMapping("Bag", ItemContainer.class);
|
||||
|
||||
GameData parsedData = (GameData) parser.parse("testdata/objxmltest.xml");
|
||||
parsedData.dump("");
|
||||
}
|
||||
}
|
||||
26
lib/slick-source/org/newdawn/slick/tests/xml/Stats.java
Normal file
26
lib/slick-source/org/newdawn/slick/tests/xml/Stats.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
/**
|
||||
* A test example of some object data that can be configured via XML
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class Stats {
|
||||
/** hit points */
|
||||
private int hp;
|
||||
/** magic points */
|
||||
private int mp;
|
||||
/** age in years */
|
||||
private float age;
|
||||
/** experience points */
|
||||
private int exp;
|
||||
|
||||
/**
|
||||
* Dump this object to sysout
|
||||
*
|
||||
* @param prefix The prefix to apply to all lines
|
||||
*/
|
||||
public void dump(String prefix) {
|
||||
System.out.println(prefix+"Stats "+hp+","+mp+","+age+","+exp);
|
||||
}
|
||||
}
|
||||
117
lib/slick-source/org/newdawn/slick/tests/xml/XMLTest.java
Normal file
117
lib/slick-source/org/newdawn/slick/tests/xml/XMLTest.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package org.newdawn.slick.tests.xml;
|
||||
|
||||
import org.newdawn.slick.SlickException;
|
||||
import org.newdawn.slick.util.xml.XMLElement;
|
||||
import org.newdawn.slick.util.xml.XMLParser;
|
||||
|
||||
/**
|
||||
* Silly test to check XML parsing functionality, note the JUnit like methods,
|
||||
* want to move it to JUnit soon but not quite there yet.
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class XMLTest {
|
||||
/**
|
||||
* Fail the test
|
||||
*
|
||||
* @param message The message to describe the failure
|
||||
*/
|
||||
private static void fail(String message) {
|
||||
throw new RuntimeException(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the given object is not null, if it is fail the test
|
||||
*
|
||||
* @param object1 The object to test
|
||||
*/
|
||||
private static void assertNotNull(Object object1) {
|
||||
if (object1 == null) {
|
||||
throw new RuntimeException("TEST FAILS: "+object1+" must not be null");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the two values given are equal, if not fail the test
|
||||
*
|
||||
* @param a1 The first value to compare
|
||||
* @param a2 The second value to compare
|
||||
*/
|
||||
private static void assertEquals(float a1, float a2) {
|
||||
if (a1 != a2) {
|
||||
throw new RuntimeException("TEST FAILS: "+a1+" should be "+a2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the two values given are equal, if not fail the test
|
||||
*
|
||||
* @param a1 The first value to compare
|
||||
* @param a2 The second value to compare
|
||||
*/
|
||||
private static void assertEquals(int a1, int a2) {
|
||||
if (a1 != a2) {
|
||||
throw new RuntimeException("TEST FAILS: "+a1+" should be "+a2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the two values given are equal, if not fail the test
|
||||
*
|
||||
* @param a1 The first value to compare
|
||||
* @param a2 The second value to compare
|
||||
*/
|
||||
private static void assertEquals(Object a1, Object a2) {
|
||||
if (!a1.equals(a2)) {
|
||||
throw new RuntimeException("TEST FAILS: "+a1+" should be "+a2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test for the XML parsing API
|
||||
*
|
||||
* @param argv The arguments given to the test
|
||||
* @throws SlickException Indicates a failure
|
||||
*/
|
||||
public static void main(String[] argv) throws SlickException {
|
||||
XMLParser parser = new XMLParser();
|
||||
|
||||
XMLElement root = parser.parse("testdata/test.xml");
|
||||
|
||||
assertEquals(root.getName(), "testRoot");
|
||||
System.out.println(root);
|
||||
assertNotNull(root.getChildrenByName("simple").get(0).getContent());
|
||||
System.out.println(root.getChildrenByName("simple").get(0).getContent());
|
||||
|
||||
XMLElement parent = root.getChildrenByName("parent").get(0);
|
||||
assertEquals(parent.getChildrenByName("grandchild").size(),0);
|
||||
assertEquals(parent.getChildrenByName("child").size(),2);
|
||||
|
||||
assertEquals(parent.getChildrenByName("child").get(0).getChildren().size(),2);
|
||||
XMLElement child = parent.getChildrenByName("child").get(0).getChildren().get(0);
|
||||
|
||||
String name = child.getAttribute("name");
|
||||
String test = child.getAttribute("nothere","defaultValue");
|
||||
int age = child.getIntAttribute("age");
|
||||
|
||||
assertEquals(name, "bob");
|
||||
assertEquals(test, "defaultValue");
|
||||
assertEquals(age, 1);
|
||||
|
||||
XMLElement other = root.getChildrenByName("other").get(0);
|
||||
float x = (float) other.getDoubleAttribute("x");
|
||||
float y = (float) other.getDoubleAttribute("y", 1.0f);
|
||||
float z = (float) other.getDoubleAttribute("z", 83.0f);
|
||||
|
||||
assertEquals(x, 5.3f);
|
||||
assertEquals(y, 5.4f);
|
||||
assertEquals(z, 83.0f);
|
||||
|
||||
try {
|
||||
z = (float) other.getDoubleAttribute("z");
|
||||
fail("Attribute z as a double should fail");
|
||||
} catch (SlickException e) {
|
||||
// expect exception
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<BODY>
|
||||
Tests for the XML Parser and Object Tree Parser
|
||||
</BODY>
|
||||
Reference in New Issue
Block a user