SHARE
TWEET

Untitled

a guest Jul 17th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ex1t.core;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. /**
  9.  * Map Builder, build map from file or database
  10.  * @author Shaolan
  11.  *
  12.  */
  13. public class MapBuilder {
  14.     /**
  15.      * The target product of our builder
  16.      * @see MapBuilder#getProduct()
  17.      * @see MapBuilder#buildFromFile(String)
  18.      * @see MapBuilder#buildFromDatabase(String)
  19.      */
  20.     private Map product;
  21.    
  22.     /**
  23.      * Private constructor
  24.      */
  25.     private MapBuilder(){
  26.         product = new Map();
  27.     }
  28.    
  29.     /**
  30.      * Read a area from the given stream.
  31.      * @param in The stream which contain area
  32.      * @param a the Area to build
  33.      * @throws MapFileException
  34.      */
  35.     private void readArea(BufferedReader in,Area a) throws MapFileException {
  36.         for (int i = 0 ; i < a.getWidth() ; i++){
  37.             try {
  38.                 String line = in.readLine();
  39.                 for (int j = 0 ; j < a.getHeight() ; j++){
  40.                     Integer c = Integer.valueOf(line.charAt(j));
  41.                     a.setMapping(i, j, c);
  42.                 }
  43.             }
  44.             catch(NumberFormatException e){ throw new MapFileException("Area incorrect : wrong mapping data(not a number ?).");}
  45.             catch(IOException e){ throw new MapFileException("Area incorrect : missing information."); }
  46.         }
  47.     }
  48.    
  49.     /**
  50.      * Parse the given Area header (contain the width and height of the area).
  51.      * @param header Area header
  52.      * @return A new area object, build with header data
  53.      * @throws MapFileException
  54.      */
  55.     private Area readAreaHeader(String header) throws MapFileException{
  56.         Area a = null;
  57.         try {
  58.             String headerData[] = header.split("|");
  59.             Integer w = Integer.valueOf(headerData[0]);
  60.             Integer h = Integer.valueOf(headerData[1]);
  61.             a = new Area(w,h);
  62.         }
  63.         catch(NumberFormatException e){ throw new MapFileException("Area header incorrect : Wrong area size (not a number ?).");}
  64.         catch(NullPointerException e){ throw new MapFileException("Area header incorrect : missing information."); }
  65.         return a;
  66.     }
  67.    
  68.     /**
  69.      * Read all map assume that header have been read.
  70.      * @param in
  71.      * @throws MapFileException
  72.      */
  73.     private void readMap(BufferedReader in) throws MapFileException {
  74.         for (int i = 0 ; i < product.getArea().length ; i++ ){
  75.             try {
  76.                 Area a = readAreaHeader(in.readLine());
  77.                 readArea(in,a);
  78.                 product.setArea(i,a);
  79.             }
  80.             catch(IOException e){ throw new MapFileException("Error when read area " + i); }
  81.             catch(MapFileException e){ throw e;}
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Read and parse the header of the map file (contain number of area).
  87.      * @param header
  88.      * @throws MapFileException
  89.      */
  90.     private void readMapHeader(String header) throws MapFileException {
  91.         try {
  92.             Integer n = Integer.valueOf(header);
  93.             product.setAreaNumber(n);
  94.         }
  95.         catch(NumberFormatException e){ throw new MapFileException("Map File header incorrect."); }
  96.     }
  97.    
  98.     private Map getProduct() { return product; }
  99.    
  100.     /**
  101.      * Build a Map from a given file
  102.      * @param path
  103.      * @return the built map
  104.      */
  105.     public static Map buildFromFile(String path){
  106.         MapBuilder director = new MapBuilder();
  107.         try{
  108.             BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  109.             director.readMapHeader(in.readLine());
  110.             director.readMap(in);
  111.             return director.getProduct();
  112.         }
  113.         catch(IOException e){ return null; }
  114.         catch(MapFileException e) { return null; }
  115.     }
  116.    
  117.     /**
  118.      * Build a map from database entry
  119.      * @param id
  120.      * @return
  121.      */
  122.     public static Map buildFromDatabase(String id){
  123.         return new Map();
  124.     }
  125. }
RAW Paste Data
Top