Java tutorial
/* * GW2PriceInqueries - A simple tool to check the price of an item at the trading post in Guild Wars 2. * Copyright 2013 Maximilian Werling * * This file is part of GW2PriceInqueries. * * GW2PriceInqueries is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GW2PriceInqueries is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GW2PriceInqueries. If not, see <http://www.gnu.org/licenses/>. */ package de.chemist.gw2.io; import java.awt.Image; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.imageio.ImageIO; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** * * @author Maximilian Werling * */ public class ResourceManager { private static final String ICON_PATH = "/img/"; private static final Map<String, Image> imageMap = new HashMap<String, Image>(); private static final String RESOURCE_FILE = "/resources.json"; public static Image downloadImage(String imageUrl) { try { return ImageIO.read(new URL(imageUrl)); } catch (IOException e) { return getImage("error"); } } public static Image getImage(String imageName) throws NoSuchResourceException { if (availableImages().contains(imageName)) { return imageMap.get(imageName); } else { throw new NoSuchResourceException(imageName); } } public static void loadResourceFile() { try { parseResourceFile(readInputStream(ResourceManager.class.getResourceAsStream(RESOURCE_FILE))); } catch (IOException e) { e.printStackTrace(); } } private static Set<String> availableImages() { return new HashSet<String>(imageMap.keySet()); } private static String buildPath(String path, String fileName, String fileEnding) { return new StringBuilder(path).append(fileName) .append(fileEnding.startsWith(".") ? fileEnding : ("." + fileEnding)).toString(); } private static void parseImages(JsonArray images) throws IOException { for (int i = 0; i < images.size(); i++) { JsonObject image = images.get(i).getAsJsonObject(); String fileName = image.get("name").getAsString(); String filePath = buildPath(ICON_PATH, fileName, image.get("fileEnding").getAsString()); imageMap.put(fileName, ImageIO.read(ResourceManager.class.getResource(filePath))); } } private static void parseResourceFile(String json) throws IOException { JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); parseImages(jsonObject.getAsJsonArray("images")); } private static String readInputStream(InputStream inputStream) throws IOException { final StringBuilder bldr = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line = null; while ((line = br.readLine()) != null) { bldr.append(line); } br.close(); return bldr.toString(); } }