/** * Loads the level collection stored in the passed file. * * @param collectionFilePath path and name of the collection to load * @return the <code>LevelCollection</code> created from the read in data * @throws IOException the collection file couldn't be read */ public final LevelCollection getLevelCollectionFromFile(String collectionFilePath) throws IOException { // ArrayList, for storing the read data. List<String> inputData = new ArrayList<String>(1000); // Create BufferedReader for the input file. BufferedReader levelFile = Utilities.getBufferedReader(collectionFilePath); // The file hasn't been found => return null. if (levelFile == null) { throw new FileNotFoundException(Texts.getText("message.fileMissing", collectionFilePath)); } // Read in line by line of the input data. String levelDataRow; try { while ((levelDataRow = levelFile.readLine()) != null) { inputData.add(levelDataRow); } } finally { levelFile.close(); } // Parse the read data and return the collection created from that data. // The level collection to be returned. LevelCollection levelCollection = dataParser.extractData(inputData, collectionFilePath); // Return the collection. return levelCollection; }
/** * Imports data from the passed Transferable-object and tries to extract level data from it. * * <p>This method is only used by the application. * * @param transferable the transferable object tried to extract level data from * @return the <code>LevelCollection</code> created from the read in data */ public final LevelCollection getLevelCollectionFromStringDataFlavor(Transferable transferable) { // Level data from the clipboard. List<String> levelData = new ArrayList<String>(); // Check if the stringFlavor is supported. If not, return an empty level collection. if (transferable == null || transferable.isDataFlavorSupported(DataFlavor.stringFlavor) == false) { return new LevelCollection.Builder().build(); } try { String transferString = ((String) transferable.getTransferData(DataFlavor.stringFlavor)); transferString = transferString.replaceAll("\\r\\n|\\r", "\n"); // Ensure there is only \n String[] levelDataString = transferString.split("\n"); // The method "extractLevelData" needs the data to be in List (or any subtype). levelData.addAll(Arrays.asList(levelDataString)); } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } // if(Settings.isDebugModeActivated) { // try { // levelDataAsArrayList = FormatConverter.getXSBList(levelDataAsArrayList); // } catch (ParseException e) { // e.printStackTrace(); // } // } // Extract the level data from the clipboard data and return the LevelCollection created from // that data. return dataParser.extractData(levelData, null); }