Advertisement
SVXX

BeanGenerator

Mar 22nd, 2014
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. public class BeanGenerator
  2. {
  3.     StringBuilder beanGen;
  4.     Pattern character;
  5.     Pattern image;
  6.    
  7.     ArrayList<CharBean> charBeanList; //Will use this as a placeholder.
  8.     private LinkedHashMap<Coordinate, ArrayList<CharBean>> beanMap = new LinkedHashMap<Coordinate, ArrayList<CharBean>>();
  9.    
  10.     public BeanGenerator(String s)
  11.     {
  12.         beanGen = new StringBuilder(s);
  13.         character = Pattern.compile(RegexHelper.charRegex);
  14.     }
  15.    
  16.     public LinkedHashMap<Coordinate, ArrayList<CharBean>> getBeanMap() {
  17.         return beanMap;
  18.     }
  19.  
  20.     public BeanGenerator()
  21.     {
  22.         beanGen = new StringBuilder("");
  23.     }
  24.    
  25.     public void makeBeans()
  26.     {
  27.         String groups = "";
  28.         String getScan = null;
  29.         //lines = beanGen.split("\\r?\\n"); //Get all lines in an array.
  30.         //for(int i = 0; i < lines.length; i++)
  31.         //Scanner scan = new Scanner(beanGen.toString());
  32.         //scan.useDelimiter("^" + character.pattern());
  33.         BufferedReader io = new BufferedReader(new StringReader(beanGen.toString()));
  34.        
  35.         try
  36.         {
  37.             while((getScan = io.readLine()) != null)
  38.             {
  39.                 Matcher m = character.matcher(getScan);
  40.                 if(m.find())
  41.                 {
  42.                     Coordinate coord = new Coordinate(m.group(5));
  43.                     groups = m.group(3) + "," + m.group(5) + "," + m.group(9)
  44.                             + "," + m.group(13) + "," + m.group(17) + ","
  45.                             + m.group(21) + "," + m.group(25) + "," + m.group(27);
  46.                     if(getBeanMap().containsKey(coord)) //If the map already contains this coordinate object
  47.                     {
  48.                         getBeanMap().get(coord).add(new CharBean(groups)); //Simply add the CharBean to the ArrayList indexed by this Coordinate object.
  49.                     }
  50.                     else
  51.                     {
  52.                         charBeanList = new ArrayList<CharBean>();
  53.                         charBeanList.add(new CharBean(groups));
  54.                         getBeanMap().put(coord, charBeanList);
  55.                     }
  56.                 }
  57.                 groups = "";
  58.             }
  59.             System.out.print("Pointless debug statement");
  60.         } catch (IOException e) {
  61.             // TODO Auto-generated catch block
  62.             e.printStackTrace();
  63.         }
  64.     }
  65.    
  66.     static class RegexHelper
  67.     {
  68.         static String words = "((?:[a-z][a-z]+))";
  69.         static String singleChar = "(.)";
  70.         static String floatNum = "([+-]?\\d*\\.\\d+)(?![-+0-9.])";
  71.         static String space = "\\s";
  72.         static String notLineEnd = "([^\r?\n])";
  73.        
  74.         static String charRegex = words + singleChar + floatNum
  75.                 + singleChar + floatNum + space + words
  76.                 + singleChar + floatNum + space + words
  77.                 + singleChar + floatNum + space + words
  78.                 + singleChar + floatNum + space + words
  79.                 + singleChar + floatNum + space + words
  80.                 + singleChar + floatNum + singleChar + singleChar;
  81.     }
  82. }
  83.  
  84. /* TEST CASE
  85. Input Line -> String[129.72401,94.0 fs=12.0 xscale=12.0 height=7.992 space=3.0 width=3.0],
  86. groups(String) = "129.72401,94.0,12.0,12.0,7.992,3.0,3.0"
  87.  
  88. Input Line -> String[135.72401,94.0 fs=12.0 xscale=12.0 height=7.992 space=3.0 width=6.671997]h
  89. groups(String) = "135.72401,94.0,12.0,12.0,7.992,3.0,6.671997,h"
  90.  
  91. Notice the difference in how the letter 'h' is matched but ',' is not.
  92. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement