/** * */ package ie.dcu.apps.ist.export.imagemap; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.zip.ZipOutputStream; /** * Exports HTML image maps * * @author Kevin McGuinness */ public class ImageMap { private static final String TEMPLATE_FILE = "template.html"; private String pageTitle; private String imageName; private String imageHref; private String imageAlt; private String mapName; private String curatorName; private String speciesName; private String collectionId; private String comments; private final List areas; private final List preloads; private final List metadata; public ImageMap() { // set defaults : this.pageTitle = "Image Map"; this.imageName = "image"; this.imageHref = ""; this.imageAlt = ""; this.mapName = "imagemap"; this.areas = new LinkedList(); this.preloads = new LinkedList(); this.metadata = new LinkedList(); } public String getPageTitle() { return pageTitle; } public void setPageTitle(String pageTitle) { assert (pageTitle != null); this.pageTitle = pageTitle; } public String getImageName() { return imageName; } public void setImageName(String imageName) { this.imageName = imageName; } public String getImageHref() { return imageHref; } public void setImageHref(String imageHref) { assert (imageHref != null); this.imageHref = imageHref; } public String getImageAlt() { return imageAlt; } public void setImageAlt(String imageAlt) { assert (imageAlt != null); this.imageAlt = imageAlt; } public String getMapName() { return mapName; } public void setMapName(String mapName) { assert (mapName != null); this.mapName = mapName; } public String getCuratorName() { return curatorName; } public void setCuratorName(String curatorName) { assert (curatorName != null); this.curatorName = curatorName; } public String getSpeciesName() { return speciesName; } public void setSpeciesName(String speciesName) { assert (speciesName != null); this.speciesName = speciesName; } public String getCollectionId() { return collectionId; } public void setCollectionId(String collectionId) { this.collectionId = collectionId; } public String getComments() { return comments; } public void setComments(String comments) { assert (comments != null); this.comments = comments; } public void addArea(MapArea area) { areas.add(area); } public List areas() { return areas; } public void addPreload(String preload) { this.preloads.add(preload); } public List preloads() { return preloads; } public List metadata() { return metadata; } public String export() { String template = loadTemplate(); // Create preloads buffer StringBuffer preloads = new StringBuffer(); StringBuffer metadata = new StringBuffer(); int idx = 0; for (String s : preloads()) { HtmlTag.indent(preloads, 8); preloads.append("var image").append(idx); preloads.append(" = new Image()\n"); HtmlTag.indent(preloads, 8); preloads.append("image").append(idx); preloads.append(".src = '").append(s).append("'\n"); idx++; } // Adding table and values for metadata HtmlTag.indent(metadata, 8); metadata.append(" Species Name ").append(getSpeciesName()).append(""); metadata.append(" Curator Name ").append(getCuratorName()).append(""); metadata.append(" Collection Id ").append(getCollectionId()).append(""); metadata.append(" Comments ").append(getComments()).append(""); StringBuffer contents = new StringBuffer(); for (MapArea area : areas) { contents.append('\n'); area.export(contents, 8); } Map subs = new HashMap(); subs.put("image-name", imageName); subs.put("page-title", pageTitle); subs.put("image-href", imageHref); subs.put("image-alt", imageAlt); subs.put("map-name", mapName); subs.put("contents", contents.toString()); subs.put("preloads", preloads.toString()); subs.put("image_metadata", metadata.toString()); return substitute(template, subs); } public void exportToFile(File file) throws IOException { FileWriter writer = new FileWriter(file); try { writer.append(export()); } finally { writer.close(); } } public void exportToFile(ZipOutputStream o) throws IOException { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(o)); out.writeBytes(export()); // Flush out.flush(); } private String substitute(String template, Map subs) { // This could be more efficient.. String result = template; for (String key : subs.keySet()) { String regex = String.format("\\$\\{%s\\}", key); result = result.replaceAll(regex, subs.get(key)); } return result; } private String loadTemplate() { BufferedReader reader = new BufferedReader(new InputStreamReader( getClass().getResourceAsStream(TEMPLATE_FILE))); StringBuffer buff = new StringBuffer(); try { String line; while ((line = reader.readLine()) != null) { buff.append(line).append('\n'); } } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // Ignore } } return buff.toString(); } public String toString() { return export(); } }