📄 buildingblock.java
字号:
/* * MegaMek - Copyright (C) 2000-2002 Ben Mazur (bmazur@sev.org) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. *//* * BuildingBlock.java * * Created on April 2, 2002, 1:57 PM *//** * * @author Nate Rowden * @version 1 */package megamek.common.util; //add to this package so BLKMechFile can read it's files...import java.io.*;import java.util.Vector;/** * buildingBlock is based on a file format I used in an * online game. The original was written in PHP, * this one is more robust, and written in Java. */public class BuildingBlock { private Vector rawData; private static final int version = 1; private static final char comment = '#'; /** * Creates new empty buildingBlock * */ public BuildingBlock() { //for holding the file we read/parse rawData = new Vector(); } /** * Creates a new buildingBlock and fills it with the data in the String[] array. * @param data This is most usefull for storing one block file inside another...but <I>data</I> * can be an array of anything...such as comments. */ public BuildingBlock(String[] data) { rawData = new Vector(); rawData = this.makeVector(data); } /** * Creates a new buildingBlock and fills it with the Vector. * @param data The Vector can be filled with anything. */ public BuildingBlock(Vector data) { rawData = data; } public BuildingBlock(InputStream is) { rawData = new Vector(); this.readInputStream(is); } public boolean readInputStream(InputStream is) { String data; BufferedReader in; in = new BufferedReader(new InputStreamReader(is)); //empty the rawData holder... rawData.clear(); try { //read the file till can't read no more... while(in.ready()) { data = in.readLine(); if (data == null) continue; data = data.trim(); //check for blank lines & comment lines... //don't add them to the rawData if they are if (data.length() > 0 && !data.startsWith(""+BuildingBlock.comment)) //$NON-NLS-1$ this.rawData.add(data); } in.close(); } catch (IOException e) { System.err.println("An IO Exception occured while attempting to read a BuildingBlock stream."); //$NON-NLS-1$ return false; } return true; } /** * Finds the starting index of a block. This is used by the class to locate data, * but is a public function that can be useful if you know what you want to do with * the <CODE>rawData</CODE> Vector. * @param blockName The name of the data block to locate. * @return Returns the start index of the block data. Or -1 if not found. * @see findEndIndex() * @see getAllDataAsVector() */ public int findStartIndex(String blockName) { String line; int startIndex = -1; StringBuffer buf = new StringBuffer(); String key = null; // Translate the block name to a key. buf.append( '<' ) .append( blockName ) .append( '>' ); key = buf.toString(); buf = null; //look for the block... for (int lineNum = 0; lineNum < rawData.size(); lineNum++) { line = rawData.get(lineNum).toString(); //look for "<blockName>" try { if ( line.length() >=3 && line.equalsIgnoreCase(key) ) { startIndex = ++lineNum; break; } }catch(StringIndexOutOfBoundsException e) { System.err.print("Was looking for " ); //$NON-NLS-1$ System.err.print( key ); System.err.println( " and caught a"); //$NON-NLS-1$ System.err.print("string index out of bounds exception on line: \""); //$NON-NLS-1$ System.err.print(line); System.err.println("\""); //$NON-NLS-1$ System.err.print("rawData index number: "); //$NON-NLS-1$ System.err.println(lineNum); } } return startIndex; } /** * Finds the starting index of a block. This is used by the class to locate data, * but is a public function that can be useful if you know what you want to do with * the <CODE>rawData</CODE> Vector. * @param blockName The name of the data block to locate. * @return Returns the end index of the block data. Or -1 if not found. * @see findStartIndex() * @see getAllDataAsVector() */ public int findEndIndex(String blockName) { String line; int endIndex = -1; StringBuffer buf = new StringBuffer(); String key = null; // Translate the block name to a key. buf.append( '<' ) .append( '/' ) .append( blockName ) .append( '>' ); key = buf.toString(); buf = null; //look for the block... for (int lineNum = 0; lineNum < rawData.size(); lineNum++) { line = rawData.get(lineNum).toString(); //look for "</blockName>" try { if ( line.length() >=3 && line.equalsIgnoreCase(key) ) { endIndex = lineNum; break; } } catch(StringIndexOutOfBoundsException e) { System.err.print("Was looking for " ); //$NON-NLS-1$ System.err.print( key ); System.err.println( " and caught a"); //$NON-NLS-1$ System.err.print("string index out of bounds exception on line: \""); //$NON-NLS-1$ System.err.print(line); System.err.println("\""); //$NON-NLS-1$ System.err.print("rawData index number: "); //$NON-NLS-1$ System.err.println(lineNum); } } return endIndex; } /** * Gets data from inside a block. * @param blockName The name of the block to grab the data from. * @return Returns an array of data. */ public String[] getDataAsString(String blockName) { String [] data; int startIndex = 0, endIndex = 0; startIndex = this.findStartIndex(blockName); endIndex = this.findEndIndex(blockName); if (startIndex == -1 || endIndex == -1) { data = new String[1]; data[0] = "0"; //$NON-NLS-1$ return data; } //calculate the size of our data array by subtracting the two indexes ... int size = endIndex - startIndex; if (size == 0) data = new String[size+1]; //add one so we always have at least a size 1 array... else data = new String[size]; int dataRecord = 0; //fill up the data array with the raw data we want... for ( int rawRecord = startIndex; rawRecord < endIndex; rawRecord++) { data[dataRecord] = rawData.get(rawRecord).toString(); dataRecord++; } return data; //hand back the goods... } /** * @see getDataAsString() */ public int[] getDataAsInt(String blockName) { int [] data; int startIndex, endIndex; startIndex = this.findStartIndex(blockName); endIndex = this.findEndIndex(blockName); if (startIndex == -1 || endIndex == -1) { data = new int[1]; data[0] = 0; return data; } //calculate the size of our data array by subtracting the two indexes ... int size = endIndex - startIndex; if (size == 0) data = new int[size+1]; //add one so we always have at least a size 1 array... else data = new int[size]; int dataRecord = 0; //fill up the data array with the raw data we want... for ( int rawRecord = startIndex; rawRecord < endIndex; rawRecord++) { try { data[dataRecord] = Integer.parseInt(rawData.get(rawRecord).toString()); dataRecord++; } catch (NumberFormatException oops) { data[0] = 0; System.err.println("getDataAsInt(\""+blockName+"\") failed. NumberFormatException was caught."); //$NON-NLS-1$ //$NON-NLS-2$ } } return data; //hand back the goods... } /** * @see getDataAsString() */ public float[] getDataAsFloat(String blockName) { float [] data; int startIndex, endIndex; startIndex = this.findStartIndex(blockName); endIndex = this.findEndIndex(blockName); if (startIndex == -1 || endIndex == -1) { data = new float[1]; data[0] = 0; return data; } //calculate the size of our data array by subtracting the two indexes ... int size = endIndex - startIndex; if (size == 0) data = new float[size+1]; //add one so we always have at least a size 1 array... else data = new float[size]; int dataRecord = 0; //fill up the data array with the raw data we want... for ( int rawRecord = startIndex; rawRecord < endIndex; rawRecord++) { try { data[dataRecord] = Float.valueOf(rawData.get(rawRecord).toString()).floatValue(); dataRecord++; }catch (NumberFormatException oops) { data[0] = 0; System.err.println("getDataAsFloat(\""+blockName+"\") failed. NumberFormatException was caught."); //$NON-NLS-1$ //$NON-NLS-2$ } } return data; //hand back the goods... } /** * Gets data from a block. * @param blockName Name of the block to get data from. * @return Returns the data as a Vector. */ public Vector getDataAsVector(String blockName) { Vector data; int startIndex = 0, endIndex = 0; startIndex = this.findStartIndex(blockName); endIndex = this.findEndIndex(blockName); if (startIndex == -1 || endIndex == -1) { data = new Vector(); data.clear(); return data; } data = new Vector(); //fill up the data vector with the raw data we want... for ( int rawRecord = startIndex; rawRecord < endIndex; rawRecord++) { data.add(rawData.get(rawRecord)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -