package ch.sahits.game.graphic.image.impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import ch.sahits.game.graphic.image.IDataImageLoader;
import ch.sahits.game.graphic.image.model.ImageData;
import ch.sahits.game.graphic.image.model.NamedPolygon;
import ch.sahits.game.image.ECrop;
/**
* Image loader that uses an XML as a base for image information.
* From the super classes only the types <code>o</code>
* are supported.
* @author aho
*
*/
public class XMLImageLoader extends ImagesLoader implements IDataImageLoader {
private static final Logger logger = Logger.getLogger(XMLImageLoader.class);
/** Maps storing additional image data. Key is the image file name */
private Map<String,ImageData> imageData;
/**
* Constructor loading language specific images from the xml file <code>fnm</code>
* @param fnm XML file name
*/
public XMLImageLoader(String fnm) {
super(fnm);
}
/**
* Throws IllegalStateException as the there is no use with XML files
*/
protected void parseImageDefinitionsAndLoad(BufferedReader br){
throw new IllegalStateException("This method may not be accessed from the XMLImageLoader");
}
@Override
protected void initLoader() {
imageData = new HashMap<String,ImageData>();
super.initLoader();
}
@Override
protected void loadImagesFile(String fnm) {
SAXBuilder builder = new SAXBuilder();
String imsFNm = IMAGE_DIR + fnm;
logger.info("Reading file: " + imsFNm);
InputStream in = this.getClass().getResourceAsStream(imsFNm);
try {
Document document = builder.build(in);
Element rootNode = document.getRootElement(); // images
List<Element> imageList = rootNode.getChildren("image");
for (Element image : imageList) {
String name = image.getAttributeValue("name");
ECrop crop;
int maxCrop;
if (image.getAttribute("crop")!=null){
crop = ECrop.valueOf(image.getAttributeValue("crop"));
maxCrop = Integer.parseInt(image.getAttributeValue("maxCrop"));
} else {
crop = ECrop.NONE;
maxCrop=0;
}
String type = image.getAttributeValue("type");
Element polygons = image.getChild("polygons");
ImageData imgData = new ImageData(name, crop, maxCrop);
if (polygons!=null){ // it might be an image definition without any polygons defined
List<Element> polygonList = polygons.getChildren("polygon");
for (Element polygon : polygonList) {
String name2 = polygon.getAttributeValue("name");
NamedPolygon poly = new NamedPolygon(name2);
int zIndex = 1;
if (polygon.getAttribute("z-index")!=null){
zIndex = Integer.parseInt(polygon.getAttributeValue("z-index"));
}
List<Element> points = polygon.getChildren("point");
for (Element point : points) {
int x = Integer.parseInt(point.getAttributeValue("x"));
int y = Integer.parseInt(point.getAttributeValue("y"));
poly.addPoint(x, y);
}
imgData.addPolygon(poly,zIndex);
} // end <polygon>
} // end extracting all <polygons>
// Load the image
parseImageDefinitionAndLoad(type, imgData);
} // end extracting <image>
} catch (IOException e){
logger.fatal("Error reading file: " + imsFNm, e);
System.exit(1);
} catch (JDOMException e) {
logger.fatal("Error reading file: " + imsFNm, e);
System.exit(1);
}
}
private void parseImageDefinitionAndLoad(String type, ImageData imgData) {
if (type.equals("o")) { // a single image
getFileNameImage("o "+imgData.getImageName());
imageData.put(getPrefix(imgData.getImageName()), imgData);
} else
logger.warn("Do not recognize type: " + type);
}
/* (non-Javadoc)
* @see ch.sahits.game.graphic.image.IXMLImageLoader#getImageData(java.lang.String)
*/
@Override
public ImageData getImageData(String imageName){
return imageData.get(imageName);
}
}