⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlstreamparser.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * MegaMek - Copyright (C) 2003,2004 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. */package megamek.common;import gd.xml.ParseException;import gd.xml.XMLParser;import gd.xml.XMLResponder;import megamek.common.loaders.EntityLoadingException;import java.io.InputStream;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.Vector;import java.util.Iterator;/** * This class parses an XML input stream.  If the stream is well formed, no * <code>Exception</code> will be thrown.  If the stream adheres to the format * described by the file, "xml-spec.txt", then this class can return entities. * If unexpected entities are encountered while parsing a well-formed stream, * a warning message will be available. * * @author  Suvarov454@sourceforge.net (James A. Damour ) * @version $Revision: 4743 $ */public class XMLStreamParser implements XMLResponder {    // Private attributes and helper functions.    /**     * The buffer containing the warning message.     */    private StringBuffer        warning = new StringBuffer();    /**     * The entities parsed from the input stream.     */    private Vector              entities = new Vector();    /**     * The parser for this object.     */    private XMLParser           parser = new XMLParser();    /**     * The stream currently being parsed.     */    private InputStream         inStream = null;    /**     * The current entity being parsed from the stream.     */    private Entity              entity = null;    /**     * The current location in the entity being parsed.     */    private int                 loc = Entity.LOC_NONE;    /**     * Flag that indicates the current location is destroyed.     */    private boolean             locDestroyed = false;    /**     * Counter for the amount of ammo already handled for the current location.     */    private int                 locAmmoCount = 0;    /**     * Marks all equipment in a location on an <code>Entity<code> as destroyed.     *     * @param   en - the <code>Entity</code> whose location is destroyed.     * @param   loc - the <code>int</code> index of the destroyed location.     */    private void destroyLocation( Entity en, int loc) {        // mark armor, internal as destroyed        en.setArmor(IArmorState.ARMOR_DESTROYED, loc, false);        en.setInternal(IArmorState.ARMOR_DESTROYED, loc);        if (en.hasRearArmor(loc)) {            en.setArmor(IArmorState.ARMOR_DESTROYED, loc, true);        }        // equipment marked missing        for (Mounted mounted : en.getEquipment()) {            if (mounted.getLocation() == loc) {                mounted.setDestroyed(true);            }        }        // all critical slots set as missing        for (int i = 0; i < en.getNumberOfCriticals(loc); i++) {            final CriticalSlot cs = en.getCritical(loc, i);            if (cs != null) {                cs.setDestroyed(true);            }        }        // Mark dependent locations as destroyed.        if (en.getDependentLocation(loc) != Mech.LOC_NONE) {            destroyLocation(en, en.getDependentLocation(loc));        }    }    // Public and Protected constants, constructors, and methods.    /**     * The names of the various elements recognized by this parser.     */    public static final String  UNIT    = "unit";    public static final String  TEMPLATE= "template";    public static final String  ENTITY  = "entity";    public static final String  FLUFF   = "fluff";    public static final String  PILOT   = "pilot";    public static final String  LOCATION= "location";    public static final String  ARMOR   = "armor";    public static final String  SLOT    = "slot";    public static final String  MOVEMENT = "movement";    public static final String  TURRETLOCK = "turretlock";    /**     * The names of the attributes recognized by this parser.     * Not every attribute is valid for every element.     */    public static final String  CHASSIS = "chassis";    public static final String  MODEL   = "model";    public static final String  NAME    = "name";    public static final String  GUNNERY = "gunnery";    public static final String  PILOTING= "piloting";    public static final String  HITS    = "hits";    public static final String  ADVS    = "advantages";    public static final String  AUTOEJECT = "autoeject";    public static final String  INDEX   = "index";    public static final String  IS_DESTROYED    = "isDestroyed";    public static final String  POINTS  = "points";    public static final String  TYPE    = "type";    public static final String  IS_REAR = "isRear";    public static final String  SHOTS   = "shots";    public static final String  IS_HIT  = "isHit";    public static final String  MUNITION= "munition";    public static final String  SPEED   = "speed";    public static final String  DIRECTION = "direction";    /**     * Special values recognized by this parser.     */    public static final String  DEAD    = "Dead";    public static final String  NA      = "N/A";    public static final String  DESTROYED       = "Destroyed";    public static final String  FRONT   = "Front";    public static final String  REAR    = "Rear";    public static final String  INTERNAL= "Internal";    public static final String  EMPTY   = "Empty";    public static final String  SYSTEM  = "System";    /**     * No <code>Entity</code>s or warning message are available if the default     * constructor is used.     */    public XMLStreamParser() { /* do nothing */ }    /**     * Parse the indicated XML stream.  Any warning message or     * <code>Entity</code>s from a previously parsed stream will be discarded.     *     * @param   input - the <code>InputStream</code> to be parsed.     * @exception ParseException is thrown if a fatal     *          error occurs during parsing.  Typically, this only     *          occurs when the XML is not well-formed.     */    public void parse( InputStream input ) throws ParseException {        // Reset the warning message.        this.warning = new StringBuffer();        // Clear the entities.        this.entities.removeAllElements();        // Parse the input stream.        this.inStream = input;        this.parser.parseXML( this );    }    /**     * Construct an object and parse the XML stream.  Any warning message or     * <code>Entity</code>s from a previously parsed stream will be discarded.     *     * @param   input - the <code>InputStream</code> to be parsed.     * @exception ParseException is thrown if a fatal     *          warning occurs during parsing.  Typically, this only     *          occurs when the XML is not well-formed.     */    public XMLStreamParser( InputStream input ) throws ParseException {        this.parse( input );    }    /**     * Determine if unexpected XML entities were encountered during parsing.     *     * @return  <code>true</code> if a non-fatal warning occured.     */    public boolean hasWarningMessage() {        return (this.warning.length() > 0);    }    /**     * Get the warning message from the last parse.     *     * @return  The <code>String</code> warning message from the last parse.     *          If there is no warning message, then an <code>null</code>     *          value is returned.     */    public String getWarningMessage() {        if ( this.warning.length() > 0 ) {            return this.warning.toString();        }        return null;    }    /**     * Get any <code>Entity</code>s parsed from the last input stream.     * Entities may have been parsed out of the stream, even if errors     * were encountered.     *     * @return  A <code>Vector</code> containing <code>Entity</code>s     *          parsed from the stream.  This <code>Vector</code> may     *          be empty, but it will never be <code>null</code>.     */    public Vector getEntities() {        // ASSUMPTION : it is safe to return a modifiable reference to the        //              vector.  If assumption is wrong, clone the vector.        return this.entities;    }    // Implementation of the XMLResponder interface:    public void recordNotationDeclaration( String name,                                           String pubID,                                           String sysID )        throws ParseException {        // Do nothing.    }    public void recordEntityDeclaration( String name,                                         String value,                                         String pubID,                                         String sysID,                                         String notation )        throws ParseException {        // Do nothing.    }    public void recordElementDeclaration( String name,                                          String content )        throws ParseException {        // Do nothing.    }    public void recordAttlistDeclaration( String element,                                          String attr,                                          boolean notation,                                          String type,                                          String defmod,                                          String def )        throws ParseException {        // Do nothing.    }    public void recordDoctypeDeclaration( String name,                                          String pubID,                                          String sysID )        throws ParseException {        // Do nothing.    }    public void recordDocStart() {        // Do nothing.    }    public void recordDocEnd() {        // Do nothing.    }    public void recordElementStart( String name,                                    Hashtable attr )        throws ParseException {        // TODO: handle template files.        // What kind of element have we started?        if ( name.equals( UNIT ) ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -