Class AbstractOrderParser

  • All Implemented Interfaces:
    OrderParser
    Direct Known Subclasses:
    AtlantisOrderParser, EresseaOrderParser

    public abstract class AbstractOrderParser
    extends java.lang.Object
    implements OrderParser
    A class for reading Eressea orders and checking their syntactical correctness. A OrderParser object can register a OrderCompleter object. In such a case the OrderParser will call the corresponding methods of the OrderCompleter if it encounters an incomplete order.
    • Field Detail

      • quotes

        protected char[] quotes
        The set of allowed quotes in orders
      • tokensIterator

        protected java.util.Iterator<OrderToken> tokensIterator
    • Constructor Detail

      • AbstractOrderParser

        public AbstractOrderParser​(GameData data)
        Creates a new AbstractOrderParser object.
    • Method Detail

      • init

        protected void init()
      • isPrefixMatching

        public boolean isPrefixMatching()
        Returns true if prefixes of tokens are matched to the token, i.e., if abbreviations are possible.
      • setPrefixMatching

        public void setPrefixMatching​(boolean prefixMatching)
        Sets the value of prefixMatching.
        Parameters:
        prefixMatching - The value for prefixMatching.
      • getData

        public GameData getData()
        Returns the value of data.
        Returns:
        Returns data.
      • setData

        protected void setData​(GameData data)
        Sets the value of data.
        Parameters:
        data - The value for data.
      • getRules

        protected Rules getRules()
      • getCompleter

        public abstract AbstractOrderCompleter getCompleter()
        Returns the value of completer.
        Returns:
        Returns completer.
      • getLocale

        public java.util.Locale getLocale()
        Return current locale.
      • getUnit

        public Unit getUnit​(java.lang.String id)
        Tries to find the unit with the given ID in the data.
        Parameters:
        id - the target's ID (as text in the report's base)
        Returns:
        The unit if it is in the data, otherwise null
        Throws:
        java.lang.NumberFormatException - if unit id is not parseable
        java.lang.NullPointerException - if unit id is null
      • getBuilding

        public Building getBuilding​(java.lang.String id)
        Tries to find the building with the given ID in the data.
        Parameters:
        id - the target's ID (as text in the report's base)
        Returns:
        The building if it is in the data, otherwise null
        Throws:
        java.lang.NumberFormatException - if unit id is not parseable
        java.lang.NullPointerException - if unit id is null
      • getShip

        public Ship getShip​(java.lang.String id)
        Tries to find the ship with the given ID in the data.
        Parameters:
        id - the target's ID (as text in the report's base)
        Returns:
        The ship if it is in the data, otherwise null
        Throws:
        java.lang.NumberFormatException - if unit id is not parseable
        java.lang.NullPointerException - if unit id is null
      • getFaction

        public Faction getFaction​(java.lang.String id)
        Tries to find the faction with the given ID in the data.
        Parameters:
        id - the target's ID (as text in the report's base)
        Returns:
        The faction if it is in the data, otherwise null
        Throws:
        java.lang.NumberFormatException - if unit id is not parseable
        java.lang.NullPointerException - if unit id is null
      • getCommandMap

        protected java.util.HashMap<StringID,​OrderHandler> getCommandMap()
        Returns the value of commandMap.
        Returns:
        Returns commandMap.
      • clearCommandMap

        public void clearCommandMap()
      • initCommands

        protected abstract void initCommands()
      • addCommand

        protected void addCommand​(StringID prefix,
                                  OrderHandler handler)
        Adds the specified handler for the specified command (and removes any previous handler for the command). The caller must make sure that #getOrderTranslation(String)(prefix, locale) returns the localized command for any order language.
        Parameters:
        prefix - A command ID
        handler - The handler for this command
      • removeCommand

        protected void removeCommand​(StringID prefix)
        Removes the handler for the specified command.
        Parameters:
        prefix -
      • getCommands

        protected java.util.Set<StringID> getCommands()
        Returns a collections of all command registered by #addCommand(String, OrderHandler).
      • getHandlers

        protected java.util.Collection<OrderHandler> getHandlers()
        Returns a collection of all registered command handlers.
      • initCommands

        protected void initCommands​(java.util.Locale loc)
        Ensures that the commands for the given locale are found by getHandlers().
        Parameters:
        loc -
      • getTokens

        public java.util.List<OrderToken> getTokens()
        Returns the tokens read by the parser.
        Specified by:
        getTokens in interface OrderParser
        Returns:
        all OrderToken object produced by the underlying OrderTokenizer by reading a order.
      • getNextToken

        protected OrderToken getNextToken()
        Returns next token from the tokenBucket.
      • getLastToken

        protected OrderToken getLastToken()
        Returns the last token retrieved by getNextToken() or null if there was none.
      • hasNextToken

        protected boolean hasNextToken()
        Returns true if there is a next token
      • getTokenIndex

        protected int getTokenIndex()
        Returns:
        the index of the last token retrieved by getLastToken() or -1 if no token was retrieved yet.
      • getErrorMessage

        public java.lang.String getErrorMessage()
        Returns the error messages produced by the last invocation of the read(Reader in) method.
        Returns:
        an error message if the last read returned false, null else.
      • setErrMsg

        protected void setErrMsg​(java.lang.String errMsg)
        Sets the value error message
      • parse

        public Order parse​(java.lang.String text,
                           java.util.Locale locale)
        Description copied from interface: OrderParser
        Parses an order line and converts it into an Order object.
        Specified by:
        parse in interface OrderParser
        Parameters:
        text - One line of text
        locale - The locale of the orders
        Returns:
        An order object with line as text and the parsed token.
        See Also:
        OrderParser.parse(String, Locale)
      • read

        @Deprecated
        public boolean read​(java.io.Reader in)
        Deprecated.
        Parses one line of text from the specified stream by tokenizing it and checking the syntax.
        Specified by:
        read in interface OrderParser
        Parameters:
        in - the stream to read the order from.
        Returns:
        true if the syntax of the order read is valid, false else.
      • readOrder

        protected Order readOrder​(OrderToken firstToken,
                                  java.lang.String text)
        Matches the token with the keys in getHandlers(OrderToken). If there is exactly one handler, it is applied to the t and the result returned. Otherwise this method returns true if t is the final token. Afterwards the completer is applied.
        Parameters:
        firstToken -
      • getHandlers

        protected java.util.List<OrderHandler> getHandlers​(OrderToken t)
        Returns a set of handlers that match the specified token.
        Parameters:
        t -
      • readDescription

        protected java.lang.String readDescription()
        Tests if the next token is a quoted (possibly empty) string at the end of the order.
        Returns:
        The string (without quotes) if a valid description was found, otherwise null
      • setDefaultQuote

        public void setDefaultQuote​(char c)
        Change default quote character
      • readDescription

        protected java.lang.String readDescription​(boolean allowEmpty)
        Tests if the next token is a quoted string at the end of the order.
        Returns:
        The string (without quotes) if a valid description was found, otherwise null
      • readDescription

        protected java.lang.String readDescription​(OrderToken t)
        Tests if t is a quoted (possibly empty) string at the end of the order.
        Returns:
        The string (without quotes) if a valid description was found, otherwise null
      • readDescription

        protected java.lang.String readDescription​(OrderToken t,
                                                   boolean allowEmpty)
        If this a quoted string at the end of the order, it is returned. If allowEmpty, it may also be an empty string.
        Returns:
        The string (without quotes) if a valid description was found, otherwise null
      • readFinalKeyword

        protected boolean readFinalKeyword​(OrderToken token)
      • readFinalString

        protected boolean readFinalString​(OrderToken token)
        Returns true if the token is a nonempty string.
      • readFinalID

        protected boolean readFinalID​(OrderToken token)
      • readFinalID

        protected boolean readFinalID​(OrderToken token,
                                      boolean tempAllowed)
      • readFinalNumber

        protected boolean readFinalNumber​(OrderToken token)
      • checkNextFinal

        protected boolean checkNextFinal()
        Checks whether the next token is the end of line or a comment, i.e. the indicating a valid end of the order. Reports an unexpected token if that is not the case.
      • checkFinal

        protected boolean checkFinal​(OrderToken t)
        Checks whether the token t is the end of line or a comment, i.e. the indicating a valid end of the order. Reports an unexpected token if that is not the case.
      • checkItem

        protected ItemType checkItem​(OrderToken token)
        Returns true if token represents an item.
      • unexpected

        protected void unexpected​(OrderToken t)
      • isEoC

        protected static final boolean isEoC​(OrderToken t)
      • isFinal

        protected static final boolean isFinal​(OrderToken t)
      • isNumeric

        protected boolean isNumeric​(java.lang.String txt,
                                    int radix,
                                    int min,
                                    int max)
        Tests if txt represents an integer with the given radix between min and max (inclusively).
        Parameters:
        txt -
        radix -
        min -
        max -
      • isNumeric

        protected boolean isNumeric​(java.lang.String txt)
        Tests if txt represents a non-negative decimal integer number.
      • isID

        protected boolean isID​(java.lang.String txt)
      • isID

        protected boolean isID​(java.lang.String txt,
                               boolean allowTemp)
        Tests if txt represents a valid ID (or TEMP ID) given the data.base and MAX_UID.
        Parameters:
        txt -
      • isTempID

        protected boolean isTempID​(java.lang.String txt)
        Tests if txt represents a valid TEMP id.
        Parameters:
        txt -
      • isTemp

        protected boolean isTemp​(java.lang.String temp)
      • getTemp

        protected abstract StringID getTemp()
      • isRID

        protected boolean isRID​(java.lang.String txt)
        tests if txt is a region id (coordinates).
      • isQuoted

        @Deprecated
        protected boolean isQuoted​(java.lang.String txt)
        Deprecated.
        nobody needs us
        Tests if txt is surrounded by double quotes.
      • isSingleQuoted

        @Deprecated
        protected boolean isSingleQuoted​(java.lang.String txt)
        Deprecated.
        nobody needs us
        Tests if txt is surrounded by single quotes.
      • isAnyString

        protected boolean isAnyString​(java.lang.String txt)
      • isSimpleString

        protected boolean isSimpleString​(java.lang.String txt)
        Returns true if txt is a nonempty string composed solely by the characters [A-Za-z~,._:0-9] that does not start with a number. TODO allow unicode characters
      • isString

        @Deprecated
        protected boolean isString​(java.lang.String txt)
        Deprecated.
        Tests if txt is a nonempty string which is either surrounded by quotes or by double quotes, or is composed solely by the characters [A-Za-z~,._:].
        Parameters:
        txt -
      • isString

        protected boolean isString​(OrderToken token)
        Same as isString(OrderToken, false)
      • isString

        protected boolean isString​(OrderToken token,
                                   boolean forceQuotes,
                                   boolean identifier)
        Returns true if token starts a string. That is, it must be either a OrderToken.TT_OPENING_QUOTE or a string token.
        Parameters:
        token - the current token
        forceQuotes - if this is true, token must be a TT_OPENING_QUOTE
      • getString

        protected OrderToken[] getString​(OrderToken token)
        Parses token and the following tokens to identify a string.
        Parameters:
        token - The current token which must be either a TT_OPENING_Quote or a simple string. You may use isString(OrderToken) to test validity of your token.
        Returns:
        result[0] contains the opening quote, result[1] the content, result[2] the closing quote, and result[3] the token that follows the string. All except result[3] may be null.
      • isEmailAddress

        protected boolean isEmailAddress​(java.lang.String txt)
        Tests if txt is (syntactically) a valid email address.
        Parameters:
        txt -
      • shallComplete

        protected boolean shallComplete​(OrderToken token,
                                        OrderToken t)
        Return true if a completions should be added to the completer for the current token. The standard version returns true if there is a OrderCompleter and token is followed by a space and t is not.
        Parameters:
        token - The last token read
        t - The current token.
      • normalizeName

        protected static java.lang.String normalizeName​(java.lang.String name)
        Returns name with '~' replaced by spaces and Umlauts replaced, too.
      • normalize

        protected java.lang.String normalize​(java.lang.String token)
      • equalsNormalized

        protected boolean equalsNormalized​(java.lang.String rulesItem,
                                           java.lang.String content)
      • toDirection

        public Direction toDirection​(java.lang.String text,
                                     java.util.Locale aLocale)
        Interpret token as direction.
      • setQuotes

        protected void setQuotes​(char[] quotes)
      • getQuotes

        protected char[] getQuotes()