Guest User

Untitled

a guest
May 24th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. // Exception: http://img534.imageshack.us/img534/9008/32921273.png
  2.  
  3. package model.db;
  4.  
  5. import Levels.Level;
  6. import java.io.BufferedReader;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.PrintWriter;
  12. import java.io.Reader;
  13. import java.io.StringWriter;
  14. import java.io.Writer;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.ListIterator;
  18. import misc.Background;
  19. import org.jdom2.Document;
  20. import org.jdom2.Element;
  21. import org.jdom2.input.SAXBuilder;
  22.  
  23.    
  24. /**
  25.  *
  26.  * @author Nindustries
  27.  */
  28. public class FIconnect {
  29.    
  30.     private static String DELI = ",";
  31.     private static String FILENAME = "scores.txt";
  32.     private static String FILE_LEVELS = "levels.xml";
  33.  
  34.    
  35.     public ArrayList<Level> loadLevels() throws Exception {    
  36.         ArrayList<Level> result = new ArrayList();
  37.         StarFactory factory = new StarFactory();
  38.        
  39.         SAXBuilder builder = new SAXBuilder(false);
  40.         Document doc = builder.build(this.getClass().getResourceAsStream("/resources/" + FIconnect.FILE_LEVELS));
  41.         Element project = doc.getRootElement();
  42.         List levels = project.getChildren();
  43.         ListIterator it2 = levels.listIterator();
  44.         while (it2.hasNext()){
  45.             //Loop through levels
  46.             Element el = (Element)it2.next();
  47.             Level l = new Level();
  48.             List blocks = el.getChildren();
  49.             ListIterator it22 = blocks.listIterator();
  50.             l.setBackground(new Background(el.getAttribute("Background").getValue()));
  51.             while (it22.hasNext()){
  52.                 //Loop through targets
  53.                 Element target = (Element)it22.next();
  54.                 l.addTarget(factory.produceTarget(target.getText()));
  55.             }
  56.             result.add(l);
  57.         }
  58.        
  59.         return result;
  60.     }
  61.  
  62.     public void saveScore( String username, String score ) throws Exception {
  63.         try {
  64.             PrintWriter output = new PrintWriter(new FileWriter(convertStreamToString(getClass().getResourceAsStream("/resources/" + FILENAME)),true));
  65.             output.printf("%s\r\n", username + FIconnect.DELI + score);
  66.             output.close();
  67.         }
  68.         catch (Exception e) {
  69.             throw new Exception("Error while saving offline score: " + e.toString());
  70.         }
  71.     }
  72.    
  73.     private String convertStreamToString(InputStream is) throws Exception {
  74.     if (is != null) {
  75.         Writer writer = new StringWriter();
  76.  
  77.         char[] buffer = new char[1024];
  78.         try {
  79.             Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  80.             int n;
  81.             while ((n = reader.read(buffer)) != -1) {
  82.                 writer.write(buffer, 0, n);
  83.             }
  84.         } finally {
  85.             is.close();
  86.         }
  87.         return writer.toString();
  88.     } else {        
  89.         return "";
  90.     }
  91. }
  92.    
  93.     public ArrayList<String> loadScores() throws Exception {
  94.         ArrayList<String> result = new ArrayList();
  95.         try {
  96.             BufferedReader br = new BufferedReader(new FileReader(convertStreamToString(getClass().getResourceAsStream("/resources/" + FILENAME))));
  97.             try {
  98.                 StringBuilder sb = new StringBuilder();
  99.                 String line = br.readLine();
  100.  
  101.                 while (line != null) {
  102.                     sb.append(line);
  103.                     sb.append("\n");
  104.                     result.add(br.readLine());
  105.                 }
  106.             } finally {
  107.                 br.close();
  108.             }
  109.         } catch (Exception e){
  110.             throw new Exception("Error while loading offline scores: " + e.toString());  
  111.         }
  112.         return result;
  113.     }
  114.    
  115.      
  116. }
Advertisement
Add Comment
Please, Sign In to add comment