Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ex1t.core;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- * Map Builder, build map from file or database
- * @author Shaolan
- *
- */
- public class MapBuilder {
- /**
- * The target product of our builder
- * @see MapBuilder#getProduct()
- * @see MapBuilder#buildFromFile(String)
- * @see MapBuilder#buildFromDatabase(String)
- */
- private Map product;
- /**
- * Private constructor
- */
- private MapBuilder(){
- product = new Map();
- }
- /**
- * Read a area from the given stream.
- * @param in The stream which contain area
- * @param a the Area to build
- * @throws MapFileException
- */
- private void readArea(BufferedReader in,Area a) throws MapFileException {
- for (int i = 0 ; i < a.getWidth() ; i++){
- try {
- String line = in.readLine();
- for (int j = 0 ; j < a.getHeight() ; j++){
- Integer c = Integer.valueOf(line.charAt(j));
- a.setMapping(i, j, c);
- }
- }
- catch(NumberFormatException e){ throw new MapFileException("Area incorrect : wrong mapping data(not a number ?).");}
- catch(IOException e){ throw new MapFileException("Area incorrect : missing information."); }
- }
- }
- /**
- * Parse the given Area header (contain the width and height of the area).
- * @param header Area header
- * @return A new area object, build with header data
- * @throws MapFileException
- */
- private Area readAreaHeader(String header) throws MapFileException{
- Area a = null;
- try {
- String headerData[] = header.split("|");
- Integer w = Integer.valueOf(headerData[0]);
- Integer h = Integer.valueOf(headerData[1]);
- a = new Area(w,h);
- }
- catch(NumberFormatException e){ throw new MapFileException("Area header incorrect : Wrong area size (not a number ?).");}
- catch(NullPointerException e){ throw new MapFileException("Area header incorrect : missing information."); }
- return a;
- }
- /**
- * Read all map assume that header have been read.
- * @param in
- * @throws MapFileException
- */
- private void readMap(BufferedReader in) throws MapFileException {
- for (int i = 0 ; i < product.getArea().length ; i++ ){
- try {
- Area a = readAreaHeader(in.readLine());
- readArea(in,a);
- product.setArea(i,a);
- }
- catch(IOException e){ throw new MapFileException("Error when read area " + i); }
- catch(MapFileException e){ throw e;}
- }
- }
- /**
- * Read and parse the header of the map file (contain number of area).
- * @param header
- * @throws MapFileException
- */
- private void readMapHeader(String header) throws MapFileException {
- try {
- Integer n = Integer.valueOf(header);
- product.setAreaNumber(n);
- }
- catch(NumberFormatException e){ throw new MapFileException("Map File header incorrect."); }
- }
- private Map getProduct() { return product; }
- /**
- * Build a Map from a given file
- * @param path
- * @return the built map
- */
- public static Map buildFromFile(String path){
- MapBuilder director = new MapBuilder();
- try{
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
- director.readMapHeader(in.readLine());
- director.readMap(in);
- return director.getProduct();
- }
- catch(IOException e){ return null; }
- catch(MapFileException e) { return null; }
- }
- /**
- * Build a map from database entry
- * @param id
- * @return
- */
- public static Map buildFromDatabase(String id){
- return new Map();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement